Skip to content

Commit

Permalink
Polish gh-1326
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrandja committed Sep 13, 2023
1 parent 3de6a7d commit 05f1371
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
* @see OAuth2AuthorizationService
* @see OidcClientRegistrationAuthenticationToken
* @see OidcClientRegistrationAuthenticationProvider
* @see RegisteredClientOidcClientRegistrationConverter
* @see <a href="https://openid.net/specs/openid-connect-registration-1_0.html#ClientConfigurationEndpoint">4. Client Configuration Endpoint</a>
*/
public final class OidcClientConfigurationAuthenticationProvider implements AuthenticationProvider {
Expand All @@ -79,9 +78,9 @@ public OidcClientConfigurationAuthenticationProvider(RegisteredClientRepository
}

/**
* Sets the {@link Converter} used for converting an {@link RegisteredClient} to a {@link OidcClientRegistration}.
* Sets the {@link Converter} used for converting a {@link RegisteredClient} to an {@link OidcClientRegistration}.
*
* @param clientRegistrationConverter the {@link Converter} used for converting an {@link RegisteredClient} to a {@link OidcClientRegistration}
* @param clientRegistrationConverter the {@link Converter} used for converting a {@link RegisteredClient} to an {@link OidcClientRegistration}
* @since 1.2.0
*/
public void setClientRegistrationConverter(Converter<RegisteredClient, OidcClientRegistration> clientRegistrationConverter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ public void setRegisteredClientConverter(Converter<OidcClientRegistration, Regis
}

/**
* Sets the {@link Converter} used for converting an {@link RegisteredClient} to a {@link OidcClientRegistration}.
* Sets the {@link Converter} used for converting a {@link RegisteredClient} to an {@link OidcClientRegistration}.
*
* @param clientRegistrationConverter the {@link Converter} used for converting an {@link RegisteredClient} to a {@link OidcClientRegistration}
* @param clientRegistrationConverter the {@link Converter} used for converting a {@link RegisteredClient} to an {@link OidcClientRegistration}
* @since 1.2.0
*/
public void setClientRegistrationConverter(Converter<RegisteredClient, OidcClientRegistration> clientRegistrationConverter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package org.springframework.security.oauth2.server.authorization.oidc.converter;

import java.time.Instant;
import java.util.Base64;
import java.util.UUID;

import org.springframework.core.convert.converter.Converter;
import org.springframework.security.crypto.keygen.Base64StringKeyGenerator;
import org.springframework.security.crypto.keygen.StringKeyGenerator;
Expand All @@ -29,11 +33,9 @@
import org.springframework.security.oauth2.server.authorization.settings.TokenSettings;
import org.springframework.util.CollectionUtils;

import java.time.Instant;
import java.util.Base64;
import java.util.UUID;

/**
* A {@link Converter} that converts the provided {@link OidcClientRegistration} to a {@link RegisteredClient}.
*
* @author Joe Grandja
* @author Dmitriy Dubson
* @since 1.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.springframework.web.util.UriComponentsBuilder;

/**
* A {@link Converter} that converts the provided {@link RegisteredClient} to an {@link OidcClientRegistration}.
*
* @author Joe Grandja
* @since 1.2.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import jakarta.servlet.http.HttpServletResponse;

import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.source.JWKSource;
import com.nimbusds.jose.proc.SecurityContext;
import jakarta.servlet.http.HttpServletResponse;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -588,7 +587,7 @@ public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity h
oidc
.clientRegistrationEndpoint(clientRegistration ->
clientRegistration
.authenticationProviders(configureRegisteredClientConverters())
.authenticationProviders(configureClientRegistrationConverters())
)
);
RequestMatcher endpointsMatcher = authorizationServerConfigurer.getEndpointsMatcher();
Expand All @@ -607,15 +606,14 @@ public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity h
}
// @formatter:on

private Consumer<List<AuthenticationProvider>> configureRegisteredClientConverters() {
private Consumer<List<AuthenticationProvider>> configureClientRegistrationConverters() {
// @formatter:off
return (authenticationProviders) ->
authenticationProviders.forEach(authenticationProvider -> {
List<String> customClientMetadata = List.of("custom-metadata-name-1", "custom-metadata-name-2");

List<String> supportedCustomClientMetadata = List.of("custom-metadata-name-1", "custom-metadata-name-2");
if (authenticationProvider instanceof OidcClientRegistrationAuthenticationProvider provider) {
provider.setRegisteredClientConverter(new CustomRegisteredClientConverter(customClientMetadata));
provider.setClientRegistrationConverter(new CustomClientRegistrationConverter(customClientMetadata));
provider.setRegisteredClientConverter(new CustomRegisteredClientConverter(supportedCustomClientMetadata));
provider.setClientRegistrationConverter(new CustomClientRegistrationConverter(supportedCustomClientMetadata));
}
});
// @formatter:on
Expand Down Expand Up @@ -695,54 +693,59 @@ PasswordEncoder passwordEncoder() {

}

static class CustomClientRegistrationConverter implements Converter<RegisteredClient, OidcClientRegistration> {
private final List<String> customMetadata;
private static class CustomRegisteredClientConverter implements Converter<OidcClientRegistration, RegisteredClient> {
private final OidcClientRegistrationRegisteredClientConverter delegate =
new OidcClientRegistrationRegisteredClientConverter();
private final List<String> supportedCustomClientMetadata;

private final RegisteredClientOidcClientRegistrationConverter delegate;

CustomClientRegistrationConverter(List<String> customMetadata) {
this.customMetadata = customMetadata;
this.delegate = new RegisteredClientOidcClientRegistrationConverter();
private CustomRegisteredClientConverter(List<String> supportedCustomClientMetadata) {
this.supportedCustomClientMetadata = supportedCustomClientMetadata;
}

public OidcClientRegistration convert(RegisteredClient registeredClient) {
var clientRegistration = delegate.convert(registeredClient);
Map<String, Object> claims = new HashMap<>(clientRegistration.getClaims());
if (!CollectionUtils.isEmpty(customMetadata)) {
ClientSettings clientSettings = registeredClient.getClientSettings();

claims.putAll(customMetadata.stream()
.filter(metadatum -> clientSettings.getSetting(metadatum) != null)
.collect(Collectors.toMap(Function.identity(), clientSettings::getSetting)));
@Override
public RegisteredClient convert(OidcClientRegistration clientRegistration) {
RegisteredClient registeredClient = this.delegate.convert(clientRegistration);

ClientSettings.Builder clientSettingsBuilder = ClientSettings
.withSettings(registeredClient.getClientSettings().getSettings());
if (!CollectionUtils.isEmpty(this.supportedCustomClientMetadata)) {
clientRegistration.getClaims().forEach((claim, value) -> {
if (this.supportedCustomClientMetadata.contains(claim)) {
clientSettingsBuilder.setting(claim, value);
}
});
}
return OidcClientRegistration.withClaims(claims).build();

return RegisteredClient.from(registeredClient).clientSettings(clientSettingsBuilder.build()).build();
}
}

static class CustomRegisteredClientConverter implements Converter<OidcClientRegistration, RegisteredClient> {
private final List<String> customMetadata;
}

private final OidcClientRegistrationRegisteredClientConverter delegate;
private static class CustomClientRegistrationConverter implements Converter<RegisteredClient, OidcClientRegistration> {
private final RegisteredClientOidcClientRegistrationConverter delegate =
new RegisteredClientOidcClientRegistrationConverter();
private final List<String> supportedCustomClientMetadata;

CustomRegisteredClientConverter(List<String> customMetadata) {
this.customMetadata = customMetadata;
this.delegate = new OidcClientRegistrationRegisteredClientConverter();
private CustomClientRegistrationConverter(List<String> supportedCustomClientMetadata) {
this.supportedCustomClientMetadata = supportedCustomClientMetadata;
}

public RegisteredClient convert(OidcClientRegistration clientRegistration) {
RegisteredClient convertedClient = delegate.convert(clientRegistration);
ClientSettings.Builder clientSettingsBuilder = ClientSettings
.withSettings(convertedClient.getClientSettings().getSettings());

if (!CollectionUtils.isEmpty(this.customMetadata)) {
clientRegistration.getClaims().forEach((claim, value) -> {
if (this.customMetadata.contains(claim)) {
clientSettingsBuilder.setting(claim, value);
@Override
public OidcClientRegistration convert(RegisteredClient registeredClient) {
OidcClientRegistration clientRegistration = this.delegate.convert(registeredClient);

Map<String, Object> clientMetadata = new HashMap<>(clientRegistration.getClaims());
if (!CollectionUtils.isEmpty(this.supportedCustomClientMetadata)) {
Map<String, Object> clientSettings = registeredClient.getClientSettings().getSettings();
this.supportedCustomClientMetadata.forEach((customClaim) -> {
if (clientSettings.containsKey(customClaim)) {
clientMetadata.put(customClaim, clientSettings.get(customClaim));
}
});
}

return RegisteredClient.from(convertedClient).clientSettings(clientSettingsBuilder.build()).build();
return OidcClientRegistration.withClaims(clientMetadata).build();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2022 the original author or authors.
* Copyright 2020-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 @@ -111,6 +111,13 @@ public void supportsWhenTypeOidcClientRegistrationAuthenticationTokenThenReturnT
assertThat(this.authenticationProvider.supports(OidcClientRegistrationAuthenticationToken.class)).isTrue();
}

@Test
public void setClientRegistrationConverterWhenNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.authenticationProvider.setClientRegistrationConverter(null))
.withMessage("clientRegistrationConverter cannot be null");
}

@Test
public void authenticateWhenPrincipalNotOAuth2TokenAuthenticationTokenThenThrowOAuth2AuthenticationException() {
TestingAuthenticationToken principal = new TestingAuthenticationToken("principal", "credentials");
Expand Down Expand Up @@ -378,13 +385,6 @@ public void authenticateWhenValidAccessTokenThenReturnClientRegistration() {
assertThat(clientRegistrationResult.getRegistrationAccessToken()).isNull();
}

@Test
public void setClientRegistrationConverterWhenNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.authenticationProvider.setClientRegistrationConverter(null))
.withMessage("clientRegistrationConverter cannot be null");
}

private static Jwt createJwtClientConfiguration() {
return createJwt(Collections.singleton("client.read"));
}
Expand Down

0 comments on commit 05f1371

Please sign in to comment.