oauth.signpost.OAuth Java Examples

The following examples show how to use oauth.signpost.OAuth. 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: OAuthInteractorTest.java    From Forage with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void retrieveAccessToken_success() throws OAuthCommunicationException, OAuthExpectationFailedException, OAuthNotAuthorizedException, OAuthMessageSignerException {
    Uri uri = mock(Uri.class);
    String verifier = "some verifier";
    when(uri.getQueryParameter(OAuth.OAUTH_VERIFIER)).thenReturn(verifier);

    String userTokenString = "some token";
    when(okHttpOAuthConsumer.getToken()).thenReturn(userTokenString);

    String userTokenSecretString = "some secret token";
    when(okHttpOAuthConsumer.getTokenSecret()).thenReturn(userTokenSecretString);


    TestSubscriber testSubscriber = new TestSubscriber();
    oAuthInteractor.retrieveAccessToken(uri)
            .subscribe(testSubscriber);

    testSubscriber.assertCompleted();

    verify(okHttpOAuthProvider, times(1)).retrieveAccessToken(okHttpOAuthConsumer, verifier);
    verify(userToken, times(1)).set(userTokenString);
    verify(userTokenSecret, times(1)).set(userTokenSecretString);
}
 
Example #2
Source File: OAuthInteractor.java    From Forage with Mozilla Public License 2.0 6 votes vote down vote up
public Completable retrieveAccessToken(final Uri callback) {
    return Completable.fromCallable(() -> {
        final String verifier = callback.getQueryParameter(OAuth.OAUTH_VERIFIER);

        // Denied OAuth authorization requests will not have an OAuth verifier
        // Throw an error and let the subscriber handle it downstream
        if (verifier == null) {
            throw new Exception("OAuth Request denied!");
        }

        // Makes a blocking HTTP request
        okHttpOAuthProvider.retrieveAccessToken(okHttpOAuthConsumer, verifier);

        return null;

    }).doOnCompleted(() -> {
        // okHttpOAuthProvider.retrieveAccessToken sets the token and secret on the
        // OAuthConsumer so all we have to do is store the values in shared preferences
        userToken.set(okHttpOAuthConsumer.getToken());
        userTokenSecret.set(okHttpOAuthConsumer.getTokenSecret());
    })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread());
}
 
Example #3
Source File: OAuth1Utils.java    From shimmer with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the request_token response from an initial oauth1 initiate request.
 *
 * @param requestTokenResponse - Response from external data provider
 * @return - Map with token and token secret.
 * @throws ShimException
 */
public static Map<String, String> parseRequestTokenResponse(
    HttpResponse requestTokenResponse) throws ShimException {

    String tokenString;
    try {
        tokenString = IOUtils.toString(requestTokenResponse.getEntity().getContent(), "UTF-8");
    } catch (IOException e) {
        throw new ShimException("Error reading request token", e);
    }
    HttpParameters responseParams = OAuth.decodeForm(tokenString);
    Map<String, String> token = new HashMap<>();
    token.put(
        OAuth.OAUTH_TOKEN,
        responseParams.getFirst(OAuth.OAUTH_TOKEN));
    token.put(
        OAuth.OAUTH_TOKEN_SECRET,
        responseParams.getFirst(OAuth.OAUTH_TOKEN_SECRET));
    return token;
}
 
Example #4
Source File: OAuthInteractorTest.java    From Forage with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void retrieveAccessToken_error() throws OAuthCommunicationException, OAuthExpectationFailedException, OAuthNotAuthorizedException, OAuthMessageSignerException {
    Uri uri = mock(Uri.class);
    when(uri.getQueryParameter(OAuth.OAUTH_VERIFIER)).thenReturn(null);

    TestSubscriber testSubscriber = new TestSubscriber();
    oAuthInteractor.retrieveAccessToken(uri)
            .subscribe(testSubscriber);

    // We don't really care what the error is just that we have one
    // Hacky way to see if there is an error (terminal event) but not success (completion)
    testSubscriber.assertTerminalEvent();
    testSubscriber.assertNotCompleted();
}
 
Example #5
Source File: MyActivity.java    From java-upwork with Apache License 2.0 5 votes vote down vote up
/**
 * Callback once we are done with the authorization of this app
 * @param intent
 */
@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    // Verify OAuth callback
    Uri uri = intent.getData();
    if (uri != null && uri.getScheme().equals(OAUTH_CALLBACK_SCHEME)) {
        String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);

        new UpworkRetrieveAccessTokenTask().execute(verifier);
    }
}
 
Example #6
Source File: OAuth1Shim.java    From shimmer with Apache License 2.0 4 votes vote down vote up
@Override
public AuthorizationResponse processRedirect(HttpServletRequest servletRequest) throws ShimException {

    // Fetch the access token.
    String stateKey = servletRequest.getParameter("state");
    String requestToken = servletRequest.getParameter(OAuth.OAUTH_TOKEN);
    final String requestVerifier = servletRequest.getParameter(OAuth.OAUTH_VERIFIER);

    AuthorizationRequestParameters authParams = authorizationRequestParametersRepo.findByStateKey(stateKey);
    if (authParams == null) {
        throw new ShimException("Invalid state, could not find corresponding auth parameters");
    }

    // Get the token secret from the original access request.
    String requestTokenSecret = authParams.getRequestParams().get(OAuth.OAUTH_TOKEN_SECRET);

    HttpResponse response;
    HttpRequestBase accessTokenRequest = null;
    try {
        accessTokenRequest = getAccessTokenRequest(getAccessTokenUrl(),
                requestToken, requestTokenSecret, new HashMap<String, String>() {{
                    put(OAuth.OAUTH_VERIFIER, requestVerifier);
                }});
        response = httpClient.execute(accessTokenRequest);
    }
    catch (IOException e) {
        e.printStackTrace();
        throw new ShimException("Could not retrieve response from token URL");
    }
    finally {
        if (accessTokenRequest != null) {
            accessTokenRequest.releaseConnection();
        }
    }
    Map<String, String> accessTokenParameters = OAuth1Utils.parseRequestTokenResponse(response);
    String accessToken = accessTokenParameters.get(OAuth.OAUTH_TOKEN);
    String accessTokenSecret = accessTokenParameters.get(OAuth.OAUTH_TOKEN_SECRET);

    if (accessToken == null) {
        throw new ShimException("Access token could not be retrieved");
    }

    AccessParameters accessParameters = new AccessParameters();
    accessParameters.setClientId(getClientSettings().getClientId());
    accessParameters.setClientSecret(getClientSettings().getClientSecret());
    accessParameters.setStateKey(stateKey);
    accessParameters.setUsername(authParams.getUsername());
    accessParameters.setAccessToken(accessToken);
    accessParameters.setTokenSecret(accessTokenSecret);
    accessParameters.setAdditionalParameters(new HashMap<String, Object>() {{
        put(OAuth.OAUTH_VERIFIER, requestVerifier);
    }});
    loadAdditionalAccessParameters(servletRequest, accessParameters);
    return AuthorizationResponse.authorized(accessParameters);
}