Skip to content

Commit c19002f

Browse files
committed
Added Scope enums (#336).
1 parent 3410cba commit c19002f

File tree

1 file changed

+59
-5
lines changed

1 file changed

+59
-5
lines changed

src/main/java/org/gitlab4j/api/utils/AccessTokenUtils.java

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
import org.gitlab4j.api.GitLabApiException;
1717

18+
import com.fasterxml.jackson.annotation.JsonCreator;
19+
import com.fasterxml.jackson.annotation.JsonValue;
20+
1821
/**
1922
* This class uses HTML scraping to create and revoke GitLab personal access tokens,
2023
* the user's Feed token, and for fetching the current Health Check access token.
@@ -24,6 +27,57 @@
2427
*/
2528
public final class AccessTokenUtils {
2629

30+
/**
31+
* This enum defines the available scopes for a personal access token.
32+
*/
33+
public enum Scope {
34+
35+
/**
36+
* Grants complete access to the API and Container Registry (read/write) (introduced in GitLab 8.15).
37+
*/
38+
API,
39+
40+
/**
41+
* Allows to read (pull) container registry images if a project is private and
42+
* authorization is required (introduced in GitLab 9.3).
43+
*/
44+
READ_REGISTRY,
45+
46+
/**
47+
* Allows read-only access (pull) to the repository through git clone.
48+
*/
49+
READ_REPOSITORY,
50+
51+
/**
52+
* Allows access to the read-only endpoints under /users. Essentially, any of the GET
53+
* requests in the Users API are allowed (introduced in GitLab 8.15).
54+
*/
55+
READ_USER,
56+
57+
/**
58+
* Allows performing API actions as any user in the system,
59+
* if the authenticated user is an admin (introduced in GitLab 10.2).
60+
*/
61+
SUDO;
62+
63+
private static JacksonJsonEnumHelper<Scope> enumHelper = new JacksonJsonEnumHelper<>(Scope.class);
64+
65+
@JsonCreator
66+
public static Scope forValue(String value) {
67+
return enumHelper.forValue(value);
68+
}
69+
70+
@JsonValue
71+
public String toValue() {
72+
return (enumHelper.toString(this));
73+
}
74+
75+
@Override
76+
public String toString() {
77+
return (enumHelper.toString(this));
78+
}
79+
}
80+
2781
protected static final String USER_AGENT = "GitLab4J Client";
2882
protected static final String COOKIES_HEADER = "Set-Cookie";
2983

@@ -57,7 +111,7 @@ public final class AccessTokenUtils {
57111
* @throws GitLabApiException if any exception occurs
58112
*/
59113
public static final String createPersonalAccessToken(final String baseUrl, final String username,
60-
final String password, final String tokenName, final List<String> scopes) throws GitLabApiException {
114+
final String password, final String tokenName, final List<Scope> scopes) throws GitLabApiException {
61115

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

124178
if (scopes != null && scopes.size() > 0) {
125-
for (String scope : scopes) {
126-
addFormData(formData, "personal_access_token[scopes][]", scope);
179+
for (Scope scope : scopes) {
180+
addFormData(formData, "personal_access_token[scopes][]", scope.toString());
127181
}
128182
}
129183

@@ -189,7 +243,7 @@ public static final String createPersonalAccessToken(final String baseUrl, final
189243
* @throws GitLabApiException if any exception occurs
190244
*/
191245
public static final void revokePersonalAccessToken(final String baseUrl, final String username,
192-
final String password, final String tokenName, final List<String> scopes) throws GitLabApiException {
246+
final String password, final String tokenName, final List<Scope> scopes) throws GitLabApiException {
193247

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

0 commit comments

Comments
 (0)