passkeys.adoc 9.6 KB

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