Skip to content

Commit 56e166d

Browse files
--wip-- [skip ci]
1 parent da5ecf6 commit 56e166d

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

test/dummy/app/controllers/second_factor_webauthn_credentials_controller.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,29 @@ def create
5050
end
5151
end
5252

53+
def upgrade
54+
webauthn_credential = WebAuthn::Credential.from_get(JSON.parse(session_params[:public_key_credential]))
55+
56+
credential = user.webauthn_credentials.find_by(external_id: webauthn_credential.id)
57+
unless credential
58+
redirect_to root_path, alert: "Credential not recognized"
59+
return
60+
end
61+
62+
begin
63+
webauthn_credential.verify(
64+
session[:current_authentication][:challenge] || session[:current_authentication]["challenge"],
65+
public_key: credential.public_key,
66+
sign_count: credential.sign_count
67+
)
68+
69+
credential.update!(authenticator_factor: "first_factor")
70+
redirect_to root_path
71+
rescue WebAuthn::Error => e
72+
redirect_to root_path, alert: "Verification failed: #{e.message}"
73+
end
74+
end
75+
5376
def destroy
5477
Current.user.second_factor_webauthn_credentials.destroy(params[:id])
5578

test/dummy/app/views/home/_credentials_list.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<span>
66
<span><%= credential.nickname %></span>
77
<span><%= credential.external_id %></span>
8+
<%=link_to "Upgrade" , second_factor_webauthn_credential_upgrade_path(credential), locals: {credential: credential} %>
89
</span>
910

1011
<%= link_to "Delete credential", delete_path.call(credential), data: { turbo_method: :delete } %>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<h3>Upgrade your security key to a passkey</h3>
2+
3+
<p>
4+
You are about to upgrade your security key <%= @credential.nickname %> to a passkey.
5+
</p>
6+
<%= form_with(
7+
url: second_factor_webauthn_credentials_upgrade,
8+
data: {
9+
controller: "webauthn-credentials",
10+
action: "webauthn-credentials#get:prevent",
11+
"webauthn-credentials-options-url-value": get_options_second_factor_webauthn_credentials_path,
12+
}) do |form| %>
13+
<%= form.hidden_field :public_key_credential, data: { "webauthn-credentials-target": "credentialHiddenInput" } %>
14+
15+
<div class="field">
16+
<%= form.submit "Upgrade to passkey", disabled: true, data: { "webauthn-credentials-target": "submitButton" } %>
17+
</div>
18+
<% end %>

test/dummy/config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
resources :second_factor_webauthn_credentials, only: [ :new, :create, :destroy ] do
1111
post :create_options, on: :collection
12+
get :upgrade
13+
post :upgrade
1214
end
1315

1416
resource :second_factor_authentication, only: [ :new, :create ] do

0 commit comments

Comments
 (0)