client-authentication.adoc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 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 `DefaultAuthorizationCodeTokenResponseClient`:
  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. OAuth2AuthorizationCodeGrantRequestEntityConverter requestEntityConverter =
  47. new OAuth2AuthorizationCodeGrantRequestEntityConverter();
  48. requestEntityConverter.addParametersConverter(
  49. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver));
  50. DefaultAuthorizationCodeTokenResponseClient tokenResponseClient =
  51. new DefaultAuthorizationCodeTokenResponseClient();
  52. tokenResponseClient.setRequestEntityConverter(requestEntityConverter);
  53. ----
  54. Kotlin::
  55. +
  56. [source,kotlin,role="secondary"]
  57. ----
  58. val jwkResolver: Function<ClientRegistration, JWK> =
  59. Function<ClientRegistration, JWK> { clientRegistration ->
  60. if (clientRegistration.clientAuthenticationMethod.equals(ClientAuthenticationMethod.PRIVATE_KEY_JWT)) {
  61. // Assuming RSA key type
  62. var publicKey: RSAPublicKey
  63. var privateKey: RSAPrivateKey
  64. RSAKey.Builder(publicKey) = //...
  65. .privateKey(privateKey) = //...
  66. .keyID(UUID.randomUUID().toString())
  67. .build()
  68. }
  69. null
  70. }
  71. val requestEntityConverter = OAuth2AuthorizationCodeGrantRequestEntityConverter()
  72. requestEntityConverter.addParametersConverter(
  73. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  74. )
  75. val tokenResponseClient = DefaultAuthorizationCodeTokenResponseClient()
  76. tokenResponseClient.setRequestEntityConverter(requestEntityConverter)
  77. ----
  78. ======
  79. [NOTE]
  80. If you're using the `client-authentication-method: client_secret_basic` and you need to skip URL encoding,
  81. create a new `DefaultOAuth2TokenRequestHeadersConverter` and set it in the Request Entity Converter above.
  82. === Authenticate using `client_secret_jwt`
  83. Given the following Spring Boot properties for an OAuth 2.0 Client registration:
  84. [source,yaml]
  85. ----
  86. spring:
  87. security:
  88. oauth2:
  89. client:
  90. registration:
  91. okta:
  92. client-id: okta-client-id
  93. client-secret: okta-client-secret
  94. client-authentication-method: client_secret_jwt
  95. authorization-grant-type: client_credentials
  96. ...
  97. ----
  98. The following example shows how to configure `DefaultClientCredentialsTokenResponseClient`:
  99. [tabs]
  100. ======
  101. Java::
  102. +
  103. [source,java,role="primary"]
  104. ----
  105. Function<ClientRegistration, JWK> jwkResolver = (clientRegistration) -> {
  106. if (clientRegistration.getClientAuthenticationMethod().equals(ClientAuthenticationMethod.CLIENT_SECRET_JWT)) {
  107. SecretKeySpec secretKey = new SecretKeySpec(
  108. clientRegistration.getClientSecret().getBytes(StandardCharsets.UTF_8),
  109. "HmacSHA256");
  110. return new OctetSequenceKey.Builder(secretKey)
  111. .keyID(UUID.randomUUID().toString())
  112. .build();
  113. }
  114. return null;
  115. };
  116. OAuth2ClientCredentialsGrantRequestEntityConverter requestEntityConverter =
  117. new OAuth2ClientCredentialsGrantRequestEntityConverter();
  118. requestEntityConverter.addParametersConverter(
  119. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver));
  120. DefaultClientCredentialsTokenResponseClient tokenResponseClient =
  121. new DefaultClientCredentialsTokenResponseClient();
  122. tokenResponseClient.setRequestEntityConverter(requestEntityConverter);
  123. ----
  124. Kotlin::
  125. +
  126. [source,kotlin,role="secondary"]
  127. ----
  128. val jwkResolver = Function<ClientRegistration, JWK?> { clientRegistration: ClientRegistration ->
  129. if (clientRegistration.clientAuthenticationMethod == ClientAuthenticationMethod.CLIENT_SECRET_JWT) {
  130. val secretKey = SecretKeySpec(
  131. clientRegistration.clientSecret.toByteArray(StandardCharsets.UTF_8),
  132. "HmacSHA256"
  133. )
  134. OctetSequenceKey.Builder(secretKey)
  135. .keyID(UUID.randomUUID().toString())
  136. .build()
  137. }
  138. null
  139. }
  140. val requestEntityConverter = OAuth2ClientCredentialsGrantRequestEntityConverter()
  141. requestEntityConverter.addParametersConverter(
  142. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  143. )
  144. val tokenResponseClient = DefaultClientCredentialsTokenResponseClient()
  145. tokenResponseClient.setRequestEntityConverter(requestEntityConverter)
  146. ----
  147. ======
  148. === Customizing the JWT assertion
  149. 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:
  150. [tabs]
  151. ======
  152. Java::
  153. +
  154. [source,java,role="primary"]
  155. ----
  156. Function<ClientRegistration, JWK> jwkResolver = ...
  157. NimbusJwtClientAuthenticationParametersConverter<OAuth2ClientCredentialsGrantRequest> converter =
  158. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver);
  159. converter.setJwtClientAssertionCustomizer((context) -> {
  160. context.getHeaders().header("custom-header", "header-value");
  161. context.getClaims().claim("custom-claim", "claim-value");
  162. });
  163. ----
  164. Kotlin::
  165. +
  166. [source,kotlin,role="secondary"]
  167. ----
  168. val jwkResolver = ...
  169. val converter: NimbusJwtClientAuthenticationParametersConverter<OAuth2ClientCredentialsGrantRequest> =
  170. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  171. converter.setJwtClientAssertionCustomizer { context ->
  172. context.headers.header("custom-header", "header-value")
  173. context.claims.claim("custom-claim", "claim-value")
  174. }
  175. ----
  176. ======