|
| 1 | +/* |
| 2 | + * Copyright 2020-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package sample.registration.custommetadata; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 19 | +import org.springframework.http.HttpHeaders; |
| 20 | +import org.springframework.http.MediaType; |
| 21 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 22 | +import org.springframework.web.reactive.function.client.WebClient; |
| 23 | +import reactor.core.publisher.Mono; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.Objects; |
| 27 | + |
| 28 | +public class ClientRegistrar { |
| 29 | + // @fold:on |
| 30 | + private final WebClient webClient; |
| 31 | + |
| 32 | + public ClientRegistrar(WebClient webClient) { |
| 33 | + this.webClient = webClient; |
| 34 | + } |
| 35 | + // @fold:off |
| 36 | + |
| 37 | + public record ClientRegistrationRequest( // <1> |
| 38 | + @JsonProperty("client_name") String clientName, |
| 39 | + @JsonProperty("grant_types") List<String> grantTypes, |
| 40 | + @JsonProperty("redirect_uris") List<String> redirectUris, |
| 41 | + @JsonProperty("logo_uri") String logoUri, |
| 42 | + List<String> contacts, |
| 43 | + String scope) { |
| 44 | + } |
| 45 | + |
| 46 | + public record ClientRegistrationResponse( // <2> |
| 47 | + @JsonProperty("registration_access_token") String registrationAccessToken, |
| 48 | + @JsonProperty("registration_client_uri") String registrationClientUri, |
| 49 | + @JsonProperty("client_name") String clientName, |
| 50 | + @JsonProperty("client_id") String clientId, |
| 51 | + @JsonProperty("client_secret") String clientSecret, |
| 52 | + @JsonProperty("grant_types") List<String> grantTypes, |
| 53 | + @JsonProperty("redirect_uris") List<String> redirectUris, |
| 54 | + @JsonProperty("logo_uri") String logoUri, |
| 55 | + List<String> contacts, |
| 56 | + String scope) { |
| 57 | + } |
| 58 | + |
| 59 | + // @fold:on |
| 60 | + public void exampleRegistration(String initialAccessToken) { // <3> |
| 61 | + ClientRegistrationRequest clientRegistrationRequest = new ClientRegistrationRequest( // <4> |
| 62 | + "client-1", |
| 63 | + List.of(AuthorizationGrantType.AUTHORIZATION_CODE.getValue()), |
| 64 | + List.of("https://client.example.org/callback", "https://client.example.org/callback2"), |
| 65 | + "https://client.example.org/logo", |
| 66 | + List.of("contact-1", "contact-2"), |
| 67 | + "openid email profile" |
| 68 | + ); |
| 69 | + |
| 70 | + ClientRegistrationResponse clientRegistrationResponse = |
| 71 | + registerClient(initialAccessToken, clientRegistrationRequest); // <5> |
| 72 | + |
| 73 | + assert (clientRegistrationResponse.logoUri().contentEquals("https://client.example.org/logo")); // <6> |
| 74 | + assert (clientRegistrationResponse.contacts().size() == 2); |
| 75 | + assert (clientRegistrationResponse.contacts().contains("contact-1")); |
| 76 | + assert (clientRegistrationResponse.contacts().contains("contact-2")); |
| 77 | + // @fold:on |
| 78 | + assert (clientRegistrationResponse.clientName().contentEquals("client-1")); |
| 79 | + assert (!Objects.isNull(clientRegistrationResponse.clientSecret())); |
| 80 | + assert (clientRegistrationResponse.scope().contentEquals("openid profile email")); |
| 81 | + assert (clientRegistrationResponse.grantTypes().contains(AuthorizationGrantType.AUTHORIZATION_CODE.getValue())); |
| 82 | + assert (clientRegistrationResponse.redirectUris().contains("https://client.example.org/callback")); |
| 83 | + assert (clientRegistrationResponse.redirectUris().contains("https://client.example.org/callback2")); |
| 84 | + assert (!clientRegistrationResponse.registrationAccessToken().isEmpty()); |
| 85 | + assert (!clientRegistrationResponse.registrationClientUri().isEmpty()); |
| 86 | + // @fold:off |
| 87 | + |
| 88 | + String registrationAccessToken = clientRegistrationResponse.registrationAccessToken(); // <7> |
| 89 | + String registrationClientUri = clientRegistrationResponse.registrationClientUri(); |
| 90 | + |
| 91 | + ClientRegistrationResponse retrievedClient = retrieveClient(registrationAccessToken, registrationClientUri); // <8> |
| 92 | + |
| 93 | + assert (clientRegistrationResponse.logoUri().contentEquals("https://client.example.org/logo")); // <9> |
| 94 | + assert (clientRegistrationResponse.contacts().size() == 2); |
| 95 | + assert (clientRegistrationResponse.contacts().contains("contact-1")); |
| 96 | + assert (clientRegistrationResponse.contacts().contains("contact-2")); |
| 97 | + // @fold:on |
| 98 | + assert (retrievedClient.clientName().contentEquals("client-1")); |
| 99 | + assert (!Objects.isNull(retrievedClient.clientId())); |
| 100 | + assert (!Objects.isNull(retrievedClient.clientSecret())); |
| 101 | + assert (retrievedClient.scope().contentEquals("openid profile email")); |
| 102 | + assert (retrievedClient.grantTypes().contains(AuthorizationGrantType.AUTHORIZATION_CODE.getValue())); |
| 103 | + assert (retrievedClient.redirectUris().contains("https://client.example.org/callback")); |
| 104 | + assert (retrievedClient.redirectUris().contains("https://client.example.org/callback2")); |
| 105 | + assert (Objects.isNull(retrievedClient.registrationAccessToken())); |
| 106 | + assert (!retrievedClient.registrationClientUri().isEmpty()); |
| 107 | + // @fold:off |
| 108 | + } |
| 109 | + |
| 110 | + // @fold:on |
| 111 | + public ClientRegistrationResponse registerClient(String initialAccessToken, ClientRegistrationRequest request) { |
| 112 | + return this.webClient |
| 113 | + .post() |
| 114 | + .uri("/connect/register") |
| 115 | + .contentType(MediaType.APPLICATION_JSON) |
| 116 | + .accept(MediaType.APPLICATION_JSON) |
| 117 | + .header(HttpHeaders.AUTHORIZATION, "Bearer %s".formatted(initialAccessToken)) |
| 118 | + .body(Mono.just(request), ClientRegistrationRequest.class) |
| 119 | + .retrieve() |
| 120 | + .bodyToMono(ClientRegistrationResponse.class) |
| 121 | + .block(); |
| 122 | + } |
| 123 | + |
| 124 | + public ClientRegistrationResponse retrieveClient(String registrationAccessToken, String registrationClientUri) { |
| 125 | + return this.webClient |
| 126 | + .get() |
| 127 | + .uri(registrationClientUri) |
| 128 | + .header(HttpHeaders.AUTHORIZATION, "Bearer %s".formatted(registrationAccessToken)) |
| 129 | + .retrieve() |
| 130 | + .bodyToMono(ClientRegistrationResponse.class) |
| 131 | + .block(); |
| 132 | + } |
| 133 | + // @fold:off |
| 134 | +} |
0 commit comments