use of io.dockstore.webservice.core.TokenScope in project dockstore by dockstore.
the class TokenResource method addOrcidToken.
@POST
@Timed
@UnitOfWork
@Path("/orcid.org")
@JsonView(TokenViews.User.class)
@ApiOperation(value = orcidSummary, authorizations = { @Authorization(value = JWT_SECURITY_DEFINITION_NAME) }, notes = orcidDescription, response = Token.class)
@Operation(operationId = "addOrcidToken", summary = orcidSummary, description = orcidDescription, security = @SecurityRequirement(name = "bearer"))
public Token addOrcidToken(@ApiParam(hidden = true) @Parameter(hidden = true, name = "user") @Auth final User user, @QueryParam("code") final String code) {
String accessToken;
String refreshToken;
String username;
String orcid;
String scope;
long expirationTime;
if (code == null || code.isEmpty()) {
throw new CustomWebApplicationException("Please provide an access code", HttpStatus.SC_BAD_REQUEST);
}
final AuthorizationCodeFlow flow = new AuthorizationCodeFlow.Builder(BearerToken.authorizationHeaderAccessMethod(), HTTP_TRANSPORT, JSON_FACTORY, new GenericUrl(orcidUrl + "oauth/token"), new ClientParametersAuthentication(orcidClientID, orcidClientSecret), orcidClientID, orcidUrl + "/authorize").build();
try {
TokenResponse tokenResponse = flow.newTokenRequest(code).setScopes(Collections.singletonList(orcidScope)).setRequestInitializer(request -> request.getHeaders().setAccept(MediaType.APPLICATION_JSON)).execute();
accessToken = tokenResponse.getAccessToken();
refreshToken = tokenResponse.getRefreshToken();
// ORCID API returns the username and orcid id along with the tokens
// get them to store in the token and user tables
username = tokenResponse.get("name").toString();
orcid = tokenResponse.get("orcid").toString();
scope = tokenResponse.getScope();
Instant instant = Instant.now();
instant.plusSeconds(tokenResponse.getExpiresInSeconds());
expirationTime = instant.getEpochSecond();
} catch (IOException e) {
LOG.error("Retrieving accessToken was unsuccessful" + e.getMessage(), e);
throw new CustomWebApplicationException(e.getMessage(), HttpStatus.SC_BAD_REQUEST);
}
if (user != null) {
// save the ORCID to the enduser table
User byId = userDAO.findById(user.getId());
byId.setOrcid(orcid);
Token token = new Token();
token.setTokenSource(TokenType.ORCID_ORG);
token.setContent(accessToken);
token.setRefreshToken(refreshToken);
token.setUserId(user.getId());
token.setUsername(username);
TokenScope tokenScope = TokenScope.getEnumByString(scope);
if (tokenScope == null) {
LOG.error("Could not convert scope string to enum: " + scope);
throw new CustomWebApplicationException("Could not save ORCID token, contact Dockstore team", HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
token.setScope(tokenScope);
token.setExpirationTime(expirationTime);
checkIfAccountHasBeenLinked(token, TokenType.ORCID_ORG);
long create = tokenDAO.create(token);
LOG.info("ORCID token created for {}", user.getUsername());
return tokenDAO.findById(create);
} else {
LOG.info("Could not find user");
throw new CustomWebApplicationException("User not found", HttpStatus.SC_CONFLICT);
}
}
Aggregations