use of org.springframework.security.oauth.provider.ConsumerAuthentication in project spring-security-oauth by spring-projects.
the class UnauthenticatedRequestTokenProcessingFilter method onValidSignature.
protected void onValidSignature(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException {
//signature is verified; create the token, send the response.
ConsumerAuthentication authentication = (ConsumerAuthentication) SecurityContextHolder.getContext().getAuthentication();
OAuthProviderToken authToken = createOAuthToken(authentication);
if (!authToken.getConsumerKey().equals(authentication.getConsumerDetails().getConsumerKey())) {
throw new IllegalStateException("The consumer key associated with the created auth token is not valid for the authenticated consumer.");
}
String tokenValue = authToken.getValue();
String callback = authentication.getOAuthParameters().get(OAuthConsumerParameter.oauth_callback.toString());
StringBuilder responseValue = new StringBuilder(OAuthProviderParameter.oauth_token.toString()).append('=').append(OAuthCodec.oauthEncode(tokenValue)).append('&').append(OAuthProviderParameter.oauth_token_secret.toString()).append('=').append(OAuthCodec.oauthEncode(authToken.getSecret()));
if (callback != null) {
responseValue.append('&').append(OAuthProviderParameter.oauth_callback_confirmed.toString()).append("=true");
}
response.setContentType(getResponseContentType());
response.getWriter().print(responseValue.toString());
response.flushBuffer();
}
use of org.springframework.security.oauth.provider.ConsumerAuthentication 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.ConsumerAuthentication 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);
}
use of org.springframework.security.oauth.provider.ConsumerAuthentication 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));
}
use of org.springframework.security.oauth.provider.ConsumerAuthentication in project spring-security-oauth by spring-projects.
the class AccessTokenProcessingFilterTests method testCreateOAuthToken.
/**
* tests creating the oauth token.
*/
@Test
public void testCreateOAuthToken() throws Exception {
ConsumerCredentials creds = new ConsumerCredentials("key", "sig", "meth", "base", "tok");
when(consumerDetails.getAuthorities()).thenReturn(new ArrayList<GrantedAuthority>());
AccessTokenProcessingFilter filter = new AccessTokenProcessingFilter();
filter.setTokenServices(tokenServices);
when(tokenServices.createAccessToken("tok")).thenReturn(token);
ConsumerAuthentication authentication = new ConsumerAuthentication(consumerDetails, creds);
assertSame(token, filter.createOAuthToken(authentication));
}
Aggregations