Search in sources :

Example 61 with OAuth20Service

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

the class BitbucketIdentityProvider method init.

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

Example 62 with OAuth20Service

use of com.github.scribejava.core.oauth.OAuth20Service in project alf.io by alfio-event.

the class MollieConnectManager method storeConnectedAccountId.

@Override
public AccessTokenResponseDetails storeConnectedAccountId(String code, int organizationId) {
    try {
        ConfigurationLevel configurationLevel = ConfigurationLevel.organization(organizationId);
        var options = configurationManager.getFor(Set.of(MOLLIE_API_KEY, MOLLIE_CONNECT_CLIENT_ID, MOLLIE_CONNECT_CLIENT_SECRET, MOLLIE_CONNECT_CALLBACK, BASE_URL), configurationLevel);
        OAuth20Service service = new ServiceBuilder(options.get(MOLLIE_CONNECT_CLIENT_ID).getRequiredValue()).apiSecret(options.get(MOLLIE_CONNECT_CLIENT_SECRET).getRequiredValue()).callback(options.get(MOLLIE_CONNECT_CALLBACK).getRequiredValue()).build(new MollieConnectApi());
        OAuth2AccessToken accessTokenResponse = service.getAccessToken(code);
        var refreshToken = accessTokenResponse.getRefreshToken();
        if (refreshToken != null) {
            configurationManager.saveConfig(Configuration.from(organizationId, MOLLIE_CONNECT_REFRESH_TOKEN), refreshToken);
        }
        return new AccessTokenResponseDetails(accessTokenResponse.getAccessToken(), refreshToken, null, true);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        log.warn("Request interrupted while retrieving access token", e);
        return new AccessTokenResponseDetails(null, null, e.getMessage(), false);
    } catch (Exception e) {
        log.warn("Got exception while retrieving access token", e);
        return new AccessTokenResponseDetails(null, null, e.getMessage(), false);
    }
}
Also used : ConfigurationLevel(alfio.manager.system.ConfigurationLevel) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) AccessTokenResponseDetails(alfio.util.oauth2.AccessTokenResponseDetails) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder)

Example 63 with OAuth20Service

use of com.github.scribejava.core.oauth.OAuth20Service in project android-oauth-handler by codepath.

the class OAuthAsyncHttpClient method fetchAccessToken.

// Get the access token by exchanging the requestToken to the defined URL
// Once receiving the access token, fires the onReceivedAccessToken method on the handler
public void fetchAccessToken(final Token requestToken, final Uri uri) {
    new AsyncSimpleTask(new AsyncSimpleTask.AsyncSimpleTaskHandler() {

        Exception e = null;

        public void doInBackground() {
            // Fetch the verifier code from redirect url parameters
            Uri authorizedUri = uri;
            try {
                if (service.getVersion() == "1.0") {
                    if (authorizedUri.getQuery().contains(OAuthConstants.VERIFIER)) {
                        String oauth_verifier = authorizedUri.getQueryParameter(OAuthConstants.VERIFIER);
                        OAuth1RequestToken oAuth1RequestToken = (OAuth1RequestToken) requestToken;
                        OAuth10aService oAuth10aService = (OAuth10aService) service;
                        accessToken = oAuth10aService.getAccessToken(oAuth1RequestToken, oauth_verifier);
                    } else {
                        // verifier was null
                        throw new OAuthException("No verifier code was returned with uri '" + uri + "' " + "and access token cannot be retrieved");
                    }
                } else if (service.getVersion() == "2.0") {
                    if (authorizedUri.getQuery().contains(OAuthConstants.CODE)) {
                        String code = authorizedUri.getQueryParameter(OAuthConstants.CODE);
                        OAuth20Service oAuth20Service = (OAuth20Service) service;
                        accessToken = oAuth20Service.getAccessToken(code);
                    } else {
                        // verifier was null
                        throw new OAuthException("No code was returned with uri '" + uri + "' " + "and access token cannot be retrieved");
                    }
                }
            } catch (Exception e) {
                this.e = e;
            }
        }

        public void onPostExecute() {
            if (e != null) {
                handler.onFailure(e);
            } else {
                setAccessToken(accessToken);
                handler.onReceivedAccessToken(accessToken, service.getVersion());
            }
        }
    });
}
Also used : OAuth1RequestToken(com.github.scribejava.core.model.OAuth1RequestToken) OAuthException(com.github.scribejava.core.exceptions.OAuthException) AsyncSimpleTask(com.codepath.utils.AsyncSimpleTask) OAuth10aService(com.github.scribejava.core.oauth.OAuth10aService) Uri(android.net.Uri) OAuthException(com.github.scribejava.core.exceptions.OAuthException) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service)

