2
0

client-authentication.adoc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 `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. === Authenticate using `client_secret_jwt`
  80. Given the following Spring Boot 2.x properties for an OAuth 2.0 Client registration:
  81. [source,yaml]
  82. ----
  83. spring:
  84. security:
  85. oauth2:
  86. client:
  87. registration:
  88. okta:
  89. client-id: okta-client-id
  90. client-secret: okta-client-secret
  91. client-authentication-method: client_secret_jwt
  92. authorization-grant-type: client_credentials
  93. ...
  94. ----
  95. The following example shows how to configure `DefaultClientCredentialsTokenResponseClient`:
  96. [tabs]
  97. ======
  98. Java::
  99. +
  100. [source,java,role="primary"]
  101. ----
  102. Function<ClientRegistration, JWK> jwkResolver = (clientRegistration) -> {
  103. if (clientRegistration.getClientAuthenticationMethod().equals(ClientAuthenticationMethod.CLIENT_SECRET_JWT)) {
  104. SecretKeySpec secretKey = new SecretKeySpec(
  105. clientRegistration.getClientSecret().getBytes(StandardCharsets.UTF_8),
  106. "HmacSHA256");
  107. return new OctetSequenceKey.Builder(secretKey)
  108. .keyID(UUID.randomUUID().toString())
  109. .build();
  110. }
  111. return null;
  112. };
  113. OAuth2ClientCredentialsGrantRequestEntityConverter requestEntityConverter =
  114. new OAuth2ClientCredentialsGrantRequestEntityConverter();
  115. requestEntityConverter.addParametersConverter(
  116. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver));
  117. DefaultClientCredentialsTokenResponseClient tokenResponseClient =
  118. new DefaultClientCredentialsTokenResponseClient();
  119. tokenResponseClient.setRequestEntityConverter(requestEntityConverter);
  120. ----
  121. Kotlin::
  122. +
  123. [source,kotlin,role="secondary"]
  124. ----
  125. val jwkResolver = Function<ClientRegistration, JWK?> { clientRegistration: ClientRegistration ->
  126. if (clientRegistration.clientAuthenticationMethod == ClientAuthenticationMethod.CLIENT_SECRET_JWT) {
  127. val secretKey = SecretKeySpec(
  128. clientRegistration.clientSecret.toByteArray(StandardCharsets.UTF_8),
  129. "HmacSHA256"
  130. )
  131. OctetSequenceKey.Builder(secretKey)
  132. .keyID(UUID.randomUUID().toString())
  133. .build()
  134. }
  135. null
  136. }
  137. val requestEntityConverter = OAuth2ClientCredentialsGrantRequestEntityConverter()
  138. requestEntityConverter.addParametersConverter(
  139. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  140. )
  141. val tokenResponseClient = DefaultClientCredentialsTokenResponseClient()
  142. tokenResponseClient.setRequestEntityConverter(requestEntityConverter)
  143. ----
  144. ======
  145. === Customizing the JWT assertion
  146. 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:
  147. [tabs]
  148. ======
  149. Java::
  150. +
  151. [source,java,role="primary"]
  152. ----
  153. Function<ClientRegistration, JWK> jwkResolver = ...
  154. NimbusJwtClientAuthenticationParametersConverter<OAuth2ClientCredentialsGrantRequest> converter =
  155. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver);
  156. converter.setJwtClientAssertionCustomizer((context) -> {
  157. context.getHeaders().header("custom-header", "header-value");
  158. context.getClaims().claim("custom-claim", "claim-value");
  159. });
  160. ----
  161. Kotlin::
  162. +
  163. [source,kotlin,role="secondary"]
  164. ----
  165. val jwkResolver = ...
  166. val converter: NimbusJwtClientAuthenticationParametersConverter<OAuth2ClientCredentialsGrantRequest> =
  167. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  168. converter.setJwtClientAssertionCustomizer { context ->
  169. context.headers.header("custom-header", "header-value")
  170. context.claims.claim("custom-claim", "claim-value")
  171. }
  172. ----
  173. ======