use of com.sap.cloud.security.xsuaa.token.Token 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));
}
Aggregations