Class: PasskeyAuth::Webauthn::Credentials::ChallengesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/passkey_auth/webauthn/credentials/challenges_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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
38
39
40
41
42
43
# File 'app/controllers/passkey_auth/webauthn/credentials/challenges_controller.rb', line 13

def create
  user = current_user || find_user_by_session_id

  # Generate WebAuthn ID if the user does not have any yet
  user.update(webauthn_id: ::WebAuthn.generate_user_id) unless user.webauthn_id

  user_email = PasskeyAuth.user_email(user)
  webauthn_name = params[:name].presence || user.try(:name) || user_email

  # Prepare the needed data for a challenge
  create_options = ::WebAuthn::Credential.options_for_create(
    user: {
      id: user.webauthn_id,
      display_name: webauthn_name,
      name: user_email # unique name for auth
    },
    exclude: user.webauthn_credentials.pluck(:external_id),
    authenticator_selection: {
      authenticator_attachment: "platform",
      user_verification: "preferred",
      resident_key: "required"
    }
  )

  # Generate the challenge and save it into the session
  session[:webauthn_registration_challenge] = create_options.challenge

  respond_to do |format|
    format.json { render json: create_options }
  end
end