-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------- Co-authored-by: Jeremie Bresson <jeremie.bresson@unblu.com>
- Loading branch information
1 parent
11b6bb2
commit bd8504e
Showing
5 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/org/gitlab4j/api/PersonalAccessTokenApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.gitlab4j.api; | ||
|
||
import javax.ws.rs.core.Response; | ||
import org.gitlab4j.api.models.PersonalAccessToken; | ||
import org.gitlab4j.api.utils.ISO8601; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* This class provides an entry point to all the GitLab API personal access token calls. | ||
* | ||
* @see <a href="https://docs.gitlab.com/ce/api/personal_access_tokens.html">Personal access token API at GitLab</a> | ||
*/ | ||
public class PersonalAccessTokenApi extends AbstractApi { | ||
|
||
public PersonalAccessTokenApi(GitLabApi gitLabApi) { | ||
super(gitLabApi); | ||
} | ||
|
||
/** | ||
* Rotates the given personal access token. | ||
* The token is revoked and a new one which will expire in one week is created to replace it. | ||
* Only working with GitLab 16.0 and above. | ||
* | ||
* <pre><code>GitLab Endpoint: POST /personal_access_tokens/self/rotate</code></pre> | ||
* | ||
* @return the newly created PersonalAccessToken. | ||
* @throws GitLabApiException if any exception occurs | ||
*/ | ||
public PersonalAccessToken rotatePersonalAccessToken() throws GitLabApiException { | ||
return rotatePersonalAccessToken(null); | ||
} | ||
|
||
/** | ||
* Rotates the given personal access token. | ||
* The token is revoked and a new one which will expire in one week is created to replace it. | ||
* Only working with GitLab 16.0 and above. | ||
* | ||
* <pre><code>GitLab Endpoint: POST /personal_access_tokens/self/rotate</code></pre> | ||
* | ||
* @param expiresAt Expiration date of the access token | ||
* @return the newly created PersonalAccessToken. | ||
* @throws GitLabApiException if any exception occurs | ||
*/ | ||
public PersonalAccessToken rotatePersonalAccessToken(Date expiresAt) throws GitLabApiException { | ||
return rotatePersonalAccessToken("self", expiresAt); | ||
} | ||
|
||
/** | ||
* Rotates the given personal access token. | ||
* The token is revoked and a new one which will expire in one week is created to replace it. | ||
* Only working with GitLab 16.0 and above. | ||
* | ||
* <pre><code>GitLab Endpoint: POST /personal_access_tokens/:id/rotate</code></pre> | ||
* | ||
* @param expiresAt Expiration date of the access token | ||
* @return the newly created PersonalAccessToken. | ||
* @throws GitLabApiException if any exception occurs | ||
*/ | ||
public PersonalAccessToken rotatePersonalAccessToken(String id, Date expiresAt) throws GitLabApiException { | ||
GitLabApiForm formData = new GitLabApiForm() | ||
.withParam("expires_at", ISO8601.dateOnly(expiresAt)); | ||
|
||
Response response = post(Response.Status.OK, formData, "personal_access_tokens", id, "rotate"); | ||
return (response.readEntity(PersonalAccessToken.class)); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
src/main/java/org/gitlab4j/api/models/PersonalAccessToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package org.gitlab4j.api.models; | ||
|
||
import org.gitlab4j.api.Constants; | ||
import org.gitlab4j.api.utils.JacksonJson; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
|
||
import java.io.Serializable; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
public class PersonalAccessToken implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private Long userId; | ||
private List<Constants.ProjectAccessTokenScope> scopes; | ||
private String name; | ||
@JsonSerialize(using = JacksonJson.DateOnlySerializer.class) | ||
private Date expiresAt; | ||
private Long id; | ||
private Boolean active; | ||
private Date createdAt; | ||
private Boolean revoked; | ||
private Date lastUsedAt; | ||
private String token; | ||
|
||
public Long getUserId() { | ||
return userId; | ||
} | ||
|
||
public void setUserId(Long userId) { | ||
this.userId = userId; | ||
} | ||
|
||
public List<Constants.ProjectAccessTokenScope> getScopes() { | ||
return scopes; | ||
} | ||
|
||
public void setScopes(List<Constants.ProjectAccessTokenScope> scopes) { | ||
this.scopes = scopes; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Date getExpiresAt() { | ||
return expiresAt; | ||
} | ||
|
||
public void setExpiresAt(Date expiredAt) { | ||
this.expiresAt = expiredAt; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public Boolean isActive() { | ||
return active; | ||
} | ||
|
||
public void setActive(Boolean active) { | ||
this.active = active; | ||
} | ||
|
||
public Date getCreatedAt() { | ||
return createdAt; | ||
} | ||
|
||
public void setCreatedAt(Date createdAt) { | ||
this.createdAt = createdAt; | ||
} | ||
|
||
public Boolean isRevoked() { | ||
return revoked; | ||
} | ||
|
||
public void setRevoked(Boolean revoked) { | ||
this.revoked = revoked; | ||
} | ||
|
||
public Date getLastUsedAt() { | ||
return lastUsedAt; | ||
} | ||
|
||
public void setLastUsedAt(Date lastUsedAt) { | ||
this.lastUsedAt = lastUsedAt; | ||
} | ||
|
||
public String getToken() { | ||
return token; | ||
} | ||
|
||
public void setToken(String token) { | ||
this.token = token; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return JacksonJson.toJsonString(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/test/resources/org/gitlab4j/api/personal-access-token.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"id": 42, | ||
"name": "Rotated Token", | ||
"revoked": false, | ||
"created_at": "2023-08-01T15:00:00Z", | ||
"scopes": ["api"], | ||
"user_id": 1337, | ||
"last_used_at": "2021-10-06T17:58:37Z", | ||
"active": true, | ||
"expires_at": "2023-08-15", | ||
"token": "s3cr3t" | ||
} |