Search in sources :

Example 1 with JwtGrantedAuthoritiesConverter

use of org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter in project spring-security by spring-projects.

the class JwtGrantedAuthoritiesConverterTests method convertWhenTokenHasUnsupportedTypeForScopeThenTranslatesToNoAuthorities.

@Test
public void convertWhenTokenHasUnsupportedTypeForScopeThenTranslatesToNoAuthorities() {
    // @formatter:off
    Jwt jwt = TestJwts.jwt().claim("scope", new String[] { "message:read", "message:write" }).build();
    // @formatter:on
    JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
    Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
    assertThat(authorities).isEmpty();
}
Also used : Jwt(org.springframework.security.oauth2.jwt.Jwt) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.jupiter.api.Test)

Example 2 with JwtGrantedAuthoritiesConverter

use of org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter in project spring-security by spring-projects.

the class JwtGrantedAuthoritiesConverterTests method convertWhenTokenHasEmptyCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToNoAuthorities.

@Test
public void convertWhenTokenHasEmptyCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToNoAuthorities() {
    // @formatter:off
    Jwt jwt = TestJwts.jwt().claim("roles", Collections.emptyList()).claim("scope", "missive:read missive:write").build();
    // @formatter:on
    JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
    jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
    Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
    assertThat(authorities).isEmpty();
}
Also used : Jwt(org.springframework.security.oauth2.jwt.Jwt) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.jupiter.api.Test)

Example 3 with JwtGrantedAuthoritiesConverter

use of org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter in project spring-security by spring-projects.

the class JwtGrantedAuthoritiesConverterTests method convertWhenTokenHasEmptyScopeAndEmptyScpAttributeThenTranslatesToNoAuthorities.

@Test
public void convertWhenTokenHasEmptyScopeAndEmptyScpAttributeThenTranslatesToNoAuthorities() {
    // @formatter:off
    Jwt jwt = TestJwts.jwt().claim("scp", Collections.emptyList()).claim("scope", Collections.emptyList()).build();
    // @formatter:on
    JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
    Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
    assertThat(authorities).isEmpty();
}
Also used : Jwt(org.springframework.security.oauth2.jwt.Jwt) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.jupiter.api.Test)

Example 4 with JwtGrantedAuthoritiesConverter

use of org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter in project spring-security by spring-projects.

the class JwtGrantedAuthoritiesConverterTests method convertWhenTokenHasScopeAttributeThenTranslatedToAuthorities.

@Test
public void convertWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
    // @formatter:off
    Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
    // @formatter:on
    JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
    Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
    assertThat(authorities).containsExactly(new SimpleGrantedAuthority("SCOPE_message:read"), new SimpleGrantedAuthority("SCOPE_message:write"));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Jwt(org.springframework.security.oauth2.jwt.Jwt) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.jupiter.api.Test)

Example 5 with JwtGrantedAuthoritiesConverter

use of org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter in project spring-security by spring-projects.

the class JwtGrantedAuthoritiesConverterTests method convertWhenTokenHasNoCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToNoAuthorities.

@Test
public void convertWhenTokenHasNoCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToNoAuthorities() {
    // @formatter:off
    Jwt jwt = TestJwts.jwt().claim("scope", "missive:read missive:write").build();
    // @formatter:on
    JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
    jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
    Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
    assertThat(authorities).isEmpty();
}
Also used : Jwt(org.springframework.security.oauth2.jwt.Jwt) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)16 GrantedAuthority (org.springframework.security.core.GrantedAuthority)16 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)16 Jwt (org.springframework.security.oauth2.jwt.Jwt)16