Sha256: 12727403f6df5df64ac563779cd1cac4961522fef1ffa79a95a03411344c6127

Contents?: true

Size: 1.48 KB

Versions: 9

Compression:

Stored size: 1.48 KB

Contents

require 'rotp'

class CASino::TwoFactorAuthenticatorsController < CASino::ApplicationController
  include CASino::SessionsHelper
  include CASino::TwoFactorAuthenticatorsHelper
  include CASino::TwoFactorAuthenticatorProcessor

  before_action :ensure_signed_in

  def new
    @two_factor_authenticator = current_user.two_factor_authenticators.create! secret: ROTP::Base32.random_base32
  end

  def create
    @two_factor_authenticator = current_user.two_factor_authenticators.where(id: params[:id]).first
    validation_result = validate_one_time_password(params[:otp], @two_factor_authenticator)
    case
    when validation_result.success?
      current_user.two_factor_authenticators.where(active: true).delete_all
      @two_factor_authenticator.update_attribute(:active, true)
      flash[:notice] = I18n.t('two_factor_authenticators.successfully_activated')
      redirect_to sessions_path
    when validation_result.error_code == 'INVALID_OTP'
      flash.now[:error] = I18n.t('two_factor_authenticators.invalid_one_time_password')
      render :new
    else
      flash[:error] = I18n.t('two_factor_authenticators.invalid_two_factor_authenticator')
      redirect_to new_two_factor_authenticator_path
    end
  end

  def destroy
    authenticators = current_user.two_factor_authenticators.where(id: params[:id])
    if authenticators.any?
      authenticators.first.destroy
      flash[:notice] = I18n.t('two_factor_authenticators.successfully_deleted')
    end
    redirect_to sessions_path
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
casino-4.1.2 app/controllers/casino/two_factor_authenticators_controller.rb
casino-4.1.1 app/controllers/casino/two_factor_authenticators_controller.rb
casino-4.1.0 app/controllers/casino/two_factor_authenticators_controller.rb
casino-4.0.3 app/controllers/casino/two_factor_authenticators_controller.rb
casino-4.0.2 app/controllers/casino/two_factor_authenticators_controller.rb
casino-4.0.1 app/controllers/casino/two_factor_authenticators_controller.rb
casino-4.0.0 app/controllers/casino/two_factor_authenticators_controller.rb
casino-4.0.0.pre.2 app/controllers/casino/two_factor_authenticators_controller.rb
casino-4.0.0.pre.1 app/controllers/casino/two_factor_authenticators_controller.rb