Package org.gitlab4j.api.utils
Class AccessTokenUtils
- java.lang.Object
-
- org.gitlab4j.api.utils.AccessTokenUtils
-
public final class AccessTokenUtils extends java.lang.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AccessTokenUtils.Scope
This enum defines the available scopes for a personal access token.
-
Field Summary
Fields Modifier and Type Field Description protected static java.util.regex.Pattern
AUTHENTICITY_TOKEN_PATTERN
protected static java.lang.String
AUTHENTICITY_TOKEN_REGEX
protected static java.lang.String
COOKIES_HEADER
protected static java.util.regex.Pattern
FEED_TOKEN_PATTERN
protected static java.lang.String
FEED_TOKEN_REGEX
protected static java.util.regex.Pattern
HEALTH_CHECK_ACCESS_TOKEN_PATTERN
protected static java.lang.String
HEALTH_CHECK_ACCESS_TOKEN_REGEX
protected static java.util.regex.Pattern
NEW_USER_AUTHENTICITY_TOKEN_PATTERN
protected static java.lang.String
NEW_USER_AUTHENTICITY_TOKEN_REGEX
protected static java.util.regex.Pattern
PERSONAL_ACCESS_TOKEN_PATTERN
protected static java.lang.String
PERSONAL_ACCESS_TOKEN_REGEX
protected static java.util.regex.Pattern
REVOKE_PERSONAL_ACCESS_TOKEN_PATTERN
protected static java.lang.String
REVOKE_PERSONAL_ACCESS_TOKEN_REGEX
protected static java.lang.String
USER_AGENT
-
Constructor Summary
Constructors Constructor Description AccessTokenUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringBuilder
addFormData(java.lang.StringBuilder formData, java.lang.String name, java.lang.String value)
Adds the specified form param to the form data StringBuilder.static java.lang.String
createPersonalAccessToken(java.lang.String baseUrl, java.lang.String username, java.lang.String password, java.lang.String tokenName, java.util.List<AccessTokenUtils.Scope> scopes)
Create a GitLab personal access token with the provided configuration.static java.lang.String
createPersonalAccessToken(java.lang.String baseUrl, java.lang.String username, java.lang.String password, java.lang.String tokenName, AccessTokenUtils.Scope[] scopes)
Create a GitLab personal access token with the provided configuration.protected static java.lang.String
getContent(java.net.URLConnection connection)
Reads and returns the content from the provided URLConnection.static java.lang.String
getFeedToken(java.lang.String baseUrl, java.lang.String username, java.lang.String password)
Fetches the user's GitLab Feed token using HTML scraping.static java.lang.String
getHealthCheckAccessToken(java.lang.String baseUrl, java.lang.String username, java.lang.String password)
Fetches the GitLab health check access token using HTML scraping.protected static java.lang.String
login(java.lang.String baseUrl, java.lang.String username, java.lang.String password)
Gets a GitLab session cookie by logging in the specified user.protected static void
logout(java.lang.String baseUrl, java.lang.String cookies)
Logs out the user associated with the GitLab session cookie.static void
revokePersonalAccessToken(java.lang.String baseUrl, java.lang.String username, java.lang.String password, java.lang.String tokenName, java.util.List<AccessTokenUtils.Scope> scopes)
Revoke the first matching GitLab personal access token.static void
revokePersonalAccessToken(java.lang.String baseUrl, java.lang.String username, java.lang.String password, java.lang.String tokenName, AccessTokenUtils.Scope[] scopes)
Revoke the first matching GitLab personal access token.
-
-
-
Field Detail
-
USER_AGENT
protected static final java.lang.String USER_AGENT
- See Also:
- Constant Field Values
-
COOKIES_HEADER
protected static final java.lang.String COOKIES_HEADER
- See Also:
- Constant Field Values
-
NEW_USER_AUTHENTICITY_TOKEN_REGEX
protected static final java.lang.String NEW_USER_AUTHENTICITY_TOKEN_REGEX
- See Also:
- Constant Field Values
-
NEW_USER_AUTHENTICITY_TOKEN_PATTERN
protected static final java.util.regex.Pattern NEW_USER_AUTHENTICITY_TOKEN_PATTERN
-
AUTHENTICITY_TOKEN_REGEX
protected static final java.lang.String AUTHENTICITY_TOKEN_REGEX
- See Also:
- Constant Field Values
-
AUTHENTICITY_TOKEN_PATTERN
protected static final java.util.regex.Pattern AUTHENTICITY_TOKEN_PATTERN
-
PERSONAL_ACCESS_TOKEN_REGEX
protected static final java.lang.String PERSONAL_ACCESS_TOKEN_REGEX
- See Also:
- Constant Field Values
-
PERSONAL_ACCESS_TOKEN_PATTERN
protected static final java.util.regex.Pattern PERSONAL_ACCESS_TOKEN_PATTERN
-
REVOKE_PERSONAL_ACCESS_TOKEN_REGEX
protected static final java.lang.String REVOKE_PERSONAL_ACCESS_TOKEN_REGEX
- See Also:
- Constant Field Values
-
REVOKE_PERSONAL_ACCESS_TOKEN_PATTERN
protected static final java.util.regex.Pattern REVOKE_PERSONAL_ACCESS_TOKEN_PATTERN
-
FEED_TOKEN_REGEX
protected static final java.lang.String FEED_TOKEN_REGEX
- See Also:
- Constant Field Values
-
FEED_TOKEN_PATTERN
protected static final java.util.regex.Pattern FEED_TOKEN_PATTERN
-
HEALTH_CHECK_ACCESS_TOKEN_REGEX
protected static final java.lang.String HEALTH_CHECK_ACCESS_TOKEN_REGEX
- See Also:
- Constant Field Values
-
HEALTH_CHECK_ACCESS_TOKEN_PATTERN
protected static final java.util.regex.Pattern HEALTH_CHECK_ACCESS_TOKEN_PATTERN
-
-
Method Detail
-
createPersonalAccessToken
public static final java.lang.String createPersonalAccessToken(java.lang.String baseUrl, java.lang.String username, java.lang.String password, java.lang.String tokenName, AccessTokenUtils.Scope[] scopes) throws GitLabApiException
Create a GitLab personal access token with the provided configuration.- Parameters:
baseUrl
- the GitLab server base URLusername
- the user name to create the personal access token forpassword
- the password of the user to create the personal access token fortokenName
- the name for the new personal access tokenscopes
- an array of scopes for the new personal access token- Returns:
- the created personal access token
- Throws:
GitLabApiException
- if any exception occurs
-
createPersonalAccessToken
public static final java.lang.String createPersonalAccessToken(java.lang.String baseUrl, java.lang.String username, java.lang.String password, java.lang.String tokenName, java.util.List<AccessTokenUtils.Scope> scopes) throws GitLabApiException
Create a GitLab personal access token with the provided configuration.- Parameters:
baseUrl
- the GitLab server base URLusername
- the user name to create the personal access token forpassword
- the password of the user to create the personal access token fortokenName
- the name for the new personal access tokenscopes
- 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(java.lang.String baseUrl, java.lang.String username, java.lang.String password, java.lang.String tokenName, AccessTokenUtils.Scope[] scopes) throws GitLabApiException
Revoke the first matching GitLab personal access token.- Parameters:
baseUrl
- the GitLab server base URLusername
- the user name to revoke the personal access token forpassword
- the password of the user to revoke the personal access token fortokenName
- the name of the personal access token to revokescopes
- an array of scopes of the personal access token to revoke- Throws:
GitLabApiException
- if any exception occurs
-
revokePersonalAccessToken
public static final void revokePersonalAccessToken(java.lang.String baseUrl, java.lang.String username, java.lang.String password, java.lang.String tokenName, java.util.List<AccessTokenUtils.Scope> scopes) throws GitLabApiException
Revoke the first matching GitLab personal access token.- Parameters:
baseUrl
- the GitLab server base URLusername
- the user name to revoke the personal access token forpassword
- the password of the user to revoke the personal access token fortokenName
- the name of the personal access token to revokescopes
- a List of scopes of the personal access token to revoke- Throws:
GitLabApiException
- if any exception occurs
-
getFeedToken
public static final java.lang.String getFeedToken(java.lang.String baseUrl, java.lang.String username, java.lang.String password) throws GitLabApiException
Fetches the user's GitLab Feed token using HTML scraping.- Parameters:
baseUrl
- the GitLab server base URLusername
- the user name the user to log in withpassword
- the password of the provided username- Returns:
- the fetched Feed token
- Throws:
GitLabApiException
- if any exception occurs
-
getHealthCheckAccessToken
public static final java.lang.String getHealthCheckAccessToken(java.lang.String baseUrl, java.lang.String username, java.lang.String password) throws GitLabApiException
Fetches the GitLab health check access token using HTML scraping.- Parameters:
baseUrl
- the GitLab server base URLusername
- the user name of an admin user to log in withpassword
- the password of the provided username- Returns:
- the fetched health check access token
- Throws:
GitLabApiException
- if any exception occurs
-
login
protected static final java.lang.String login(java.lang.String baseUrl, java.lang.String username, java.lang.String password) throws GitLabApiException
Gets a GitLab session cookie by logging in the specified user.- Parameters:
baseUrl
- the GitLab server base URLusername
- the user name to to login forpassword
- 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(java.lang.String baseUrl, java.lang.String cookies) throws GitLabApiException
Logs out the user associated with the GitLab session cookie.- Parameters:
baseUrl
- the GitLab server base URLcookies
- the GitLab session cookie to logout- Throws:
GitLabApiException
- if any error occurs
-
addFormData
public static final java.lang.StringBuilder addFormData(java.lang.StringBuilder formData, java.lang.String name, java.lang.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 StringBuildername
- the form param namevalue
- the form param value- Returns:
- the form data StringBuilder
- Throws:
GitLabApiException
- if any error occurs.
-
getContent
protected static java.lang.String getContent(java.net.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
-
-