Class: Profiles::TwoFactorAuthsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/profiles/two_factor_auths_controller.rb

Instance Method Summary collapse

Instance Method Details

#codesObject


42
43
44
45
# File 'app/controllers/profiles/two_factor_auths_controller.rb', line 42

def codes
  @codes = current_user.generate_otp_backup_codes!
  current_user.save!
end

#createObject


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/profiles/two_factor_auths_controller.rb', line 27

def create
  if current_user.validate_and_consume_otp!(params[:pin_code])
    current_user.two_factor_enabled = true
    @codes = current_user.generate_otp_backup_codes!
    current_user.save!

    render 'create'
  else
    @error = 'Invalid pin code'
    @qr_code = build_qr_code

    render 'new'
  end
end

#destroyObject


47
48
49
50
51
# File 'app/controllers/profiles/two_factor_auths_controller.rb', line 47

def destroy
  current_user.disable_two_factor!

  redirect_to 
end

#newObject


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/profiles/two_factor_auths_controller.rb', line 4

def new
  unless current_user.otp_secret
    current_user.otp_secret = User.generate_otp_secret(32)
  end

  unless current_user.otp_grace_period_started_at && two_factor_grace_period
    current_user.otp_grace_period_started_at = Time.current
  end

  current_user.save! if current_user.changed?

  if two_factor_authentication_required?
    if two_factor_grace_period_expired?
      flash.now[:alert] = 'You must enable Two-factor Authentication for your account.'
    else
      grace_period_deadline = current_user.otp_grace_period_started_at + two_factor_grace_period.hours
      flash.now[:alert] = "You must enable Two-factor Authentication for your account before #{l(grace_period_deadline)}."
    end
  end

  @qr_code = build_qr_code
end

#skipObject


53
54
55
56
57
58
59
60
# File 'app/controllers/profiles/two_factor_auths_controller.rb', line 53

def skip
  if two_factor_grace_period_expired?
    redirect_to new_profile_two_factor_auth_path, alert: 'Cannot skip two factor authentication setup'
  else
    session[:skip_tfa] = current_user.otp_grace_period_started_at + two_factor_grace_period.hours
    redirect_to root_path
  end
end