Sha256: 83a3d03c0e2d463f9ced9728611e84873d23425869cb4232d0f3da315cda30ab

Contents?: true

Size: 1.16 KB

Versions: 24

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Koi
  module Controller
    module HasWebauthn
      extend ActiveSupport::Concern

      included do
        helper_method :webauthn_auth_options
      end

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

      def webauthn_auth_options
        options = webauthn_relying_party.options_for_authentication(
          allow: Admin::Credential.pluck(:external_id),
        )
        session[:authentication_challenge] = options.challenge

        options
      end

      def webauthn_authenticate!
        return if session_params[:response].blank?

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

        stored_credential.update!(sign_count: webauthn_credential.sign_count)

        stored_credential.admin
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
katalyst-koi-4.14.0 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.13.2 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.13.0 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.12.6 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.12.5 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.12.4 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.12.3 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.12.2 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.12.1 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.12.0 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.11.2 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.11.1 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.11.0 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.10.3 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.10.2 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.10.1 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.10.0 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.9.5 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.9.4 app/controllers/concerns/koi/controller/has_webauthn.rb
katalyst-koi-4.9.3 app/controllers/concerns/koi/controller/has_webauthn.rb