Module: DeviseOtpAuthenticatable::Controllers::Helpers

Defined in:
lib/devise_otp_authenticatable/controllers/helpers.rb

Instance Method Summary collapse

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?

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 65

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

#needs_credentials_refresh?(resource) ⇒ Boolean

fixme do cookies and persistence need to be scoped? probably

check if the resource needs a credentials refresh. IE, they need to be asked a password again to access this resource.

Returns:

  • (Boolean)


47
48
49
50
51
52
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 47

def needs_credentials_refresh?(resource)
  return false unless resource.class.otp_credentials_refresh

  (!session[otp_scoped_refresh_property].present? ||
      (session[otp_scoped_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



125
126
127
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 125

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



110
111
112
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 110

def otp_clear_trusted_device_for(resource)
  cookies.delete(otp_scoped_persistence_cookie)
end

#otp_fetch_refresh_return_urlObject



91
92
93
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 91

def otp_fetch_refresh_return_url
  session.delete(otp_scoped_refresh_return_url_property) { :root }
end

#otp_refresh_credentials_for(resource) ⇒ Object

credentials are refreshed



57
58
59
60
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 57

def otp_refresh_credentials_for(resource)
  return false unless resource.class.otp_credentials_refresh
  session[otp_scoped_refresh_property] = (Time.now + resource.class.otp_credentials_refresh)
end

#otp_reset_persistence_for(resource) ⇒ Object

clears the persistence list for this kind of resource



117
118
119
120
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 117

def otp_reset_persistence_for(resource)
  otp_clear_trusted_device_for(resource)
  resource.reset_otp_persistence!
end


103
104
105
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 103

def otp_scoped_persistence_cookie
  "otp_#{resource_name}_device_trusted"
end

#otp_scoped_refresh_propertyObject



99
100
101
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 99

def otp_scoped_refresh_property
  "otp_#{resource_name}refresh_after".to_sym
end

#otp_scoped_refresh_return_url_propertyObject



95
96
97
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 95

def otp_scoped_refresh_return_url_property
  "otp_#{resource_name}refresh_return_url".to_sym
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 otp_set_flash_message(key, kind, options = {})
  options[:scope] ||= "devise.otp.#{controller_name}"
  options[:default] = Array(options[:default]).unshift(kind.to_sym)
  options[:resource_name] = resource_name
  options = devise_i18n_options(options) if respond_to?(:devise_i18n_options, true)
  message = I18n.t("#{options[:resource_name]}.#{kind}", **options)
  flash[key] = message if message.present?
end

#otp_set_refresh_return_urlObject



87
88
89
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 87

def otp_set_refresh_return_url
  session[otp_scoped_refresh_return_url_property] = request.fullpath
end

#otp_set_trusted_device_for(resource) ⇒ Object

make the current browser trusted



78
79
80
81
82
83
84
85
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 78

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

#otp_tObject



22
23
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 22

def otp_t
end

#recovery_enabled?Boolean

Returns:

  • (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

Returns:

  • (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