how-to-dynamic-client-registration.adoc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. [[how-to-dynamic-client-registration]]
  2. = How-to: Register a client dynamically
  3. :index-link: ../how-to.html
  4. :docs-dir: ..
  5. This guide shows how to configure OpenID Connect Dynamic Client Registration in Spring Authorization Server and walks through an example of how to register a client.
  6. Spring Authorization Server implements the https://openid.net/specs/openid-connect-registration-1_0.html[OpenID Connect Dynamic Client Registration 1.0] specification, providing the capability to dynamically register and retrieve OpenID Connect clients.
  7. * xref:guides/how-to-dynamic-client-registration.adoc#enable-dynamic-client-registration[Enable Dynamic Client Registration]
  8. * xref:guides/how-to-dynamic-client-registration.adoc#configure-client-registrar[Configure client registrar]
  9. * xref:guides/how-to-dynamic-client-registration.adoc#obtain-initial-access-token[Obtain initial access token]
  10. * xref:guides/how-to-dynamic-client-registration.adoc#register-client[Register a client]
  11. [[enable-dynamic-client-registration]]
  12. == Enable Dynamic Client Registration
  13. By default, dynamic client registration functionality is disabled in Spring Authorization Server.
  14. To enable, add the following configuration:
  15. [[sample.SecurityConfig]]
  16. [source,java]
  17. ----
  18. include::{examples-dir}/main/java/sample/registration/SecurityConfig.java[]
  19. ----
  20. <1> Enable the xref:protocol-endpoints.adoc#oidc-client-registration-endpoint[OpenID Connect 1.0 Client Registration Endpoint] with the default configuration.
  21. <2> Optionally, customize the default ``AuthenticationProvider``'s to support custom client metadata parameters.
  22. In order to support custom client metadata parameters when registering a client, a few additional implementation details are required.
  23. The following example shows a sample implementation of ``Converter``'s that support custom client metadata parameters (`logo_uri` and `contacts`) and are configured in `OidcClientRegistrationAuthenticationProvider` and `OidcClientConfigurationAuthenticationProvider`.
  24. [[sample.CustomClientMetadataConfig]]
  25. [source,java]
  26. ----
  27. include::{examples-dir}/main/java/sample/registration/CustomClientMetadataConfig.java[]
  28. ----
  29. <1> Define a `Consumer<List<AuthenticationProvider>>` providing the ability to customize the default ``AuthenticationProvider``'s.
  30. <2> Define custom client metadata parameters that are supported for client registration.
  31. <3> Configure `OidcClientRegistrationAuthenticationProvider.setRegisteredClientConverter()` with a `CustomRegisteredClientConverter`.
  32. <4> Configure `OidcClientRegistrationAuthenticationProvider.setClientRegistrationConverter()` with a `CustomClientRegistrationConverter`.
  33. <5> Configure `OidcClientConfigurationAuthenticationProvider.setClientRegistrationConverter()` with a `CustomClientRegistrationConverter`.
  34. [[configure-client-registrar]]
  35. == Configure client registrar
  36. An existing client is used to register new clients with the authorization server.
  37. The client must be configured with scopes `client.create` and optionally `client.read` for registering clients and retrieving clients, respectively.
  38. The following listing shows an example client:
  39. [[sample.ClientConfig]]
  40. [source,java]
  41. ----
  42. include::{examples-dir}/main/java/sample/registration/ClientConfig.java[]
  43. ----
  44. <1> `client_credentials` grant type is configured to obtain access tokens directly.
  45. <2> `client.create` scope is configured to allow the client to register a new client.
  46. <3> `client.read` scope is configured to allow the client to retrieve a registered client.
  47. [[obtain-initial-access-token]]
  48. == Obtain initial access token
  49. An "initial" access token is required for the client registration request.
  50. The access token request *MUST* contain the `scope` parameter value `client.create` only.
  51. [source,httprequest]
  52. ----
  53. POST /oauth2/token HTTP/1.1
  54. Authorization: Basic <base64-encoded-credentials>
  55. Content-Type: application/x-www-form-urlencoded
  56. grant_type=client_credentials&scope=client.create
  57. ----
  58. [WARNING]
  59. ====
  60. The client registration request requires an access token with a single scope of `client.create`.
  61. If the access token contains additional scope, the client registration request will be denied.
  62. ====
  63. [TIP]
  64. ====
  65. To obtain encoded credentials for the above request, `base64` encode the client credentials in the format of `<clientId>:<clientSecret>`.
  66. Below is an encoding operation for the example in this guide.
  67. [source,console]
  68. ----
  69. echo -n "registrar-client:secret" | base64
  70. ----
  71. ====
  72. [[register-client]]
  73. == Register a client
  74. With an access token obtained from the previous step, a client can now be dynamically registered.
  75. [NOTE]
  76. The "initial" access token can only be used once.
  77. After the client is registered, the access token is invalidated.
  78. [[sample.ClientRegistrar]]
  79. [source,java]
  80. ----
  81. include::{examples-dir}/main/java/sample/registration/ClientRegistrar.java[]
  82. ----
  83. <1> A minimal representation of a client registration request. You may add additional client metadata parameters as per https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationRequest[Client Registration Request]. This example request contains custom client metadata parameters `logo_uri` and `contacts`.
  84. <2> A minimal representation of a client registration response. You may add additional client metadata parameters as per https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationResponse[Client Registration Response]. This example response contains custom client metadata parameters `logo_uri` and `contacts`.
  85. <3> Example demonstrating client registration and client retrieval.
  86. <4> A sample client registration request object.
  87. <5> Register the client using the "initial" access token and client registration request object.
  88. <6> After successful registration, assert on the client metadata parameters that should be populated in the response.
  89. <7> Extract `registration_access_token` and `registration_client_uri` response parameters, for use in retrieval of the newly registered client.
  90. <8> Retrieve the client using the `registration_access_token` and `registration_client_uri`.
  91. <9> After client retrieval, assert on the client metadata parameters that should be populated in the response.
  92. <10> Sample https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationRequest[Client Registration Request] using `WebClient`.
  93. <11> Sample https://openid.net/specs/openid-connect-registration-1_0.html#ReadRequest[Client Read Request] using `WebClient`.
  94. [NOTE]
  95. The https://openid.net/specs/openid-connect-registration-1_0.html#ReadResponse[Client Read Response] should contain the same client metadata parameters as the https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationResponse[Client Registration Response], except the `registration_access_token` parameter.