|
15 | 15 |
|
16 | 16 | import org.gitlab4j.api.GitLabApiException;
|
17 | 17 |
|
| 18 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 19 | +import com.fasterxml.jackson.annotation.JsonValue; |
| 20 | + |
18 | 21 | /**
|
19 | 22 | * This class uses HTML scraping to create and revoke GitLab personal access tokens,
|
20 | 23 | * the user's Feed token, and for fetching the current Health Check access token.
|
|
24 | 27 | */
|
25 | 28 | public final class AccessTokenUtils {
|
26 | 29 |
|
| 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 | + |
27 | 81 | protected static final String USER_AGENT = "GitLab4J Client";
|
28 | 82 | protected static final String COOKIES_HEADER = "Set-Cookie";
|
29 | 83 |
|
@@ -57,7 +111,7 @@ public final class AccessTokenUtils {
|
57 | 111 | * @throws GitLabApiException if any exception occurs
|
58 | 112 | */
|
59 | 113 | 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 { |
61 | 115 |
|
62 | 116 | // Save the follow redirect state so it can be restored later
|
63 | 117 | boolean savedFollowRedirects = HttpURLConnection.getFollowRedirects();
|
@@ -122,8 +176,8 @@ public static final String createPersonalAccessToken(final String baseUrl, final
|
122 | 176 | addFormData(formData, "personal_access_token[expires_at]", "");
|
123 | 177 |
|
124 | 178 | 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()); |
127 | 181 | }
|
128 | 182 | }
|
129 | 183 |
|
@@ -189,7 +243,7 @@ public static final String createPersonalAccessToken(final String baseUrl, final
|
189 | 243 | * @throws GitLabApiException if any exception occurs
|
190 | 244 | */
|
191 | 245 | 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 { |
193 | 247 |
|
194 | 248 | // Save the follow redirect state so it can be restored later
|
195 | 249 | boolean savedFollowRedirects = HttpURLConnection.getFollowRedirects();
|
@@ -252,7 +306,7 @@ public static final void revokePersonalAccessToken(final String baseUrl, final S
|
252 | 306 | String scopesText = "";
|
253 | 307 | if (scopes != null && scopes.size() > 0) {
|
254 | 308 | final StringJoiner joiner = new StringJoiner(", ");
|
255 |
| - scopes.forEach(s -> joiner.add(s)); |
| 309 | + scopes.forEach(s -> joiner.add(s.toString())); |
256 | 310 | scopesText = joiner.toString();
|
257 | 311 | }
|
258 | 312 |
|
|
0 commit comments