client-authentication.adoc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. ====
  29. .Java
  30. [source,java,role="primary"]
  31. ----
  32. Function<ClientRegistration, JWK> jwkResolver = (clientRegistration) -> {
  33. if (clientRegistration.getClientAuthenticationMethod().equals(ClientAuthenticationMethod.PRIVATE_KEY_JWT)) {
  34. // Assuming RSA key type
  35. RSAPublicKey publicKey = ...
  36. RSAPrivateKey privateKey = ...
  37. return new RSAKey.Builder(publicKey)
  38. .privateKey(privateKey)
  39. .keyID(UUID.randomUUID().toString())
  40. .build();
  41. }
  42. return null;
  43. };
  44. WebClientReactiveAuthorizationCodeTokenResponseClient tokenResponseClient =
  45. new WebClientReactiveAuthorizationCodeTokenResponseClient();
  46. tokenResponseClient.addParametersConverter(
  47. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver));
  48. ----
  49. .Kotlin
  50. [source,kotlin,role="secondary"]
  51. ----
  52. val jwkResolver: Function<ClientRegistration, JWK> =
  53. Function<ClientRegistration, JWK> { clientRegistration ->
  54. if (clientRegistration.clientAuthenticationMethod.equals(ClientAuthenticationMethod.PRIVATE_KEY_JWT)) {
  55. // Assuming RSA key type
  56. var publicKey: RSAPublicKey = ...
  57. var privateKey: RSAPrivateKey = ...
  58. RSAKey.Builder(publicKey)
  59. .privateKey(privateKey)
  60. .keyID(UUID.randomUUID().toString())
  61. .build()
  62. }
  63. null
  64. }
  65. val tokenResponseClient = WebClientReactiveAuthorizationCodeTokenResponseClient()
  66. tokenResponseClient.addParametersConverter(
  67. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  68. )
  69. ----
  70. ====
  71. === Authenticate using `client_secret_jwt`
  72. Given the following Spring Boot 2.x properties for an OAuth 2.0 Client registration:
  73. [source,yaml]
  74. ----
  75. spring:
  76. security:
  77. oauth2:
  78. client:
  79. registration:
  80. okta:
  81. client-id: okta-client-id
  82. client-secret: okta-client-secret
  83. client-authentication-method: client_secret_jwt
  84. authorization-grant-type: client_credentials
  85. ...
  86. ----
  87. The following example shows how to configure `WebClientReactiveClientCredentialsTokenResponseClient`:
  88. ====
  89. .Java
  90. [source,java,role="primary"]
  91. ----
  92. Function<ClientRegistration, JWK> jwkResolver = (clientRegistration) -> {
  93. if (clientRegistration.getClientAuthenticationMethod().equals(ClientAuthenticationMethod.CLIENT_SECRET_JWT)) {
  94. SecretKeySpec secretKey = new SecretKeySpec(
  95. clientRegistration.getClientSecret().getBytes(StandardCharsets.UTF_8),
  96. "HmacSHA256");
  97. return new OctetSequenceKey.Builder(secretKey)
  98. .keyID(UUID.randomUUID().toString())
  99. .build();
  100. }
  101. return null;
  102. };
  103. WebClientReactiveClientCredentialsTokenResponseClient tokenResponseClient =
  104. new WebClientReactiveClientCredentialsTokenResponseClient();
  105. tokenResponseClient.addParametersConverter(
  106. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver));
  107. ----
  108. .Kotlin
  109. [source,kotlin,role="secondary"]
  110. ----
  111. val jwkResolver = Function<ClientRegistration, JWK?> { clientRegistration: ClientRegistration ->
  112. if (clientRegistration.clientAuthenticationMethod == ClientAuthenticationMethod.CLIENT_SECRET_JWT) {
  113. val secretKey = SecretKeySpec(
  114. clientRegistration.clientSecret.toByteArray(StandardCharsets.UTF_8),
  115. "HmacSHA256"
  116. )
  117. OctetSequenceKey.Builder(secretKey)
  118. .keyID(UUID.randomUUID().toString())
  119. .build()
  120. }
  121. null
  122. }
  123. val tokenResponseClient = WebClientReactiveClientCredentialsTokenResponseClient()
  124. tokenResponseClient.addParametersConverter(
  125. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  126. )
  127. ----
  128. ====
  129. === Customizing the JWT assertion
  130. 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:
  131. ====
  132. .Java
  133. [source,java,role="primary"]
  134. ----
  135. Function<ClientRegistration, JWK> jwkResolver = ...
  136. NimbusJwtClientAuthenticationParametersConverter<OAuth2ClientCredentialsGrantRequest> converter =
  137. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver);
  138. converter.setJwtClientAssertionCustomizer((context) -> {
  139. context.getHeaders().header("custom-header", "header-value");
  140. context.getClaims().claim("custom-claim", "claim-value");
  141. });
  142. ----
  143. .Kotlin
  144. [source,kotlin,role="secondary"]
  145. ----
  146. val jwkResolver = ...
  147. val converter: NimbusJwtClientAuthenticationParametersConverter<OAuth2ClientCredentialsGrantRequest> =
  148. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  149. converter.setJwtClientAssertionCustomizer { context ->
  150. context.headers.header("custom-header", "header-value")
  151. context.claims.claim("custom-claim", "claim-value")
  152. }
  153. ----
  154. ====