Skip to content

Commit 1c22c25

Browse files
otaryjmini
andauthored
add revokePersonalAccessToken(tokenId) and addSshKey expiresAt field (#1195)
Co-authored-by: Jeremie Bresson <[email protected]>
1 parent 60259a6 commit 1c22c25

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

Diff for: src/main/java/org/gitlab4j/api/PersonalAccessTokenApi.java

+16
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,20 @@ public PersonalAccessToken getPersonalAccessToken(String id) throws GitLabApiExc
9393
Response response = get(Response.Status.OK, null, "personal_access_tokens", id);
9494
return (response.readEntity(PersonalAccessToken.class));
9595
}
96+
97+
/**
98+
* Revokes a personal access token. Available only for admin users.
99+
*
100+
* <pre><code>GitLab Endpoint: DELETE /personal_access_tokens/:token_id</code></pre>
101+
* @param tokenId the personal access token ID to revoke
102+
* @throws GitLabApiException if any exception occurs
103+
*/
104+
public void revokePersonalAccessToken(Long tokenId) throws GitLabApiException {
105+
if (tokenId == null) {
106+
throw new RuntimeException("tokenId cannot be null");
107+
}
108+
Response.Status expectedStatus =
109+
(isApiVersion(GitLabApi.ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
110+
delete(expectedStatus, null, "personal_access_tokens", tokenId);
111+
}
96112
}

Diff for: src/main/java/org/gitlab4j/api/UserApi.java

+61-3
Original file line numberDiff line numberDiff line change
@@ -786,9 +786,30 @@ public Optional<SshKey> getOptionalSshKey(Long keyId) {
786786
* @param key the new SSH key
787787
* @return an SshKey instance with info on the added SSH key
788788
* @throws GitLabApiException if any exception occurs
789+
* @deprecated use {@link #addSshKey(String, String, Date)} instead
789790
*/
791+
@Deprecated
790792
public SshKey addSshKey(String title, String key) throws GitLabApiException {
791-
GitLabApiForm formData = new GitLabApiForm().withParam("title", title).withParam("key", key);
793+
return addSshKey(title, key, null);
794+
}
795+
796+
/**
797+
* Creates a new key owned by the currently authenticated user.
798+
*
799+
* <pre><code>GitLab Endpoint: POST /user/keys</code></pre>
800+
*
801+
* @param title the new SSH Key's title
802+
* @param key the new SSH key
803+
* @param expiresAt the expiration date of the ssh key, optional
804+
* @return an SshKey instance with info on the added SSH key
805+
* @throws GitLabApiException if any exception occurs
806+
*/
807+
public SshKey addSshKey(String title, String key, Date expiresAt) throws GitLabApiException {
808+
GitLabApiForm formData = new GitLabApiForm()
809+
.withParam("title", title)
810+
.withParam("key", key)
811+
.withParam("expires_at", expiresAt);
812+
792813
Response response = post(Response.Status.CREATED, formData, "user", "keys");
793814
return (response.readEntity(SshKey.class));
794815
}
@@ -803,20 +824,40 @@ public SshKey addSshKey(String title, String key) throws GitLabApiException {
803824
* @param key the new SSH key
804825
* @return an SshKey instance with info on the added SSH key
805826
* @throws GitLabApiException if any exception occurs
827+
* @deprecated use {@link #addSshKey(Long, String, String, Date)} instead
806828
*/
829+
@Deprecated
807830
public SshKey addSshKey(Long userId, String title, String key) throws GitLabApiException {
831+
return addSshKey(userId, title, key, null);
832+
}
808833

834+
/**
835+
* Create new key owned by specified user. Available only for admin users.
836+
*
837+
* <pre><code>GitLab Endpoint: POST /users/:id/keys</code></pre>
838+
*
839+
* @param userId the ID of the user to add the SSH key for
840+
* @param title the new SSH Key's title
841+
* @param key the new SSH key
842+
* @param expiresAt the expiration date of the ssh key, optional
843+
* @return an SshKey instance with info on the added SSH key
844+
* @throws GitLabApiException if any exception occurs
845+
*/
846+
public SshKey addSshKey(Long userId, String title, String key, Date expiresAt) throws GitLabApiException {
809847
if (userId == null) {
810848
throw new RuntimeException("userId cannot be null");
811849
}
812850

813-
GitLabApiForm formData = new GitLabApiForm().withParam("title", title).withParam("key", key);
851+
GitLabApiForm formData = new GitLabApiForm()
852+
.withParam("title", title)
853+
.withParam("key", key)
854+
.withParam("expires_at", expiresAt);
855+
814856
Response response = post(Response.Status.CREATED, formData, "users", userId, "keys");
815857
SshKey sshKey = response.readEntity(SshKey.class);
816858
if (sshKey != null) {
817859
sshKey.setUserId(userId);
818860
}
819-
820861
return (sshKey);
821862
}
822863

@@ -993,6 +1034,23 @@ public ImpersonationToken createPersonalAccessToken(
9931034
return createPersonalAccessTokenOrImpersonationToken(userIdOrUsername, name, expiresAt, scopes, false);
9941035
}
9951036

1037+
/**
1038+
* Revokes a personal access token. Available only for admin users.
1039+
*
1040+
* <pre><code>GitLab Endpoint: DELETE /personal_access_tokens/:token_id</code></pre>
1041+
* @param tokenId the personal access token ID to revoke
1042+
* @throws GitLabApiException if any exception occurs
1043+
*/
1044+
public void revokePersonalAccessToken(Long tokenId) throws GitLabApiException {
1045+
if (tokenId == null) {
1046+
throw new RuntimeException("tokenId cannot be null");
1047+
}
1048+
1049+
Response.Status expectedStatus =
1050+
(isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
1051+
delete(expectedStatus, null, "personal_access_tokens", tokenId);
1052+
}
1053+
9961054
// as per https://docs.gitlab.com/ee/api/README.html#impersonation-tokens, impersonation tokens are a type of
9971055
// personal access token
9981056
private ImpersonationToken createPersonalAccessTokenOrImpersonationToken(

0 commit comments

Comments
 (0)