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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 client registration endpoint authentication providers for providing custom metadata. Providing custom metadata is optional.
  21. In order to accept custom client metadata when registering a client, a few additional implementation details
  22. are necessary.
  23. [NOTE]
  24. ====
  25. The following example depicts custom metadata `logo_uri` (string type) and `contacts` (string array type)
  26. ====
  27. Create a set of custom `Converter` classes in order to retain custom client claims.
  28. [[sample.CustomMetadataConfig]]
  29. [source,java]
  30. ----
  31. include::{examples-dir}/main/java/sample/registration/CustomMetadataConfig.java[]
  32. ----
  33. <1> Create a `Consumer<List<AuthenticationProvider>>` implementation.
  34. <2> Identify custom fields that should be accepted during client registration.
  35. <3> Filter for `OidcClientRegistrationAuthenticationProvider` and `OidcClientConfigurationAuthenticationProvider` instances.
  36. <4> Add a custom registered client `Converter` (implementation in #7)
  37. <5> Add a custom client registration `Converter` to `OidcClientRegistrationAuthenticationProvider` (implementation in #8)
  38. <6> Add a custom client registration `Converter` to `OidcClientConfigurationAuthenticationProvider` (implementation in #8)
  39. <7> Custom registered client `Converter` implementation that adds custom claims to registered client settings.
  40. <8> Custom client registration `Converter` implementation that modifies client registration claims with custom metadata.
  41. [[configure-client-registrar]]
  42. == Configure client registrar
  43. An existing client is used to register new clients with the authorization server.
  44. The client must be configured with scopes `client.create` and optionally `client.read` for registering clients and retrieving clients, respectively.
  45. The following listing shows an example client:
  46. [[sample.ClientConfig]]
  47. [source,java]
  48. ----
  49. include::{examples-dir}/main/java/sample/registration/ClientConfig.java[]
  50. ----
  51. <1> `client_credentials` grant type is configured to obtain access tokens directly.
  52. <2> `client.create` scope is configured to allow the client to register a new client.
  53. <3> `client.read` scope is configured to allow the client to retrieve a registered client.
  54. [[obtain-initial-access-token]]
  55. == Obtain initial access token
  56. An "initial" access token is required for the client registration request.
  57. The access token request *MUST* contain the `scope` parameter value `client.create` only.
  58. [source,httprequest]
  59. ----
  60. POST /oauth2/token HTTP/1.1
  61. Authorization: Basic <base64-encoded-credentials>
  62. Content-Type: application/x-www-form-urlencoded
  63. grant_type=client_credentials&scope=client.create
  64. ----
  65. [WARNING]
  66. ====
  67. The client registration request requires an access token with a single scope of `client.create`.
  68. If the access token contains additional scope, the client registration request will be denied.
  69. ====
  70. [TIP]
  71. ====
  72. To obtain encoded credentials for the above request, `base64` encode the client credentials in the format of `<clientId>:<clientSecret>`.
  73. Below is an encoding operation for the example in this guide.
  74. [source,console]
  75. ----
  76. echo -n "registrar-client:secret" | base64
  77. ----
  78. ====
  79. [[register-client]]
  80. == Register a client
  81. With an access token obtained from the previous step, a client can now be dynamically registered.
  82. [NOTE]
  83. The "initial" access token can only be used once.
  84. After the client is registered, the access token is invalidated.
  85. [[sample.ClientRegistrar]]
  86. [source,java]
  87. ----
  88. include::{examples-dir}/main/java/sample/registration/ClientRegistrar.java[]
  89. ----
  90. <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 metadata fields `logo_uri` and `contacts`.
  91. <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 metadata fields `logo_uri` and `contacts`.
  92. <3> Example demonstrating client registration and client retrieval.
  93. <4> A sample client registration request object.
  94. <5> Register the client using the "initial" access token and client registration request object.
  95. <6> After successful registration, assert on the client metadata parameters that should be populated in the response.
  96. <7> Extract `registration_access_token` and `registration_client_uri` response parameters, for use in retrieval of the newly registered client.
  97. <8> Retrieve the client using the `registration_access_token` and `registration_client_uri`.
  98. <9> After client retrieval, assert on the client metadata parameters that should be populated in the response.
  99. <10> Sample https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationRequest[Client Registration Request] using `WebClient`.
  100. <11> Sample https://openid.net/specs/openid-connect-registration-1_0.html#ReadRequest[Client Read Request] using `WebClient`.
  101. [NOTE]
  102. 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.