Search in sources :

Example 1 with OAuth2Exception

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.");
    }
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) AuthenticatedUserDisplayInfo(edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo) OAuth2UserRecord(edu.harvard.iq.dataverse.authorization.providers.oauth2.OAuth2UserRecord) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) OAuth2Exception(edu.harvard.iq.dataverse.authorization.providers.oauth2.OAuth2Exception) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service)

Aggregations

OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)1 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)1 Response (com.github.scribejava.core.model.Response)1 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)1 AuthenticatedUserDisplayInfo (edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo)1 OAuth2Exception (edu.harvard.iq.dataverse.authorization.providers.oauth2.OAuth2Exception)1 OAuth2UserRecord (edu.harvard.iq.dataverse.authorization.providers.oauth2.OAuth2UserRecord)1