Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/main/java/org/gitlab4j/api/UserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,24 @@ public SshKey addSshKey(Long userId, String title, String key) throws GitLabApiE
return (sshKey);
}

/**
* Create new key owned by specified user. Available only for admin users.
*
* <pre><code>GitLab Endpoint: POST /users/:id/keys</code></pre>
*
* @param userId the ID of the user to add the SSH key for
* @param title the new SSH Key's title
* @param key the new SSH key
* @param expiresAt the expiration date of the ssh key, optional
* @return an SshKey instance with info on the added SSH key
* @throws GitLabApiException if any exception occurs
*/
public SshKey addSshKey(Long userId, String title, String key, Date expiresAt) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm().withParam("title", title).withParam("key", key).withParam("expires_at", expiresAt);
Response response = post(Response.Status.CREATED, formData, "user", "keys");
return (response.readEntity(SshKey.class));
}

/**
* Deletes key owned by currently authenticated user. This is an idempotent function and calling it
* on a key that is already deleted or not available results in success.
Expand Down Expand Up @@ -993,6 +1011,22 @@ public ImpersonationToken createPersonalAccessToken(
return createPersonalAccessTokenOrImpersonationToken(userIdOrUsername, name, expiresAt, scopes, false);
}

/**
* Revokes a personal access token. Available only for admin users.
*
* <pre><code>GitLab Endpoint: DELETE /personal_access_tokens/:token_id</code></pre>
* @param tokenId the personal access token ID to revoke
* @throws GitLabApiException if any exception occurs
*/
public void revokePersonalAccessToken(Long tokenId) throws GitLabApiException {
if (tokenId == null) {
throw new RuntimeException("tokenId cannot be null");
}

Response.Status expectedStatus =
(isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
delete(expectedStatus, null, "personal_access_tokens", tokenId);
}
// as per https://docs.gitlab.com/ee/api/README.html#impersonation-tokens, impersonation tokens are a type of
// personal access token
private ImpersonationToken createPersonalAccessTokenOrImpersonationToken(
Expand Down
Loading