Module: DeviseOtpAuthenticatable::Controllers::Helpers
- Defined in:
- lib/devise_otp_authenticatable/controllers/helpers.rb
Instance Method Summary collapse
- #authenticate_scope! ⇒ Object
-
#ensure_resource! ⇒ Object
Sanity check for resource validity.
-
#is_otp_trusted_browser_for?(resource) ⇒ Boolean
is the current browser trusted?.
-
#needs_credentials_refresh?(resource) ⇒ Boolean
check if the resource needs a credentials refresh.
-
#otp_authenticator_token_image(resource) ⇒ Object
returns the URL for the QR Code to initialize the Authenticator device.
-
#otp_clear_trusted_device_for(resource) ⇒ Object
make the current browser NOT trusted.
- #otp_fetch_refresh_return_url ⇒ Object
-
#otp_refresh_credentials_for(resource) ⇒ Object
credentials are refreshed.
- #otp_refresh_property ⇒ Object
- #otp_refresh_return_url_property ⇒ Object
-
#otp_reset_persistence_for(resource) ⇒ Object
clears the persistence list for this kind of resource.
- #otp_scoped_persistence_cookie ⇒ Object
-
#otp_set_flash_message(key, kind, options = {}) ⇒ Object
similar to DeviseController#set_flash_message, but sets the scope inside the otp controller.
- #otp_set_refresh_return_url ⇒ Object
-
#otp_set_trusted_device_for(resource) ⇒ Object
make the current browser trusted.
- #otp_t ⇒ Object
- #recovery_enabled? ⇒ Boolean
- #trusted_devices_enabled? ⇒ Boolean
Instance Method Details
#authenticate_scope! ⇒ Object
4 5 6 7 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 4 def authenticate_scope! send(:"authenticate_#{resource_name}!", force: true) self.resource = send("current_#{resource_name}") end |
#ensure_resource! ⇒ Object
Sanity check for resource validity
36 37 38 39 40 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 36 def ensure_resource! if resource.nil? raise ArgumentError, "Should not happen" end end |
#is_otp_trusted_browser_for?(resource) ⇒ Boolean
is the current browser trusted?
64 65 66 67 68 69 70 71 72 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 64 def is_otp_trusted_browser_for?(resource) return false unless resource.class.otp_trust_persistence if [].present? .signed[] == [resource.to_key, resource.authenticatable_salt, resource.otp_persistence_seed] else false end end |
#needs_credentials_refresh?(resource) ⇒ Boolean
check if the resource needs a credentials refresh. IE, they need to be asked a password again to access this resource.
46 47 48 49 50 51 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 46 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 |
#otp_authenticator_token_image(resource) ⇒ Object
returns the URL for the QR Code to initialize the Authenticator device
124 125 126 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 124 def otp_authenticator_token_image(resource) otp_authenticator_token_image_js(resource.otp_provisioning_uri) end |
#otp_clear_trusted_device_for(resource) ⇒ Object
make the current browser NOT trusted
109 110 111 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 109 def otp_clear_trusted_device_for(resource) .delete() end |
#otp_fetch_refresh_return_url ⇒ Object
90 91 92 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 90 def otp_fetch_refresh_return_url warden.session(resource_name).delete(otp_refresh_return_url_property) { :root } end |
#otp_refresh_credentials_for(resource) ⇒ Object
credentials are refreshed
56 57 58 59 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 56 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 |
#otp_refresh_property ⇒ Object
98 99 100 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 98 def otp_refresh_property "credentials_refreshed_at" end |
#otp_refresh_return_url_property ⇒ Object
94 95 96 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 94 def otp_refresh_return_url_property "refresh_return_url" end |
#otp_reset_persistence_for(resource) ⇒ Object
clears the persistence list for this kind of resource
116 117 118 119 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 116 def otp_reset_persistence_for(resource) otp_clear_trusted_device_for(resource) resource.reset_otp_persistence! end |
#otp_scoped_persistence_cookie ⇒ Object
102 103 104 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 102 def "otp_#{resource_name}_device_trusted" end |
#otp_set_flash_message(key, kind, options = {}) ⇒ Object
similar to DeviseController#set_flash_message, but sets the scope inside the otp controller
13 14 15 16 17 18 19 20 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 13 def (key, kind, = {}) [:scope] ||= "devise.otp.#{controller_name}" [:default] = Array([:default]).unshift(kind.to_sym) [:resource_name] = resource_name = () if respond_to?(:devise_i18n_options, true) = I18n.t("#{[:resource_name]}.#{kind}", **) flash[key] = if .present? end |
#otp_set_refresh_return_url ⇒ Object
86 87 88 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 86 def otp_set_refresh_return_url warden.session(resource_name)[otp_refresh_return_url_property] = request.fullpath end |
#otp_set_trusted_device_for(resource) ⇒ Object
make the current browser trusted
77 78 79 80 81 82 83 84 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 77 def otp_set_trusted_device_for(resource) return unless resource.class.otp_trust_persistence .signed[] = { httponly: true, expires: Time.now + resource.class.otp_trust_persistence, value: [resource.to_key, resource.authenticatable_salt, resource.otp_persistence_seed] } end |
#otp_t ⇒ Object
22 23 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 22 def otp_t end |
#recovery_enabled? ⇒ Boolean
29 30 31 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 29 def recovery_enabled? resource_class.otp_recovery_tokens && (resource_class.otp_recovery_tokens > 0) end |
#trusted_devices_enabled? ⇒ Boolean
25 26 27 |
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 25 def trusted_devices_enabled? resource.class.otp_trust_persistence && (resource.class.otp_trust_persistence > 0) end |