Sha256: 58179505a38e155b01cf6aa494bc193b30fa724bad2e137ad18946ea54bf4934

Contents?: true

Size: 1.57 KB

Versions: 24

Compression:

Stored size: 1.57 KB

Contents

class TwoFactorAuthentication::Profile::SecurityKeysController < ApplicationController
  before_action :set_user
  before_action :set_security_key, only: %i[ edit update destroy ]

  def index
    @security_keys = @user.security_keys
  end

  def new
    respond_to do |format|
      format.html
      format.json { render json: options_for_create }
    end
  end

  def edit
  end

  def create
    @security_key = @user.security_keys.create!(credential_params)
    render json: { status: "ok", location: edit_two_factor_authentication_profile_security_key_url(@security_key, confirmation: true) }, status: :created
  end

  def update
    @security_key.update! name: params[:name]
    redirect_to two_factor_authentication_profile_security_keys_path, notice: "Your changes have been saved"
  end

  def destroy
    @security_key.destroy
    redirect_to two_factor_authentication_profile_security_keys_path, notice: "#{@security_key.name} has been removed"
  end

  private
    def set_user
      @user = Current.user
    end

    def set_security_key
      @security_key = @user.security_keys.find(params[:id])
    end

    def options_for_create
      WebAuthn::Credential.options_for_create(user: user_info, exclude: external_ids)
    end

    def user_info
      { id: @user.webauthn_id, name: @user.email }
    end

    def external_ids
      @user.security_keys.pluck(:external_id)
    end

    def credential_params
      { external_id: credential.id, name: "security key" }
    end

    def credential
      @credential ||= WebAuthn::Credential.from_create(params.require(:credential))
    end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
authentication-zero-4.0.3 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-4.0.2 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-4.0.1 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-4.0.0 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-3.0.2 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-3.0.1 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-3.0.0 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-3.0.0.alpha1 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-2.16.36 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-2.16.35 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-2.16.34 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-2.16.33 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-2.16.32 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-2.16.31 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-2.16.30 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-2.16.29 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-2.16.28 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-2.16.27 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-2.16.26 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt
authentication-zero-2.16.25 lib/generators/authentication/templates/controllers/html/two_factor_authentication/profile/security_keys_controller.rb.tt