11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/generators/webauthn_authentication/templates/app/controllers/webauthn_sessions_controller.rb', line 11
def create
webauthn_credential = WebAuthn::Credential.from_get(JSON.parse(session_params[:public_key_credential]))
stored_credential = WebauthnCredential.passkey.find_by(external_id: webauthn_credential.id)
unless stored_credential
redirect_to new_session_path, alert: "Credential not recognized"
return
end
begin
webauthn_credential.verify(
session[:current_authentication][:challenge] || session[:current_authentication]["challenge"],
public_key: stored_credential.public_key,
sign_count: stored_credential.sign_count,
user_verification: true,
)
stored_credential.update!(sign_count: webauthn_credential.sign_count)
start_new_session_for stored_credential.user
redirect_to after_authentication_url
rescue WebAuthn::Error => e
redirect_to new_session_path, alert: "Verification failed: #{e.message}"
end
ensure
session.delete(:current_authentication)
end
|