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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. [[configure-client-registrar]]
  22. == Configure client registrar
  23. An existing client is used to register new clients with the authorization server.
  24. The client must be configured with scopes `client.create` and optionally `client.read` for registering clients and retrieving clients, respectively.
  25. The following listing shows an example client:
  26. [[sample.ClientConfig]]
  27. [source,java]
  28. ----
  29. include::{examples-dir}/main/java/sample/registration/ClientConfig.java[]
  30. ----
  31. <1> `client_credentials` grant type is configured to obtain access tokens directly.
  32. <2> `client.create` scope is configured to allow the client to register a new client.
  33. <3> `client.read` scope is configured to allow the client to retrieve a registered client.
  34. [[obtain-initial-access-token]]
  35. == Obtain initial access token
  36. An "initial" access token is required for the client registration request.
  37. The access token request *MUST* contain the `scope` parameter value `client.create` only.
  38. [source,httprequest]
  39. ----
  40. POST /oauth2/token HTTP/1.1
  41. Authorization: Basic <base64-encoded-credentials>
  42. Content-Type: application/x-www-form-urlencoded
  43. grant_type=client_credentials&scope=client.create
  44. ----
  45. [WARNING]
  46. ====
  47. The client registration request requires an access token with a single scope of `client.create`.
  48. If the access token contains additional scope, the client registration request will be denied.
  49. ====
  50. [TIP]
  51. ====
  52. To obtain encoded credentials for the above request, `base64` encode the client credentials in the format of `<clientId>:<clientSecret>`.
  53. Below is an encoding operation for the example in this guide.
  54. [source,console]
  55. ----
  56. echo -n "registrar-client:secret" | base64
  57. ----
  58. ====
  59. [[register-client]]
  60. == Register a client
  61. With an access token obtained from the previous step, a client can now be dynamically registered.
  62. [NOTE]
  63. The "initial" access token can only be used once.
  64. After the client is registered, the access token is invalidated.
  65. [[sample.ClientRegistrar]]
  66. [source,java]
  67. ----
  68. include::{examples-dir}/main/java/sample/registration/ClientRegistrar.java[]
  69. ----
  70. <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].
  71. <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].
  72. <3> Example demonstrating client registration and client retrieval.
  73. <4> A sample client registration request object.
  74. <5> Register the client using the "initial" access token and client registration request object.
  75. <6> After successful registration, assert on the client metadata parameters that should be populated in the response.
  76. <7> Extract `registration_access_token` and `registration_client_uri` response parameters, for use in retrieval of the newly registered client.
  77. <8> Retrieve the client using the `registration_access_token` and `registration_client_uri`.
  78. <9> After client retrieval, assert on the client metadata parameters that should be populated in the response.
  79. <10> Sample https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationRequest[Client Registration Request] using `WebClient`.
  80. <11> Sample https://openid.net/specs/openid-connect-registration-1_0.html#ReadRequest[Client Read Request] using `WebClient`.
  81. [NOTE]
  82. 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.