Search in sources :

Example 16 with AbstractAuthenticationToken

use of org.springframework.security.authentication.AbstractAuthenticationToken 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 17 with AbstractAuthenticationToken

use of org.springframework.security.authentication.AbstractAuthenticationToken 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 18 with AbstractAuthenticationToken

use of org.springframework.security.authentication.AbstractAuthenticationToken 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 19 with AbstractAuthenticationToken

use of org.springframework.security.authentication.AbstractAuthenticationToken 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)

Example 20 with AbstractAuthenticationToken

use of org.springframework.security.authentication.AbstractAuthenticationToken in project spring-security by spring-projects.

the class OpaqueTokenAuthenticationProvider method authenticate.

/**
 * Introspect and validate the opaque
 * <a href="https://tools.ietf.org/html/rfc6750#section-1.2" target="_blank">Bearer
 * Token</a>.
 * @param authentication the authentication request object.
 * @return A successful authentication
 * @throws AuthenticationException if authentication failed for some reason
 */
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (!(authentication instanceof BearerTokenAuthenticationToken)) {
        return null;
    }
    BearerTokenAuthenticationToken bearer = (BearerTokenAuthenticationToken) authentication;
    OAuth2AuthenticatedPrincipal principal = getOAuth2AuthenticatedPrincipal(bearer);
    AbstractAuthenticationToken result = convert(principal, bearer.getToken());
    result.setDetails(bearer.getDetails());
    this.logger.debug("Authenticated token");
    return result;
}
Also used : AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) OAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal) BearerTokenAuthenticationToken(org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken)

Aggregations

AbstractAuthenticationToken (org.springframework.security.authentication.AbstractAuthenticationToken)37 GrantedAuthority (org.springframework.security.core.GrantedAuthority)19 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)17 Jwt (org.springframework.security.oauth2.jwt.Jwt)16 Test (org.junit.jupiter.api.Test)15 Authentication (org.springframework.security.core.Authentication)13 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)12 WebAuthenticationDetails (org.springframework.security.web.authentication.WebAuthenticationDetails)10 User (org.springframework.security.core.userdetails.User)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 UserDetails (org.springframework.security.core.userdetails.UserDetails)7 SignedJWT (com.nimbusds.jwt.SignedJWT)3 ParseException (java.text.ParseException)3 ArrayList (java.util.ArrayList)3 RangerAuthenticationProvider (org.apache.ranger.security.handler.RangerAuthenticationProvider)3 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 Collection (java.util.Collection)2