Search in sources :

Example 6 with ReactiveJwtDecoder

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

the class ReactiveOAuth2ResourceServerAutoConfigurationTests method autoConfigurationShouldConfigureResourceServerUsingOidcRfc8414IssuerUri.

@Test
@SuppressWarnings("unchecked")
void autoConfigurationShouldConfigureResourceServerUsingOidcRfc8414IssuerUri() throws Exception {
    this.server = new MockWebServer();
    this.server.start();
    String issuer = this.server.url("").toString();
    String cleanIssuerPath = cleanIssuerPath(issuer);
    setupMockResponsesWithErrors(cleanIssuerPath, 1);
    this.contextRunner.withPropertyValues("spring.security.oauth2.resourceserver.jwt.issuer-uri=http://" + this.server.getHostName() + ":" + this.server.getPort()).run((context) -> {
        assertThat(context).hasSingleBean(SupplierReactiveJwtDecoder.class);
        assertFilterConfiguredWithJwtAuthenticationManager(context);
        assertThat(context.containsBean("jwtDecoderByIssuerUri")).isTrue();
        SupplierReactiveJwtDecoder supplierReactiveJwtDecoder = context.getBean(SupplierReactiveJwtDecoder.class);
        Mono<ReactiveJwtDecoder> reactiveJwtDecoderSupplier = (Mono<ReactiveJwtDecoder>) ReflectionTestUtils.getField(supplierReactiveJwtDecoder, "jwtDecoderMono");
        ReactiveJwtDecoder reactiveJwtDecoder = reactiveJwtDecoderSupplier.block();
    });
    // The last request is to the JWK Set endpoint to look up the algorithm
    assertThat(this.server.getRequestCount()).isEqualTo(2);
}
Also used : NimbusReactiveJwtDecoder(org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder) ReactiveJwtDecoder(org.springframework.security.oauth2.jwt.ReactiveJwtDecoder) SupplierReactiveJwtDecoder(org.springframework.security.oauth2.jwt.SupplierReactiveJwtDecoder) Mono(reactor.core.publisher.Mono) MockWebServer(okhttp3.mockwebserver.MockWebServer) SupplierReactiveJwtDecoder(org.springframework.security.oauth2.jwt.SupplierReactiveJwtDecoder) Test(org.junit.jupiter.api.Test)

Example 7 with ReactiveJwtDecoder

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

the class OAuth2ResourceServerSpecTests method getJwtDecoderWhenBeanWiredAndDslWiredThenDslTakesPrecedence.

@Test
public void getJwtDecoderWhenBeanWiredAndDslWiredThenDslTakesPrecedence() {
    GenericWebApplicationContext context = autowireWebServerGenericWebApplicationContext();
    ServerHttpSecurity http = new ServerHttpSecurity();
    http.setApplicationContext(context);
    ReactiveJwtDecoder beanWiredJwtDecoder = mock(ReactiveJwtDecoder.class);
    ReactiveJwtDecoder dslWiredJwtDecoder = mock(ReactiveJwtDecoder.class);
    context.registerBean(ReactiveJwtDecoder.class, () -> beanWiredJwtDecoder);
    ServerHttpSecurity.OAuth2ResourceServerSpec.JwtSpec jwt = http.oauth2ResourceServer().jwt();
    jwt.jwtDecoder(dslWiredJwtDecoder);
    assertThat(jwt.getJwtDecoder()).isEqualTo(dslWiredJwtDecoder);
}
Also used : ReactiveJwtDecoder(org.springframework.security.oauth2.jwt.ReactiveJwtDecoder) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 8 with ReactiveJwtDecoder

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

the class ReactiveOidcIdTokenDecoderFactory method createDecoder.

