Class AccessTokenUtils


  • public final class AccessTokenUtils
    extends Object
    This class uses HTML scraping to create and revoke GitLab personal access tokens, the user's Feed token, and for fetching the current Health Check access token.

    NOTE: This relies on HTML scraping and has been tested on GitLab-CE 11.0.0 to 11.10.1 for proper functionality. It may not work on earlier or later versions.

    • Field Detail

      • NEW_USER_AUTHENTICITY_TOKEN_PATTERN

        protected static final Pattern NEW_USER_AUTHENTICITY_TOKEN_PATTERN
      • AUTHENTICITY_TOKEN_PATTERN

        protected static final Pattern AUTHENTICITY_TOKEN_PATTERN
      • PERSONAL_ACCESS_TOKEN_PATTERN

        protected static final Pattern PERSONAL_ACCESS_TOKEN_PATTERN
      • REVOKE_PERSONAL_ACCESS_TOKEN_PATTERN

        protected static final Pattern REVOKE_PERSONAL_ACCESS_TOKEN_PATTERN
      • FEED_TOKEN_PATTERN

        protected static final Pattern FEED_TOKEN_PATTERN
      • HEALTH_CHECK_ACCESS_TOKEN_PATTERN

        protected static final Pattern HEALTH_CHECK_ACCESS_TOKEN_PATTERN
    • Constructor Detail

      • AccessTokenUtils

        public AccessTokenUtils()
    • Method Detail

      • createPersonalAccessToken

        public static final String createPersonalAccessToken​(String baseUrl,
                                                             String username,
                                                             String password,
                                                             String tokenName,
                                                             List<String> scopes)
                                                      throws GitLabApiException
        Create a GitLab personal access token with the provided configuration.
        Parameters:
        baseUrl - the GitLab server base URL
        username - the user name to create the personal access token for
        password - the password of the user to create the personal access token for
        tokenName - the name for the new personal access token
        scopes - a List of scopes for the new personal access token
        Returns:
        the created personal access token
        Throws:
        GitLabApiException - if any exception occurs
      • revokePersonalAccessToken

        public static final void revokePersonalAccessToken​(String baseUrl,
                                                           String username,
                                                           String password,
                                                           String tokenName,
                                                           List<String> scopes)
                                                    throws GitLabApiException
        Revoke the first matching GitLab personal access token.
        Parameters:
        baseUrl - the GitLab server base URL
        username - the user name to revoke the personal access token for
        password - the password of the user to revoke the personal access token for
        tokenName - the name of the personal access token to revoke
        scopes - a List of scopes of the personal access token to revoke
        Throws:
        GitLabApiException - if any exception occurs
      • getFeedToken

        public static final String getFeedToken​(String baseUrl,
                                                String username,
                                                String password)
                                         throws GitLabApiException
        Fetches the user's GitLab Feed token using HTML scraping.
        Parameters:
        baseUrl - the GitLab server base URL
        username - the user name the user to log in with
        password - the password of the provided username
        Returns:
        the fetched Feed token
        Throws:
        GitLabApiException - if any exception occurs
      • getHealthCheckAccessToken

        public static final String getHealthCheckAccessToken​(String baseUrl,
                                                             String username,
                                                             String password)
                                                      throws GitLabApiException
        Fetches the GitLab health check access token using HTML scraping.
        Parameters:
        baseUrl - the GitLab server base URL
        username - the user name of an admin user to log in with
        password - the password of the provided username
        Returns:
        the fetched health check access token
        Throws:
        GitLabApiException - if any exception occurs
      • login

        protected static final String login​(String baseUrl,
                                            String username,
                                            String password)
                                     throws GitLabApiException
        Gets a GitLab session cookie by logging in the specified user.
        Parameters:
        baseUrl - the GitLab server base URL
        username - the user name to to login for
        password - the password of the user to login for
        Returns:
        the GitLab seesion token as a cookie value
        Throws:
        GitLabApiException - if any error occurs
      • logout

        protected static final void logout​(String baseUrl,
                                           String cookies)
                                    throws GitLabApiException
        Logs out the user associated with the GitLab session cookie.
        Parameters:
        baseUrl - the GitLab server base URL
        cookies - the GitLab session cookie to logout
        Throws:
        GitLabApiException - if any error occurs
      • addFormData

        public static final StringBuilder addFormData​(StringBuilder formData,
                                                      String name,
                                                      String value)
                                               throws GitLabApiException
        Adds the specified form param to the form data StringBuilder. If the provided formData is null, will create the StringBuilder instance first.
        Parameters:
        formData - the StringBuilder instance holding the form data, if null will create the StringBuilder
        name - the form param name
        value - the form param value
        Returns:
        the form data StringBuilder
        Throws:
        GitLabApiException - if any error occurs.
      • getContent

        protected static String getContent​(URLConnection connection)
                                    throws GitLabApiException
        Reads and returns the content from the provided URLConnection.
        Parameters:
        connection - the URLConnection to read the content from
        Returns:
        the read content as a String
        Throws:
        GitLabApiException - if any error occurs