use of org.springframework.security.core.GrantedAuthority in project spring-security by spring-projects.
the class AbstractAuthenticationTokenTests method testAuthoritiesAreImmutable.
@Test(expected = UnsupportedOperationException.class)
public void testAuthoritiesAreImmutable() {
MockAuthenticationImpl token = new MockAuthenticationImpl("Test", "Password", authorities);
List<GrantedAuthority> gotAuthorities = (List<GrantedAuthority>) token.getAuthorities();
assertThat(gotAuthorities).isNotSameAs(authorities);
gotAuthorities.set(0, new SimpleGrantedAuthority("ROLE_SUPER_USER"));
}
use of org.springframework.security.core.GrantedAuthority in project spring-security by spring-projects.
the class PreAuthenticatedAuthenticationTokenDeserializer method deserialize.
/**
* This method construct {@link PreAuthenticatedAuthenticationToken} object from serialized json.
* @param jp the JsonParser
* @param ctxt the DeserializationContext
* @return the user
* @throws IOException if a exception during IO occurs
* @throws JsonProcessingException if an error during JSON processing occurs
*/
@Override
public PreAuthenticatedAuthenticationToken deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
PreAuthenticatedAuthenticationToken token = null;
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
JsonNode jsonNode = mapper.readTree(jp);
Boolean authenticated = readJsonNode(jsonNode, "authenticated").asBoolean();
JsonNode principalNode = readJsonNode(jsonNode, "principal");
Object principal = null;
if (principalNode.isObject()) {
principal = mapper.readValue(principalNode.toString(), new TypeReference<User>() {
});
} else {
principal = principalNode.asText();
}
Object credentials = readJsonNode(jsonNode, "credentials").asText();
List<GrantedAuthority> authorities = mapper.readValue(readJsonNode(jsonNode, "authorities").toString(), new TypeReference<List<GrantedAuthority>>() {
});
if (authenticated) {
token = new PreAuthenticatedAuthenticationToken(principal, credentials, authorities);
} else {
token = new PreAuthenticatedAuthenticationToken(principal, credentials);
}
token.setDetails(readJsonNode(jsonNode, "details"));
return token;
}
use of org.springframework.security.core.GrantedAuthority in project spring-security by spring-projects.
the class SwitchUserFilter method getSourceAuthentication.
/**
* Find the original <code>Authentication</code> object from the current user's
* granted authorities. A successfully switched user should have a
* <code>SwitchUserGrantedAuthority</code> that contains the original source user
* <code>Authentication</code> object.
*
* @param current The current <code>Authentication</code> object
*
* @return The source user <code>Authentication</code> object or <code>null</code>
* otherwise.
*/
private Authentication getSourceAuthentication(Authentication current) {
Authentication original = null;
// iterate over granted authorities and find the 'switch user' authority
Collection<? extends GrantedAuthority> authorities = current.getAuthorities();
for (GrantedAuthority auth : authorities) {
// check for switch user type of authority
if (auth instanceof SwitchUserGrantedAuthority) {
original = ((SwitchUserGrantedAuthority) auth).getSource();
this.logger.debug("Found original switch user granted authority [" + original + "]");
}
}
return original;
}
use of org.springframework.security.core.GrantedAuthority 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");
}
use of org.springframework.security.core.GrantedAuthority in project spring-security-oauth by spring-projects.
the class OAuthProcessingFilterTests method testDoFilter.
/**
* tests do filter.
*/
@Test
public void testDoFilter() throws Exception {
final boolean[] triggers = new boolean[2];
Arrays.fill(triggers, false);
OAuthProviderProcessingFilter filter = new OAuthProviderProcessingFilter() {
@Override
protected boolean requiresAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) {
return true;
}
protected void onValidSignature(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(null, null);
}
@Override
protected void validateOAuthParams(ConsumerDetails consumerDetails, Map<String, String> oauthParams) throws InvalidOAuthParametersException {
triggers[0] = true;
}
@Override
protected void validateSignature(ConsumerAuthentication authentication) throws AuthenticationException {
triggers[1] = true;
}
@Override
protected void fail(HttpServletRequest request, HttpServletResponse response, AuthenticationException failure) throws IOException, ServletException {
throw failure;
}
@Override
protected Object createDetails(HttpServletRequest request, ConsumerDetails consumerDetails) {
return null;
}
@Override
protected void resetPreviousAuthentication(Authentication previousAuthentication) {
// no-op
}
@Override
protected boolean skipProcessing(HttpServletRequest request) {
return false;
}
};
filter.setProviderSupport(providerSupport);
filter.setConsumerDetailsService(consumerDetailsService);
filter.setNonceServices(nonceServices);
filter.setSignatureMethodFactory(signatureFactory);
filter.setTokenServices(tokenServices);
when(request.getMethod()).thenReturn("DELETE");
filter.doFilter(request, response, filterChain);
verify(response).sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
assertFalse(triggers[0]);
assertFalse(triggers[1]);
Arrays.fill(triggers, false);
when(request.getMethod()).thenReturn("GET");
HashMap<String, String> requestParams = new HashMap<String, String>();
when(providerSupport.parseParameters(request)).thenReturn(requestParams);
try {
filter.doFilter(request, response, filterChain);
fail("should have required a consumer key.");
} catch (InvalidOAuthParametersException e) {
assertFalse(triggers[0]);
assertFalse(triggers[1]);
Arrays.fill(triggers, false);
}
when(request.getMethod()).thenReturn("GET");
requestParams = new HashMap<String, String>();
requestParams.put(OAuthConsumerParameter.oauth_consumer_key.toString(), "consumerKey");
when(providerSupport.parseParameters(request)).thenReturn(requestParams);
ConsumerDetails consumerDetails = mock(ConsumerDetails.class);
when(consumerDetails.getAuthorities()).thenReturn(new ArrayList<GrantedAuthority>());
when(consumerDetailsService.loadConsumerByConsumerKey("consumerKey")).thenReturn(consumerDetails);
requestParams.put(OAuthConsumerParameter.oauth_token.toString(), "tokenvalue");
requestParams.put(OAuthConsumerParameter.oauth_signature_method.toString(), "methodvalue");
requestParams.put(OAuthConsumerParameter.oauth_signature.toString(), "signaturevalue");
when(providerSupport.getSignatureBaseString(request)).thenReturn("sigbasestring");
filter.doFilter(request, response, filterChain);
verify(filterChain).doFilter(null, null);
verify(request).setAttribute(OAuthProviderProcessingFilter.OAUTH_PROCESSING_HANDLED, Boolean.TRUE);
ConsumerAuthentication authentication = (ConsumerAuthentication) SecurityContextHolder.getContext().getAuthentication();
assertSame(consumerDetails, authentication.getConsumerDetails());
assertEquals("tokenvalue", authentication.getConsumerCredentials().getToken());
assertEquals("methodvalue", authentication.getConsumerCredentials().getSignatureMethod());
assertEquals("signaturevalue", authentication.getConsumerCredentials().getSignature());
assertEquals("sigbasestring", authentication.getConsumerCredentials().getSignatureBaseString());
assertEquals("consumerKey", authentication.getConsumerCredentials().getConsumerKey());
assertTrue(authentication.isSignatureValidated());
SecurityContextHolder.getContext().setAuthentication(null);
assertTrue(triggers[0]);
assertTrue(triggers[1]);
Arrays.fill(triggers, false);
}
Aggregations