Skip to content

Commit be094a4

Browse files
Merge pull request #501 from ttanimichi/authenticator-selection
Require discoverable credentials and user verification in README examples
2 parents 87af3c6 + 1f0bf23 commit be094a4

1 file changed

Lines changed: 21 additions & 6 deletions

File tree

‎README.md‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@ end
176176

177177
options = WebAuthn::Credential.options_for_create(
178178
user: { id: user.webauthn_id, name: user.name },
179-
exclude: user.credentials.map { |c| c.webauthn_id }
179+
exclude: user.credentials.map { |c| c.webauthn_id },
180+
authenticator_selection: {
181+
resident_key: "discouraged", # For a passwordless login or 2FA. Use "required" for a passkey-based (passwordless and usernameless) login.
182+
user_verification: "required" # For a passwordless or passkey-based (passwordless and usernameless) login. Use "discouraged" for 2FA.
183+
}
180184
)
181185

182186
# Store the newly generated challenge somewhere so you can have it
@@ -204,7 +208,8 @@ session[:creation_challenge] = options.challenge
204208
webauthn_credential = WebAuthn::Credential.from_create(params[:publicKeyCredential])
205209

206210
begin
207-
webauthn_credential.verify(session[:creation_challenge])
211+
# Enforce user verification (pairs with the "required" request above). Omit for 2FA.
212+
webauthn_credential.verify(session[:creation_challenge], user_verification: true)
208213

209214
# Store Credential ID, Credential Public Key and Sign Count for future authentications
210215
user.credentials.create!(
@@ -224,7 +229,12 @@ end
224229
#### Initiation phase
225230

226231
```ruby
227-
options = WebAuthn::Credential.options_for_get(allow: user.credentials.map { |c| c.webauthn_id })
232+
options = WebAuthn::Credential.options_for_get(
233+
# Pass `allow` when the user is already known (passwordless/2FA/reauth).
234+
# For a passkey-based (usernameless and passwordless) login, omit it and resolve the user from `user_handle` after `from_get`.
235+
allow: user.credentials.map { |c| c.webauthn_id },
236+
user_verification: "required" # For a passwordless or passkey-based (passwordless and usernameless) login. Use "discouraged" for 2FA.
237+
)
228238

229239
# Store the newly generated challenge somewhere so you can have it
230240
# for the verification phase.
@@ -260,7 +270,8 @@ begin
260270
webauthn_credential.verify(
261271
session[:authentication_challenge],
262272
public_key: stored_credential.public_key,
263-
sign_count: stored_credential.sign_count
273+
sign_count: stored_credential.sign_count,
274+
user_verification: true # For a passwordless or passkey-based (passwordless and usernameless) login. Omit for 2FA.
264275
)
265276

266277
# Update the stored credential sign count with the value from `webauthn_credential.sign_count`
@@ -290,7 +301,11 @@ Extensions can be requested in the initiation phase in both Credential Registrat
290301
creation_options = WebAuthn::Credential.options_for_create(
291302
user: { id: user.webauthn_id, name: user.name },
292303
exclude: user.credentials.map { |c| c.webauthn_id },
293-
extensions: { appidExclude: domain.to_s }
304+
extensions: { appidExclude: domain.to_s },
305+
authenticator_selection: {
306+
resident_key: "discouraged", # For a passwordless login or 2FA. Use "required" for a passkey-based (passwordless and usernameless) login.
307+
user_verification: "required" # For a passwordless or passkey-based (passwordless and usernameless) login. Use "discouraged" for 2FA.
308+
}
294309
)
295310

296311
# OR
@@ -344,7 +359,7 @@ to be used in the client-side code to call `navigator.credentials.create({ "publ
344359

345360
```ruby
346361
creation_options = WebAuthn::Credential.options_for_create(
347-
user: { id: user.webauthn_id, name: user.name }
362+
user: { id: user.webauthn_id, name: user.name },
348363
exclude: user.credentials.map { |c| c.webauthn_id }
349364
)
350365

0 commit comments

Comments
 (0)