Search in sources :

Example 26 with OAuth20Service

use of com.github.scribejava.core.oauth.OAuth20Service in project sonarqube by SonarSource.

the class GitHubIdentityProvider method init.

@Override
public void init(InitContext context) {
    String state = context.generateCsrfState();
    OAuth20Service scribe = newScribeBuilder(context).defaultScope(getScope()).build(scribeApi);
    String url = scribe.getAuthorizationUrl(state);
    context.redirectTo(url);
}
Also used : OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service)

Example 27 with OAuth20Service

use of com.github.scribejava.core.oauth.OAuth20Service in project sonarqube by SonarSource.

the class GitLabIdentityProvider method onCallback.

private void onCallback(CallbackContext context) throws InterruptedException, ExecutionException, IOException {
    HttpServletRequest request = context.getRequest();
    OAuth20Service scribe = newScribeBuilder(context, gitLabSettings.syncUserGroups()).build(scribeApi);
    String code = request.getParameter(OAuthConstants.CODE);
    OAuth2AccessToken accessToken = scribe.getAccessToken(code);
    GsonUser user = gitLabRestClient.getUser(scribe, accessToken);
    UserIdentity.Builder builder = UserIdentity.builder().setProviderId(Long.toString(user.getId())).setProviderLogin(user.getUsername()).setName(user.getName()).setEmail(user.getEmail());
    if (gitLabSettings.syncUserGroups()) {
        builder.setGroups(getGroups(scribe, accessToken));
    }
    context.authenticate(builder.build());
    context.redirectToRequestedPage();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) UserIdentity(org.sonar.api.server.authentication.UserIdentity) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service)

Example 28 with OAuth20Service

use of com.github.scribejava.core.oauth.OAuth20Service in project scribejava by scribejava.

the class OkHttpHttpClientTest method setUp.

@Before
public void setUp() {
    final HttpClient client = new OkHttpHttpClient(new OkHttpClient());
    oAuthService = new OAuth20Service(null, new OAuthConfig("test", "test", null, null, null, null, null, null, null, null, client));
}
Also used : OkHttpClient(okhttp3.OkHttpClient) OkHttpClient(okhttp3.OkHttpClient) HttpClient(com.github.scribejava.core.httpclient.HttpClient) OAuthConfig(com.github.scribejava.core.model.OAuthConfig) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) Before(org.junit.Before)

Example 29 with OAuth20Service

use of com.github.scribejava.core.oauth.OAuth20Service in project pac4j by pac4j.

the class FacebookProfileCreator method retrieveUserProfileFromToken.

@Override
protected FacebookProfile retrieveUserProfileFromToken(final WebContext context, final OAuth2AccessToken accessToken) {
    final OAuth20ProfileDefinition<FacebookProfile, OAuth20Configuration> profileDefinition = (OAuth20ProfileDefinition<FacebookProfile, OAuth20Configuration>) configuration.getProfileDefinition();
    final FacebookConfiguration facebookConfiguration = (FacebookConfiguration) configuration;
    final String profileUrl = profileDefinition.getProfileUrl(accessToken, configuration);
    final OAuth20Service service = this.configuration.buildService(context, client, null);
    String body = sendRequestForData(service, accessToken, profileUrl, Verb.GET);
    if (body == null) {
        throw new HttpCommunicationException("Not data found for accessToken: " + accessToken);
    }
    final FacebookProfile profile = profileDefinition.extractUserProfile(body);
    addAccessTokenToProfile(profile, accessToken);
    if (profile != null && facebookConfiguration.isRequiresExtendedToken()) {
        String url = CommonHelper.addParameter(EXCHANGE_TOKEN_URL, OAuthConstants.CLIENT_ID, configuration.getKey());
        url = CommonHelper.addParameter(url, OAuthConstants.CLIENT_SECRET, configuration.getSecret());
        url = addExchangeToken(url, accessToken);
        final OAuthRequest request = createOAuthRequest(url, Verb.GET);
        final long t0 = System.currentTimeMillis();
        final Response response;
        final int code;
        try {
            response = service.execute(request);
            body = response.getBody();
            code = response.getCode();
        } catch (final IOException | InterruptedException | ExecutionException e) {
            throw new HttpCommunicationException("Error getting body:" + e.getMessage());
        }
        final long t1 = System.currentTimeMillis();
        logger.debug("Request took: " + (t1 - t0) + " ms for: " + url);
        logger.debug("response code: {} / response body: {}", code, body);
        if (code == 200) {
            logger.debug("Retrieve extended token from  {}", body);
            final OAuth2AccessToken extendedAccessToken;
            try {
                extendedAccessToken = ((DefaultApi20) configuration.getApi()).getAccessTokenExtractor().extract(response);
            } catch (IOException | OAuthException ex) {
                throw new HttpCommunicationException("Error extracting token: " + ex.getMessage());
            }
            logger.debug("Extended token: {}", extendedAccessToken);
            addAccessTokenToProfile(profile, extendedAccessToken);
        } else {
            logger.error("Cannot get extended token: {} / {}", code, body);
        }
    }
    return profile;
}
Also used : HttpCommunicationException(org.pac4j.core.exception.HttpCommunicationException) OAuthException(com.github.scribejava.core.exceptions.OAuthException) IOException(java.io.IOException) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) OAuth20ProfileDefinition(org.pac4j.oauth.profile.definition.OAuth20ProfileDefinition) DefaultApi20(com.github.scribejava.core.builder.api.DefaultApi20) ExecutionException(java.util.concurrent.ExecutionException) OAuth20Configuration(org.pac4j.oauth.config.OAuth20Configuration)

Example 30 with OAuth20Service

use of com.github.scribejava.core.oauth.OAuth20Service 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

OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)63 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)49 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)47 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)45 Response (com.github.scribejava.core.model.Response)45 Scanner (java.util.Scanner)40 Random (java.util.Random)17 HashMap (java.util.HashMap)6 OAuthException (com.github.scribejava.core.exceptions.OAuthException)4 NingHttpClientConfig (com.github.scribejava.httpclient.ning.NingHttpClientConfig)3 AsyncHttpClientConfig (com.ning.http.client.AsyncHttpClientConfig)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 AsyncSimpleTask (com.codepath.utils.AsyncSimpleTask)2 SalesforceToken (com.github.scribejava.apis.salesforce.SalesforceToken)2 OAuth1RequestToken (com.github.scribejava.core.model.OAuth1RequestToken)2 OAuth10aService (com.github.scribejava.core.oauth.OAuth10aService)2 OAuthBattleNetApi (com.greatmancode.legendarybot.plugins.wowlink.utils.OAuthBattleNetApi)2 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2