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