-
Notifications
You must be signed in to change notification settings - Fork 45
/
otp_tokens_controller.rb
125 lines (104 loc) · 3.13 KB
/
otp_tokens_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
module DeviseOtp
module Devise
class OtpTokensController < DeviseController
include ::Devise::Controllers::Helpers
prepend_before_action :ensure_credentials_refresh
prepend_before_action :authenticate_scope!
protect_from_forgery except: [:clear_persistence, :delete_persistence]
#
# Displays the status of OTP authentication
#
def show
if resource.nil?
redirect_to stored_location_for(scope) || :root
else
render :show
end
end
#
# Displays the QR Code and Validation Token form for enabling the OTP
#
def edit
resource.populate_otp_secrets!
end
#
# Updates the status of OTP authentication
#
def update
if resource.valid_otp_token?(params[:confirmation_code])
resource.enable_otp!
otp_set_flash_message :success, :successfully_updated
redirect_to otp_token_path_for(resource)
else
otp_set_flash_message :danger, :could_not_confirm, :now => true
render :edit
end
end
#
# Resets OTP authentication, generates new credentials, sets it to off
#
def destroy
if resource.disable_otp!
otp_set_flash_message :success, :successfully_disabled_otp
end
redirect_to otp_token_path_for(resource)
end
#
# makes the current browser persistent
#
def get_persistence
if otp_set_trusted_device_for(resource)
otp_set_flash_message :success, :successfully_set_persistence
end
redirect_to otp_token_path_for(resource)
end
#
# clears persistence for the current browser
#
def clear_persistence
if otp_clear_trusted_device_for(resource)
otp_set_flash_message :success, :successfully_cleared_persistence
end
redirect_to otp_token_path_for(resource)
end
#
# rehash the persistence secret, thus, making all the persistence cookies invalid
#
def delete_persistence
if otp_reset_persistence_for(resource)
otp_set_flash_message :notice, :successfully_reset_persistence
end
redirect_to otp_token_path_for(resource)
end
def recovery
respond_to do |format|
format.html
format.js
format.text do
send_data render_to_string(template: "#{controller_path}/recovery_codes"), filename: "otp-recovery-codes.txt", format: "text"
end
end
end
def reset
if resource.disable_otp!
resource.clear_otp_fields!
otp_set_flash_message :success, :successfully_reset_otp
end
redirect_to edit_otp_token_path_for(resource)
end
private
def ensure_credentials_refresh
ensure_resource!
if needs_credentials_refresh?(resource)
redirect_to refresh_otp_credential_path_for(resource)
end
end
def scope
resource_name.to_sym
end
def self.controller_path
"#{::Devise.otp_controller_path}/otp_tokens"
end
end
end
end