Module: Kingsman::Models::Authenticatable

Extended by:
ActiveSupport::Concern
Defined in:
lib/kingsman/models/authenticatable.rb

Overview

Authenticatable module. Holds common settings for authentication.

Options

Authenticatable adds the following options to kingsman:

* +authentication_keys+: parameters used for authentication. By default [:email].

* +http_authentication_key+: map the username passed via HTTP Auth to this parameter. Defaults to
  the first element in +authentication_keys+.

* +request_keys+: parameters from the request object used for authentication.
  By specifying a symbol (which should be a request method), it will automatically be
  passed to find_for_authentication method and considered in your model lookup.

  For instance, if you set :request_keys to [:subdomain], :subdomain will be considered
  as key on authentication. This can also be a hash where the value is a boolean specifying
  if the value is required or not.

* +http_authenticatable+: if this model allows http authentication. By default false.
  It also accepts an array specifying the strategies that should allow http.

* +params_authenticatable+: if this model allows authentication through request params. By default true.
  It also accepts an array specifying the strategies that should allow params authentication.

* +skip_session_storage+: By default Kingsman will store the user in session.
  By default is set to skip_session_storage: [:http_auth].

active_for_authentication?

After authenticating a user and in each request, Kingsman checks if your model is active by calling model.active_for_authentication?. This method is overwritten by other kingsman modules. For instance, :confirmable overwrites .active_for_authentication? to only return true if your model was confirmed.

You can overwrite this method yourself, but if you do, don’t forget to call super:

def active_for_authentication?
  super && special_condition_is_valid?
end

Whenever active_for_authentication? returns false, Kingsman asks the reason why your model is inactive using the inactive_message method. You can overwrite it as well:

def inactive_message
  special_condition_is_valid? ? super : :special_condition_is_not_valid
end

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

UNSAFE_ATTRIBUTES_FOR_SERIALIZATION =
[:encrypted_password, :reset_password_token, :reset_password_sent_at,
:remember_created_at, :sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip,
:last_sign_in_ip, :password_salt, :confirmation_token, :confirmed_at, :confirmation_sent_at,
:remember_token, :unconfirmed_email, :failed_attempts, :unlock_token, :locked_at]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.required_fields(klass) ⇒ Object



71
72
73
# File 'lib/kingsman/models/authenticatable.rb', line 71

def self.required_fields(klass)
  []
end

Instance Method Details

#active_for_authentication?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/kingsman/models/authenticatable.rb', line 89

def active_for_authentication?
  true
end

#authenticatable_saltObject



97
98
# File 'lib/kingsman/models/authenticatable.rb', line 97

def authenticatable_salt
end

#inactive_messageObject



93
94
95
# File 'lib/kingsman/models/authenticatable.rb', line 93

def inactive_message
  :inactive
end

#inspectObject

Redefine inspect using serializable_hash, to ensure we don’t accidentally leak passwords into exceptions.



120
121
122
123
124
125
# File 'lib/kingsman/models/authenticatable.rb', line 120

def inspect
  inspection = serializable_hash.collect do |k,v|
    "#{k}: #{respond_to?(:attribute_for_inspect) ? attribute_for_inspect(k) : v.inspect}"
  end
  "#<#{self.class} #{inspection.join(", ")}>"
end

#serializable_hash(options = nil) ⇒ Object

Redefine serializable_hash in models for more secure defaults. By default, it removes from the serializable model all attributes that are not accessible. You can remove this default by using :force_except and passing a new list of attributes you want to exempt. All attributes given to :except will simply add names to exempt to Kingsman internal list.



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/kingsman/models/authenticatable.rb', line 105

def serializable_hash(options = nil)
  options = options.try(:dup) || {}
  options[:except] = Array(options[:except]).dup

  if options[:force_except]
    options[:except].concat Array(options[:force_except])
  else
    options[:except].concat UNSAFE_ATTRIBUTES_FOR_SERIALIZATION
  end

  super(options)
end

#unauthenticated_messageObject



85
86
87
# File 'lib/kingsman/models/authenticatable.rb', line 85

def unauthenticated_message
  :invalid
end

#valid_for_authentication?Boolean

Check if the current object is valid for authentication. This method and find_for_authentication are the methods used in a Warden::Strategy to check if a model should be signed in or not.

However, you should not overwrite this method, you should overwrite active_for_authentication? and inactive_message instead.

Returns:

  • (Boolean)


81
82
83
# File 'lib/kingsman/models/authenticatable.rb', line 81

def valid_for_authentication?
  block_given? ? yield : true
end