Example 64 with OAuth20Service

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

the class AccountService method login.

@RequestMapping("/login")
public OAuthResponse login() {
    UUID uuid = UUID.randomUUID();
    State state = new State();
    state.setUuid(uuid);
    state.setApiVersion(RuneLiteAPI.getVersion());
    OAuth20Service service = new ServiceBuilder().apiKey(oauthClientId).apiSecret(oauthClientSecret).scope(SCOPE).callback(RL_OAUTH_URL).state(gson.toJson(state)).build(GoogleApi20.instance());
    String authorizationUrl = service.getAuthorizationUrl();
    OAuthResponse lr = new OAuthResponse();
    lr.setOauthUrl(authorizationUrl);
    lr.setUid(uuid);
    return lr;
}
Also used : UUID(java.util.UUID) OAuthResponse(net.runelite.http.api.account.OAuthResponse) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 65 with OAuth20Service

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

the class AccountService method callback.

@RequestMapping("/callback")
public Object callback(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) String error, @RequestParam String code, @RequestParam("state") String stateStr) throws InterruptedException, ExecutionException, IOException {
    if (error != null) {
        logger.info("Error in oauth callback: {}", error);
        return null;
    }
    State state = gson.fromJson(stateStr, State.class);
    logger.info("Got authorization code {} for uuid {}", code, state.getUuid());
    OAuth20Service service = new ServiceBuilder().apiKey(oauthClientId).apiSecret(oauthClientSecret).scope(SCOPE).callback(RL_OAUTH_URL).state(gson.toJson(state)).build(GoogleApi20.instance());
    OAuth2AccessToken accessToken = service.getAccessToken(code);
    // Access user info
    OAuthRequest orequest = new OAuthRequest(Verb.GET, USERINFO);
    service.signRequest(accessToken, orequest);
    Response oresponse = service.execute(orequest);
    if (oresponse.getCode() / 100 != 2) {
        // Could be a forged result
        return null;
    }
    UserInfo userInfo = gson.fromJson(oresponse.getBody(), UserInfo.class);
    logger.info("Got user info: {}", userInfo);
    try (Connection con = sql2o.open()) {
        con.createQuery("insert ignore into users (username) values (:username)").addParameter("username", userInfo.getEmail()).executeUpdate();
        UserEntry user = con.createQuery("select id from users where username = :username").addParameter("username", userInfo.getEmail()).executeAndFetchFirst(UserEntry.class);
        if (user == null) {
            logger.warn("Unable to find newly created user session");
            // that's weird
            return null;
        }
        // insert session
        con.createQuery("insert ignore into sessions (user, uuid) values (:user, :uuid)").addParameter("user", user.getId()).addParameter("uuid", state.getUuid().toString()).executeUpdate();
        logger.info("Created session for user {}", userInfo.getEmail());
    }
    response.sendRedirect(RL_REDIR);
    notifySession(state.getUuid(), userInfo.getEmail());
    return "";
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) LoginResponse(net.runelite.http.api.ws.messages.LoginResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) OAuthResponse(net.runelite.http.api.account.OAuthResponse) Response(com.github.scribejava.core.model.Response) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) Connection(org.sql2o.Connection) UserEntry(net.runelite.http.service.account.beans.UserEntry) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)66 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)51 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)50 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 AccessTokenResponseDetails (alfio.util.oauth2.AccessTokenResponseDetails)3 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