Skip to content

Commit

Permalink
refactor name, add comments, use in Datasets toolparam call
Browse files Browse the repository at this point in the history
  • Loading branch information
qqmyers committed Nov 3, 2023
1 parent 8e6b19f commit 25451f6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public void explore(GuestbookResponse guestbookResponse, FileMetadata fmd, Exter
User user = session.getUser();
DatasetVersion version = fmd.getDatasetVersion();
if (version.isDraft() || fmd.getDatasetVersion().isDeaccessioned() || (fmd.getDataFile().isRestricted()) || (FileUtil.isActivelyEmbargoed(fmd))) {
apiToken = authService.getApiTokenForUser(user);
apiToken = authService.getValidApiTokenForUser(user);
}
DataFile dataFile = null;
if (fmd != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/harvard/iq/dataverse/FilePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ public String preview(ExternalTool externalTool) {
ApiToken apiToken = null;
User user = session.getUser();
if (fileMetadata.getDatasetVersion().isDraft() || fileMetadata.getDatasetVersion().isDeaccessioned() || (fileMetadata.getDataFile().isRestricted()) || (FileUtil.isActivelyEmbargoed(fileMetadata))) {
apiToken=authService.getApiTokenForUser(user);
apiToken=authService.getValidApiTokenForUser(user);
}
if(externalTool == null){
return "";
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -3834,10 +3834,7 @@ public Response getExternalToolDVParams(@Context ContainerRequestContext crc,
}
ApiToken apiToken = null;
User u = getRequestUser(crc);
if (u instanceof AuthenticatedUser) {
apiToken = authSvc.findApiTokenByUser((AuthenticatedUser) u);
}

apiToken = authSvc.getValidApiTokenForUser(u);

ExternalToolHandler eth = new ExternalToolHandler(externalTool, target.getDataset(), apiToken, locale);
return ok(eth.createPostBody(eth.getParams(JsonUtil.getJsonObject(externalTool.getToolParameters()))));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/harvard/iq/dataverse/api/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ public Response getExternalToolFMParams(@Context ContainerRequestContext crc, @P
}
ApiToken apiToken = null;
User user = getRequestUser(crc);
apiToken = authSvc.getApiTokenForUser(user);
apiToken = authSvc.getValidApiTokenForUser(user);
FileMetadata target = fileSvc.findFileMetadata(fmid);
if (target == null) {
return error(BAD_REQUEST, "FileMetadata not found.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,13 @@ public List <WorkflowComment> getWorkflowCommentsByAuthenticatedUser(Authenticat
return query.getResultList();
}

/**
* This method gets a valid api token for an AuthenticatedUser, creating a new
* token if one doesn't exist or if the token is expired.
*
* @param user
* @return
*/
public ApiToken getValidApiTokenForAuthenticatedUser(AuthenticatedUser user) {
ApiToken apiToken = null;
apiToken = findApiTokenByUser(user);
Expand All @@ -961,8 +968,15 @@ public ApiToken getValidApiTokenForAuthenticatedUser(AuthenticatedUser user) {
return apiToken;
}

//Gets a token for an AuthenticatedUser or a PrivateUrlUser
public ApiToken getApiTokenForUser(User user) {
/**
* Gets a token for an AuthenticatedUser or a PrivateUrlUser. It will create a
* new token if needed for an AuthenticatedUser. Note that, for a PrivateUrlUser, this method creates a token
* with a temporary AuthenticateUser that only has a userIdentifier - needed in generating signed Urls.
* @param user
* @return a token or null (i.e. if the user is not an AuthenticatedUser or PrivateUrlUser)
*/

public ApiToken getValidApiTokenForUser(User user) {
ApiToken apiToken = null;
if (user instanceof AuthenticatedUser) {
apiToken = getValidApiTokenForAuthenticatedUser((AuthenticatedUser) user);
Expand Down

0 comments on commit 25451f6

Please sign in to comment.