Package org.gitlab4j.api.utils
Class AccessTokenUtils
java.lang.Object
org.gitlab4j.api.utils.AccessTokenUtils
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 ClassesModifier and TypeClassDescriptionstatic enum
This enum defines the available scopes for a personal access token. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Pattern
protected static final String
protected static final String
protected static final Pattern
protected static final String
protected static final Pattern
protected static final String
protected static final Pattern
protected static final String
protected static final Pattern
protected static final String
protected static final Pattern
protected static final String
protected static final String
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final StringBuilder
addFormData
(StringBuilder formData, String name, String value) Adds the specified form param to the form data StringBuilder.static final String
createPersonalAccessToken
(String baseUrl, String username, String password, String tokenName, List<AccessTokenUtils.Scope> scopes) Create a GitLab personal access token with the provided configuration.static final String
createPersonalAccessToken
(String baseUrl, String username, String password, String tokenName, AccessTokenUtils.Scope[] scopes) Create a GitLab personal access token with the provided configuration.protected static String
getContent
(URLConnection connection) Reads and returns the content from the provided URLConnection.static final String
getFeedToken
(String baseUrl, String username, String password) Fetches the user's GitLab Feed token using HTML scraping.static final String
getHealthCheckAccessToken
(String baseUrl, String username, String password) Fetches the GitLab health check access token using HTML scraping.protected static final String
Gets a GitLab session cookie by logging in the specified user.protected static final void
Logs out the user associated with the GitLab session cookie.static final void
revokePersonalAccessToken
(String baseUrl, String username, String password, String tokenName, List<AccessTokenUtils.Scope> scopes) Revoke the first matching GitLab personal access token.static final void
revokePersonalAccessToken
(String baseUrl, String username, String password, String tokenName, AccessTokenUtils.Scope[] scopes) Revoke the first matching GitLab personal access token.
-
Field Details
-
USER_AGENT
- See Also:
-
COOKIES_HEADER
- See Also:
-
NEW_USER_AUTHENTICITY_TOKEN_REGEX
- See Also:
-
NEW_USER_AUTHENTICITY_TOKEN_PATTERN
-
AUTHENTICITY_TOKEN_REGEX
- See Also:
-
AUTHENTICITY_TOKEN_PATTERN
-
PERSONAL_ACCESS_TOKEN_REGEX
- See Also:
-
PERSONAL_ACCESS_TOKEN_PATTERN
-
REVOKE_PERSONAL_ACCESS_TOKEN_REGEX
- See Also:
-
REVOKE_PERSONAL_ACCESS_TOKEN_PATTERN
-
FEED_TOKEN_REGEX
- See Also:
-
FEED_TOKEN_PATTERN
-
HEALTH_CHECK_ACCESS_TOKEN_REGEX
- See Also:
-
HEALTH_CHECK_ACCESS_TOKEN_PATTERN
-
-
Constructor Details
-
AccessTokenUtils
public AccessTokenUtils()
-
-
Method Details
-
createPersonalAccessToken
public static final String createPersonalAccessToken(String baseUrl, String username, String password, 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 String createPersonalAccessToken(String baseUrl, String username, String password, String tokenName, 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(String baseUrl, String username, String password, 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(String baseUrl, String username, String password, String tokenName, 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 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 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 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 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 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 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
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 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 StringBuildername
- the form param namevalue
- the form param value- Returns:
- the form data StringBuilder
- Throws:
GitLabApiException
- if any error occurs.
-
getContent
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
-