Search in sources :

Example 1 with OAuthProviderTokenServices

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

the class OAuthUserAuthorizationProcessingFilterTests method testAttemptAuthentication.

/**
	 * tests the attempt to authenticate.
	 */
@Test
public void testAttemptAuthentication() throws Exception {
    UserAuthorizationProcessingFilter filter = new UserAuthorizationProcessingFilter("/");
    OAuthVerifierServices vs = mock(OAuthVerifierServices.class);
    filter.setVerifierServices(vs);
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    Authentication authentication = mock(Authentication.class);
    OAuthProviderTokenServices tokenServices = mock(OAuthProviderTokenServices.class);
    filter.setTokenServices(tokenServices);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    when(request.getParameter("requestToken")).thenReturn("tok");
    OAuthProviderTokenImpl token = new OAuthProviderTokenImpl();
    token.setCallbackUrl("callback");
    when(tokenServices.getToken("tok")).thenReturn(token);
    when(authentication.isAuthenticated()).thenReturn(false);
    try {
        filter.attemptAuthentication(request, response);
        fail();
    } catch (InsufficientAuthenticationException e) {
    }
    verify(request).setAttribute(UserAuthorizationProcessingFilter.CALLBACK_ATTRIBUTE, "callback");
    reset(request);
    when(authentication.isAuthenticated()).thenReturn(true);
    when(request.getParameter("requestToken")).thenReturn("tok");
    when(tokenServices.getToken("tok")).thenReturn(token);
    when(vs.createVerifier()).thenReturn("verifier");
    tokenServices.authorizeRequestToken("tok", "verifier", authentication);
    filter.setTokenServices(tokenServices);
    filter.attemptAuthentication(request, response);
    verify(request).setAttribute(UserAuthorizationProcessingFilter.CALLBACK_ATTRIBUTE, "callback");
    verify(request).setAttribute(UserAuthorizationProcessingFilter.VERIFIER_ATTRIBUTE, "verifier");
    SecurityContextHolder.getContext().setAuthentication(null);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) OAuthProviderTokenServices(org.springframework.security.oauth.provider.token.OAuthProviderTokenServices) Authentication(org.springframework.security.core.Authentication) HttpServletResponse(javax.servlet.http.HttpServletResponse) OAuthVerifierServices(org.springframework.security.oauth.provider.verifier.OAuthVerifierServices) InsufficientAuthenticationException(org.springframework.security.authentication.InsufficientAuthenticationException) OAuthProviderTokenImpl(org.springframework.security.oauth.provider.token.OAuthProviderTokenImpl) Test(org.junit.Test)

Example 2 with OAuthProviderTokenServices

use of org.springframework.security.oauth.provider.token.OAuthProviderTokenServices 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 OAuthProviderTokenServices

use of org.springframework.security.oauth.provider.token.OAuthProviderTokenServices 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

Test (org.junit.Test)3 OAuthProviderTokenServices (org.springframework.security.oauth.provider.token.OAuthProviderTokenServices)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Authentication (org.springframework.security.core.Authentication)2 ConsumerAuthentication (org.springframework.security.oauth.provider.ConsumerAuthentication)2 ConsumerCredentials (org.springframework.security.oauth.provider.ConsumerCredentials)2 ConsumerDetails (org.springframework.security.oauth.provider.ConsumerDetails)2 OAuthAccessProviderToken (org.springframework.security.oauth.provider.token.OAuthAccessProviderToken)2 TreeMap (java.util.TreeMap)1 FilterChain (javax.servlet.FilterChain)1 InsufficientAuthenticationException (org.springframework.security.authentication.InsufficientAuthenticationException)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 ProtectedResourceProcessingFilter (org.springframework.security.oauth.provider.filter.ProtectedResourceProcessingFilter)1 UnauthenticatedRequestTokenProcessingFilter (org.springframework.security.oauth.provider.filter.UnauthenticatedRequestTokenProcessingFilter)1 OAuthProviderTokenImpl (org.springframework.security.oauth.provider.token.OAuthProviderTokenImpl)1 OAuthVerifierServices (org.springframework.security.oauth.provider.verifier.OAuthVerifierServices)1