Class: SecondFactorAuthenticationsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
lib/generators/webauthn_authentication/templates/app/controllers/second_factor_authentications_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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

#get_optionsObject



6
7
8
9
10
11
12
13
14
# File 'lib/generators/webauthn_authentication/templates/app/controllers/second_factor_authentications_controller.rb', line 6

def get_options
  get_options = WebAuthn::Credential.options_for_get(
    allow: user.webauthn_credentials.pluck(:external_id),
    user_verification: "discouraged"
  )
  session[:current_authentication][:challenge] = get_options.challenge

  render json: get_options
end