Module: Koi::Controller::HasWebauthn

Extended by:
ActiveSupport::Concern
Included in:
Admin::CredentialsController, Admin::ProfilesController, Admin::SessionsController
Defined in:
app/controllers/concerns/koi/controller/has_webauthn.rb

Defined Under Namespace

Modules: Helper

Instance Method Summary collapse

Instance Method Details

#webauthn_authenticate!(response) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/concerns/koi/controller/has_webauthn.rb', line 49

def webauthn_authenticate!(response)
  return if response.blank?

  webauthn_credential, stored_credential = webauthn_relying_party.verify_authentication(
    JSON.parse(response),
    session.delete(:authentication_challenge),
  ) do |credential|
    Admin::Credential.find_by!(external_id: credential.id)
  end

  stored_credential.update(
    sign_count: webauthn_credential.sign_count,
    updated_at: DateTime.current,
  )

  stored_credential.admin
rescue ActiveRecord::RecordNotFound, WebAuthn::VerificationError
  false
end

#webauthn_nicknameObject



88
89
90
91
# File 'app/controllers/concerns/koi/controller/has_webauthn.rb', line 88

def webauthn_nickname
  user_agent = UserAgent.parse(request.user_agent)
  "#{user_agent.browser} (#{user_agent.platform})"
end

#webauthn_register!(response) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/concerns/koi/controller/has_webauthn.rb', line 69

def webauthn_register!(response)
  return if response.blank?

  webauthn_credential = webauthn_relying_party.verify_registration(
    JSON.parse(response),
    session.delete(:registration_challenge),
  )

  Koi::Current
    .admin_user
    .credentials
    .create_with(nickname:   webauthn_nickname,
                 public_key: webauthn_credential.public_key,
                 sign_count: webauthn_credential.sign_count)
    .create_or_find_by!(
      external_id: webauthn_credential.id,
    )
end

#webauthn_relying_partyObject



12
13
14
15
16
17
18
# File 'app/controllers/concerns/koi/controller/has_webauthn.rb', line 12

def webauthn_relying_party
  @webauthn_relying_party ||=
    WebAuthn::RelyingParty.new(
      name:            Koi.config.admin_name,
      allowed_origins: [request.base_url],
    )
end