Search in sources :

Example 21 with Jwt

use of org.springframework.security.oauth2.jwt.Jwt in project spring-security by spring-projects.

the class ReactiveJwtAuthenticationConverterTests method convertWhenDefaultGrantedAuthoritiesConverterSet.

@Test
public void convertWhenDefaultGrantedAuthoritiesConverterSet() {
    Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
    AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block();
    Collection<GrantedAuthority> authorities = authentication.getAuthorities();
    assertThat(authorities).containsExactly(new SimpleGrantedAuthority("SCOPE_message:read"), new SimpleGrantedAuthority("SCOPE_message:write"));
}
Also used : AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) 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 22 with Jwt

use of org.springframework.security.oauth2.jwt.Jwt in project spring-security by spring-projects.

the class ReactiveJwtAuthenticationConverterTests method convertWithOverriddenGrantedAuthoritiesConverter.

@Test
public void convertWithOverriddenGrantedAuthoritiesConverter() {
    Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
    Converter<Jwt, Flux<GrantedAuthority>> grantedAuthoritiesConverter = (token) -> Flux.just(new SimpleGrantedAuthority("blah"));
    this.jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(grantedAuthoritiesConverter);
    AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block();
    Collection<GrantedAuthority> authorities = authentication.getAuthorities();
    assertThat(authorities).containsExactly(new SimpleGrantedAuthority("blah"));
}
Also used : Test(org.junit.jupiter.api.Test) Converter(org.springframework.core.convert.converter.Converter) Flux(reactor.core.publisher.Flux) AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) TestJwts(org.springframework.security.oauth2.jwt.TestJwts) Jwt(org.springframework.security.oauth2.jwt.Jwt) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) Jwt(org.springframework.security.oauth2.jwt.Jwt) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Flux(reactor.core.publisher.Flux) Test(org.junit.jupiter.api.Test)

Example 23 with Jwt

use of org.springframework.security.oauth2.jwt.Jwt in project spring-security by spring-projects.

the class ReactiveJwtAuthenticationConverterAdapterTests method convertWhenTokenHasEmptyScopeAttributeThenTranslatedToNoAuthorities.

@Test
public void convertWhenTokenHasEmptyScopeAttributeThenTranslatedToNoAuthorities() {
    Jwt jwt = TestJwts.jwt().claim("scope", "").build();
    AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block();
    Collection<GrantedAuthority> authorities = authentication.getAuthorities();
    assertThat(authorities).containsExactly();
}
Also used : AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) 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 24 with Jwt

use of org.springframework.security.oauth2.jwt.Jwt in project spring-security by spring-projects.

the class ReactiveJwtAuthenticationConverterAdapterTests method convertWhenTokenHasScpAttributeThenTranslatedToAuthorities.

@Test
public void convertWhenTokenHasScpAttributeThenTranslatedToAuthorities() {
    Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList("message:read", "message:write")).build();
    AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block();
    Collection<GrantedAuthority> authorities = authentication.getAuthorities();
    // @formatter:off
    assertThat(authorities).containsExactly(new SimpleGrantedAuthority("SCOPE_message:read"), new SimpleGrantedAuthority("SCOPE_message:write"));
// @formatter:on
}
Also used : AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) 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 25 with Jwt

use of org.springframework.security.oauth2.jwt.Jwt in project spring-security by spring-projects.

the class JwtAuthenticationConverterTests method convertWhenDefaultGrantedAuthoritiesConverterSet.

@Test
public void convertWhenDefaultGrantedAuthoritiesConverterSet() {
    Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
    AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt);
    Collection<GrantedAuthority> authorities = authentication.getAuthorities();
    assertThat(authorities).containsExactly(new SimpleGrantedAuthority("SCOPE_message:read"), new SimpleGrantedAuthority("SCOPE_message:write"));
}
Also used : AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) 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)

Aggregations

Test (org.junit.jupiter.api.Test)139 Jwt (org.springframework.security.oauth2.jwt.Jwt)83 GrantedAuthority (org.springframework.security.core.GrantedAuthority)47 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)37 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)36 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)36 OAuth2AccessTokenResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse)30 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)29 BeforeEach (org.junit.jupiter.api.BeforeEach)29 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)27 TestClientRegistrations (org.springframework.security.oauth2.client.registration.TestClientRegistrations)24 Instant (java.time.Instant)23 HttpHeaders (org.springframework.http.HttpHeaders)22 OAuth2AccessToken (org.springframework.security.oauth2.core.OAuth2AccessToken)22 JwtDecoder (org.springframework.security.oauth2.jwt.JwtDecoder)22 Collections (java.util.Collections)21 MediaType (org.springframework.http.MediaType)21 NimbusJwtDecoder (org.springframework.security.oauth2.jwt.NimbusJwtDecoder)21 MockWebServer (okhttp3.mockwebserver.MockWebServer)20 OAuth2Error (org.springframework.security.oauth2.core.OAuth2Error)20