Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extends users/token GET endpoint to support any auth mechanism for retrieving the token #10924

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions doc/release-notes/10914-users-token-api-credentials.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Extended the users/token GET endpoint to support any auth mechanism for retrieving the token information.

Previously, this endpoint only accepted an API token to retrieve its information. Now, it accepts any authentication mechanism and returns the associated API token information.
21 changes: 13 additions & 8 deletions src/main/java/edu/harvard/iq/dataverse/api/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,20 @@ public Response deleteToken(@Context ContainerRequestContext crc) {
@Path("token")
@AuthRequired
@GET
public Response getTokenExpirationDate() {
ApiToken token = authSvc.findApiToken(getRequestApiKey());

if (token == null) {
return notFound("Token " + getRequestApiKey() + " not found.");
public Response getTokenExpirationDate(@Context ContainerRequestContext crc) {
try {
AuthenticatedUser user = getRequestAuthenticatedUserOrDie(crc);
ApiToken token = authSvc.findApiTokenByUser(user);

if (token == null) {
return notFound("Token not found.");
}

return ok(String.format("Token %s expires on %s", token.getTokenString(), token.getExpireTime()));

} catch (WrappedResponse wr) {
return wr.getResponse();
}

return ok("Token " + getRequestApiKey() + " expires on " + token.getExpireTime());

}

@Path("token/recreate")
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/edu/harvard/iq/dataverse/api/UsersIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,6 @@ public void testAPITokenEndpoints() {
*/

createUser = UtilIT.createRandomUser();
String username = UtilIT.getUsernameFromResponse(createUser);
String apiToken = UtilIT.getApiTokenFromResponse(createUser);
Response createDataverseResponse = UtilIT.createRandomDataverse(apiToken);
createDataverseResponse.prettyPrint();
Expand All @@ -428,7 +427,7 @@ public void testAPITokenEndpoints() {
getExpiration = UtilIT.getTokenExpiration(tokenForPrivateUrlUser);
getExpiration.prettyPrint();
getExpiration.then().assertThat()
.statusCode(NOT_FOUND.getStatusCode());
.statusCode(UNAUTHORIZED.getStatusCode());

createUser = UtilIT.createRandomUser();
assertEquals(OK.getStatusCode(), createUser.getStatusCode());
Expand Down
Loading