passkeys.adoc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. http
  54. // ...
  55. .formLogin(withDefaults())
  56. .webAuthn((webAuthn) -> webAuthn
  57. .rpId("example.com")
  58. .allowedOrigins("https://example.com")
  59. );
  60. return http.build();
  61. }
  62. @Bean
  63. UserDetailsService userDetailsService() {
  64. UserDetails userDetails = User.withDefaultPasswordEncoder()
  65. .username("user")
  66. .password("password")
  67. .roles("USER")
  68. .build();
  69. return new InMemoryUserDetailsManager(userDetails);
  70. }
  71. ----
  72. Kotlin::
  73. +
  74. [source,kotlin,role="secondary"]
  75. ----
  76. @Bean
  77. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  78. http {
  79. webAuthn {
  80. rpId = "example.com"
  81. allowedOrigins = setOf("https://example.com")
  82. }
  83. }
  84. }
  85. @Bean
  86. open fun userDetailsService(): UserDetailsService {
  87. val userDetails = User.withDefaultPasswordEncoder()
  88. .username("user")
  89. .password("password")
  90. .roles("USER")
  91. .build()
  92. return InMemoryUserDetailsManager(userDetails)
  93. }
  94. ----
  95. ======
  96. [[passkeys-register]]
  97. == Register a New Credential
  98. 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].
  99. Registering a new credential is composed of two steps:
  100. 1. Requesting the Registration Options
  101. 2. Registering the Credential
  102. [[passkeys-register-options]]
  103. === Request the Registration Options
  104. The first step in registration of a new credential is to request the registration options.
  105. In Spring Security, a request for the registration options is typically done using JavaScript and looks like:
  106. [NOTE]
  107. ====
  108. Spring Security provides a default registration page that can be used as a reference on how to register credentials.
  109. ====
  110. .Request for Registration Options
  111. [source,http]
  112. ----
  113. POST /webauthn/register/options
  114. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  115. ----
  116. The request above will obtain the registration options for the currently authenticated user.
  117. 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.
  118. .Response for Registration Options
  119. [source,json]
  120. ----
  121. {
  122. "rp": {
  123. "name": "SimpleWebAuthn Example",
  124. "id": "example.localhost"
  125. },
  126. "user": {
  127. "name": "user@example.localhost",
  128. "id": "oWJtkJ6vJ_m5b84LB4_K7QKTCTEwLIjCh4tFMCGHO4w",
  129. "displayName": "user@example.localhost"
  130. },
  131. "challenge": "q7lCdd3SVQxdC-v8pnRAGEn1B2M-t7ZECWPwCAmhWvc",
  132. "pubKeyCredParams": [
  133. {
  134. "type": "public-key",
  135. "alg": -8
  136. },
  137. {
  138. "type": "public-key",
  139. "alg": -7
  140. },
  141. {
  142. "type": "public-key",
  143. "alg": -257
  144. }
  145. ],
  146. "timeout": 300000,
  147. "excludeCredentials": [],
  148. "authenticatorSelection": {
  149. "residentKey": "required",
  150. "userVerification": "preferred"
  151. },
  152. "attestation": "none",
  153. "extensions": {
  154. "credProps": true
  155. }
  156. }
  157. ----
  158. [[passkeys-register-create]]
  159. === Registering the Credential
  160. After the registration options are obtained, they are used to create the credentials that are registered.
  161. 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`.
  162. The returned value can then be sent to the server as a JSON request.
  163. An example registration request can be found below:
  164. .Example Registration Request
  165. [source,http]
  166. ----
  167. POST /webauthn/register
  168. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  169. {
  170. "publicKey": { // <1>
  171. "credential": {
  172. "id": "dYF7EGnRFFIXkpXi9XU2wg",
  173. "rawId": "dYF7EGnRFFIXkpXi9XU2wg",
  174. "response": {
  175. "attestationObject": "o2NmbXRkbm9uZWdhdHRTdG10oGhhdXRoRGF0YViUy9GqwTRaMpzVDbXq1dyEAXVOxrou08k22ggRC45MKNhdAAAAALraVWanqkAfvZZFYZpVEg0AEHWBexBp0RRSF5KV4vV1NsKlAQIDJiABIVggQjmrekPGzyqtoKK9HPUH-8Z2FLpoqkklFpFPQVICQ3IiWCD6I9Jvmor685fOZOyGXqUd87tXfvJk8rxj9OhuZvUALA",
  176. "clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uY3JlYXRlIiwiY2hhbGxlbmdlIjoiSl9RTi10SFJYRWVKYjlNcUNrWmFPLUdOVmlibXpGVGVWMk43Z0ptQUdrQSIsIm9yaWdpbiI6Imh0dHBzOi8vZXhhbXBsZS5sb2NhbGhvc3Q6ODQ0MyIsImNyb3NzT3JpZ2luIjpmYWxzZX0",
  177. "transports": [
  178. "internal",
  179. "hybrid"
  180. ]
  181. },
  182. "type": "public-key",
  183. "clientExtensionResults": {},
  184. "authenticatorAttachment": "platform"
  185. },
  186. "label": "1password" // <2>
  187. }
  188. }
  189. ----
  190. <1> The result of calling `navigator.credentials.create` with binary values base64url encoded.
  191. <2> A label that the user selects to have associated with this credential to help the user distinguish the credential.
  192. .Example Successful Registration Response
  193. [source,http]
  194. ----
  195. HTTP/1.1 200 OK
  196. {
  197. "success": true
  198. }
  199. ----
  200. [[passkeys-verify]]
  201. == Verifying an Authentication Assertion
  202. After xref:./passkeys.adoc#passkeys-register[] the passkey can be https://www.w3.org/TR/webauthn-3/#sctn-verifying-assertion[verified] (authenticated).
  203. Verifying a credential is composed of two steps:
  204. 1. Requesting the Verification Options
  205. 2. Verifying the Credential
  206. [[passkeys-verify-options]]
  207. === Request the Verification Options
  208. The first step in verification of a credential is to request the verification options.
  209. In Spring Security, a request for the verification options is typically done using JavaScript and looks like:
  210. [NOTE]
  211. ====
  212. Spring Security provides a default log in page that can be used as a reference on how to verify credentials.
  213. ====
  214. .Request for Verification Options
  215. [source,http]
  216. ----
  217. POST /webauthn/authenticate/options
  218. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  219. ----
  220. The request above will obtain the verification options.
  221. 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.
  222. The response will contain the options for obtaining a credential with binary values such as `challenge` base64url encoded.
  223. .Example Response for Verification Options
  224. [source,json]
  225. ----
  226. {
  227. "challenge": "cQfdGrj9zDg3zNBkOH3WPL954FTOShVy0-CoNgSewNM",
  228. "timeout": 300000,
  229. "rpId": "example.localhost",
  230. "allowCredentials": [],
  231. "userVerification": "preferred",
  232. "extensions": {}
  233. }
  234. ----
  235. [[passkeys-verify-get]]
  236. === Verifying the Credential
  237. After the verification options are obtained, they are used to get a credential.
  238. 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`.
  239. The returned value of `navigator.credentials.get` can then be sent to the server as a JSON request.
  240. Binary values such as `rawId` and `response.*` must be base64url encoded.
  241. An example authentication request can be found below:
  242. .Example Authentication Request
  243. [source,http]
  244. ----
  245. POST /login/webauthn
  246. X-CSRF-TOKEN: 4bfd1575-3ad1-4d21-96c7-4ef2d9f86721
  247. {
  248. "id": "dYF7EGnRFFIXkpXi9XU2wg",
  249. "rawId": "dYF7EGnRFFIXkpXi9XU2wg",
  250. "response": {
  251. "authenticatorData": "y9GqwTRaMpzVDbXq1dyEAXVOxrou08k22ggRC45MKNgdAAAAAA",
  252. "clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uZ2V0IiwiY2hhbGxlbmdlIjoiRFVsRzRDbU9naWhKMG1vdXZFcE9HdUk0ZVJ6MGRRWmxUQmFtbjdHQ1FTNCIsIm9yaWdpbiI6Imh0dHBzOi8vZXhhbXBsZS5sb2NhbGhvc3Q6ODQ0MyIsImNyb3NzT3JpZ2luIjpmYWxzZX0",
  253. "signature": "MEYCIQCW2BcUkRCAXDmGxwMi78jknenZ7_amWrUJEYoTkweldAIhAMD0EMp1rw2GfwhdrsFIeDsL7tfOXVPwOtfqJntjAo4z",
  254. "userHandle": "Q3_0Xd64_HW0BlKRAJnVagJTpLKLgARCj8zjugpRnVo"
  255. },
  256. "clientExtensionResults": {},
  257. "authenticatorAttachment": "platform"
  258. }
  259. ----
  260. .Example Successful Authentication Response
  261. [source,http]
  262. ----
  263. HTTP/1.1 200 OK
  264. {
  265. "redirectUrl": "/", // <1>
  266. "authenticated": true // <2>
  267. }
  268. ----
  269. <1> The URL to redirect to
  270. <2> Indicates that the user is authenticated
  271. .Example Authentication Failure Response
  272. [source,http]
  273. ----
  274. HTTP/1.1 401 OK
  275. ----