use of edu.harvard.iq.dataverse.authorization.providers.oauth2.OAuth2Exception in project dataverse by IQSS.
the class OrcidOAuth2AP method getUserRecord.
@Override
public OAuth2UserRecord getUserRecord(String code, String state, String redirectUrl) throws IOException, OAuth2Exception {
OAuth20Service service = getService(state, redirectUrl);
OAuth2AccessToken accessToken = service.getAccessToken(code);
if (!accessToken.getScope().contains(scope)) {
// We did not get the permissions on the scope we need. Abort and inform the user.
throw new OAuth2Exception(200, BundleUtil.getStringFromBundle("auth.providers.orcid.insufficientScope"), "");
}
String orcidNumber = extractOrcidNumber(accessToken.getRawResponse());
final String userEndpoint = getUserEndpoint(accessToken);
final OAuthRequest request = new OAuthRequest(Verb.GET, userEndpoint, service);
request.addHeader("Authorization", "Bearer " + accessToken.getAccessToken());
request.setCharset("UTF-8");
final Response response = request.send();
int responseCode = response.getCode();
final String body = response.getBody();
logger.log(Level.FINE, "In getUserRecord. Body: {0}", body);
if (responseCode == 200) {
final ParsedUserResponse parsed = parseUserResponse(body);
AuthenticatedUserDisplayInfo orgData = getOrganizationalData(userEndpoint, accessToken.getAccessToken(), service);
parsed.displayInfo.setAffiliation(orgData.getAffiliation());
parsed.displayInfo.setPosition(orgData.getPosition());
return new OAuth2UserRecord(getId(), orcidNumber, parsed.username, OAuth2TokenData.from(accessToken), parsed.displayInfo, parsed.emails);
} else {
throw new OAuth2Exception(responseCode, body, "Error getting the user info record.");
}
}
Aggregations