2
0

client-authentication.adoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. [[oauth2Client-client-auth-support]]
  2. = Client Authentication Support
  3. [[oauth2Client-client-credentials-auth]]
  4. == Client Credentials
  5. === Authenticate using `client_secret_basic`
  6. Client Authentication with HTTP Basic is supported out of the box and no customization is necessary to enable it.
  7. The default implementation is provided by `DefaultOAuth2TokenRequestHeadersConverter`.
  8. Given the following Spring Boot properties for an OAuth 2.0 client registration:
  9. [source,yaml]
  10. ----
  11. spring:
  12. security:
  13. oauth2:
  14. client:
  15. registration:
  16. okta:
  17. client-id: client-id
  18. client-secret: client-secret
  19. client-authentication-method: client_secret_basic
  20. authorization-grant-type: authorization_code
  21. ...
  22. ----
  23. The following example shows how to configure `DefaultAuthorizationCodeTokenResponseClient` to disable URL encoding of the client credentials:
  24. [tabs]
  25. ======
  26. Java::
  27. +
  28. [source,java,role="primary"]
  29. ----
  30. DefaultOAuth2TokenRequestHeadersConverter<OAuth2AuthorizationCodeGrantRequest> headersConverter =
  31. new DefaultOAuth2TokenRequestHeadersConverter<>();
  32. headersConverter.setEncodeClientCredentials(false);
  33. OAuth2AuthorizationCodeGrantRequestEntityConverter requestEntityConverter =
  34. new OAuth2AuthorizationCodeGrantRequestEntityConverter();
  35. requestEntityConverter.setHeadersConverter(headersConverter);
  36. DefaultAuthorizationCodeTokenResponseClient tokenResponseClient =
  37. new DefaultAuthorizationCodeTokenResponseClient();
  38. tokenResponseClient.setRequestEntityConverter(requestEntityConverter);
  39. ----
  40. Kotlin::
  41. +
  42. [source,kotlin,role="secondary"]
  43. ----
  44. val headersConverter = DefaultOAuth2TokenRequestHeadersConverter<OAuth2AuthorizationCodeGrantRequest>()
  45. headersConverter.setEncodeClientCredentials(false)
  46. val requestEntityConverter = OAuth2AuthorizationCodeGrantRequestEntityConverter()
  47. requestEntityConverter.setHeadersConverter(headersConverter)
  48. val tokenResponseClient = DefaultAuthorizationCodeTokenResponseClient()
  49. tokenResponseClient.setRequestEntityConverter(requestEntityConverter)
  50. ----
  51. ======
  52. === Authenticate using `client_secret_post`
  53. Client Authentication with client credentials included in the request-body is supported out of the box and no customization is necessary to enable it.
  54. The following Spring Boot properties for an OAuth 2.0 client registration demonstrate the configuration:
  55. [source,yaml]
  56. ----
  57. spring:
  58. security:
  59. oauth2:
  60. client:
  61. registration:
  62. okta:
  63. client-id: client-id
  64. client-secret: client-secret
  65. client-authentication-method: client_secret_post
  66. authorization-grant-type: authorization_code
  67. ...
  68. ----
  69. [[oauth2Client-jwt-bearer-auth]]
  70. == JWT Bearer
  71. [NOTE]
  72. 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.
  73. The default implementation for JWT Bearer Client Authentication is `NimbusJwtClientAuthenticationParametersConverter`,
  74. which is a `Converter` that customizes the Token Request parameters by adding
  75. a signed JSON Web Token (JWS) in the `client_assertion` parameter.
  76. The `java.security.PrivateKey` or `javax.crypto.SecretKey` used for signing the JWS
  77. is supplied by the `com.nimbusds.jose.jwk.JWK` resolver associated with `NimbusJwtClientAuthenticationParametersConverter`.
  78. === Authenticate using `private_key_jwt`
  79. Given the following Spring Boot properties for an OAuth 2.0 Client registration:
  80. [source,yaml]
  81. ----
  82. spring:
  83. security:
  84. oauth2:
  85. client:
  86. registration:
  87. okta:
  88. client-id: okta-client-id
  89. client-authentication-method: private_key_jwt
  90. authorization-grant-type: authorization_code
  91. ...
  92. ----
  93. The following example shows how to configure `DefaultAuthorizationCodeTokenResponseClient`:
  94. [tabs]
  95. ======
  96. Java::
  97. +
  98. [source,java,role="primary"]
  99. ----
  100. Function<ClientRegistration, JWK> jwkResolver = (clientRegistration) -> {
  101. if (clientRegistration.getClientAuthenticationMethod().equals(ClientAuthenticationMethod.PRIVATE_KEY_JWT)) {
  102. // Assuming RSA key type
  103. RSAPublicKey publicKey = ...
  104. RSAPrivateKey privateKey = ...
  105. return new RSAKey.Builder(publicKey)
  106. .privateKey(privateKey)
  107. .keyID(UUID.randomUUID().toString())
  108. .build();
  109. }
  110. return null;
  111. };
  112. OAuth2AuthorizationCodeGrantRequestEntityConverter requestEntityConverter =
  113. new OAuth2AuthorizationCodeGrantRequestEntityConverter();
  114. requestEntityConverter.addParametersConverter(
  115. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver));
  116. DefaultAuthorizationCodeTokenResponseClient tokenResponseClient =
  117. new DefaultAuthorizationCodeTokenResponseClient();
  118. tokenResponseClient.setRequestEntityConverter(requestEntityConverter);
  119. ----
  120. Kotlin::
  121. +
  122. [source,kotlin,role="secondary"]
  123. ----
  124. val jwkResolver: Function<ClientRegistration, JWK> =
  125. Function<ClientRegistration, JWK> { clientRegistration ->
  126. if (clientRegistration.clientAuthenticationMethod.equals(ClientAuthenticationMethod.PRIVATE_KEY_JWT)) {
  127. // Assuming RSA key type
  128. var publicKey: RSAPublicKey
  129. var privateKey: RSAPrivateKey
  130. RSAKey.Builder(publicKey) = //...
  131. .privateKey(privateKey) = //...
  132. .keyID(UUID.randomUUID().toString())
  133. .build()
  134. }
  135. null
  136. }
  137. val requestEntityConverter = OAuth2AuthorizationCodeGrantRequestEntityConverter()
  138. requestEntityConverter.addParametersConverter(
  139. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  140. )
  141. val tokenResponseClient = DefaultAuthorizationCodeTokenResponseClient()
  142. tokenResponseClient.setRequestEntityConverter(requestEntityConverter)
  143. ----
  144. ======
  145. === Authenticate using `client_secret_jwt`
  146. Given the following Spring Boot properties for an OAuth 2.0 Client registration:
  147. [source,yaml]
  148. ----
  149. spring:
  150. security:
  151. oauth2:
  152. client:
  153. registration:
  154. okta:
  155. client-id: okta-client-id
  156. client-secret: okta-client-secret
  157. client-authentication-method: client_secret_jwt
  158. authorization-grant-type: client_credentials
  159. ...
  160. ----
  161. The following example shows how to configure `DefaultClientCredentialsTokenResponseClient`:
  162. [tabs]
  163. ======
  164. Java::
  165. +
  166. [source,java,role="primary"]
  167. ----
  168. Function<ClientRegistration, JWK> jwkResolver = (clientRegistration) -> {
  169. if (clientRegistration.getClientAuthenticationMethod().equals(ClientAuthenticationMethod.CLIENT_SECRET_JWT)) {
  170. SecretKeySpec secretKey = new SecretKeySpec(
  171. clientRegistration.getClientSecret().getBytes(StandardCharsets.UTF_8),
  172. "HmacSHA256");
  173. return new OctetSequenceKey.Builder(secretKey)
  174. .keyID(UUID.randomUUID().toString())
  175. .build();
  176. }
  177. return null;
  178. };
  179. OAuth2ClientCredentialsGrantRequestEntityConverter requestEntityConverter =
  180. new OAuth2ClientCredentialsGrantRequestEntityConverter();
  181. requestEntityConverter.addParametersConverter(
  182. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver));
  183. DefaultClientCredentialsTokenResponseClient tokenResponseClient =
  184. new DefaultClientCredentialsTokenResponseClient();
  185. tokenResponseClient.setRequestEntityConverter(requestEntityConverter);
  186. ----
  187. Kotlin::
  188. +
  189. [source,kotlin,role="secondary"]
  190. ----
  191. val jwkResolver = Function<ClientRegistration, JWK?> { clientRegistration: ClientRegistration ->
  192. if (clientRegistration.clientAuthenticationMethod == ClientAuthenticationMethod.CLIENT_SECRET_JWT) {
  193. val secretKey = SecretKeySpec(
  194. clientRegistration.clientSecret.toByteArray(StandardCharsets.UTF_8),
  195. "HmacSHA256"
  196. )
  197. OctetSequenceKey.Builder(secretKey)
  198. .keyID(UUID.randomUUID().toString())
  199. .build()
  200. }
  201. null
  202. }
  203. val requestEntityConverter = OAuth2ClientCredentialsGrantRequestEntityConverter()
  204. requestEntityConverter.addParametersConverter(
  205. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  206. )
  207. val tokenResponseClient = DefaultClientCredentialsTokenResponseClient()
  208. tokenResponseClient.setRequestEntityConverter(requestEntityConverter)
  209. ----
  210. ======
  211. === Customizing the JWT assertion
  212. 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:
  213. [tabs]
  214. ======
  215. Java::
  216. +
  217. [source,java,role="primary"]
  218. ----
  219. Function<ClientRegistration, JWK> jwkResolver = ...
  220. NimbusJwtClientAuthenticationParametersConverter<OAuth2ClientCredentialsGrantRequest> converter =
  221. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver);
  222. converter.setJwtClientAssertionCustomizer((context) -> {
  223. context.getHeaders().header("custom-header", "header-value");
  224. context.getClaims().claim("custom-claim", "claim-value");
  225. });
  226. ----
  227. Kotlin::
  228. +
  229. [source,kotlin,role="secondary"]
  230. ----
  231. val jwkResolver = ...
  232. val converter: NimbusJwtClientAuthenticationParametersConverter<OAuth2ClientCredentialsGrantRequest> =
  233. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  234. converter.setJwtClientAssertionCustomizer { context ->
  235. context.headers.header("custom-header", "header-value")
  236. context.claims.claim("custom-claim", "claim-value")
  237. }
  238. ----
  239. ======
  240. [[oauth2Client-public-auth]]
  241. == Public Authentication
  242. Public Client Authentication is supported out of the box and no customization is necessary to enable it.
  243. The following Spring Boot properties for an OAuth 2.0 client registration demonstrate the configuration:
  244. [source,yaml]
  245. ----
  246. spring:
  247. security:
  248. oauth2:
  249. client:
  250. registration:
  251. okta:
  252. client-id: client-id
  253. client-authentication-method: none
  254. authorization-grant-type: authorization_code
  255. ...
  256. ----
  257. [NOTE]
  258. Public Clients are supported using https://tools.ietf.org/html/rfc7636[Proof Key for Code Exchange] (PKCE).
  259. PKCE will automatically be used when `client-authentication-method` is set to "none" (`ClientAuthenticationMethod.NONE`).