Skip to content

Commit

Permalink
Added Scope enums (#336).
Browse files Browse the repository at this point in the history
  • Loading branch information
gmessner committed May 31, 2019
1 parent 3410cba commit c19002f
Showing 1 changed file with 59 additions and 5 deletions.
64 changes: 59 additions & 5 deletions src/main/java/org/gitlab4j/api/utils/AccessTokenUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

import org.gitlab4j.api.GitLabApiException;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

/**
* 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.
Expand All @@ -24,6 +27,57 @@
*/
public final class AccessTokenUtils {

/**
* This enum defines the available scopes for a personal access token.
*/
public enum Scope {

/**
* Grants complete access to the API and Container Registry (read/write) (introduced in GitLab 8.15).
*/
API,

/**
* Allows to read (pull) container registry images if a project is private and
* authorization is required (introduced in GitLab 9.3).
*/
READ_REGISTRY,

/**
* Allows read-only access (pull) to the repository through git clone.
*/
READ_REPOSITORY,

/**
* Allows access to the read-only endpoints under /users. Essentially, any of the GET
* requests in the Users API are allowed (introduced in GitLab 8.15).
*/
READ_USER,

/**
* Allows performing API actions as any user in the system,
* if the authenticated user is an admin (introduced in GitLab 10.2).
*/
SUDO;

private static JacksonJsonEnumHelper<Scope> enumHelper = new JacksonJsonEnumHelper<>(Scope.class);

@JsonCreator
public static Scope forValue(String value) {
return enumHelper.forValue(value);
}

@JsonValue
public String toValue() {
return (enumHelper.toString(this));
}

@Override
public String toString() {
return (enumHelper.toString(this));
}
}

protected static final String USER_AGENT = "GitLab4J Client";
protected static final String COOKIES_HEADER = "Set-Cookie";

Expand Down Expand Up @@ -57,7 +111,7 @@ public final class AccessTokenUtils {
* @throws GitLabApiException if any exception occurs
*/
public static final String createPersonalAccessToken(final String baseUrl, final String username,
final String password, final String tokenName, final List<String> scopes) throws GitLabApiException {
final String password, final String tokenName, final List<Scope> scopes) throws GitLabApiException {

// Save the follow redirect state so it can be restored later
boolean savedFollowRedirects = HttpURLConnection.getFollowRedirects();
Expand Down Expand Up @@ -122,8 +176,8 @@ public static final String createPersonalAccessToken(final String baseUrl, final
addFormData(formData, "personal_access_token[expires_at]", "");

if (scopes != null && scopes.size() > 0) {
for (String scope : scopes) {
addFormData(formData, "personal_access_token[scopes][]", scope);
for (Scope scope : scopes) {
addFormData(formData, "personal_access_token[scopes][]", scope.toString());
}
}

Expand Down Expand Up @@ -189,7 +243,7 @@ public static final String createPersonalAccessToken(final String baseUrl, final
* @throws GitLabApiException if any exception occurs
*/
public static final void revokePersonalAccessToken(final String baseUrl, final String username,
final String password, final String tokenName, final List<String> scopes) throws GitLabApiException {
final String password, final String tokenName, final List<Scope> scopes) throws GitLabApiException {

// Save the follow redirect state so it can be restored later
boolean savedFollowRedirects = HttpURLConnection.getFollowRedirects();
Expand Down Expand Up @@ -252,7 +306,7 @@ public static final void revokePersonalAccessToken(final String baseUrl, final S
String scopesText = "";
if (scopes != null && scopes.size() > 0) {
final StringJoiner joiner = new StringJoiner(", ");
scopes.forEach(s -> joiner.add(s));
scopes.forEach(s -> joiner.add(s.toString()));
scopesText = joiner.toString();
}

Expand Down

0 comments on commit c19002f

Please sign in to comment.