use of com.sap.cloud.security.xsuaa.extractor.LocalAuthoritiesExtractor in project cloud-security-xsuaa-integration by SAP.
the class TokenAuthenticationConverter method setLocalScopeAsAuthorities.
/**
* This method allows to overwrite the default behavior of the
* {@link Token#getAuthorities()} implementation.
*
* @param extractLocalScopesOnly
* true when {@link Token#getAuthorities()} should only extract local
* scopes. Local scopes means that non-application specific scopes
* are filtered out and scopes are returned without appId prefix,
* e.g. "Display". Creates a new converter with a new
* {@link LocalAuthoritiesExtractor}
* @return the token authenticator itself
*/
public TokenAuthenticationConverter setLocalScopeAsAuthorities(boolean extractLocalScopesOnly) {
if (extractLocalScopesOnly) {
Assert.state(appId != null, "For local Scope extraction 'appId' must be provided to `TokenAuthenticationConverter`");
authoritiesExtractor = new LocalAuthoritiesExtractor(appId);
} else {
authoritiesExtractor = new DefaultAuthoritiesExtractor();
}
return this;
}
use of com.sap.cloud.security.xsuaa.extractor.LocalAuthoritiesExtractor in project cloud-security-xsuaa-integration by SAP.
the class InitializeSpringSecurityContextTest method initializeSecurityContext_succeeds.
@Test
public void initializeSecurityContext_succeeds() {
String jwt = new JwtGenerator(clientId, "subdomain").addScopes("openid", appId + ".Display", "otherXSAPP.Display").deriveAudiences(true).getToken().getTokenValue();
assertThat(SecurityContextHolder.getContext().getAuthentication(), is(nullValue()));
SpringSecurityContext.init(jwt, jwtDecoder, new LocalAuthoritiesExtractor(appId));
// test authentication - isAuthenticated()
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
assertThat(authentication, is(notNullValue()));
assertThat(authentication.isAuthenticated(), is(true));
// test authorities
Collection<GrantedAuthority> authorities = (Collection<GrantedAuthority>) authentication.getAuthorities();
Assert.assertThat(authorities.size(), is(1));
Assert.assertThat(authorities, hasItem(new SimpleGrantedAuthority("Display")));
Assert.assertThat(authorities, not(hasItem(new SimpleGrantedAuthority("Other"))));
// test principal (Token)
Token token = (Token) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
assertThat(token.getAuthorities(), is(authorities));
assertThat(token.getClientId(), is(clientId));
}
use of com.sap.cloud.security.xsuaa.extractor.LocalAuthoritiesExtractor in project cloud-security-xsuaa-integration by SAP.
the class InitializeSpringSecurityContextTest method clearSecurityContext_succeeds.
@Test
public void clearSecurityContext_succeeds() {
String jwt = new JwtGenerator(clientId, "subdomain").deriveAudiences(true).getToken().getTokenValue();
SpringSecurityContext.init(jwt, jwtDecoder, new LocalAuthoritiesExtractor(appId));
SpringSecurityContext.clear();
assertThat(SecurityContextHolder.getContext().getAuthentication(), is(nullValue()));
}
Aggregations