Module: EasyAdmin::TwoFactor

Extended by:
ActiveSupport::Concern
Included in:
ProfileController
Defined in:
app/controllers/concerns/easy_admin/two_factor.rb,
app/components/easy_admin/two_factor/setup_component.rb,
app/components/easy_admin/two_factor/status_component.rb,
app/components/easy_admin/two_factor/backup_codes_component.rb

Defined Under Namespace

Classes: BackupCodesComponent, SetupComponent, StatusComponent

Instance Method Summary collapse

Instance Method Details

#change_passwordObject



78
79
80
81
82
# File 'app/controllers/concerns/easy_admin/two_factor.rb', line 78

def change_password
  respond_to do |format|
    format.html { render "change_password", layout: !turbo_frame_request? }
  end
end

#regenerate_backup_codesObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/concerns/easy_admin/two_factor.rb', line 63

def regenerate_backup_codes
  unless current_admin_user.two_factor_enabled?
    respond_to do |format|
      format.turbo_stream { render "easy_admin/profile/two_factor_not_enabled" }
    end
    return
  end
  
  current_admin_user.generate_backup_codes!
  
  respond_to do |format|
    format.turbo_stream { render "easy_admin/profile/backup_codes_regenerated" }
  end
end

#two_factor_backup_codesObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/concerns/easy_admin/two_factor.rb', line 50

def two_factor_backup_codes
  unless current_admin_user.two_factor_enabled?
    respond_to do |format|
      format.turbo_stream { render "easy_admin/profile/two_factor_not_enabled" }
    end
    return
  end
  
  respond_to do |format|
    format.html { render "two_factor_backup_codes", layout: !turbo_frame_request? }
  end
end

#two_factor_enableObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/concerns/easy_admin/two_factor.rb', line 25

def two_factor_enable
  unless current_admin_user.two_factor_available?
    respond_to do |format|
      format.turbo_stream { render "easy_admin/profile/two_factor_unavailable" }
    end
    return
  end
  
  @otp_code = params[:otp_code]
  
  if current_admin_user.validate_and_consume_otp!(@otp_code)
    current_admin_user.update!(otp_required_for_login: true)
    current_admin_user.generate_backup_codes!
    
    respond_to do |format|
      format.turbo_stream { render "easy_admin/profile/two_factor_enabled" }
    end
  else
    respond_to do |format|
      format.turbo_stream { render "easy_admin/profile/two_factor_invalid_code" }
    end
  end
end

#two_factor_setupObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/concerns/easy_admin/two_factor.rb', line 5

def two_factor_setup
  unless current_admin_user.two_factor_available?
    respond_to do |format|
      format.turbo_stream { render "easy_admin/profile/two_factor_unavailable" }
      format.html { redirect_to profile_path, alert: "2FA is not available" }
    end
    return
  end
  
  # Generate secret if not already present
  unless current_admin_user.otp_secret.present?
    current_admin_user.generate_otp_secret!
    current_admin_user.save!
  end

  respond_to do |format|
    format.html { render "two_factor_setup", layout: !turbo_frame_request? }
  end
end

#update_passwordObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/concerns/easy_admin/two_factor.rb', line 84

def update_password
  if current_admin_user.valid_password?(params[:admin_user][:current_password])
    if current_admin_user.update_with_password(password_params.merge(current_password: params[:admin_user][:current_password]))
      # Bypass auto sign out by signing the user back in
      (current_admin_user)
      respond_to do |format|
        format.turbo_stream { render "easy_admin/profile/password_updated" }
      end
    else
      respond_to do |format|
        format.turbo_stream { render "easy_admin/profile/password_error" }
      end
    end
  else
    respond_to do |format|
      format.turbo_stream { render "easy_admin/profile/password_invalid_current" }
    end
  end
end