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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 1.0 in Spring Authorization Server and walks through an example of how to register a client.
  6. Spring Authorization Server implements https://openid.net/specs/openid-connect-registration-1_0.html[OpenID Connect Dynamic Client Registration 1.0]
  7. specification, gaining the ability to dynamically register and retrieve OpenID clients.
  8. - xref:guides/how-to-dynamic-client-registration.adoc#enable[Enable Dynamic Client Registration]
  9. - xref:guides/how-to-dynamic-client-registration.adoc#configure-initial-client[Configure initial client]
  10. - xref:guides/how-to-dynamic-client-registration.adoc#obtain-initial-access-token[Obtain initial access token]
  11. - xref:guides/how-to-dynamic-client-registration.adoc#register-client[Register a client]
  12. [[enable]]
  13. == Enable Dynamic Client Registration
  14. By default, dynamic client registration functionality is disabled in Spring Authorization Server.
  15. To enable, add the following configuration:
  16. [[sample.dcrAuthServerConfig]]
  17. [source,java]
  18. ----
  19. include::{examples-dir}/main/java/sample/dcr/DcrConfiguration.java[]
  20. ----
  21. <1> Add a `SecurityFilterChain` `@Bean` that registers an `OAuth2AuthorizationServerConfigurer`
  22. <2> In the configurer, apply OIDC client registration endpoint customizer with default values.
  23. This enables dynamic client registration functionality.
  24. Please refer to xref:protocol-endpoints.adoc#oidc-client-registration-endpoint[Client Registration Endpoint docs] for in-depth configuration details.
  25. [[configure-initial-client]]
  26. == Configure initial client
  27. An initial client is required in order to register new clients in the authorization server.
  28. The client must be configured with scopes `client.create` and optionally `client.read` for creating clients and reading clients, respectively.
  29. A programmatic example of such a client is below.
  30. [[sample.dcrRegisteredClientConfig]]
  31. [source,java]
  32. ----
  33. include::{examples-dir}/main/java/sample/dcr/RegisteredClientConfiguration.java[]
  34. ----
  35. <1> A `RegisteredClientRepository` `@Bean` is configured with a set of clients.
  36. <2> An initial client with client id `dcr-client` is configured.
  37. <3> `client_credentials` grant type is set to fetch access tokens directly.
  38. <4> `client.create` scope is configured for the client to ensure they are able to create clients.
  39. <5> `client.read` scope is configured for the client to ensure they are able to fetch and read clients.
  40. <6> The initial client is saved into the data store.
  41. After configuring the above, run the authorization server in your preferred environment.
  42. [[obtain-initial-access-token]]
  43. == Obtain initial access token
  44. An initial access token is required to be able to create client registration requests.
  45. The token request must contain a request for scope `client.create` only.
  46. [source,httprequest]
  47. ----
  48. POST /oauth2/token HTTP/1.1
  49. Authorization: Basic <base64-encoded-credentials>
  50. Content-Type: application/x-www-form-urlencoded
  51. grant_type=client_credentials&scope=client.create
  52. ----
  53. [WARNING]
  54. ====
  55. If you provide more than one scope in the request, you will not be able to register a client.
  56. The client creation request requires an access token with a single scope of `client.create`
  57. ====
  58. [TIP]
  59. ====
  60. To obtain encoded credentials for the above request, `base64` encode the client credentials in the format of
  61. `<clientId>:<clientSecret>`. Below is an encoding operation for the example in this guide.
  62. [source,console]
  63. ----
  64. echo -n "initial-app:secret" | base64
  65. ----
  66. ====
  67. [[register-client]]
  68. == Register a client
  69. With an access token obtained from the previous step, a client can now be dynamically registered.
  70. [NOTE]
  71. The access token can only be used once. After a single registration request, the access token is invalidated.
  72. [[sample.dcrClientRegistration]]
  73. [source,java]
  74. ----
  75. include::{examples-dir}/main/java/sample/dcr/DcrClient.java[]
  76. ----
  77. <1> A minimal client registration request object.
  78. You may add additional fields as per https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationRequest[OpenID Connect Dynamic Client Registration 1.0 spec - Client Registration Request].
  79. <2> A minimal client registration response object.
  80. You may add additional response fields as per https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationResponse[OpenID Connect Dynamic Client Registration 1.0 spec - Client Registration Response].
  81. <3> A sample client registration request object which will be used to register a sample client.
  82. <4> Example dynamic client registration procedure, demonstrating dynamic registration and client retrieval.
  83. <5> Register a client using sample request from step 2, using initial access token from previous step.
  84. Skip to step 10 for implementation.
  85. <6> After registration, assert on the fields that should be populated in the response upon successful registration.
  86. <7> Extract `registration_access_token` and `registration_client_uri` fields, for use in retrieval of the newly registered client.
  87. <8> Retrieve client. Skip to step 11 for implementation.
  88. <9> After client retrieval, assert on the fields that should be populated in the response.
  89. <10> Sample client registration procedure using Spring WebFlux's `WebClient`.
  90. Note that the `WebClient` must have `baseUrl` of the authorization server configured.
  91. <11> Sample client retrieval procedure using Spring WebFlux's `WebClient`.
  92. Note that the `WebClient` must have `baseUrl` of the authorization server configured.
  93. The retrieve client response should contain the same information about the client as seen when the client was first
  94. registered, except for `registration_access_token` field.