-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathhelpers.rb
131 lines (110 loc) · 3.87 KB
/
helpers.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
126
127
128
129
130
131
require "rqrcode"
module DeviseOtpAuthenticatable
module Controllers
module Helpers
def authenticate_scope!
send(:"authenticate_#{resource_name}!", force: true)
self.resource = send("current_#{resource_name}")
end
#
# similar to DeviseController#set_flash_message, but sets the scope inside
# the otp controller
#
def otp_set_flash_message(key, kind, options = {})
options[:scope] ||= "devise.otp.#{controller_name}"
set_flash_message(key, kind, options)
end
def otp_t
end
def trusted_devices_enabled?
resource.class.otp_trust_persistence && (resource.class.otp_trust_persistence > 0)
end
def recovery_enabled?
resource_class.otp_recovery_tokens && (resource_class.otp_recovery_tokens > 0)
end
#
# Sanity check for resource validity
#
def ensure_resource!
if resource.nil?
raise ArgumentError, "Should not happen"
end
end
#
# check if the resource needs a credentials refresh. IE, they need to be asked a password again to access
# this resource.
#
def needs_credentials_refresh?(resource)
return false unless resource.class.otp_credentials_refresh
(!warden.session(resource_name)[otp_refresh_property].present? ||
(warden.session(resource_name)[otp_refresh_property] < DateTime.now)).tap { |need| otp_set_refresh_return_url if need }
end
#
# credentials are refreshed
#
def otp_refresh_credentials_for(resource)
return false unless resource.class.otp_credentials_refresh
warden.session(resource_name)[otp_refresh_property] = (Time.now + resource.class.otp_credentials_refresh)
end
#
# is the current browser trusted?
#
def is_otp_trusted_browser_for?(resource)
return false unless resource.class.otp_trust_persistence
if cookies[otp_scoped_persistence_cookie].present?
cookies.signed[otp_scoped_persistence_cookie] ==
[resource.to_key, resource.authenticatable_salt, resource.otp_persistence_seed]
else
false
end
end
#
# make the current browser trusted
#
def otp_set_trusted_device_for(resource)
return unless resource.class.otp_trust_persistence
cookies.signed[otp_scoped_persistence_cookie] = {
httponly: true,
expires: Time.now + resource.class.otp_trust_persistence,
value: [resource.to_key, resource.authenticatable_salt, resource.otp_persistence_seed]
}
end
def otp_set_refresh_return_url
warden.session(resource_name)[otp_refresh_return_url_property] = request.fullpath
end
def otp_fetch_refresh_return_url
warden.session(resource_name).delete(otp_refresh_return_url_property) { :root }
end
def otp_refresh_return_url_property
"refresh_return_url"
end
def otp_refresh_property
"credentials_refreshed_at"
end
def otp_scoped_persistence_cookie
"otp_#{resource_name}_device_trusted"
end
#
# make the current browser NOT trusted
#
def otp_clear_trusted_device_for(resource)
cookies.delete(otp_scoped_persistence_cookie)
end
#
# clears the persistence list for this kind of resource
#
def otp_reset_persistence_for(resource)
otp_clear_trusted_device_for(resource)
resource.reset_otp_persistence!
end
#
# returns the URL for the QR Code to initialize the Authenticator device
#
def otp_authenticator_token_image(resource)
content_tag(:div, class: "qrcode-container") do
raw RQRCode::QRCode.new(resource.otp_provisioning_uri).as_svg(:module_size => 5, :viewbox => true, :use_path => true)
end
end
end
end
end