Search in sources :

Example 1 with PersonalAccessToken

use of org.gitlab4j.api.models.PersonalAccessToken in project catma by forTEXT.

the class GitLabServerManagerTest method testInstantiationCreatesGitLabUser.

@Test
public void testInstantiationCreatesGitLabUser() throws Exception {
    UserApi userApi = gitlabManagerPrivileged.getGitLabApi().getUserApi();
    List<User> users = userApi.getUsers();
    // we should have an admin user, the "ghost" user & one representing the CATMA user
    assertEquals(3, users.size());
    // hamcrest's hasItem(T item) matcher is not behaving as documented and is expecting the
    // users collection to contain *only* this.serverManager.getGitLabUser()
    // assertThat(users, hasItem(this.serverManager.getGitLabUser()));
    User matchedUser = null;
    for (User user : users) {
        if (user.getId().equals(gitlabManagerRestricted.getUser().getUserId())) {
            matchedUser = user;
            break;
        }
    }
    assertNotNull(matchedUser);
    assertEquals(gitlabManagerRestricted.getUser().getIdentifier(), matchedUser.getUsername());
    assertEquals(gitlabManagerRestricted.getUser().getName(), matchedUser.getName());
    // assert that the user has the expected impersonation token
    List<PersonalAccessToken> impersonationTokens = userApi.getImpersonationTokens(gitlabManagerRestricted.getUser().getUserId());
    assertEquals(1, impersonationTokens.size());
    assertEquals(GitlabManagerPrivileged.GITLAB_DEFAULT_IMPERSONATION_TOKEN_NAME, impersonationTokens.get(0).getName());
}
Also used : User(org.gitlab4j.api.models.User) PersonalAccessToken(org.gitlab4j.api.models.PersonalAccessToken) UserApi(org.gitlab4j.api.UserApi) Test(org.junit.jupiter.api.Test)

Example 2 with PersonalAccessToken

use of org.gitlab4j.api.models.PersonalAccessToken in project catma by forTEXT.

the class GitlabManagerPrivileged method createPersonalAccessToken.

@Override
public String createPersonalAccessToken(int userId, String tokenName, LocalDate expiresAt) throws IOException {
    UserApi userApi = this.privilegedGitLabApi.getUserApi();
    try {
        PersonalAccessToken personalAccessToken = userApi.createPersonalAccessToken(userId, tokenName, Date.from(expiresAt.atStartOfDay(ZoneId.systemDefault()).toInstant()), new Scope[] { Scope.READ_API });
        logger.info(String.format("Created personal access token for user with ID %1$s.", userId));
        return personalAccessToken.getToken();
    } catch (GitLabApiException e) {
        throw new IOException("Failed to create personal access token", e);
    }
}
Also used : PersonalAccessToken(org.gitlab4j.api.models.PersonalAccessToken) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) UserApi(org.gitlab4j.api.UserApi)

Example 3 with PersonalAccessToken

use of org.gitlab4j.api.models.PersonalAccessToken in project catma by forTEXT.

the class GitlabManagerPrivileged method acquireImpersonationToken.

@Override
public Pair<GitUser, String> acquireImpersonationToken(String identifier, String provider, String email, String name) throws IOException {
    User user = this.acquireUser(identifier, provider, email, name);
    UserApi customUserApi = this.privilegedGitLabApi.getUserApi();
    try {
        List<PersonalAccessToken> impersonationTokens = customUserApi.getImpersonationTokens(user.getId(), ImpersonationState.ACTIVE);
        // revoke the default token if it exists actively
        for (PersonalAccessToken token : impersonationTokens) {
            if (token.getName().equals(GITLAB_DEFAULT_IMPERSONATION_TOKEN_NAME)) {
                privilegedGitLabApi.getUserApi().revokeImpersonationToken(user.getId(), token.getId());
                break;
            }
        }
    } catch (GitLabApiException e) {
        throw new IOException("Failed to revoke existing impersonation token", e);
    }
    String impersonationToken = this.createImpersonationToken(user.getId(), GITLAB_DEFAULT_IMPERSONATION_TOKEN_NAME);
    if (impersonationToken == null) {
        String errorMessage = String.format("Failed to acquire impersonation token for CATMA with identifier `%s`. " + "The creation of the token the associated GitLab user ID `%s` failed, no " + "active impersonation token called `%s` can be found!", identifier, user.getId(), GITLAB_DEFAULT_IMPERSONATION_TOKEN_NAME);
        throw new IOException(errorMessage);
    }
    Pair<GitUser, String> retVal = new Pair<>(new GitUser(user), impersonationToken);
    return retVal;
}
Also used : User(org.gitlab4j.api.models.User) GitUser(de.catma.repository.git.GitUser) PersonalAccessToken(org.gitlab4j.api.models.PersonalAccessToken) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) UserApi(org.gitlab4j.api.UserApi) GitUser(de.catma.repository.git.GitUser) Pair(de.catma.util.Pair)

Aggregations

UserApi (org.gitlab4j.api.UserApi)3 PersonalAccessToken (org.gitlab4j.api.models.PersonalAccessToken)3 IOException (java.io.IOException)2 GitLabApiException (org.gitlab4j.api.GitLabApiException)2 User (org.gitlab4j.api.models.User)2 GitUser (de.catma.repository.git.GitUser)1 Pair (de.catma.util.Pair)1 Test (org.junit.jupiter.api.Test)1