passkeys.adoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. );
  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. rpName = "Spring Security Relying Party"
  86. rpId = "example.com"
  87. allowedOrigins = setOf("https://example.com")
  88. // optional properties
  89. creationOptionsRepository = CustomPublicKeyCredentialCreationOptionsRepository()
  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-pkccor]]
  105. === Custom PublicKeyCredentialCreationOptionsRepository
  106. The `PublicKeyCredentialCreationOptionsRepository` is used to persist the `PublicKeyCredentialCreationOptions` between requests.
  107. The default is to persist it the `HttpSession`, but at times users may need to customize this behavior.
  108. This can be done by setting the optional property `creationOptionsRepository` demonstrated in xref:./passkeys.adoc#passkeys-configuration[Configuration] or by exposing a `PublicKeyCredentialCreationOptionsRepository` Bean:
  109. [tabs]
  110. ======
  111. Java::
  112. +
  113. [source,java,role="primary"]
  114. ----
  115. @Bean
  116. CustomPublicKeyCredentialCreationOptionsRepository creationOptionsRepository() {
  117. return new CustomPublicKeyCredentialCreationOptionsRepository();
  118. }
  119. ----
  120. Kotlin::
  121. +
  122. [source,kotlin,role="secondary"]
  123. ----
  124. @Bean
  125. open fun creationOptionsRepository(): CustomPublicKeyCredentialCreationOptionsRepository {
  126. return CustomPublicKeyCredentialCreationOptionsRepository()
  127. }
  128. ----
  129. ======
  130. [[passkeys-register]]
  131. == Register a New Credential
  132. 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].
  133. Registering a new credential is composed of two steps:
  134. 1. Requesting the Registration Options
  135. 2. Registering the Credential
  136. [[passkeys-register-options]]
  137. === Request the Registration Options
  138. The first step in registration of a new credential is to request the registration options.
  139. In Spring Security, a request for the registration options is typically done using JavaScript and looks like:
  140. [NOTE]
  141. ====
  142. Spring Security provides a default registration page that can be used as a reference on how to register credentials.
  143. ====
  144. .Request for Registration Options
  145. [source,http]
  146. ----
  147. POST /webauthn/register/options
  148. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  149. ----
  150. The request above will obtain the registration options for the currently authenticated user.
  151. 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.
  152. .Response for Registration Options
  153. [source,json]
  154. ----
  155. {
  156. "rp": {
  157. "name": "SimpleWebAuthn Example",
  158. "id": "example.localhost"
  159. },
  160. "user": {
  161. "name": "user@example.localhost",
  162. "id": "oWJtkJ6vJ_m5b84LB4_K7QKTCTEwLIjCh4tFMCGHO4w",
  163. "displayName": "user@example.localhost"
  164. },
  165. "challenge": "q7lCdd3SVQxdC-v8pnRAGEn1B2M-t7ZECWPwCAmhWvc",
  166. "pubKeyCredParams": [
  167. {
  168. "type": "public-key",
  169. "alg": -8
  170. },
  171. {
  172. "type": "public-key",
  173. "alg": -7
  174. },
  175. {
  176. "type": "public-key",
  177. "alg": -257
  178. }
  179. ],
  180. "timeout": 300000,
  181. "excludeCredentials": [],
  182. "authenticatorSelection": {
  183. "residentKey": "required",
  184. "userVerification": "preferred"
  185. },
  186. "attestation": "none",
  187. "extensions": {
  188. "credProps": true
  189. }
  190. }
  191. ----
  192. [[passkeys-register-create]]
  193. === Registering the Credential
  194. After the registration options are obtained, they are used to create the credentials that are registered.
  195. 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`.
  196. The returned value can then be sent to the server as a JSON request.
  197. An example registration request can be found below:
  198. .Example Registration Request
  199. [source,http]
  200. ----
  201. POST /webauthn/register
  202. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  203. {
  204. "publicKey": { // <1>
  205. "credential": {
  206. "id": "dYF7EGnRFFIXkpXi9XU2wg",
  207. "rawId": "dYF7EGnRFFIXkpXi9XU2wg",
  208. "response": {
  209. "attestationObject": "o2NmbXRkbm9uZWdhdHRTdG10oGhhdXRoRGF0YViUy9GqwTRaMpzVDbXq1dyEAXVOxrou08k22ggRC45MKNhdAAAAALraVWanqkAfvZZFYZpVEg0AEHWBexBp0RRSF5KV4vV1NsKlAQIDJiABIVggQjmrekPGzyqtoKK9HPUH-8Z2FLpoqkklFpFPQVICQ3IiWCD6I9Jvmor685fOZOyGXqUd87tXfvJk8rxj9OhuZvUALA",
  210. "clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uY3JlYXRlIiwiY2hhbGxlbmdlIjoiSl9RTi10SFJYRWVKYjlNcUNrWmFPLUdOVmlibXpGVGVWMk43Z0ptQUdrQSIsIm9yaWdpbiI6Imh0dHBzOi8vZXhhbXBsZS5sb2NhbGhvc3Q6ODQ0MyIsImNyb3NzT3JpZ2luIjpmYWxzZX0",
  211. "transports": [
  212. "internal",
  213. "hybrid"
  214. ]
  215. },
  216. "type": "public-key",
  217. "clientExtensionResults": {},
  218. "authenticatorAttachment": "platform"
  219. },
  220. "label": "1password" // <2>
  221. }
  222. }
  223. ----
  224. <1> The result of calling `navigator.credentials.create` with binary values base64url encoded.
  225. <2> A label that the user selects to have associated with this credential to help the user distinguish the credential.
  226. .Example Successful Registration Response
  227. [source,http]
  228. ----
  229. HTTP/1.1 200 OK
  230. {
  231. "success": true
  232. }
  233. ----
  234. [[passkeys-verify]]
  235. == Verifying an Authentication Assertion
  236. After xref:./passkeys.adoc#passkeys-register[] the passkey can be https://www.w3.org/TR/webauthn-3/#sctn-verifying-assertion[verified] (authenticated).
  237. Verifying a credential is composed of two steps:
  238. 1. Requesting the Verification Options
  239. 2. Verifying the Credential
  240. [[passkeys-verify-options]]
  241. === Request the Verification Options
  242. The first step in verification of a credential is to request the verification options.
  243. In Spring Security, a request for the verification options is typically done using JavaScript and looks like:
  244. [NOTE]
  245. ====
  246. Spring Security provides a default log in page that can be used as a reference on how to verify credentials.
  247. ====
  248. .Request for Verification Options
  249. [source,http]
  250. ----
  251. POST /webauthn/authenticate/options
  252. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  253. ----
  254. The request above will obtain the verification options.
  255. 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.
  256. The response will contain the options for obtaining a credential with binary values such as `challenge` base64url encoded.
  257. .Example Response for Verification Options
  258. [source,json]
  259. ----
  260. {
  261. "challenge": "cQfdGrj9zDg3zNBkOH3WPL954FTOShVy0-CoNgSewNM",
  262. "timeout": 300000,
  263. "rpId": "example.localhost",
  264. "allowCredentials": [],
  265. "userVerification": "preferred",
  266. "extensions": {}
  267. }
  268. ----
  269. [[passkeys-verify-get]]
  270. === Verifying the Credential
  271. After the verification options are obtained, they are used to get a credential.
  272. 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`.
  273. The returned value of `navigator.credentials.get` can then be sent to the server as a JSON request.
  274. Binary values such as `rawId` and `response.*` must be base64url encoded.
  275. An example authentication request can be found below:
  276. .Example Authentication Request
  277. [source,http]
  278. ----
  279. POST /login/webauthn
  280. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  281. {
  282. "id": "dYF7EGnRFFIXkpXi9XU2wg",
  283. "rawId": "dYF7EGnRFFIXkpXi9XU2wg",
  284. "response": {
  285. "authenticatorData": "y9GqwTRaMpzVDbXq1dyEAXVOxrou08k22ggRC45MKNgdAAAAAA",
  286. "clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uZ2V0IiwiY2hhbGxlbmdlIjoiRFVsRzRDbU9naWhKMG1vdXZFcE9HdUk0ZVJ6MGRRWmxUQmFtbjdHQ1FTNCIsIm9yaWdpbiI6Imh0dHBzOi8vZXhhbXBsZS5sb2NhbGhvc3Q6ODQ0MyIsImNyb3NzT3JpZ2luIjpmYWxzZX0",
  287. "signature": "MEYCIQCW2BcUkRCAXDmGxwMi78jknenZ7_amWrUJEYoTkweldAIhAMD0EMp1rw2GfwhdrsFIeDsL7tfOXVPwOtfqJntjAo4z",
  288. "userHandle": "Q3_0Xd64_HW0BlKRAJnVagJTpLKLgARCj8zjugpRnVo"
  289. },
  290. "clientExtensionResults": {},
  291. "authenticatorAttachment": "platform"
  292. }
  293. ----
  294. .Example Successful Authentication Response
  295. [source,http]
  296. ----
  297. HTTP/1.1 200 OK
  298. {
  299. "redirectUrl": "/", // <1>
  300. "authenticated": true // <2>
  301. }
  302. ----
  303. <1> The URL to redirect to
  304. <2> Indicates that the user is authenticated
  305. .Example Authentication Failure Response
  306. [source,http]
  307. ----
  308. HTTP/1.1 401 OK
  309. ----