com.github.scribejava.core.model.OAuth1RequestToken Java Examples

The following examples show how to use com.github.scribejava.core.model.OAuth1RequestToken. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TwitterStartAuthFlowPath.java    From PYX-Reloaded with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    exchange.startBlocking();
    if (exchange.isInIoThread()) {
        exchange.dispatch(this);
        return;
    }

    try {
        OAuth1RequestToken token = helper.requestToken();
        CookieImpl cookie = new CookieImpl("PYX-Twitter-Token", token.getRawResponse());
        cookie.setMaxAge(COOKIE_MAX_AGE);
        exchange.setResponseCookie(cookie);
        exchange.getResponseHeaders().add(Headers.LOCATION, helper.authorizationUrl(token) + "&force_login=false");
        exchange.setStatusCode(StatusCodes.TEMPORARY_REDIRECT);
    } catch (Throwable ex) {
        logger.error("Failed processing the request." + exchange, ex);
        throw ex;
    }
}
 
Example #2
Source File: MainActivity.java    From jam-collaboration-sample with Apache License 2.0 6 votes vote down vote up
private void processOAuthVerifier(final OAuth1RequestToken requestToken, final String oauthVerifier) {
    final OAuth10aService service = JamAuthConfig.instance().getOAuth10aService();

    AsyncTask network = new AsyncTask() {
        @Override
        protected Object doInBackground(Object[] params) {
            final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, oauthVerifier);

            JamAuthConfig.instance().storeCredentials(accessToken);

            return true;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);

            goToMainApp();
        }
    };
    network.execute();
}
 
Example #3
Source File: UserController.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
private OauthResponse auth(Provider provider, String oauthVerifier) {
    Optional<String> optional = sessionInfo.get(REQUEST_TOKEN_PREFIX + provider.name());
    if (optional.isEmpty()) throw new NotFoundException("missing request token,provider=" + provider.name());
    RequestTokenModel model = JSON.fromJSON(optional.get(), RequestTokenModel.class);
    OAuth1RequestToken requestToken = new OAuth1RequestToken(model.token, model.tokenSecret, model.oauthCallbackConfirmed, model.rawResponse);
    return oauth10aService.auth(provider, oauthVerifier, requestToken);
}
 
Example #4
Source File: Oauth10aService.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
public String redirectUri(Provider provider) {
    OAuth10aService service = service(provider);
    try {
        OAuth1RequestToken requestToken = service.getRequestToken();
        sessionInfo.put(REQUEST_TOKEN_PREFIX + provider.name(), JSON.toJSON(requestToken));
        return service.getAuthorizationUrl(requestToken);
    } catch (InterruptedException | ExecutionException | IOException e) {
        throw new NotAuthorizedException("login failure", e);
    }
}
 
Example #5
Source File: TwitterCallbackPath.java    From PYX-Reloaded with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    exchange.startBlocking();
    if (exchange.isInIoThread()) {
        exchange.dispatch(this);
        return;
    }

    try {
        String token = Utils.extractParam(exchange, "oauth_token");
        String verifier = Utils.extractParam(exchange, "oauth_verifier");

        if (token == null || verifier == null)
            throw new IllegalArgumentException("Missing token or verifier!");

        Cookie tokensCookie = exchange.getRequestCookies().get("PYX-Twitter-Token");
        if (tokensCookie == null)
            throw new IllegalArgumentException("Missing 'PYX-Twitter-Token' cookie!");

        List<NameValuePair> tokens = URLEncodedUtils.parse(tokensCookie.getValue(), Charset.forName("UTF-8"));
        if (!Objects.equals(token, Utils.get(tokens, "oauth_token")))
            throw new IllegalStateException("Missing token in cookie or tokens don't match!");

        String secret = Utils.get(tokens, "oauth_token_secret");
        if (secret == null)
            throw new IllegalArgumentException("Missing token secret in cookie!");

        OAuth1AccessToken accessToken = helper.accessToken(new OAuth1RequestToken(token, secret), verifier);
        CookieImpl cookie = new CookieImpl("PYX-Twitter-Token", accessToken.getRawResponse());
        cookie.setMaxAge(COOKIE_MAX_AGE);
        exchange.setResponseCookie(cookie);
        exchange.getResponseHeaders().add(Headers.LOCATION, REDIRECT_LOCATION);
        exchange.setStatusCode(StatusCodes.TEMPORARY_REDIRECT);
    } catch (Throwable ex) {
        logger.error("Failed processing the request: " + exchange, ex);
        throw ex;
    }
}
 
