use of org.springframework.security.oauth.provider.token.OAuthAccessProviderToken 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);
}
use of org.springframework.security.oauth.provider.token.OAuthAccessProviderToken in project spring-security-oauth by spring-projects.
the class ProtectedResourceProcessingFilter method onValidSignature.
protected void onValidSignature(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
ConsumerAuthentication authentication = (ConsumerAuthentication) SecurityContextHolder.getContext().getAuthentication();
String token = authentication.getConsumerCredentials().getToken();
OAuthAccessProviderToken accessToken = null;
if (StringUtils.hasText(token)) {
OAuthProviderToken authToken = getTokenServices().getToken(token);
if (authToken == null) {
throw new AccessDeniedException("Invalid access token.");
} else if (!authToken.isAccessToken()) {
throw new AccessDeniedException("Token should be an access token.");
} else if (authToken instanceof OAuthAccessProviderToken) {
accessToken = (OAuthAccessProviderToken) authToken;
}
} else if ((!(authentication.getConsumerDetails() instanceof ExtraTrustConsumerDetails)) || ((ExtraTrustConsumerDetails) authentication.getConsumerDetails()).isRequiredToObtainAuthenticatedToken()) {
throw new InvalidOAuthParametersException(messages.getMessage("ProtectedResourceProcessingFilter.missingToken", "Missing auth token."));
}
Authentication userAuthentication = authHandler.createAuthentication(request, authentication, accessToken);
SecurityContextHolder.getContext().setAuthentication(userAuthentication);
chain.doFilter(request, response);
}
use of org.springframework.security.oauth.provider.token.OAuthAccessProviderToken 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));
}
Aggregations