Class: ActionAuth::WebauthnCredentialsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/action_auth/webauthn_credentials_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/action_auth/webauthn_credentials_controller.rb', line 31

def create
  webauthn_credential = WebAuthn::Credential.from_create(params)

  begin
    webauthn_credential.verify(session[:current_challenge])

    credential = current_user.webauthn_credentials.build(
      external_id: webauthn_credential.id,
      nickname: params[:credential_nickname],
      public_key: webauthn_credential.public_key,
      sign_count: webauthn_credential.sign_count,
      key_type: key_type
    )

    if credential.save
      render json: { status: "ok" }, status: :ok
    else
      render json: "Couldn't add your Security Key", status: :unprocessable_entity
    end
  rescue WebAuthn::Error => e
    Rails.logger.error "❌ Verification failed: #{e.message}"
    render json: "Verification failed: #{e.message}", status: :unprocessable_entity
  end
end

#destroyObject



56
57
58
59
60
# File 'app/controllers/action_auth/webauthn_credentials_controller.rb', line 56

def destroy
  current_user.webauthn_credentials.destroy(params[:id])

  redirect_to sessions_path
end

#newObject



5
6
# File 'app/controllers/action_auth/webauthn_credentials_controller.rb', line 5

def new
end

#optionsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/action_auth/webauthn_credentials_controller.rb', line 8

def options
  if current_user.webauthn_id.blank?
    current_user.update!(webauthn_id: WebAuthn.generate_user_id)
  end

  create_options = WebAuthn::Credential.options_for_create(
    user: {
      id: current_user.webauthn_id,
      name: current_user.email
    },
    exclude: current_user.webauthn_credentials.pluck(:external_id)
  )

  session[:current_challenge] = create_options.challenge

  respond_to do |format|
    format.json { render json: create_options }
    if defined?(Turbo)
      format.turbo_stream { render json: create_options }
    end
  end
end