Example #6
Source File: OAuthManagerFragmentController.java    From react-native-oauth with MIT License 5 votes vote down vote up
public Load1AccessTokenTask(
  OAuthManagerFragmentController ctrl, 
  AdvancedWebView view,
  OAuth1RequestToken requestToken,
  String oauthVerifier
) {
  super(ctrl, view);
  this.oauthVerifier = oauthVerifier;
}
 
Example #7
Source File: MainActivity.java    From jam-collaboration-sample with Apache License 2.0 5 votes vote down vote up
public void beginOAuthLogin() {
    final OAuth10aService service = JamAuthConfig.instance().getOAuth10aService();

    AsyncTask network = new AsyncTask() {
        @Override
        protected Object doInBackground(Object[] params) {
            return service.getRequestToken();
        }

        @Override
        protected void onPostExecute(Object object) {
            super.onPostExecute(object);

            if (object != null) {
                final OAuth1RequestToken requestToken = (OAuth1RequestToken) object;
                String authUrl = service.getAuthorizationUrl(requestToken);

                JamOAuthDialog dialog = new JamOAuthDialog(MainActivity.this, authUrl);
                dialog.oauthListener = new JamOAuthDialog.ConfirmedOAuthAccessListener() {
                    @Override
                    public void onFinishOAuthAccess(String oauthToken, String oauthVerifier) {
                        processOAuthVerifier(requestToken, oauthVerifier);
                    }
                };
                dialog.show();
            }
        }
    };

    network.execute();
}
 
Example #8
Source File: DefaultJsonConverter.java    From vertx-pac4j with Apache License 2.0 5 votes vote down vote up
public DefaultJsonConverter() {
    mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);

    mapper.addMixIn(OAuth1RequestToken.class, OAuth1RequestTokenMixin.class)
        .addMixIn(BearerAccessToken.class, BearerAccessTokenMixin.class)
        .addMixIn(Scope.Value.class, ValueMixin.class)
        .addMixIn(Token.class, TokenMixin.class);
}
 
Example #9
Source File: OAuthBaseClient.java    From android-oauth-handler with MIT License 5 votes vote down vote up
protected @Nullable Token getOAuth1RequestToken() {
    int oAuthVersion = prefs.getInt(OAuthConstants.VERSION, 0);

    if (oAuthVersion == 1) {
        return new OAuth1RequestToken(prefs.getString(OAUTH1_REQUEST_TOKEN, ""),
                prefs.getString(OAUTH1_REQUEST_TOKEN_SECRET, ""));
    }
    return null;
}
 
Example #10
Source File: OAuthManagerFragmentController.java    From react-native-oauth with MIT License 4 votes vote down vote up
public void setRequestToken(
  final OAuth1RequestToken requestToken
) {
  this.oauth1RequestToken = requestToken;
}
 
Example #11
Source File: SAPJamApi.java    From jam-collaboration-sample with Apache License 2.0 4 votes vote down vote up
@Override
public String getAuthorizationUrl(OAuth1RequestToken requestToken) {
    return authorizeUrl + "?oauth_token=" + requestToken.getToken();
}
 
Example #12
Source File: OAuthBaseClient.java    From android-oauth-handler with MIT License 4 votes vote down vote up
public OAuthBaseClient(Context c, final BaseApi apiInstance, String consumerUrl, final String consumerKey, final String consumerSecret, @Nullable String scope, String callbackUrl) {
    this.baseUrl = consumerUrl;
    this.callbackUrl = callbackUrl;
    tokenClient = new OAuthTokenClient(apiInstance, consumerKey,
            consumerSecret, callbackUrl, scope, new OAuthTokenClient.OAuthTokenHandler() {

        // Store request token and launch the authorization URL in the browser
        @Override
        public void onReceivedRequestToken(Token requestToken, String authorizeUrl, String oAuthVersion) {
            if (requestToken != null) {
                if (oAuthVersion == OAUTH1_VERSION) {  // store for OAuth1.0a
                    OAuth1RequestToken oAuth1RequestToken = (OAuth1RequestToken) requestToken;
                    editor.putString(OAUTH1_REQUEST_TOKEN, oAuth1RequestToken.getToken());
                    editor.putString(OAUTH1_REQUEST_TOKEN_SECRET, oAuth1RequestToken.getTokenSecret());
                    editor.putInt(OAuthConstants.VERSION, 1);
                    editor.commit();
                }
            }
            // Launch the authorization URL in the browser
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authorizeUrl));
            if (requestIntentFlags != -1) {
                intent.setFlags(requestIntentFlags);
            }
            OAuthBaseClient.this.context.startActivity(intent);
        }

        // Store the access token in preferences, set the token in the tokenClient and fire the success callback
        @Override
        public void onReceivedAccessToken(Token accessToken, String oAuthVersion) {

            if (oAuthVersion == OAUTH1_VERSION) {
                OAuth1AccessToken oAuth1AccessToken = (OAuth1AccessToken) accessToken;

                tokenClient.setAccessToken(accessToken);
                instantiateClient(consumerKey, consumerSecret, oAuth1AccessToken);
                editor.putString(OAuthConstants.TOKEN, oAuth1AccessToken.getToken());
                editor.putString(OAuthConstants.TOKEN_SECRET, oAuth1AccessToken.getTokenSecret());
                editor.putInt(OAuthConstants.VERSION, 1);
                editor.commit();
            } else if (oAuthVersion == OAUTH2_VERSION) {
                OAuth2AccessToken oAuth2AccessToken = (OAuth2AccessToken) accessToken;
                instantiateClient(consumerKey, consumerSecret, oAuth2AccessToken);
                tokenClient.setAccessToken(accessToken);
                editor.putString(OAuthConstants.TOKEN, oAuth2AccessToken.getAccessToken());
                editor.putString(OAuthConstants.SCOPE, oAuth2AccessToken.getScope());
                editor.putString(OAuthConstants.REFRESH_TOKEN, oAuth2AccessToken.getRefreshToken());
                editor.putInt(OAuthConstants.VERSION, 2);
                editor.commit();

            }
            accessHandler.onLoginSuccess();
        }

        @Override
        public void onFailure(Exception e) {
            accessHandler.onLoginFailure(e);
        }

    });

    this.context = c;
    // Store preferences namespaced by the class and consumer key used
    this.prefs = this.context.getSharedPreferences("OAuth_" + apiInstance.getClass().getSimpleName() + "_" + consumerKey, 0);
    this.editor = this.prefs.edit();
    // Set access token in the tokenClient if already stored in preferences
    Token accessToken = this.checkAccessToken();
    if (accessToken != null) {
        tokenClient.setAccessToken(accessToken);
        instantiateClient(consumerKey, consumerSecret, accessToken);
    }
}
 
Example #13
Source File: OAuthTokenClient.java    From android-oauth-handler with MIT License 4 votes vote down vote up
public void fetchAccessToken(final Token requestToken, final Uri uri) {

        Uri authorizedUri = uri;

        if (service.getVersion() == "1.0") {
            // Use verifier token to fetch access token

            if (authorizedUri.getQuery().contains(OAuthConstants.VERIFIER)) {
                String oauth_verifier = authorizedUri.getQueryParameter(OAuthConstants.VERIFIER);
                OAuth1RequestToken oAuth1RequestToken = (OAuth1RequestToken) requestToken;
                OAuth10aService oAuth10aService = (OAuth10aService) service;

                oAuth10aService.getAccessTokenAsync(oAuth1RequestToken, oauth_verifier,
                        new OAuthAsyncRequestCallback<OAuth1AccessToken>() {

                            @Override
                            public void onCompleted(OAuth1AccessToken oAuth1AccessToken) {
                                setAccessToken(oAuth1AccessToken);
                                handler.onReceivedAccessToken(oAuth1AccessToken, service.getVersion());
                            }

                            @Override
                            public void onThrowable(Throwable e) {
                                handler.onFailure(new OAuthException(e.getMessage()));
                            }
                        });

            }
            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;
                oAuth20Service.getAccessToken(code, new OAuthAsyncRequestCallback<OAuth2AccessToken>() {
                    @Override
                    public void onCompleted(OAuth2AccessToken accessToken) {
                        setAccessToken(accessToken);
                        handler.onReceivedAccessToken(accessToken, service.getVersion());

                    }

                    @Override
                    public void onThrowable(Throwable t) {

                    }
                });
            }
            else { // verifier was null
                handler.onFailure(new OAuthException("No code was returned with uri '" + uri + "' " +
                        "and access token cannot be retrieved"));
            }
        }
    }