Commit 1a6f7e7c authored by Greg Messner's avatar Greg Messner
Browse files

Added WRITE_REPOSITORY scope and test for same.

parent 970a3690
...@@ -8,6 +8,7 @@ import java.net.HttpURLConnection; ...@@ -8,6 +8,7 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.StringJoiner; import java.util.StringJoiner;
import java.util.regex.Matcher; import java.util.regex.Matcher;
...@@ -39,7 +40,9 @@ public final class AccessTokenUtils { ...@@ -39,7 +40,9 @@ public final class AccessTokenUtils {
/** /**
* Allows to read (pull) container registry images if a project is private and * Allows to read (pull) container registry images if a project is private and
* authorization is required (introduced in GitLab 9.3). * authorization is required (introduced in GitLab 9.3). If the GitLab server you
* are using does not have the Registry properly configured, using this scope will
* result in an exception.
*/ */
READ_REGISTRY, READ_REGISTRY,
...@@ -58,7 +61,12 @@ public final class AccessTokenUtils { ...@@ -58,7 +61,12 @@ public final class AccessTokenUtils {
* Allows performing API actions as any user in the system, * Allows performing API actions as any user in the system,
* if the authenticated user is an admin (introduced in GitLab 10.2). * if the authenticated user is an admin (introduced in GitLab 10.2).
*/ */
SUDO; SUDO,
/**
* Grants read-write access to repositories on private projects using Git-over-HTTP (not using the API).
*/
WRITE_REPOSITORY;
private static JacksonJsonEnumHelper<Scope> enumHelper = new JacksonJsonEnumHelper<>(Scope.class); private static JacksonJsonEnumHelper<Scope> enumHelper = new JacksonJsonEnumHelper<>(Scope.class);
...@@ -99,6 +107,27 @@ public final class AccessTokenUtils { ...@@ -99,6 +107,27 @@ public final class AccessTokenUtils {
protected static final String HEALTH_CHECK_ACCESS_TOKEN_REGEX = "id=\"health-check-token\">([^<]*)<\\/code>"; protected static final String HEALTH_CHECK_ACCESS_TOKEN_REGEX = "id=\"health-check-token\">([^<]*)<\\/code>";
protected static final Pattern HEALTH_CHECK_ACCESS_TOKEN_PATTERN = Pattern.compile(HEALTH_CHECK_ACCESS_TOKEN_REGEX); protected static final Pattern HEALTH_CHECK_ACCESS_TOKEN_PATTERN = Pattern.compile(HEALTH_CHECK_ACCESS_TOKEN_REGEX);
/**
* Create a GitLab personal access token with the provided configuration.
*
* @param baseUrl the GitLab server base URL
* @param username the user name to create the personal access token for
* @param password the password of the user to create the personal access token for
* @param tokenName the name for the new personal access token
* @param scopes an array of scopes for the new personal access token
* @return the created personal access token
* @throws GitLabApiException if any exception occurs
*/
public static final String createPersonalAccessToken(final String baseUrl, final String username,
final String password, final String tokenName, final Scope[] scopes) throws GitLabApiException {
if (scopes == null || scopes.length == 0) {
throw new RuntimeException("scopes cannot be null or empty");
}
return (createPersonalAccessToken(baseUrl, username, password, tokenName, Arrays.asList(scopes)));
}
/** /**
* Create a GitLab personal access token with the provided configuration. * Create a GitLab personal access token with the provided configuration.
* *
...@@ -232,6 +261,26 @@ public final class AccessTokenUtils { ...@@ -232,6 +261,26 @@ public final class AccessTokenUtils {
} }
} }
/**
* Revoke the first matching GitLab personal access token.
*
* @param baseUrl the GitLab server base URL
* @param username the user name to revoke the personal access token for
* @param password the password of the user to revoke the personal access token for
* @param tokenName the name of the personal access token to revoke
* @param scopes an array of scopes of the personal access token to revoke
* @throws GitLabApiException if any exception occurs
*/
public static final void revokePersonalAccessToken(final String baseUrl, final String username,
final String password, final String tokenName, final Scope[] scopes) throws GitLabApiException {
if (scopes == null || scopes.length == 0) {
throw new RuntimeException("scopes cannot be null or empty");
}
revokePersonalAccessToken(baseUrl, username, password, tokenName, Arrays.asList(scopes));
}
/** /**
* Revoke the first matching GitLab personal access token. * Revoke the first matching GitLab personal access token.
* *
......
...@@ -70,9 +70,11 @@ public class TestAccessTokenUtils { ...@@ -70,9 +70,11 @@ public class TestAccessTokenUtils {
final String tokenName = "Testing Token Creation-" + HelperUtils.getRandomInt(1000); final String tokenName = "Testing Token Creation-" + HelperUtils.getRandomInt(1000);
// NOTE: READ_REGISTRY scope is left out because the GitLab server docker instance does not have the
// registry configured and the test would thus fail.
Scope[] scopes = {Scope.API, Scope.READ_USER, Scope.READ_REPOSITORY, Scope.WRITE_REPOSITORY, Scope.SUDO};
String accessToken = AccessTokenUtils.createPersonalAccessToken( String accessToken = AccessTokenUtils.createPersonalAccessToken(
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD, TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD, tokenName, scopes);
tokenName, Arrays.asList(Scope.API, Scope.SUDO));
System.out.format("Created '%s' personal access token: %s%n", tokenName, accessToken); System.out.format("Created '%s' personal access token: %s%n", tokenName, accessToken);
assertNotNull(accessToken); assertNotNull(accessToken);
...@@ -81,8 +83,7 @@ public class TestAccessTokenUtils { ...@@ -81,8 +83,7 @@ public class TestAccessTokenUtils {
// Go ahead and revoke (delete) the just created access token // Go ahead and revoke (delete) the just created access token
try { try {
AccessTokenUtils.revokePersonalAccessToken( AccessTokenUtils.revokePersonalAccessToken(
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD, TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD, tokenName, scopes);
tokenName, Arrays.asList(Scope.API, Scope.SUDO));
System.out.format("Revoked '%s' personal access token: %s%n", tokenName, accessToken); System.out.format("Revoked '%s' personal access token: %s%n", tokenName, accessToken);
} catch (Exception ignore) {} } catch (Exception ignore) {}
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment