class Authie::SessionModel
Attributes
Public Class Methods
Source
# File lib/authie/session_model.rb, line 140 def cleanup Authie.notify(:cleanup) do # Invalidate transient sessions that haven't been used active.where('expires_at IS NULL AND last_activity_at < ?', Authie.config.session_inactivity_timeout.ago).each(&:invalidate!) # Invalidate persistent sessions that have expired active.where('expires_at IS NOT NULL AND expires_at < ?', Time.now).each(&:invalidate!) end true end
Cleanup any old sessions.
Source
# File lib/authie/session_model.rb, line 133 def find_session_by_token(token) return nil if token.blank? active.where(token_hash: hash_token(token)).first end
Find a session by a token (either from a hash or from the raw token)
Source
# File lib/authie/session_model.rb, line 152 def hash_token(token) Digest::SHA256.hexdigest(token) end
Return a hash of a given token
Public Instance Methods
Source
# File lib/authie/session_model.rb, line 60 def activate! self.active = true save! end
Source
# File lib/authie/session_model.rb, line 45 def expired? expires_at.present? && expires_at < Time.now end
Source
# File lib/authie/session_model.rb, line 98 def first_session_for_browser? self.class.where('id < ?', id).for_user(user).where(browser_id: browser_id).empty? end
Is this the first session for this session’s browser?
Source
# File lib/authie/session_model.rb, line 103 def first_session_for_ip? self.class.where('id < ?', id).for_user(user).where(login_ip: login_ip).empty? end
Is this the first session for the IP?
Source
# File lib/authie/session_model.rb, line 79 def get(key) (self.data ||= {})[key.to_s] end
Source
# File lib/authie/session_model.rb, line 50 def inactive? expires_at.nil? && last_activity_at.present? && last_activity_at < Authie.config.session_inactivity_timeout.ago end
Source
# File lib/authie/session_model.rb, line 65 def invalidate! active_now = active? self.active = false save! Authie.notify(:session_invalidate, session: self) if active_now true end
Source
# File lib/authie/session_model.rb, line 83 def invalidate_others! self.class.where('id != ?', id).active.for_user(user).each(&:invalidate!) end
Source
# File lib/authie/session_model.rb, line 88 def recently_seen_password? !!(password_seen_at && password_seen_at >= Authie.config.sudo_session_timeout.ago) end
Have we seen the user’s password recently in this sesion?
Source
# File lib/authie/session_model.rb, line 110 def reset_token set_new_token save! temporary_token end
Reset a new token for the session and return the new token
@return [String]
Source
# File lib/authie/session_model.rb, line 73 def set(key, value) self.data ||= {} self.data[key.to_s] = value save! end
Source
# File lib/authie/session_model.rb, line 93 def two_factored? !!(two_factored_at || parent_id) end
Is two factor authentication required for this request?
Source
# File lib/authie/session_model.rb, line 26 def user return unless user_id && user_type return @user if instance_variable_defined?('@user') @user = user_type.constantize.find_by(id: user_id) end
Return the user that
Source
# File lib/authie/session_model.rb, line 34 def user=(user) @user = user if user self.user_type = user.class.name self.user_id = user.id else self.user_type = nil self.user_id = nil end end
Set the user
Private Instance Methods
Source
# File lib/authie/session_model.rb, line 123 def set_new_token self.temporary_token = SecureRandom.alphanumeric(Authie.config.session_token_length) self.token_hash = self.class.hash_token(temporary_token) end
Source
# File lib/authie/session_model.rb, line 118 def shorten_strings self.user_agent = user_agent[0, 255] if user_agent.is_a?(String) self.last_activity_path = last_activity_path[0, 255] if last_activity_path.is_a?(String) end