Skip to content

Support overriding WebClient/RestOperations in (Reactive)OidcIdTokenDecoderFactory #14357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,6 +49,8 @@
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

/**
* A {@link JwtDecoderFactory factory} that provides a {@link JwtDecoder} used for
Expand Down Expand Up @@ -89,6 +91,9 @@ public final class OidcIdTokenDecoderFactory implements JwtDecoderFactory<Client
private Function<ClientRegistration, Converter<Map<String, Object>, Map<String, Object>>> claimTypeConverterFactory = (
clientRegistration) -> DEFAULT_CLAIM_TYPE_CONVERTER;

private Function<ClientRegistration, RestOperations> restOperationsFactory = (
clientRegistration) -> new RestTemplate();

/**
* Returns the default {@link Converter}'s used for type conversion of claim values
* for an {@link OidcIdToken}.
Expand Down Expand Up @@ -164,7 +169,10 @@ private NimbusJwtDecoder buildDecoder(ClientRegistration clientRegistration) {
null);
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
}
return NimbusJwtDecoder.withJwkSetUri(jwkSetUri).jwsAlgorithm((SignatureAlgorithm) jwsAlgorithm).build();
return NimbusJwtDecoder.withJwkSetUri(jwkSetUri)
.jwsAlgorithm((SignatureAlgorithm) jwsAlgorithm)
.restOperations(restOperationsFactory.apply(clientRegistration))
.build();
}
if (jwsAlgorithm != null && MacAlgorithm.class.isAssignableFrom(jwsAlgorithm.getClass())) {
// https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation
Expand Down Expand Up @@ -237,4 +245,18 @@ public void setClaimTypeConverterFactory(
this.claimTypeConverterFactory = claimTypeConverterFactory;
}

/**
* Sets the factory that provides a {@link RestOperations} used by
* {@link NimbusJwtDecoder} to coordinate with the authorization servers indicated in
* the <a href="https://tools.ietf.org/html/rfc7517#section-5">JWK Set</a> uri.
* @param restOperationsFactory the factory that provides a {@link RestOperations}
* used by {@link NimbusJwtDecoder}
*
* @since 6.3
*/
public void setRestOperationsFactory(Function<ClientRegistration, RestOperations> restOperationsFactory) {
Assert.notNull(restOperationsFactory, "restOperationsFactory cannot be null");
this.restOperationsFactory = restOperationsFactory;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,6 +49,7 @@
import org.springframework.security.oauth2.jwt.ReactiveJwtDecoderFactory;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.function.client.WebClient;

/**
* A {@link ReactiveJwtDecoderFactory factory} that provides a {@link ReactiveJwtDecoder}
Expand Down Expand Up @@ -89,6 +90,8 @@ public final class ReactiveOidcIdTokenDecoderFactory implements ReactiveJwtDecod
private Function<ClientRegistration, Converter<Map<String, Object>, Map<String, Object>>> claimTypeConverterFactory = (
clientRegistration) -> DEFAULT_CLAIM_TYPE_CONVERTER;

private Function<ClientRegistration, WebClient> webClientFactory = (clientRegistration) -> WebClient.create();

/**
* Returns the default {@link Converter}'s used for type conversion of claim values
* for an {@link OidcIdToken}.
Expand Down Expand Up @@ -165,6 +168,7 @@ private NimbusReactiveJwtDecoder buildDecoder(ClientRegistration clientRegistrat
}
return NimbusReactiveJwtDecoder.withJwkSetUri(jwkSetUri)
.jwsAlgorithm((SignatureAlgorithm) jwsAlgorithm)
.webClient(webClientFactory.apply(clientRegistration))
.build();
}
if (jwsAlgorithm != null && MacAlgorithm.class.isAssignableFrom(jwsAlgorithm.getClass())) {
Expand Down Expand Up @@ -241,4 +245,19 @@ public void setClaimTypeConverterFactory(
this.claimTypeConverterFactory = claimTypeConverterFactory;
}

/**
* Sets the factory that provides a {@link WebClient} used by
* {@link NimbusReactiveJwtDecoder} to coordinate with the authorization servers
* indicated in the <a href="https://tools.ietf.org/html/rfc7517#section-5">JWK
* Set</a> uri.
* @param webClientFactory the factory that provides a {@link WebClient} used by
* {@link NimbusReactiveJwtDecoder}
*
* @since 6.3
*/
public void setWebClientFactory(Function<ClientRegistration, WebClient> webClientFactory) {
Assert.notNull(webClientFactory, "webClientFactory cannot be null");
this.webClientFactory = webClientFactory;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,6 +34,8 @@
import org.springframework.security.oauth2.jose.jws.MacAlgorithm;
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
Expand Down Expand Up @@ -95,6 +97,12 @@ public void setClaimTypeConverterFactoryWhenNullThenThrowIllegalArgumentExceptio
.isThrownBy(() -> this.idTokenDecoderFactory.setClaimTypeConverterFactory(null));
}

@Test
public void setRestOperationsFactoryWhenNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.idTokenDecoderFactory.setRestOperationsFactory(null));
}

@Test
public void createDecoderWhenClientRegistrationNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException().isThrownBy(() -> this.idTokenDecoderFactory.createDecoder(null));
Expand Down Expand Up @@ -177,4 +185,15 @@ public void createDecoderWhenCustomClaimTypeConverterFactorySetThenApplied() {
verify(customClaimTypeConverterFactory).apply(same(clientRegistration));
}

@Test
public void createDecoderWhenCustomRestOperationsFactorySetThenApplied() {
Function<ClientRegistration, RestOperations> customRestOperationsFactory = mock(
Function.class);
this.idTokenDecoderFactory.setRestOperationsFactory(customRestOperationsFactory);
ClientRegistration clientRegistration = this.registration.build();
given(customRestOperationsFactory.apply(same(clientRegistration)))
.willReturn(new RestTemplate());
this.idTokenDecoderFactory.createDecoder(clientRegistration);
verify(customRestOperationsFactory).apply(same(clientRegistration));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,6 +34,7 @@
import org.springframework.security.oauth2.jose.jws.MacAlgorithm;
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.reactive.function.client.WebClient;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
Expand Down Expand Up @@ -94,6 +95,12 @@ public void setClaimTypeConverterFactoryWhenNullThenThrowIllegalArgumentExceptio
.isThrownBy(() -> this.idTokenDecoderFactory.setClaimTypeConverterFactory(null));
}

@Test
public void setWebClientFactoryWhenNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.idTokenDecoderFactory.setWebClientFactory(null));
}

@Test
public void createDecoderWhenClientRegistrationNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException().isThrownBy(() -> this.idTokenDecoderFactory.createDecoder(null));
Expand Down Expand Up @@ -176,4 +183,15 @@ public void createDecoderWhenCustomClaimTypeConverterFactorySetThenApplied() {
verify(customClaimTypeConverterFactory).apply(same(clientRegistration));
}

@Test
public void createDecoderWhenCustomWebClientFactorySetThenApplied() {
Function<ClientRegistration, WebClient> customWebClientFactory = mock(
Function.class);
this.idTokenDecoderFactory.setWebClientFactory(customWebClientFactory);
ClientRegistration clientRegistration = this.registration.build();
given(customWebClientFactory.apply(same(clientRegistration)))
.willReturn(WebClient.create());
this.idTokenDecoderFactory.createDecoder(clientRegistration);
verify(customWebClientFactory).apply(same(clientRegistration));
}
}