Search in sources :

Example 16 with JWT

use of com.auth0.jwt.JWT in project spring-security by spring-projects.

the class JwtAuthenticationProviderTests method authenticateWhenJwtDecodesThenAuthenticationHasAttributesContainedInJwt.

@Test
public void authenticateWhenJwtDecodesThenAuthenticationHasAttributesContainedInJwt() {
    BearerTokenAuthenticationToken token = this.authentication();
    Jwt jwt = TestJwts.jwt().claim("name", "value").build();
    given(this.jwtDecoder.decode("token")).willReturn(jwt);
    given(this.jwtAuthenticationConverter.convert(jwt)).willReturn(new JwtAuthenticationToken(jwt));
    JwtAuthenticationToken authentication = (JwtAuthenticationToken) this.provider.authenticate(token);
    assertThat(authentication.getTokenAttributes()).containsEntry("name", "value");
}
Also used : Jwt(org.springframework.security.oauth2.jwt.Jwt) BearerTokenAuthenticationToken(org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken) Test(org.junit.jupiter.api.Test)

Example 17 with JWT

use of com.auth0.jwt.JWT in project spring-security by spring-projects.

the class JwtBearerTokenAuthenticationConverterTests method convertWhenJwtWithScopeAttributeThenBearerTokenAuthentication.

@Test
public void convertWhenJwtWithScopeAttributeThenBearerTokenAuthentication() {
    // @formatter:off
    Jwt jwt = Jwt.withTokenValue("token-value").claim("scope", "message:read message:write").header("header", "value").build();
    // @formatter:on
    AbstractAuthenticationToken token = this.converter.convert(jwt);
    assertThat(token).isInstanceOf(BearerTokenAuthentication.class);
    BearerTokenAuthentication bearerToken = (BearerTokenAuthentication) token;
    assertThat(bearerToken.getAuthorities()).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) Test(org.junit.jupiter.api.Test)

Example 18 with JWT

use of com.auth0.jwt.JWT in project spring-security by spring-projects.

the class JwtBearerTokenAuthenticationConverterTests method convertWhenJwtWithScpAttributeThenBearerTokenAuthentication.

@Test
public void convertWhenJwtWithScpAttributeThenBearerTokenAuthentication() {
    // @formatter:off
    Jwt jwt = Jwt.withTokenValue("token-value").claim("scp", Arrays.asList("message:read", "message:write")).header("header", "value").build();
    // @formatter:on
    AbstractAuthenticationToken token = this.converter.convert(jwt);
    assertThat(token).isInstanceOf(BearerTokenAuthentication.class);
    BearerTokenAuthentication bearerToken = (BearerTokenAuthentication) token;
    assertThat(bearerToken.getAuthorities()).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) Test(org.junit.jupiter.api.Test)

Example 19 with JWT

use of com.auth0.jwt.JWT in project spring-security by spring-projects.

the class ReactiveJwtAuthenticationConverterAdapterTests method convertWhenTokenHasBothScopeAndScpThenScopeAttributeIsTranslatedToAuthorities.

@Test
public void convertWhenTokenHasBothScopeAndScpThenScopeAttributeIsTranslatedToAuthorities() {
    Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList("message:read", "message:write")).claim("scope", "missive:read missive:write").build();
    AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block();
    Collection<GrantedAuthority> authorities = authentication.getAuthorities();
    // @formatter:off
    assertThat(authorities).containsExactly(new SimpleGrantedAuthority("SCOPE_missive:read"), new SimpleGrantedAuthority("SCOPE_missive: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 20 with JWT

use of com.auth0.jwt.JWT in project spring-security by spring-projects.

the class ReactiveJwtAuthenticationConverterAdapterTests method convertWhenTokenHasEmptyScopeAndNonEmptyScpThenScopeAttributeIsTranslatedToNoAuthorities.

@Test
public void convertWhenTokenHasEmptyScopeAndNonEmptyScpThenScopeAttributeIsTranslatedToNoAuthorities() {
    // @formatter:off
    Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList("message:read", "message:write")).claim("scope", "").build();
    // @formatter:on
    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)

Aggregations

Jwt (org.springframework.security.oauth2.jwt.Jwt)96 Test (org.junit.jupiter.api.Test)78 GrantedAuthority (org.springframework.security.core.GrantedAuthority)49 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)39 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)19 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)18 TestJwts (org.springframework.security.oauth2.jwt.TestJwts)18 Arrays (java.util.Arrays)17 List (java.util.List)17 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)16 AbstractAuthenticationToken (org.springframework.security.authentication.AbstractAuthenticationToken)16 Test (org.junit.Test)14 OAuth2AccessTokenResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse)13 Authentication (org.springframework.security.core.Authentication)12 HashMap (java.util.HashMap)11 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)11 BeforeEach (org.junit.jupiter.api.BeforeEach)11 Map (java.util.Map)10 SecurityContext (org.springframework.security.core.context.SecurityContext)10 JwtAuthenticationToken (org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken)10