passkeys.adoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. [[passkeys]]
  2. = Passkeys
  3. Spring Security provides support for https://www.passkeys.com[passkeys].
  4. Passkeys are a more secure method of authenticating than passwords and are built using https://www.w3.org/TR/webauthn-3/[WebAuthn].
  5. In order to use a passkey to authenticate, a user must first xref:servlet/authentication/passkeys.adoc#passkeys-register[Register a New Credential].
  6. After the credential is registered, it can be used to authenticate by xref:servlet/authentication/passkeys.adoc#passkeys-verify[verifying an authentication assertion].
  7. [[passkeys-dependencies]]
  8. == Required Dependencies
  9. To get started, add the `webauthn4j-core` dependency to your project.
  10. [NOTE]
  11. ====
  12. This assumes that you are managing Spring Security's versions with Spring Boot or Spring Security's BOM as described in xref:getting-spring-security.adoc[].
  13. ====
  14. .Passkeys Dependencies
  15. [tabs]
  16. ======
  17. Maven::
  18. +
  19. [source,xml,role="primary",subs="verbatim,attributes"]
  20. ----
  21. <dependency>
  22. <groupId>org.springframework.security</groupId>
  23. <artifactId>spring-security-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>com.webauthn4j</groupId>
  27. <artifactId>webauthn4j-core</artifactId>
  28. <version>{webauthn4j-core-version}</version>
  29. </dependency>
  30. ----
  31. Gradle::
  32. +
  33. [source,groovy,role="secondary",subs="verbatim,attributes"]
  34. ----
  35. depenendencies {
  36. implementation "org.springframework.security:spring-security-web"
  37. implementation "com.webauthn4j:webauthn4j-core:{webauthn4j-core-version}"
  38. }
  39. ----
  40. ======
  41. [[passkeys-configuration]]
  42. == Configuration
  43. The following configuration enables passkey authentication.
  44. It provides a way to xref:./passkeys.adoc#passkeys-register[] at `/webauthn/register` and a default log in page that allows xref:./passkeys.adoc#passkeys-verify[authenticating with passkeys].
  45. [tabs]
  46. ======
  47. Java::
  48. +
  49. [source,java,role="primary"]
  50. ----
  51. @Bean
  52. SecurityFilterChain filterChain(HttpSecurity http) {
  53. // ...
  54. http
  55. // ...
  56. .formLogin(withDefaults())
  57. .webAuthn((webAuthn) -> webAuthn
  58. .rpName("Spring Security Relying Party")
  59. .rpId("example.com")
  60. .allowedOrigins("https://example.com")
  61. // optional properties
  62. .creationOptionsRepository(new CustomPublicKeyCredentialCreationOptionsRepository())
  63. .messageConverter(new CustomHttpMessageConverter())
  64. );
  65. return http.build();
  66. }
  67. @Bean
  68. UserDetailsService userDetailsService() {
  69. UserDetails userDetails = User.withDefaultPasswordEncoder()
  70. .username("user")
  71. .password("password")
  72. .roles("USER")
  73. .build();
  74. return new InMemoryUserDetailsManager(userDetails);
  75. }
  76. ----
  77. Kotlin::
  78. +
  79. [source,kotlin,role="secondary"]
  80. ----
  81. @Bean
  82. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  83. // ...
  84. http {
  85. webAuthn {
  86. rpName = "Spring Security Relying Party"
  87. rpId = "example.com"
  88. allowedOrigins = setOf("https://example.com")
  89. // optional properties
  90. creationOptionsRepository = CustomPublicKeyCredentialCreationOptionsRepository()
  91. messageConverter = CustomHttpMessageConverter()
  92. }
  93. }
  94. }
  95. @Bean
  96. open fun userDetailsService(): UserDetailsService {
  97. val userDetails = User.withDefaultPasswordEncoder()
  98. .username("user")
  99. .password("password")
  100. .roles("USER")
  101. .build()
  102. return InMemoryUserDetailsManager(userDetails)
  103. }
  104. ----
  105. ======
  106. [[passkeys-configuration-pkccor]]
  107. === Custom PublicKeyCredentialCreationOptionsRepository
  108. The `PublicKeyCredentialCreationOptionsRepository` is used to persist the `PublicKeyCredentialCreationOptions` between requests.
  109. The default is to persist it the `HttpSession`, but at times users may need to customize this behavior.
  110. This can be done by setting the optional property `creationOptionsRepository` demonstrated in xref:./passkeys.adoc#passkeys-configuration[Configuration] or by exposing a `PublicKeyCredentialCreationOptionsRepository` Bean:
  111. [tabs]
  112. ======
  113. Java::
  114. +
  115. [source,java,role="primary"]
  116. ----
  117. @Bean
  118. CustomPublicKeyCredentialCreationOptionsRepository creationOptionsRepository() {
  119. return new CustomPublicKeyCredentialCreationOptionsRepository();
  120. }
  121. ----
  122. Kotlin::
  123. +
  124. [source,kotlin,role="secondary"]
  125. ----
  126. @Bean
  127. open fun creationOptionsRepository(): CustomPublicKeyCredentialCreationOptionsRepository {
  128. return CustomPublicKeyCredentialCreationOptionsRepository()
  129. }
  130. ----
  131. ======
  132. [[passkeys-register]]
  133. == Register a New Credential
  134. In order to use a passkey, a user must first https://www.w3.org/TR/webauthn-3/#sctn-registering-a-new-credential[Register a New Credential].
  135. Registering a new credential is composed of two steps:
  136. 1. Requesting the Registration Options
  137. 2. Registering the Credential
  138. [[passkeys-register-options]]
  139. === Request the Registration Options
  140. The first step in registration of a new credential is to request the registration options.
  141. In Spring Security, a request for the registration options is typically done using JavaScript and looks like:
  142. [NOTE]
  143. ====
  144. Spring Security provides a default registration page that can be used as a reference on how to register credentials.
  145. ====
  146. .Request for Registration Options
  147. [source,http]
  148. ----
  149. POST /webauthn/register/options
  150. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  151. ----
  152. The request above will obtain the registration options for the currently authenticated user.
  153. Since the challenge is persisted (state is changed) to be compared at the time of registration, the request must be a POST and include a CSRF token.
  154. .Response for Registration Options
  155. [source,json]
  156. ----
  157. {
  158. "rp": {
  159. "name": "SimpleWebAuthn Example",
  160. "id": "example.localhost"
  161. },
  162. "user": {
  163. "name": "user@example.localhost",
  164. "id": "oWJtkJ6vJ_m5b84LB4_K7QKTCTEwLIjCh4tFMCGHO4w",
  165. "displayName": "user@example.localhost"
  166. },
  167. "challenge": "q7lCdd3SVQxdC-v8pnRAGEn1B2M-t7ZECWPwCAmhWvc",
  168. "pubKeyCredParams": [
  169. {
  170. "type": "public-key",
  171. "alg": -8
  172. },
  173. {
  174. "type": "public-key",
  175. "alg": -7
  176. },
  177. {
  178. "type": "public-key",
  179. "alg": -257
  180. }
  181. ],
  182. "timeout": 300000,
  183. "excludeCredentials": [],
  184. "authenticatorSelection": {
  185. "residentKey": "required",
  186. "userVerification": "preferred"
  187. },
  188. "attestation": "none",
  189. "extensions": {
  190. "credProps": true
  191. }
  192. }
  193. ----
  194. [[passkeys-register-create]]
  195. === Registering the Credential
  196. After the registration options are obtained, they are used to create the credentials that are registered.
  197. To register a new credential, the application should pass the options to https://w3c.github.io/webappsec-credential-management/#dom-credentialscontainer-create[`navigator.credentials.create`] after base64url decoding the binary values such as `user.id`, `challenge`, and `excludeCredentials[].id`.
  198. The returned value can then be sent to the server as a JSON request.
  199. An example registration request can be found below:
  200. .Example Registration Request
  201. [source,http]
  202. ----
  203. POST /webauthn/register
  204. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  205. {
  206. "publicKey": { // <1>
  207. "credential": {
  208. "id": "dYF7EGnRFFIXkpXi9XU2wg",
  209. "rawId": "dYF7EGnRFFIXkpXi9XU2wg",
  210. "response": {
  211. "attestationObject": "o2NmbXRkbm9uZWdhdHRTdG10oGhhdXRoRGF0YViUy9GqwTRaMpzVDbXq1dyEAXVOxrou08k22ggRC45MKNhdAAAAALraVWanqkAfvZZFYZpVEg0AEHWBexBp0RRSF5KV4vV1NsKlAQIDJiABIVggQjmrekPGzyqtoKK9HPUH-8Z2FLpoqkklFpFPQVICQ3IiWCD6I9Jvmor685fOZOyGXqUd87tXfvJk8rxj9OhuZvUALA",
  212. "clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uY3JlYXRlIiwiY2hhbGxlbmdlIjoiSl9RTi10SFJYRWVKYjlNcUNrWmFPLUdOVmlibXpGVGVWMk43Z0ptQUdrQSIsIm9yaWdpbiI6Imh0dHBzOi8vZXhhbXBsZS5sb2NhbGhvc3Q6ODQ0MyIsImNyb3NzT3JpZ2luIjpmYWxzZX0",
  213. "transports": [
  214. "internal",
  215. "hybrid"
  216. ]
  217. },
  218. "type": "public-key",
  219. "clientExtensionResults": {},
  220. "authenticatorAttachment": "platform"
  221. },
  222. "label": "1password" // <2>
  223. }
  224. }
  225. ----
  226. <1> The result of calling `navigator.credentials.create` with binary values base64url encoded.
  227. <2> A label that the user selects to have associated with this credential to help the user distinguish the credential.
  228. .Example Successful Registration Response
  229. [source,http]
  230. ----
  231. HTTP/1.1 200 OK
  232. {
  233. "success": true
  234. }
  235. ----
  236. [[passkeys-verify]]
  237. == Verifying an Authentication Assertion
  238. After xref:./passkeys.adoc#passkeys-register[] the passkey can be https://www.w3.org/TR/webauthn-3/#sctn-verifying-assertion[verified] (authenticated).
  239. Verifying a credential is composed of two steps:
  240. 1. Requesting the Verification Options
  241. 2. Verifying the Credential
  242. [[passkeys-verify-options]]
  243. === Request the Verification Options
  244. The first step in verification of a credential is to request the verification options.
  245. In Spring Security, a request for the verification options is typically done using JavaScript and looks like:
  246. [NOTE]
  247. ====
  248. Spring Security provides a default log in page that can be used as a reference on how to verify credentials.
  249. ====
  250. .Request for Verification Options
  251. [source,http]
  252. ----
  253. POST /webauthn/authenticate/options
  254. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  255. ----
  256. The request above will obtain the verification options.
  257. Since the challenge is persisted (state is changed) to be compared at the time of authentication, the request must be a POST and include a CSRF token.
  258. The response will contain the options for obtaining a credential with binary values such as `challenge` base64url encoded.
  259. .Example Response for Verification Options
  260. [source,json]
  261. ----
  262. {
  263. "challenge": "cQfdGrj9zDg3zNBkOH3WPL954FTOShVy0-CoNgSewNM",
  264. "timeout": 300000,
  265. "rpId": "example.localhost",
  266. "allowCredentials": [],
  267. "userVerification": "preferred",
  268. "extensions": {}
  269. }
  270. ----
  271. [[passkeys-verify-get]]
  272. === Verifying the Credential
  273. After the verification options are obtained, they are used to get a credential.
  274. To get a credential, the application should pass the options to https://w3c.github.io/webappsec-credential-management/#dom-credentialscontainer-create[`navigator.credentials.get`] after base64url decoding the binary values such as `challenge`.
  275. The returned value of `navigator.credentials.get` can then be sent to the server as a JSON request.
  276. Binary values such as `rawId` and `response.*` must be base64url encoded.
  277. An example authentication request can be found below:
  278. .Example Authentication Request
  279. [source,http]
  280. ----
  281. POST /login/webauthn
  282. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  283. {
  284. "id": "dYF7EGnRFFIXkpXi9XU2wg",
  285. "rawId": "dYF7EGnRFFIXkpXi9XU2wg",
  286. "response": {
  287. "authenticatorData": "y9GqwTRaMpzVDbXq1dyEAXVOxrou08k22ggRC45MKNgdAAAAAA",
  288. "clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uZ2V0IiwiY2hhbGxlbmdlIjoiRFVsRzRDbU9naWhKMG1vdXZFcE9HdUk0ZVJ6MGRRWmxUQmFtbjdHQ1FTNCIsIm9yaWdpbiI6Imh0dHBzOi8vZXhhbXBsZS5sb2NhbGhvc3Q6ODQ0MyIsImNyb3NzT3JpZ2luIjpmYWxzZX0",
  289. "signature": "MEYCIQCW2BcUkRCAXDmGxwMi78jknenZ7_amWrUJEYoTkweldAIhAMD0EMp1rw2GfwhdrsFIeDsL7tfOXVPwOtfqJntjAo4z",
  290. "userHandle": "Q3_0Xd64_HW0BlKRAJnVagJTpLKLgARCj8zjugpRnVo"
  291. },
  292. "clientExtensionResults": {},
  293. "authenticatorAttachment": "platform"
  294. }
  295. ----
  296. .Example Successful Authentication Response
  297. [source,http]
  298. ----
  299. HTTP/1.1 200 OK
  300. {
  301. "redirectUrl": "/", // <1>
  302. "authenticated": true // <2>
  303. }
  304. ----
  305. <1> The URL to redirect to
  306. <2> Indicates that the user is authenticated
  307. .Example Authentication Failure Response
  308. [source,http]
  309. ----
  310. HTTP/1.1 401 OK
  311. ----