client-authentication.adoc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. [[oauth2Client-client-auth-support]]
  2. = Client Authentication Support
  3. [[oauth2Client-jwt-bearer-auth]]
  4. == JWT Bearer
  5. [NOTE]
  6. Please refer to JSON Web Token (JWT) Profile for OAuth 2.0 Client Authentication and Authorization Grants for further details on https://datatracker.ietf.org/doc/html/rfc7523#section-2.2[JWT Bearer] Client Authentication.
  7. The default implementation for JWT Bearer Client Authentication is `NimbusJwtClientAuthenticationParametersConverter`,
  8. which is a `Converter` that customizes the Token Request parameters by adding
  9. a signed JSON Web Token (JWS) in the `client_assertion` parameter.
  10. The `java.security.PrivateKey` or `javax.crypto.SecretKey` used for signing the JWS
  11. is supplied by the `com.nimbusds.jose.jwk.JWK` resolver associated with `NimbusJwtClientAuthenticationParametersConverter`.
  12. === Authenticate using `private_key_jwt`
  13. Given the following Spring Boot 2.x properties for an OAuth 2.0 Client registration:
  14. [source,yaml]
  15. ----
  16. spring:
  17. security:
  18. oauth2:
  19. client:
  20. registration:
  21. okta:
  22. client-id: okta-client-id
  23. client-authentication-method: private_key_jwt
  24. authorization-grant-type: authorization_code
  25. ...
  26. ----
  27. The following example shows how to configure `WebClientReactiveAuthorizationCodeTokenResponseClient`:
  28. [tabs]
  29. ======
  30. Java::
  31. +
  32. [source,java,role="primary"]
  33. ----
  34. Function<ClientRegistration, JWK> jwkResolver = (clientRegistration) -> {
  35. if (clientRegistration.getClientAuthenticationMethod().equals(ClientAuthenticationMethod.PRIVATE_KEY_JWT)) {
  36. // Assuming RSA key type
  37. RSAPublicKey publicKey = ...
  38. RSAPrivateKey privateKey = ...
  39. return new RSAKey.Builder(publicKey)
  40. .privateKey(privateKey)
  41. .keyID(UUID.randomUUID().toString())
  42. .build();
  43. }
  44. return null;
  45. };
  46. WebClientReactiveAuthorizationCodeTokenResponseClient tokenResponseClient =
  47. new WebClientReactiveAuthorizationCodeTokenResponseClient();
  48. tokenResponseClient.addParametersConverter(
  49. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver));
  50. ----
  51. Kotlin::
  52. +
  53. [source,kotlin,role="secondary"]
  54. ----
  55. val jwkResolver: Function<ClientRegistration, JWK> =
  56. Function<ClientRegistration, JWK> { clientRegistration ->
  57. if (clientRegistration.clientAuthenticationMethod.equals(ClientAuthenticationMethod.PRIVATE_KEY_JWT)) {
  58. // Assuming RSA key type
  59. var publicKey: RSAPublicKey = ...
  60. var privateKey: RSAPrivateKey = ...
  61. RSAKey.Builder(publicKey)
  62. .privateKey(privateKey)
  63. .keyID(UUID.randomUUID().toString())
  64. .build()
  65. }
  66. null
  67. }
  68. val tokenResponseClient = WebClientReactiveAuthorizationCodeTokenResponseClient()
  69. tokenResponseClient.addParametersConverter(
  70. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  71. )
  72. ----
  73. ======
  74. === Authenticate using `client_secret_jwt`
  75. Given the following Spring Boot 2.x properties for an OAuth 2.0 Client registration:
  76. [source,yaml]
  77. ----
  78. spring:
  79. security:
  80. oauth2:
  81. client:
  82. registration:
  83. okta:
  84. client-id: okta-client-id
  85. client-secret: okta-client-secret
  86. client-authentication-method: client_secret_jwt
  87. authorization-grant-type: client_credentials
  88. ...
  89. ----
  90. The following example shows how to configure `WebClientReactiveClientCredentialsTokenResponseClient`:
  91. [tabs]
  92. ======
  93. Java::
  94. +
  95. [source,java,role="primary"]
  96. ----
  97. Function<ClientRegistration, JWK> jwkResolver = (clientRegistration) -> {
  98. if (clientRegistration.getClientAuthenticationMethod().equals(ClientAuthenticationMethod.CLIENT_SECRET_JWT)) {
  99. SecretKeySpec secretKey = new SecretKeySpec(
  100. clientRegistration.getClientSecret().getBytes(StandardCharsets.UTF_8),
  101. "HmacSHA256");
  102. return new OctetSequenceKey.Builder(secretKey)
  103. .keyID(UUID.randomUUID().toString())
  104. .build();
  105. }
  106. return null;
  107. };
  108. WebClientReactiveClientCredentialsTokenResponseClient tokenResponseClient =
  109. new WebClientReactiveClientCredentialsTokenResponseClient();
  110. tokenResponseClient.addParametersConverter(
  111. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver));
  112. ----
  113. Kotlin::
  114. +
  115. [source,kotlin,role="secondary"]
  116. ----
  117. val jwkResolver = Function<ClientRegistration, JWK?> { clientRegistration: ClientRegistration ->
  118. if (clientRegistration.clientAuthenticationMethod == ClientAuthenticationMethod.CLIENT_SECRET_JWT) {
  119. val secretKey = SecretKeySpec(
  120. clientRegistration.clientSecret.toByteArray(StandardCharsets.UTF_8),
  121. "HmacSHA256"
  122. )
  123. OctetSequenceKey.Builder(secretKey)
  124. .keyID(UUID.randomUUID().toString())
  125. .build()
  126. }
  127. null
  128. }
  129. val tokenResponseClient = WebClientReactiveClientCredentialsTokenResponseClient()
  130. tokenResponseClient.addParametersConverter(
  131. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  132. )
  133. ----
  134. ======
  135. === Customizing the JWT assertion
  136. The JWT produced by `NimbusJwtClientAuthenticationParametersConverter` contains the `iss`, `sub`, `aud`, `jti`, `iat` and `exp` claims by default. You can customize the headers and/or claims by providing a `Consumer<NimbusJwtClientAuthenticationParametersConverter.JwtClientAuthenticationContext<T>>` to `setJwtClientAssertionCustomizer()`. The following example shows how to customize claims of the JWT:
  137. [tabs]
  138. ======
  139. Java::
  140. +
  141. [source,java,role="primary"]
  142. ----
  143. Function<ClientRegistration, JWK> jwkResolver = ...
  144. NimbusJwtClientAuthenticationParametersConverter<OAuth2ClientCredentialsGrantRequest> converter =
  145. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver);
  146. converter.setJwtClientAssertionCustomizer((context) -> {
  147. context.getHeaders().header("custom-header", "header-value");
  148. context.getClaims().claim("custom-claim", "claim-value");
  149. });
  150. ----
  151. Kotlin::
  152. +
  153. [source,kotlin,role="secondary"]
  154. ----
  155. val jwkResolver = ...
  156. val converter: NimbusJwtClientAuthenticationParametersConverter<OAuth2ClientCredentialsGrantRequest> =
  157. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  158. converter.setJwtClientAssertionCustomizer { context ->
  159. context.headers.header("custom-header", "header-value")
  160. context.claims.claim("custom-claim", "claim-value")
  161. }
  162. ----
  163. ======