Search in sources :

Example 1 with ConsumerCredentials

use of org.springframework.security.oauth.provider.ConsumerCredentials in project spring-security-oauth by spring-projects.

the class OAuthProcessingFilterTests method testValidateSignature.

/**
	 * test validating the signature.
	 */
@Test
public void testValidateSignature() throws Exception {
    OAuthProviderProcessingFilter filter = new OAuthProviderProcessingFilter() {

        @Override
        protected void onValidSignature(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        }
    };
    ConsumerDetails details = mock(ConsumerDetails.class);
    SignatureSecret secret = mock(SignatureSecret.class);
    OAuthProviderToken token = mock(OAuthProviderToken.class);
    OAuthSignatureMethod sigMethod = mock(OAuthSignatureMethod.class);
    ConsumerCredentials credentials = new ConsumerCredentials("id", "sig", "method", "base", "token");
    when(details.getAuthorities()).thenReturn(new ArrayList<GrantedAuthority>());
    when(details.getSignatureSecret()).thenReturn(secret);
    filter.setTokenServices(tokenServices);
    when(tokenServices.getToken("token")).thenReturn(token);
    filter.setSignatureMethodFactory(signatureFactory);
    when(token.getSecret()).thenReturn("shhh!!!");
    when(signatureFactory.getSignatureMethod("method", secret, "shhh!!!")).thenReturn(sigMethod);
    ConsumerAuthentication authentication = new ConsumerAuthentication(details, credentials);
    filter.validateSignature(authentication);
    verify(sigMethod).verify("base", "sig");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SignatureSecret(org.springframework.security.oauth.common.signature.SignatureSecret) OAuthProviderToken(org.springframework.security.oauth.provider.token.OAuthProviderToken) ConsumerCredentials(org.springframework.security.oauth.provider.ConsumerCredentials) FilterChain(javax.servlet.FilterChain) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ConsumerAuthentication(org.springframework.security.oauth.provider.ConsumerAuthentication) HttpServletResponse(javax.servlet.http.HttpServletResponse) OAuthSignatureMethod(org.springframework.security.oauth.common.signature.OAuthSignatureMethod) ConsumerDetails(org.springframework.security.oauth.provider.ConsumerDetails) Test(org.junit.Test)

Example 2 with ConsumerCredentials

use of org.springframework.security.oauth.provider.ConsumerCredentials in project spring-security-oauth by spring-projects.

the class ProtectedResourceProcessingFilterTests method testOnValidSignature.

/**
	 * test onValidSignature
	 */
@Test
public void testOnValidSignature() throws Exception {
    ProtectedResourceProcessingFilter filter = new ProtectedResourceProcessingFilter();
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    FilterChain chain = mock(FilterChain.class);
    ConsumerCredentials creds = new ConsumerCredentials("key", "sig", "meth", "base", "tok");
    ConsumerAuthentication authentication = new ConsumerAuthentication(mock(ConsumerDetails.class), creds);
    authentication.setAuthenticated(true);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    OAuthProviderTokenServices tokenServices = mock(OAuthProviderTokenServices.class);
    OAuthAccessProviderToken token = mock(OAuthAccessProviderToken.class);
    filter.setTokenServices(tokenServices);
    when(tokenServices.getToken("tok")).thenReturn(token);
    when(token.isAccessToken()).thenReturn(true);
    Authentication userAuthentication = mock(Authentication.class);
    when(token.getUserAuthentication()).thenReturn(userAuthentication);
    filter.onValidSignature(request, response, chain);
    verify(chain).doFilter(request, response);
    assertSame(userAuthentication, SecurityContextHolder.getContext().getAuthentication());
    SecurityContextHolder.getContext().setAuthentication(null);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ProtectedResourceProcessingFilter(org.springframework.security.oauth.provider.filter.ProtectedResourceProcessingFilter) OAuthProviderTokenServices(org.springframework.security.oauth.provider.token.OAuthProviderTokenServices) ConsumerCredentials(org.springframework.security.oauth.provider.ConsumerCredentials) ConsumerAuthentication(org.springframework.security.oauth.provider.ConsumerAuthentication) Authentication(org.springframework.security.core.Authentication) FilterChain(javax.servlet.FilterChain) ConsumerAuthentication(org.springframework.security.oauth.provider.ConsumerAuthentication) HttpServletResponse(javax.servlet.http.HttpServletResponse) OAuthAccessProviderToken(org.springframework.security.oauth.provider.token.OAuthAccessProviderToken) ConsumerDetails(org.springframework.security.oauth.provider.ConsumerDetails) Test(org.junit.Test)

Example 3 with ConsumerCredentials

use of org.springframework.security.oauth.provider.ConsumerCredentials in project spring-security-oauth by spring-projects.

the class OAuthProviderProcessingFilter method doFilter.

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    if (!skipProcessing(request)) {
        if (requiresAuthentication(request, response, chain)) {
            if (!allowMethod(request.getMethod().toUpperCase())) {
                if (log.isDebugEnabled()) {
                    log.debug("Method " + request.getMethod() + " not supported.");
                }
                response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
                return;
            }
            try {
                Map<String, String> oauthParams = getProviderSupport().parseParameters(request);
                if (parametersAreAdequate(oauthParams)) {
                    if (log.isDebugEnabled()) {
                        StringBuilder builder = new StringBuilder("OAuth parameters parsed: ");
                        for (String param : oauthParams.keySet()) {
                            builder.append(param).append('=').append(oauthParams.get(param)).append(' ');
                        }
                        log.debug(builder.toString());
                    }
                    String consumerKey = oauthParams.get(OAuthConsumerParameter.oauth_consumer_key.toString());
                    if (consumerKey == null) {
                        throw new InvalidOAuthParametersException(messages.getMessage("OAuthProcessingFilter.missingConsumerKey", "Missing consumer key."));
                    }
                    //load the consumer details.
                    ConsumerDetails consumerDetails = getConsumerDetailsService().loadConsumerByConsumerKey(consumerKey);
                    if (log.isDebugEnabled()) {
                        log.debug("Consumer details loaded for " + consumerKey + ": " + consumerDetails);
                    }
                    //validate the parameters for the consumer.
                    validateOAuthParams(consumerDetails, oauthParams);
                    if (log.isDebugEnabled()) {
                        log.debug("Parameters validated.");
                    }
                    //extract the credentials.
                    String token = oauthParams.get(OAuthConsumerParameter.oauth_token.toString());
                    String signatureMethod = oauthParams.get(OAuthConsumerParameter.oauth_signature_method.toString());
                    String signature = oauthParams.get(OAuthConsumerParameter.oauth_signature.toString());
                    String signatureBaseString = getProviderSupport().getSignatureBaseString(request);
                    ConsumerCredentials credentials = new ConsumerCredentials(consumerKey, signature, signatureMethod, signatureBaseString, token);
                    //create an authentication request.
                    ConsumerAuthentication authentication = new ConsumerAuthentication(consumerDetails, credentials, oauthParams);
                    authentication.setDetails(createDetails(request, consumerDetails));
                    Authentication previousAuthentication = SecurityContextHolder.getContext().getAuthentication();
                    try {
                        //set the authentication request (unauthenticated) into the context.
                        SecurityContextHolder.getContext().setAuthentication(authentication);
                        //validate the signature.
                        validateSignature(authentication);
                        //mark the authentication request as validated.
                        authentication.setSignatureValidated(true);
                        //mark that processing has been handled.
                        request.setAttribute(OAUTH_PROCESSING_HANDLED, Boolean.TRUE);
                        if (log.isDebugEnabled()) {
                            log.debug("Signature validated.");
                        }
                        //go.
                        onValidSignature(request, response, chain);
                    } finally {
                        //clear out the consumer authentication to make sure it doesn't get cached.
                        resetPreviousAuthentication(previousAuthentication);
                    }
                } else if (!isIgnoreInadequateCredentials()) {
                    throw new InvalidOAuthParametersException(messages.getMessage("OAuthProcessingFilter.missingCredentials", "Inadequate OAuth consumer credentials."));
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("Supplied OAuth parameters are inadequate. Ignoring.");
                    }
                    chain.doFilter(request, response);
                }
            } catch (AuthenticationException ae) {
                fail(request, response, ae);
            } catch (ServletException e) {
                if (e.getRootCause() instanceof AuthenticationException) {
                    fail(request, response, (AuthenticationException) e.getRootCause());
                } else {
                    throw e;
                }
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Request does not require authentication.  OAuth processing skipped.");
            }
            chain.doFilter(servletRequest, servletResponse);
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Processing explicitly skipped.");
        }
        chain.doFilter(servletRequest, servletResponse);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) InvalidOAuthParametersException(org.springframework.security.oauth.provider.InvalidOAuthParametersException) ConsumerCredentials(org.springframework.security.oauth.provider.ConsumerCredentials) AuthenticationException(org.springframework.security.core.AuthenticationException) ConsumerAuthentication(org.springframework.security.oauth.provider.ConsumerAuthentication) Authentication(org.springframework.security.core.Authentication) ConsumerAuthentication(org.springframework.security.oauth.provider.ConsumerAuthentication) HttpServletResponse(javax.servlet.http.HttpServletResponse) ConsumerDetails(org.springframework.security.oauth.provider.ConsumerDetails)

Example 4 with ConsumerCredentials

use of org.springframework.security.oauth.provider.ConsumerCredentials in project spring-security-oauth by spring-projects.

the class UnauthenticatedRequestTokenProcessingFilterTests method testOnValidSignature.

/**
	 * test onValidSignature
	 */
@Test
public void testOnValidSignature() throws Exception {
    final OAuthProviderToken authToken = mock(OAuthProviderToken.class);
    UnauthenticatedRequestTokenProcessingFilter filter = new UnauthenticatedRequestTokenProcessingFilter() {

        @Override
        protected OAuthProviderToken createOAuthToken(ConsumerAuthentication authentication) {
            return authToken;
        }
    };
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    FilterChain filterChain = mock(FilterChain.class);
    ConsumerCredentials creds = new ConsumerCredentials("key", "sig", "meth", "base", "tok");
    ConsumerDetails consumerDetails = mock(ConsumerDetails.class);
    when(authToken.getConsumerKey()).thenReturn("chi");
    when(authToken.getValue()).thenReturn("tokvalue");
    when(authToken.getSecret()).thenReturn("shhhhhh");
    when(consumerDetails.getAuthorities()).thenReturn(new ArrayList<GrantedAuthority>());
    when(consumerDetails.getConsumerKey()).thenReturn("chi");
    response.setContentType("text/plain;charset=utf-8");
    StringWriter writer = new StringWriter();
    when(response.getWriter()).thenReturn(new PrintWriter(writer));
    response.flushBuffer();
    TreeMap<String, String> params = new TreeMap<String, String>();
    params.put(OAuthConsumerParameter.oauth_callback.toString(), "mycallback");
    ConsumerAuthentication authentication = new ConsumerAuthentication(consumerDetails, creds, params);
    authentication.setAuthenticated(true);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    filter.onValidSignature(request, response, filterChain);
    assertEquals("oauth_token=tokvalue&oauth_token_secret=shhhhhh&oauth_callback_confirmed=true", writer.toString());
    SecurityContextHolder.getContext().setAuthentication(null);
}
Also used : ConsumerCredentials(org.springframework.security.oauth.provider.ConsumerCredentials) FilterChain(javax.servlet.FilterChain) GrantedAuthority(org.springframework.security.core.GrantedAuthority) HttpServletResponse(javax.servlet.http.HttpServletResponse) TreeMap(java.util.TreeMap) UnauthenticatedRequestTokenProcessingFilter(org.springframework.security.oauth.provider.filter.UnauthenticatedRequestTokenProcessingFilter) HttpServletRequest(javax.servlet.http.HttpServletRequest) OAuthProviderToken(org.springframework.security.oauth.provider.token.OAuthProviderToken) StringWriter(java.io.StringWriter) ConsumerAuthentication(org.springframework.security.oauth.provider.ConsumerAuthentication) ConsumerDetails(org.springframework.security.oauth.provider.ConsumerDetails) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 5 with ConsumerCredentials

use of org.springframework.security.oauth.provider.ConsumerCredentials in project spring-security-oauth by spring-projects.

the class UnauthenticatedRequestTokenProcessingFilterTests method testCreateOAuthToken.

/**
	 * tests creating the oauth token.
	 */
@Test
public void testCreateOAuthToken() throws Exception {
    ConsumerDetails consumerDetails = mock(ConsumerDetails.class);
    ConsumerCredentials creds = new ConsumerCredentials("key", "sig", "meth", "base", "tok");
    OAuthProviderTokenServices tokenServices = mock(OAuthProviderTokenServices.class);
    OAuthAccessProviderToken token = mock(OAuthAccessProviderToken.class);
    UnauthenticatedRequestTokenProcessingFilter filter = new UnauthenticatedRequestTokenProcessingFilter();
    filter.setTokenServices(tokenServices);
    when(consumerDetails.getConsumerKey()).thenReturn("chi");
    when(consumerDetails.getAuthorities()).thenReturn(new ArrayList<GrantedAuthority>());
    when(tokenServices.createUnauthorizedRequestToken("chi", "callback")).thenReturn(token);
    TreeMap<String, String> map = new TreeMap<String, String>();
    map.put(OAuthConsumerParameter.oauth_callback.toString(), "callback");
    ConsumerAuthentication authentication = new ConsumerAuthentication(consumerDetails, creds, map);
    assertSame(token, filter.createOAuthToken(authentication));
}
Also used : UnauthenticatedRequestTokenProcessingFilter(org.springframework.security.oauth.provider.filter.UnauthenticatedRequestTokenProcessingFilter) OAuthProviderTokenServices(org.springframework.security.oauth.provider.token.OAuthProviderTokenServices) ConsumerCredentials(org.springframework.security.oauth.provider.ConsumerCredentials) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ConsumerAuthentication(org.springframework.security.oauth.provider.ConsumerAuthentication) OAuthAccessProviderToken(org.springframework.security.oauth.provider.token.OAuthAccessProviderToken) TreeMap(java.util.TreeMap) ConsumerDetails(org.springframework.security.oauth.provider.ConsumerDetails) Test(org.junit.Test)

Aggregations

ConsumerAuthentication (org.springframework.security.oauth.provider.ConsumerAuthentication)6 ConsumerCredentials (org.springframework.security.oauth.provider.ConsumerCredentials)6 Test (org.junit.Test)5 ConsumerDetails (org.springframework.security.oauth.provider.ConsumerDetails)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 GrantedAuthority (org.springframework.security.core.GrantedAuthority)4 FilterChain (javax.servlet.FilterChain)3 TreeMap (java.util.TreeMap)2 Authentication (org.springframework.security.core.Authentication)2 UnauthenticatedRequestTokenProcessingFilter (org.springframework.security.oauth.provider.filter.UnauthenticatedRequestTokenProcessingFilter)2 OAuthAccessProviderToken (org.springframework.security.oauth.provider.token.OAuthAccessProviderToken)2 OAuthProviderToken (org.springframework.security.oauth.provider.token.OAuthProviderToken)2 OAuthProviderTokenServices (org.springframework.security.oauth.provider.token.OAuthProviderTokenServices)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 AuthenticationException (org.springframework.security.core.AuthenticationException)1 OAuthSignatureMethod (org.springframework.security.oauth.common.signature.OAuthSignatureMethod)1 SignatureSecret (org.springframework.security.oauth.common.signature.SignatureSecret)1 InvalidOAuthParametersException (org.springframework.security.oauth.provider.InvalidOAuthParametersException)1