Search in sources :

Example 11 with OAuthConsumerToken

use of org.springframework.security.oauth.consumer.OAuthConsumerToken in project spring-security-oauth by spring-projects.

the class HttpSessionOAuthRememberMeServicesTests method testEmptySession.

@Test
public void testEmptySession() {
    MockHttpSession mockHttpSession = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setSession(mockHttpSession);
    HttpSessionOAuthRememberMeServices oAuthRememberMeService = new HttpSessionOAuthRememberMeServices();
    Map<String, OAuthConsumerToken> tokens = oAuthRememberMeService.loadRememberedTokens(request, response);
    Assert.assertNull(tokens);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) OAuthConsumerToken(org.springframework.security.oauth.consumer.OAuthConsumerToken) Test(org.junit.Test)

Example 12 with OAuthConsumerToken

use of org.springframework.security.oauth.consumer.OAuthConsumerToken in project spring-security-oauth by spring-projects.

the class HttpSessionOAuthRememberMeServicesTests method testStoreEverything.

@Test
public void testStoreEverything() {
    MockHttpSession mockHttpSession = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setSession(mockHttpSession);
    HttpSessionOAuthRememberMeServices oAuthRememberMeService = new HttpSessionOAuthRememberMeServices();
    Map<String, OAuthConsumerToken> tokens = new HashMap<String, OAuthConsumerToken>();
    {
        OAuthConsumerToken token = new OAuthConsumerToken();
        token.setAccessToken(false);
        tokens.put("resourceID1", token);
    }
    {
        OAuthConsumerToken token = new OAuthConsumerToken();
        token.setAccessToken(true);
        tokens.put("resourceID2", token);
    }
    oAuthRememberMeService.rememberTokens(tokens, request, response);
    Assert.assertEquals(1, oAuthRememberMeService.loadRememberedTokens(request, response).size());
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) OAuthConsumerToken(org.springframework.security.oauth.consumer.OAuthConsumerToken) Test(org.junit.Test)

Example 13 with OAuthConsumerToken

use of org.springframework.security.oauth.consumer.OAuthConsumerToken in project spring-security-oauth by spring-projects.

the class HttpSessionOAuthRememberMeServicesTests method testStoreRequestTokensOnly.

@Test
public void testStoreRequestTokensOnly() {
    MockHttpSession mockHttpSession = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setSession(mockHttpSession);
    HttpSessionOAuthRememberMeServices oAuthRememberMeService = new HttpSessionOAuthRememberMeServices();
    Map<String, OAuthConsumerToken> tokens = new HashMap<String, OAuthConsumerToken>();
    {
        OAuthConsumerToken token = new OAuthConsumerToken();
        token.setAccessToken(false);
        tokens.put("resourceID1", token);
    }
    {
        OAuthConsumerToken token = new OAuthConsumerToken();
        token.setAccessToken(true);
        tokens.put("resourceID2", token);
    }
    oAuthRememberMeService.rememberTokens(tokens, request, response);
    Map<String, OAuthConsumerToken> storedTokens = oAuthRememberMeService.loadRememberedTokens(request, response);
    Assert.assertEquals(1, storedTokens.size());
    Assert.assertNotNull(storedTokens.get("resourceID1"));
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) OAuthConsumerToken(org.springframework.security.oauth.consumer.OAuthConsumerToken) Test(org.junit.Test)

Example 14 with OAuthConsumerToken

use of org.springframework.security.oauth.consumer.OAuthConsumerToken in project spring-security-oauth by spring-projects.

the class CoreOAuthConsumerSupport method getTokenFromProvider.

/**
 * 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.
 * @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;
}
Also used : OAuthRequestFailedException(org.springframework.security.oauth.consumer.OAuthRequestFailedException) OAuthConsumerToken(org.springframework.security.oauth.consumer.OAuthConsumerToken) DecoderException(org.apache.commons.codec.DecoderException)

Example 15 with OAuthConsumerToken

use of org.springframework.security.oauth.consumer.OAuthConsumerToken in project spring-security-oauth by spring-projects.

the class OAuthClientHttpRequestFactory method createRequest.

public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
    OAuthSecurityContext context = OAuthSecurityContextHolder.getContext();
    if (context == null) {
        context = new OAuthSecurityContextImpl();
    }
    Map<String, OAuthConsumerToken> accessTokens = context.getAccessTokens();
    OAuthConsumerToken accessToken = accessTokens == null ? null : accessTokens.get(this.resource.getId());
    boolean useAuthHeader = this.resource.isAcceptsAuthorizationHeader();
    if (!useAuthHeader) {
        String queryString = this.support.getOAuthQueryString(this.resource, accessToken, uri.toURL(), httpMethod.name(), this.additionalOAuthParameters);
        String uriValue = String.valueOf(uri);
        uri = URI.create((uriValue.contains("?") ? uriValue.substring(0, uriValue.indexOf('?')) : uriValue) + "?" + queryString);
    }
    ClientHttpRequest req = delegate.createRequest(uri, httpMethod);
    if (useAuthHeader) {
        String authHeader = this.support.getAuthorizationHeader(this.resource, accessToken, uri.toURL(), httpMethod.name(), this.additionalOAuthParameters);
        req.getHeaders().add("Authorization", authHeader);
    }
    Map<String, String> additionalHeaders = this.resource.getAdditionalRequestHeaders();
    if (additionalHeaders != null) {
        for (Map.Entry<String, String> header : additionalHeaders.entrySet()) {
            req.getHeaders().add(header.getKey(), header.getValue());
        }
    }
    return req;
}
Also used : OAuthSecurityContextImpl(org.springframework.security.oauth.consumer.OAuthSecurityContextImpl) OAuthSecurityContext(org.springframework.security.oauth.consumer.OAuthSecurityContext) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) HashMap(java.util.HashMap) Map(java.util.Map) OAuthConsumerToken(org.springframework.security.oauth.consumer.OAuthConsumerToken)

Aggregations

OAuthConsumerToken (org.springframework.security.oauth.consumer.OAuthConsumerToken)18 HashMap (java.util.HashMap)12 Test (org.junit.Test)12 Map (java.util.Map)9 TreeMap (java.util.TreeMap)7 ProtectedResourceDetails (org.springframework.security.oauth.consumer.ProtectedResourceDetails)7 URL (java.net.URL)6 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 MockHttpSession (org.springframework.mock.web.MockHttpSession)4 LinkedHashSet (java.util.LinkedHashSet)3 Set (java.util.Set)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 AccessTokenRequiredException (org.springframework.security.oauth.consumer.AccessTokenRequiredException)3 OAuthRequestFailedException (org.springframework.security.oauth.consumer.OAuthRequestFailedException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 HttpSession (javax.servlet.http.HttpSession)2 OAuthSecurityContext (org.springframework.security.oauth.consumer.OAuthSecurityContext)2