@Override
public ReactiveJwtDecoder createDecoder(ClientRegistration clientRegistration) {
    Assert.notNull(clientRegistration, "clientRegistration cannot be null");
    return this.jwtDecoders.computeIfAbsent(clientRegistration.getRegistrationId(), (key) -> {
        NimbusReactiveJwtDecoder jwtDecoder = buildDecoder(clientRegistration);
        jwtDecoder.setJwtValidator(this.jwtValidatorFactory.apply(clientRegistration));
        Converter<Map<String, Object>, Map<String, Object>> claimTypeConverter = this.claimTypeConverterFactory.apply(clientRegistration);
        if (claimTypeConverter != null) {
            jwtDecoder.setClaimSetConverter(claimTypeConverter);
        }
        return jwtDecoder;
    });
}
Also used : NimbusReactiveJwtDecoder(org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 9 with ReactiveJwtDecoder

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

the class OidcAuthorizationCodeReactiveAuthenticationManager method createOidcToken.

private Mono<OidcIdToken> createOidcToken(ClientRegistration clientRegistration, OAuth2AccessTokenResponse accessTokenResponse) {
    ReactiveJwtDecoder jwtDecoder = this.jwtDecoderFactory.createDecoder(clientRegistration);
    String rawIdToken = (String) accessTokenResponse.getAdditionalParameters().get(OidcParameterNames.ID_TOKEN);
    // @formatter:off
    return jwtDecoder.decode(rawIdToken).map((jwt) -> new OidcIdToken(jwt.getTokenValue(), jwt.getIssuedAt(), jwt.getExpiresAt(), jwt.getClaims()));
// @formatter:on
}
Also used : ReactiveJwtDecoder(org.springframework.security.oauth2.jwt.ReactiveJwtDecoder) OidcIdToken(org.springframework.security.oauth2.core.oidc.OidcIdToken)

Example 10 with ReactiveJwtDecoder

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

the class ReactiveOAuth2ResourceServerAutoConfigurationTests method autoConfigurationShouldConfigureResourceServerUsingOAuthIssuerUri.

@Test
@SuppressWarnings("unchecked")
void autoConfigurationShouldConfigureResourceServerUsingOAuthIssuerUri() throws Exception {
    this.server = new MockWebServer();
    this.server.start();
    String issuer = this.server.url("").toString();
    String cleanIssuerPath = cleanIssuerPath(issuer);
    setupMockResponsesWithErrors(cleanIssuerPath, 2);
    this.contextRunner.withPropertyValues("spring.security.oauth2.resourceserver.jwt.issuer-uri=http://" + this.server.getHostName() + ":" + this.server.getPort()).run((context) -> {
        assertThat(context).hasSingleBean(SupplierReactiveJwtDecoder.class);
        assertFilterConfiguredWithJwtAuthenticationManager(context);
        assertThat(context.containsBean("jwtDecoderByIssuerUri")).isTrue();
        SupplierReactiveJwtDecoder supplierReactiveJwtDecoder = context.getBean(SupplierReactiveJwtDecoder.class);
        Mono<ReactiveJwtDecoder> reactiveJwtDecoderSupplier = (Mono<ReactiveJwtDecoder>) ReflectionTestUtils.getField(supplierReactiveJwtDecoder, "jwtDecoderMono");
        ReactiveJwtDecoder reactiveJwtDecoder = reactiveJwtDecoderSupplier.block();
    });
    // The last request is to the JWK Set endpoint to look up the algorithm
    assertThat(this.server.getRequestCount()).isEqualTo(3);
}
Also used : NimbusReactiveJwtDecoder(org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder) ReactiveJwtDecoder(org.springframework.security.oauth2.jwt.ReactiveJwtDecoder) SupplierReactiveJwtDecoder(org.springframework.security.oauth2.jwt.SupplierReactiveJwtDecoder) Mono(reactor.core.publisher.Mono) MockWebServer(okhttp3.mockwebserver.MockWebServer) SupplierReactiveJwtDecoder(org.springframework.security.oauth2.jwt.SupplierReactiveJwtDecoder) Test(org.junit.jupiter.api.Test)

Aggregations

ReactiveJwtDecoder (org.springframework.security.oauth2.jwt.ReactiveJwtDecoder)9 Test (org.junit.jupiter.api.Test)8 MockWebServer (okhttp3.mockwebserver.MockWebServer)5 NimbusReactiveJwtDecoder (org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder)5 SupplierReactiveJwtDecoder (org.springframework.security.oauth2.jwt.SupplierReactiveJwtDecoder)4 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)4 Mono (reactor.core.publisher.Mono)4 NoUniqueBeanDefinitionException (org.springframework.beans.factory.NoUniqueBeanDefinitionException)2 Jwt (org.springframework.security.oauth2.jwt.Jwt)2 PreDestroy (jakarta.annotation.PreDestroy)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 KeyFactory (java.security.KeyFactory)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)1 Base64 (java.util.Base64)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1