Skip to content

Commit bec8cbd

Browse files
chore: fetch options endpoints from javascript
1 parent 56db786 commit bec8cbd

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

app/assets/javascript/devise/webauthn.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ export class WebauthnCreateElement extends HTMLElement {
44
event.preventDefault();
55

66
try {
7-
const options = JSON.parse(this.getAttribute('data-options-json'));
8-
const publicKey = PublicKeyCredential.parseCreationOptionsFromJSON(options);
7+
debugger
8+
const response = await fetch(this.getAttribute('data-options-url'));
9+
const publicKey = PublicKeyCredential.parseCreationOptionsFromJSON(await response.json());
910
const credential = await navigator.credentials.create({ publicKey });
1011

1112
this.querySelector('[data-webauthn-target="response"]').value = JSON.stringify(credential);
@@ -37,8 +38,8 @@ export class WebauthnGetElement extends HTMLElement {
3738
event.preventDefault();
3839

3940
try {
40-
const options = JSON.parse(this.getAttribute('data-options-json'));
41-
const publicKey = PublicKeyCredential.parseRequestOptionsFromJSON(options);
41+
const response = await fetch(this.getAttribute('data-options-url'));
42+
const publicKey = PublicKeyCredential.parseRequestOptionsFromJSON(await response.json());
4243
const credential = await navigator.credentials.get({ publicKey });
4344

4445
this.querySelector('[data-webauthn-target="response"]').value = JSON.stringify(credential);

lib/devise/webauthn/helpers/credentials_helper.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def passkey_creation_form_for(resource, form_classes: nil, &block)
99
method: :post,
1010
class: form_classes
1111
) do |f|
12-
tag.webauthn_create(data: { options_json: create_passkey_options(resource) }) do
12+
tag.webauthn_create(data: { options_url: options_for_create_passkeys_path(resource) }) do
1313
concat f.hidden_field(:public_key_credential, data: { webauthn_target: "response" })
1414
concat capture(f, &block)
1515
end
@@ -22,7 +22,7 @@ def login_with_passkey_button(text = nil, session_path:, button_classes: nil, fo
2222
method: :post,
2323
class: form_classes
2424
) do |f|
25-
tag.webauthn_get(data: { options_json: passkey_authentication_options }) do
25+
tag.webauthn_get(data: { options_url: options_for_get_passkeys_path(resource) }) do
2626
concat f.hidden_field(:public_key_credential, data: { webauthn_target: "response" })
2727

2828
concat f.button(text, type: "submit", class: button_classes, &block)
@@ -36,7 +36,9 @@ def security_key_creation_form_for(resource, form_classes: nil, &block)
3636
method: :post,
3737
class: form_classes
3838
) do |f|
39-
tag.webauthn_create(data: { options_json: create_security_key_options(resource) }) do
39+
tag.webauthn_create(
40+
data: { options_url: options_for_create_second_factor_webauthn_credentials_path(resource) }
41+
) do
4042
concat f.hidden_field(:public_key_credential, data: { webauthn_target: "response" })
4143
concat capture(f, &block)
4244
end
@@ -49,7 +51,7 @@ def login_with_security_key_button(text = nil, resource:, button_classes: nil, f
4951
method: :post,
5052
class: form_classes
5153
) do |f|
52-
tag.webauthn_get(data: { options_json: security_key_authentication_options(resource) }) do
54+
tag.webauthn_get(data: { options_url: options_for_get_second_factor_webauthn_credentials_path(resource) }) do
5355
concat f.hidden_field(:public_key_credential, data: { webauthn_target: "response" })
5456
concat f.button(text, type: "submit", class: button_classes, &block)
5557
end

0 commit comments

Comments
 (0)