Skip to content

Commit

Permalink
Merge pull request #7345 from IQSS/7307-get-user-by-token
Browse files Browse the repository at this point in the history
7307 get user by token
  • Loading branch information
kcondon committed Nov 3, 2020
2 parents 155ea1e + bb9eefe commit 3da9c40
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 10 deletions.
3 changes: 3 additions & 0 deletions doc/release-notes/7307-get-user-info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Notes for Tool Developers and Integrators

New API endpoint to retrieve user info so that tools can email users if needed.
17 changes: 16 additions & 1 deletion doc/sphinx-guides/source/api/native-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2525,6 +2525,18 @@ Get All Notifications by User
Each user can get a dump of their notifications by passing in their API token::
curl -H "X-Dataverse-key:$API_TOKEN" $SERVER_URL/api/notifications/all
.. _User Information:
User Information
----------------
Get User Information in JSON Format
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Each user can get a dump of their basic information in JSON format by passing in their API token::
curl -H "X-Dataverse-key:$API_TOKEN" $SERVER_URL/api/users/:me
.. _pids-api:
Expand Down Expand Up @@ -2876,7 +2888,10 @@ Sample output using "dataverseAdmin" as the ``identifier``::
"affiliation": "Dataverse.org"
}
Create an authenticateUser::
Create an Authenticated User
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Create an authenticatedUser::
POST http://$SERVER/api/admin/authenticatedUsers
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/edu/harvard/iq/dataverse/api/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,15 @@ public Response deleteAuthenticationProvider(@PathParam("id") String id) {
: ""));
}

@GET
@Path("authenticatedUsers/{identifier}/")
public Response getAuthenticatedUser(@PathParam("identifier") String identifier) {
AuthenticatedUser authenticatedUser = authSvc.getAuthenticatedUser(identifier);
if (authenticatedUser != null) {
return ok(json(authenticatedUser));
}
return error(Response.Status.BAD_REQUEST, "User " + identifier + " not found.");
}
@GET
@Path("authenticatedUsers/{identifier}/")
public Response getAuthenticatedUserByIdentifier(@PathParam("identifier") String identifier) {
AuthenticatedUser authenticatedUser = authSvc.getAuthenticatedUser(identifier);
if (authenticatedUser != null) {
return ok(json(authenticatedUser));
}
return error(Response.Status.BAD_REQUEST, "User " + identifier + " not found.");
}

@DELETE
@Path("authenticatedUsers/{identifier}/")
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/api/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import edu.harvard.iq.dataverse.authorization.users.User;
import edu.harvard.iq.dataverse.engine.command.impl.ChangeUserIdentifierCommand;
import edu.harvard.iq.dataverse.engine.command.impl.MergeInAccountCommand;
import static edu.harvard.iq.dataverse.util.json.JsonPrinter.json;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.ws.rs.DELETE;
Expand Down Expand Up @@ -174,5 +175,20 @@ public Response recreateToken() {
return ok("New token for " + au.getUserIdentifier() + " is " + newToken.getTokenString());

}

@GET
@Path(":me")
public Response getAuthenticatedUserByToken() {

String tokenFromRequestAPI = getRequestApiKey();

AuthenticatedUser authenticatedUser = findUserByApiToken(tokenFromRequestAPI);
if (authenticatedUser == null) {
return error(Response.Status.BAD_REQUEST, "User with token " + tokenFromRequestAPI + " not found.");
} else {
return ok(json(authenticatedUser));
}

}

}
26 changes: 26 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/BuiltinUsersIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.stream.Stream;
import javax.json.Json;
import javax.json.JsonObjectBuilder;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.OK;
import static javax.ws.rs.core.Response.Status.FORBIDDEN;
import static junit.framework.Assert.assertEquals;
Expand Down Expand Up @@ -72,6 +73,31 @@ public void testCreateTimeAndApiLastUse() {
.statusCode(OK.getStatusCode());

}

@Test
public void testFindByToken() {

Response createUser = UtilIT.createRandomUser();
createUser.prettyPrint();
createUser.then().assertThat()
.statusCode(OK.getStatusCode());

String username = UtilIT.getUsernameFromResponse(createUser);
String apiToken = UtilIT.getApiTokenFromResponse(createUser);

Response getUserAsJsonByToken = UtilIT.getAuthenticatedUserByToken(apiToken);

getUserAsJsonByToken.then().assertThat()
.statusCode(OK.getStatusCode());

getUserAsJsonByToken = UtilIT.getAuthenticatedUserByToken("badcode");
getUserAsJsonByToken.then().assertThat()
.body("status", equalTo("ERROR"))
.body("message", equalTo("User with token badcode not found."))
.statusCode(BAD_REQUEST.getStatusCode());

}


@Test
public void testLastApiUse() {
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,14 @@ static Response getAuthenticatedUser(String userIdentifier, String apiToken) {
.get("/api/admin/authenticatedUsers/" + userIdentifier);
return response;
}

static Response getAuthenticatedUserByToken(String apiToken) {
Response response = given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
.urlEncodingEnabled(false)
.get("/api/users/:me");
return response;
}

/**
* Used to the test the filter Authenticated Users API endpoint
Expand Down

0 comments on commit 3da9c40

Please sign in to comment.