passkeys.adoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. dependencies {
  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. .rpId("example.com")
  59. .allowedOrigins("https://example.com")
  60. // optional properties
  61. .creationOptionsRepository(new CustomPublicKeyCredentialCreationOptionsRepository())
  62. .messageConverter(new CustomHttpMessageConverter())
  63. );
  64. return http.build();
  65. }
  66. @Bean
  67. UserDetailsService userDetailsService() {
  68. UserDetails userDetails = User.withDefaultPasswordEncoder()
  69. .username("user")
  70. .password("password")
  71. .roles("USER")
  72. .build();
  73. return new InMemoryUserDetailsManager(userDetails);
  74. }
  75. ----
  76. Kotlin::
  77. +
  78. [source,kotlin,role="secondary"]
  79. ----
  80. @Bean
  81. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  82. // ...
  83. http {
  84. webAuthn {
  85. rpId = "example.com"
  86. allowedOrigins = setOf("https://example.com")
  87. // optional properties
  88. creationOptionsRepository = CustomPublicKeyCredentialCreationOptionsRepository()
  89. messageConverter = CustomHttpMessageConverter()
  90. }
  91. }
  92. }
  93. @Bean
  94. open fun userDetailsService(): UserDetailsService {
  95. val userDetails = User.withDefaultPasswordEncoder()
  96. .username("user")
  97. .password("password")
  98. .roles("USER")
  99. .build()
  100. return InMemoryUserDetailsManager(userDetails)
  101. }
  102. ----
  103. ======
  104. [[passkeys-configuration-persistence]]
  105. === JDBC & Custom Persistence
  106. WebAuthn performs persistence with javadoc:org.springframework.security.web.webauthn.management.PublicKeyCredentialUserEntityRepository[] and javadoc:org.springframework.security.web.webauthn.management.UserCredentialRepository[].
  107. The default is to use in memory persistence, but JDBC persistence is support with javadoc:org.springframework.security.web.webauthn.management.JdbcPublicKeyCredentialUserEntityRepository[] and javadoc:org.springframework.security.web.webauthn.management.JdbcUserCredentialRepository[].
  108. To configure JDBC based persistence, expose the repositories as a Bean:
  109. [tabs]
  110. ======
  111. Java::
  112. +
  113. [source,java,role="primary"]
  114. ----
  115. @Bean
  116. JdbcPublicKeyCredentialUserEntityRepository jdbcPublicKeyCredentialRepository(JdbcOperations jdbc) {
  117. return new JdbcPublicKeyCredentialUserEntityRepository(jdbc);
  118. }
  119. @Bean
  120. JdbcUserCredentialRepository jdbcUserCredentialRepository(JdbcOperations jdbc) {
  121. return new JdbcUserCredentialRepository(jdbc);
  122. }
  123. ----
  124. Kotlin::
  125. +
  126. [source,kotlin,role="secondary"]
  127. ----
  128. @Bean
  129. fun jdbcPublicKeyCredentialRepository(jdbc: JdbcOperations): JdbcPublicKeyCredentialUserEntityRepository {
  130. return JdbcPublicKeyCredentialUserEntityRepository(jdbc)
  131. }
  132. @Bean
  133. fun jdbcUserCredentialRepository(jdbc: JdbcOperations): JdbcUserCredentialRepository {
  134. return JdbcUserCredentialRepository(jdbc)
  135. }
  136. ----
  137. ======
  138. If JDBC does not meet your needs, you can create your own implementations of the interfaces and use them by exposing them as a Bean similar to the example above.
  139. [[passkeys-configuration-pkccor]]
  140. === Custom PublicKeyCredentialCreationOptionsRepository
  141. The `PublicKeyCredentialCreationOptionsRepository` is used to persist the `PublicKeyCredentialCreationOptions` between requests.
  142. The default is to persist it the `HttpSession`, but at times users may need to customize this behavior.
  143. This can be done by setting the optional property `creationOptionsRepository` demonstrated in xref:./passkeys.adoc#passkeys-configuration[Configuration] or by exposing a `PublicKeyCredentialCreationOptionsRepository` Bean:
  144. [tabs]
  145. ======
  146. Java::
  147. +
  148. [source,java,role="primary"]
  149. ----
  150. @Bean
  151. CustomPublicKeyCredentialCreationOptionsRepository creationOptionsRepository() {
  152. return new CustomPublicKeyCredentialCreationOptionsRepository();
  153. }
  154. ----
  155. Kotlin::
  156. +
  157. [source,kotlin,role="secondary"]
  158. ----
  159. @Bean
  160. open fun creationOptionsRepository(): CustomPublicKeyCredentialCreationOptionsRepository {
  161. return CustomPublicKeyCredentialCreationOptionsRepository()
  162. }
  163. ----
  164. ======
  165. [[passkeys-register]]
  166. == Register a New Credential
  167. 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].
  168. Registering a new credential is composed of two steps:
  169. 1. Requesting the Registration Options
  170. 2. Registering the Credential
  171. [[passkeys-register-options]]
  172. === Request the Registration Options
  173. The first step in registration of a new credential is to request the registration options.
  174. In Spring Security, a request for the registration options is typically done using JavaScript and looks like:
  175. [NOTE]
  176. ====
  177. Spring Security provides a default registration page that can be used as a reference on how to register credentials.
  178. ====
  179. .Request for Registration Options
  180. [source,http]
  181. ----
  182. POST /webauthn/register/options
  183. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  184. ----
  185. The request above will obtain the registration options for the currently authenticated user.
  186. 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.
  187. .Response for Registration Options
  188. [source,json]
  189. ----
  190. {
  191. "rp": {
  192. "name": "SimpleWebAuthn Example",
  193. "id": "example.localhost"
  194. },
  195. "user": {
  196. "name": "user@example.localhost",
  197. "id": "oWJtkJ6vJ_m5b84LB4_K7QKTCTEwLIjCh4tFMCGHO4w",
  198. "displayName": "user@example.localhost"
  199. },
  200. "challenge": "q7lCdd3SVQxdC-v8pnRAGEn1B2M-t7ZECWPwCAmhWvc",
  201. "pubKeyCredParams": [
  202. {
  203. "type": "public-key",
  204. "alg": -8
  205. },
  206. {
  207. "type": "public-key",
  208. "alg": -7
  209. },
  210. {
  211. "type": "public-key",
  212. "alg": -257
  213. }
  214. ],
  215. "timeout": 300000,
  216. "excludeCredentials": [],
  217. "authenticatorSelection": {
  218. "residentKey": "required",
  219. "userVerification": "preferred"
  220. },
  221. "attestation": "none",
  222. "extensions": {
  223. "credProps": true
  224. }
  225. }
  226. ----
  227. [[passkeys-register-create]]
  228. === Registering the Credential
  229. After the registration options are obtained, they are used to create the credentials that are registered.
  230. 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`.
  231. The returned value can then be sent to the server as a JSON request.
  232. An example registration request can be found below:
  233. .Example Registration Request
  234. [source,http]
  235. ----
  236. POST /webauthn/register
  237. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  238. {
  239. "publicKey": { // <1>
  240. "credential": {
  241. "id": "dYF7EGnRFFIXkpXi9XU2wg",
  242. "rawId": "dYF7EGnRFFIXkpXi9XU2wg",
  243. "response": {
  244. "attestationObject": "o2NmbXRkbm9uZWdhdHRTdG10oGhhdXRoRGF0YViUy9GqwTRaMpzVDbXq1dyEAXVOxrou08k22ggRC45MKNhdAAAAALraVWanqkAfvZZFYZpVEg0AEHWBexBp0RRSF5KV4vV1NsKlAQIDJiABIVggQjmrekPGzyqtoKK9HPUH-8Z2FLpoqkklFpFPQVICQ3IiWCD6I9Jvmor685fOZOyGXqUd87tXfvJk8rxj9OhuZvUALA",
  245. "clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uY3JlYXRlIiwiY2hhbGxlbmdlIjoiSl9RTi10SFJYRWVKYjlNcUNrWmFPLUdOVmlibXpGVGVWMk43Z0ptQUdrQSIsIm9yaWdpbiI6Imh0dHBzOi8vZXhhbXBsZS5sb2NhbGhvc3Q6ODQ0MyIsImNyb3NzT3JpZ2luIjpmYWxzZX0",
  246. "transports": [
  247. "internal",
  248. "hybrid"
  249. ]
  250. },
  251. "type": "public-key",
  252. "clientExtensionResults": {},
  253. "authenticatorAttachment": "platform"
  254. },
  255. "label": "1password" // <2>
  256. }
  257. }
  258. ----
  259. <1> The result of calling `navigator.credentials.create` with binary values base64url encoded.
  260. <2> A label that the user selects to have associated with this credential to help the user distinguish the credential.
  261. .Example Successful Registration Response
  262. [source,http]
  263. ----
  264. HTTP/1.1 200 OK
  265. {
  266. "success": true
  267. }
  268. ----
  269. [[passkeys-verify]]
  270. == Verifying an Authentication Assertion
  271. After xref:./passkeys.adoc#passkeys-register[] the passkey can be https://www.w3.org/TR/webauthn-3/#sctn-verifying-assertion[verified] (authenticated).
  272. Verifying a credential is composed of two steps:
  273. 1. Requesting the Verification Options
  274. 2. Verifying the Credential
  275. [[passkeys-verify-options]]
  276. === Request the Verification Options
  277. The first step in verification of a credential is to request the verification options.
  278. In Spring Security, a request for the verification options is typically done using JavaScript and looks like:
  279. [NOTE]
  280. ====
  281. Spring Security provides a default log in page that can be used as a reference on how to verify credentials.
  282. ====
  283. .Request for Verification Options
  284. [source,http]
  285. ----
  286. POST /webauthn/authenticate/options
  287. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  288. ----
  289. The request above will obtain the verification options.
  290. 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.
  291. The response will contain the options for obtaining a credential with binary values such as `challenge` base64url encoded.
  292. .Example Response for Verification Options
  293. [source,json]
  294. ----
  295. {
  296. "challenge": "cQfdGrj9zDg3zNBkOH3WPL954FTOShVy0-CoNgSewNM",
  297. "timeout": 300000,
  298. "rpId": "example.localhost",
  299. "allowCredentials": [],
  300. "userVerification": "preferred",
  301. "extensions": {}
  302. }
  303. ----
  304. [[passkeys-verify-get]]
  305. === Verifying the Credential
  306. After the verification options are obtained, they are used to get a credential.
  307. 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`.
  308. The returned value of `navigator.credentials.get` can then be sent to the server as a JSON request.
  309. Binary values such as `rawId` and `response.*` must be base64url encoded.
  310. An example authentication request can be found below:
  311. .Example Authentication Request
  312. [source,http]
  313. ----
  314. POST /login/webauthn
  315. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  316. {
  317. "id": "dYF7EGnRFFIXkpXi9XU2wg",
  318. "rawId": "dYF7EGnRFFIXkpXi9XU2wg",
  319. "response": {
  320. "authenticatorData": "y9GqwTRaMpzVDbXq1dyEAXVOxrou08k22ggRC45MKNgdAAAAAA",
  321. "clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uZ2V0IiwiY2hhbGxlbmdlIjoiRFVsRzRDbU9naWhKMG1vdXZFcE9HdUk0ZVJ6MGRRWmxUQmFtbjdHQ1FTNCIsIm9yaWdpbiI6Imh0dHBzOi8vZXhhbXBsZS5sb2NhbGhvc3Q6ODQ0MyIsImNyb3NzT3JpZ2luIjpmYWxzZX0",
  322. "signature": "MEYCIQCW2BcUkRCAXDmGxwMi78jknenZ7_amWrUJEYoTkweldAIhAMD0EMp1rw2GfwhdrsFIeDsL7tfOXVPwOtfqJntjAo4z",
  323. "userHandle": "Q3_0Xd64_HW0BlKRAJnVagJTpLKLgARCj8zjugpRnVo"
  324. },
  325. "clientExtensionResults": {},
  326. "authenticatorAttachment": "platform"
  327. }
  328. ----
  329. .Example Successful Authentication Response
  330. [source,http]
  331. ----
  332. HTTP/1.1 200 OK
  333. {
  334. "redirectUrl": "/", // <1>
  335. "authenticated": true // <2>
  336. }
  337. ----
  338. <1> The URL to redirect to
  339. <2> Indicates that the user is authenticated
  340. .Example Authentication Failure Response
  341. [source,http]
  342. ----
  343. HTTP/1.1 401 OK
  344. ----