Module: DeviseOtpAuthenticatable::Controllers::Helpers

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

Instance Method Summary collapse

Instance Method Details

#authenticate_scope!Object



6
7
8
9
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 6

def authenticate_scope!
  send(:"authenticate_#{resource_name}!", force: true)
  self.resource = send("current_#{resource_name}")
end

#ensure_resource!Object

Sanity check for resource validity



35
36
37
38
39
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 35

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)


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

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

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

Returns:

  • (Boolean)


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

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



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

def otp_authenticator_token_image(resource)
  (: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

#otp_clear_trusted_device_for(resource) ⇒ Object

make the current browser NOT trusted



108
109
110
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 108

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

#otp_fetch_refresh_return_urlObject



89
90
91
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 89

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



55
56
57
58
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 55

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_propertyObject



97
98
99
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 97

def otp_refresh_property
  "credentials_refreshed_at"
end

#otp_refresh_return_url_propertyObject



93
94
95
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 93

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



115
116
117
118
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 115

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


101
102
103
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 101

def otp_scoped_persistence_cookie
  "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



15
16
17
18
19
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 15

def otp_set_flash_message(key, kind, options = {})
  options[:scope] ||= "devise.otp.#{controller_name}"

  set_flash_message(key, kind, options)
end

#otp_set_refresh_return_urlObject



85
86
87
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 85

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



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

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



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

def otp_t
end

#recovery_enabled?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 28

def recovery_enabled?
  resource_class.otp_recovery_tokens && (resource_class.otp_recovery_tokens > 0)
end

#trusted_devices_enabled?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/devise_otp_authenticatable/controllers/helpers.rb', line 24

def trusted_devices_enabled?
  resource.class.otp_trust_persistence && (resource.class.otp_trust_persistence > 0)
end