Example usage for org.springframework.security.oauth.consumer OAuthConsumerToken setSecret

List of usage examples for org.springframework.security.oauth.consumer OAuthConsumerToken setSecret

Introduction

In this page you can find the example usage for org.springframework.security.oauth.consumer OAuthConsumerToken setSecret.

Prototype

public void setSecret(String secret) 

Source Link

Document

The token secret.

Usage

From source file:org.cloudfoundry.identity.uaa.client.OAuthClientAuthenticationFilterTests.java

private void setUpContext(String tokenName, String secretName, String keyName, String sharedName) {
    resource.setId("foo");
    String consumerKey = System.getProperty(keyName);
    Assume.assumeNotNull(consumerKey);/*w ww  .  ja v  a 2s.c  om*/
    String sharedSecret = System.getProperty(sharedName);
    Assume.assumeNotNull(sharedSecret);
    String accessToken = System.getProperty(tokenName);
    Assume.assumeNotNull(accessToken);
    String secret = System.getProperty(secretName);
    Assume.assumeNotNull(accessToken);
    OAuthSecurityContextImpl context = new OAuthSecurityContextImpl();
    OAuthConsumerToken token = new OAuthConsumerToken();
    resource.setConsumerKey(consumerKey);
    resource.setSharedSecret(new SharedConsumerSecretImpl(sharedSecret));
    token.setValue(accessToken);
    token.setSecret(secret);
    context.setAccessTokens(Collections.singletonMap("foo", token));
    OAuthSecurityContextHolder.setContext(context);
}

From source file:org.springframework.security.oauth.consumer.client.CoreOAuthConsumerSupport.java

/**
 * Get the consumer token with the given parameters and URL. The determination of whether the retrieved token
 * is an access token depends on whether a request token is provided.
 *
 * @param details      The resource details.
 * @param tokenURL     The token URL./*  w ww . ja  va2s .  com*/
 * @param httpMethod   The http method.
 * @param requestToken The request token, or null if none.
 * @param additionalParameters The additional request parameter.
 * @return The token.
 */
protected OAuthConsumerToken getTokenFromProvider(ProtectedResourceDetails details, URL tokenURL,
        String httpMethod, OAuthConsumerToken requestToken, Map<String, String> additionalParameters) {
    boolean isAccessToken = requestToken != null;
    if (!isAccessToken) {
        //create an empty token to make a request for a new unauthorized request token.
        requestToken = new OAuthConsumerToken();
    }

    TreeMap<String, String> requestHeaders = new TreeMap<String, String>();
    if ("POST".equalsIgnoreCase(httpMethod)) {
        requestHeaders.put("Content-Type", "application/x-www-form-urlencoded");
    }
    InputStream inputStream = readResource(details, tokenURL, httpMethod, requestToken, additionalParameters,
            requestHeaders);
    String tokenInfo;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = inputStream.read(buffer);
        while (len >= 0) {
            out.write(buffer, 0, len);
            len = inputStream.read(buffer);
        }

        tokenInfo = new String(out.toByteArray(), "UTF-8");
    } catch (IOException e) {
        throw new OAuthRequestFailedException("Unable to read the token.", e);
    }

    StringTokenizer tokenProperties = new StringTokenizer(tokenInfo, "&");
    Map<String, String> tokenPropertyValues = new TreeMap<String, String>();
    while (tokenProperties.hasMoreElements()) {
        try {
            String tokenProperty = (String) tokenProperties.nextElement();
            int equalsIndex = tokenProperty.indexOf('=');
            if (equalsIndex > 0) {
                String propertyName = OAuthCodec.oauthDecode(tokenProperty.substring(0, equalsIndex));
                String propertyValue = OAuthCodec.oauthDecode(tokenProperty.substring(equalsIndex + 1));
                tokenPropertyValues.put(propertyName, propertyValue);
            } else {
                tokenProperty = OAuthCodec.oauthDecode(tokenProperty);
                tokenPropertyValues.put(tokenProperty, null);
            }
        } catch (DecoderException e) {
            throw new OAuthRequestFailedException("Unable to decode token parameters.");
        }
    }

    String tokenValue = tokenPropertyValues.remove(OAuthProviderParameter.oauth_token.toString());
    if (tokenValue == null) {
        throw new OAuthRequestFailedException("OAuth provider failed to return a token.");
    }

    String tokenSecret = tokenPropertyValues.remove(OAuthProviderParameter.oauth_token_secret.toString());
    if (tokenSecret == null) {
        throw new OAuthRequestFailedException("OAuth provider failed to return a token secret.");
    }

    OAuthConsumerToken consumerToken = new OAuthConsumerToken();
    consumerToken.setValue(tokenValue);
    consumerToken.setSecret(tokenSecret);
    consumerToken.setResourceId(details.getId());
    consumerToken.setAccessToken(isAccessToken);
    if (!tokenPropertyValues.isEmpty()) {
        consumerToken.setAdditionalParameters(tokenPropertyValues);
    }
    return consumerToken;
}