forked from spring-projects/spring-authorization-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add how-to guide for dynamic client registration with custom metadata
Fixes spring-projectsgh-1044
- Loading branch information
Showing
9 changed files
with
459 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
docs/src/main/java/sample/registration/custommetadata/ClientRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package sample.registration.custommetadata; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.oauth2.core.AuthorizationGrantType; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class ClientRegistrar { | ||
// @fold:on | ||
private final WebClient webClient; | ||
|
||
public ClientRegistrar(WebClient webClient) { | ||
this.webClient = webClient; | ||
} | ||
// @fold:off | ||
|
||
public record ClientRegistrationRequest( // <1> | ||
@JsonProperty("client_name") String clientName, | ||
@JsonProperty("grant_types") List<String> grantTypes, | ||
@JsonProperty("redirect_uris") List<String> redirectUris, | ||
@JsonProperty("logo_uri") String logoUri, | ||
List<String> contacts, | ||
String scope) { | ||
} | ||
|
||
public record ClientRegistrationResponse( // <2> | ||
@JsonProperty("registration_access_token") String registrationAccessToken, | ||
@JsonProperty("registration_client_uri") String registrationClientUri, | ||
@JsonProperty("client_name") String clientName, | ||
@JsonProperty("client_id") String clientId, | ||
@JsonProperty("client_secret") String clientSecret, | ||
@JsonProperty("grant_types") List<String> grantTypes, | ||
@JsonProperty("redirect_uris") List<String> redirectUris, | ||
@JsonProperty("logo_uri") String logoUri, | ||
List<String> contacts, | ||
String scope) { | ||
} | ||
|
||
// @fold:on | ||
public void register(String initialAccessToken) { // <3> | ||
ClientRegistrationRequest clientRegistrationRequest = new ClientRegistrationRequest( // <4> | ||
"client-1", | ||
List.of(AuthorizationGrantType.AUTHORIZATION_CODE.getValue()), | ||
List.of("https://client.example.org/callback", "https://client.example.org/callback2"), | ||
"https://client.example.org/logo", | ||
List.of("contact-1", "contact-2"), | ||
"openid email profile" | ||
); | ||
|
||
ClientRegistrationResponse clientRegistrationResponse = | ||
registerClient(initialAccessToken, clientRegistrationRequest); // <5> | ||
|
||
assert (clientRegistrationResponse.logoUri().contentEquals("https://client.example.org/logo")); // <6> | ||
assert (clientRegistrationResponse.contacts().size() == 2); | ||
assert (clientRegistrationResponse.contacts().contains("contact-1")); | ||
assert (clientRegistrationResponse.contacts().contains("contact-2")); | ||
// @fold:on | ||
assert (clientRegistrationResponse.clientName().contentEquals("client-1")); | ||
assert (!Objects.isNull(clientRegistrationResponse.clientSecret())); | ||
assert (clientRegistrationResponse.scope().contentEquals("openid profile email")); | ||
assert (clientRegistrationResponse.grantTypes().contains(AuthorizationGrantType.AUTHORIZATION_CODE.getValue())); | ||
assert (clientRegistrationResponse.redirectUris().contains("https://client.example.org/callback")); | ||
assert (clientRegistrationResponse.redirectUris().contains("https://client.example.org/callback2")); | ||
assert (!clientRegistrationResponse.registrationAccessToken().isEmpty()); | ||
assert (!clientRegistrationResponse.registrationClientUri().isEmpty()); | ||
// @fold:off | ||
|
||
String registrationAccessToken = clientRegistrationResponse.registrationAccessToken(); // <7> | ||
String registrationClientUri = clientRegistrationResponse.registrationClientUri(); | ||
|
||
ClientRegistrationResponse retrievedClient = retrieveClient(registrationAccessToken, registrationClientUri); // <8> | ||
|
||
assert (clientRegistrationResponse.logoUri().contentEquals("https://client.example.org/logo")); // <9> | ||
assert (clientRegistrationResponse.contacts().size() == 2); | ||
assert (clientRegistrationResponse.contacts().contains("contact-1")); | ||
assert (clientRegistrationResponse.contacts().contains("contact-2")); | ||
// @fold:on | ||
assert (retrievedClient.clientName().contentEquals("client-1")); | ||
assert (!Objects.isNull(retrievedClient.clientId())); | ||
assert (!Objects.isNull(retrievedClient.clientSecret())); | ||
assert (retrievedClient.scope().contentEquals("openid profile email")); | ||
assert (retrievedClient.grantTypes().contains(AuthorizationGrantType.AUTHORIZATION_CODE.getValue())); | ||
assert (retrievedClient.redirectUris().contains("https://client.example.org/callback")); | ||
assert (retrievedClient.redirectUris().contains("https://client.example.org/callback2")); | ||
assert (Objects.isNull(retrievedClient.registrationAccessToken())); | ||
assert (!retrievedClient.registrationClientUri().isEmpty()); | ||
// @fold:off | ||
} | ||
|
||
// @fold:on | ||
public ClientRegistrationResponse registerClient(String initialAccessToken, ClientRegistrationRequest request) { | ||
return this.webClient | ||
.post() | ||
.uri("/connect/register") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.header(HttpHeaders.AUTHORIZATION, "Bearer %s".formatted(initialAccessToken)) | ||
.body(Mono.just(request), ClientRegistrationRequest.class) | ||
.retrieve() | ||
.bodyToMono(ClientRegistrationResponse.class) | ||
.block(); | ||
} | ||
|
||
public ClientRegistrationResponse retrieveClient(String registrationAccessToken, String registrationClientUri) { | ||
return this.webClient | ||
.get() | ||
.uri(registrationClientUri) | ||
.header(HttpHeaders.AUTHORIZATION, "Bearer %s".formatted(registrationAccessToken)) | ||
.retrieve() | ||
.bodyToMono(ClientRegistrationResponse.class) | ||
.block(); | ||
} | ||
// @fold:off | ||
} |
99 changes: 99 additions & 0 deletions
99
docs/src/main/java/sample/registration/custommetadata/CustomMetadataConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package sample.registration.custommetadata; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.security.authentication.AuthenticationProvider; | ||
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; | ||
import org.springframework.security.oauth2.server.authorization.oidc.OidcClientRegistration; | ||
import org.springframework.security.oauth2.server.authorization.oidc.authentication.OidcClientRegistrationAuthenticationProvider; | ||
import org.springframework.security.oauth2.server.authorization.oidc.converter.OidcClientRegistrationRegisteredClientConverter; | ||
import org.springframework.security.oauth2.server.authorization.oidc.converter.RegisteredClientOidcClientRegistrationConverter; | ||
import org.springframework.security.oauth2.server.authorization.settings.ClientSettings; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
public class CustomMetadataConfig { | ||
public static Consumer<List<AuthenticationProvider>> registeredClientConverters() { | ||
List<String> customClientMetadata = List.of("logo_uri", "contacts"); // <1> | ||
|
||
return (authenticationProviders) -> // <2> | ||
authenticationProviders.forEach(authenticationProvider -> { | ||
if (authenticationProvider instanceof OidcClientRegistrationAuthenticationProvider provider) { // <3> | ||
provider.setRegisteredClientConverter(new CustomRegisteredClientConverter(customClientMetadata)); // <4> | ||
provider.setClientRegistrationConverter(new CustomClientRegistrationConverter(customClientMetadata)); // <5> | ||
} | ||
}); | ||
} | ||
|
||
static class CustomRegisteredClientConverter implements Converter<OidcClientRegistration, RegisteredClient> { // <6> | ||
private final List<String> customMetadata; | ||
|
||
private final OidcClientRegistrationRegisteredClientConverter delegate; | ||
|
||
CustomRegisteredClientConverter(List<String> customMetadata) { | ||
this.customMetadata = customMetadata; | ||
this.delegate = new OidcClientRegistrationRegisteredClientConverter(); | ||
} | ||
|
||
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); | ||
} | ||
}); | ||
} | ||
|
||
return RegisteredClient.from(convertedClient).clientSettings(clientSettingsBuilder.build()).build(); | ||
} | ||
} | ||
|
||
static class CustomClientRegistrationConverter implements Converter<RegisteredClient, OidcClientRegistration> { // <7> | ||
private final List<String> customMetadata; | ||
|
||
private final RegisteredClientOidcClientRegistrationConverter delegate; | ||
|
||
CustomClientRegistrationConverter(List<String> customMetadata) { | ||
this.customMetadata = customMetadata; | ||
this.delegate = new RegisteredClientOidcClientRegistrationConverter(); | ||
} | ||
|
||
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))); | ||
} | ||
return OidcClientRegistration.withClaims(claims).build(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.