Class: Authie::SessionModel
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Authie::SessionModel
- Defined in:
- lib/authie/session_model.rb
Instance Attribute Summary collapse
-
#temporary_token ⇒ Object
Returns the value of attribute temporary_token.
Class Method Summary collapse
-
.cleanup ⇒ Object
Cleanup any old sessions.
-
.find_session_by_token(token) ⇒ Object
Find a session by a token (either from a hash or from the raw token).
-
.hash_token(token) ⇒ Object
Return a hash of a given token.
Instance Method Summary collapse
- #activate! ⇒ Object
- #expired? ⇒ Boolean
-
#first_session_for_browser? ⇒ Boolean
Is this the first session for this session’s browser?.
-
#first_session_for_ip? ⇒ Boolean
Is this the first session for the IP?.
- #get(key) ⇒ Object
- #inactive? ⇒ Boolean
- #invalidate! ⇒ Object
- #invalidate_others! ⇒ Object
- #persistent? ⇒ Boolean
-
#recently_seen_password? ⇒ Boolean
Have we seen the user’s password recently in this sesion?.
-
#reset_token ⇒ String
Reset a new token for the session and return the new token.
- #set(key, value) ⇒ Object
-
#two_factored? ⇒ Boolean
Is two factor authentication required for this request?.
-
#user ⇒ Object
Return the user that.
-
#user=(user) ⇒ Object
Set the user.
Instance Attribute Details
#temporary_token ⇒ Object
Returns the value of attribute temporary_token.
9 10 11 |
# File 'lib/authie/session_model.rb', line 9 def temporary_token @temporary_token end |
Class Method Details
.cleanup ⇒ Object
Cleanup any old sessions.
140 141 142 143 144 145 146 147 148 149 |
# 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 |
.find_session_by_token(token) ⇒ Object
Find a session by a token (either from a hash or from the raw token)
133 134 135 136 137 |
# 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 |
.hash_token(token) ⇒ Object
Return a hash of a given token
152 153 154 |
# File 'lib/authie/session_model.rb', line 152 def hash_token(token) Digest::SHA256.hexdigest(token) end |
Instance Method Details
#activate! ⇒ Object
60 61 62 63 |
# File 'lib/authie/session_model.rb', line 60 def activate! self.active = true save! end |
#expired? ⇒ Boolean
45 46 47 48 |
# File 'lib/authie/session_model.rb', line 45 def expired? expires_at.present? && expires_at < Time.now end |
#first_session_for_browser? ⇒ Boolean
Is this the first session for this session’s browser?
98 99 100 |
# 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 |
#first_session_for_ip? ⇒ Boolean
Is this the first session for the IP?
103 104 105 |
# 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 |
#get(key) ⇒ Object
79 80 81 |
# File 'lib/authie/session_model.rb', line 79 def get(key) (self.data ||= {})[key.to_s] end |
#inactive? ⇒ Boolean
50 51 52 53 54 |
# 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 |
#invalidate! ⇒ Object
65 66 67 68 69 70 71 |
# 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 |
#invalidate_others! ⇒ Object
83 84 85 |
# File 'lib/authie/session_model.rb', line 83 def invalidate_others! self.class.where('id != ?', id).active.for_user(user).each(&:invalidate!) end |
#persistent? ⇒ Boolean
56 57 58 |
# File 'lib/authie/session_model.rb', line 56 def persistent? !!expires_at end |
#recently_seen_password? ⇒ Boolean
Have we seen the user’s password recently in this sesion?
88 89 90 |
# 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 |
#reset_token ⇒ String
Reset a new token for the session and return the new token
110 111 112 113 114 |
# File 'lib/authie/session_model.rb', line 110 def reset_token set_new_token save! temporary_token end |
#set(key, value) ⇒ Object
73 74 75 76 77 |
# File 'lib/authie/session_model.rb', line 73 def set(key, value) self.data ||= {} self.data[key.to_s] = value save! end |
#two_factored? ⇒ Boolean
Is two factor authentication required for this request?
93 94 95 |
# File 'lib/authie/session_model.rb', line 93 def two_factored? !!(two_factored_at || parent_id) end |
#user ⇒ Object
Return the user that
26 27 28 29 30 31 |
# 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 |
#user=(user) ⇒ Object
Set the user
34 35 36 37 38 39 40 41 42 43 |
# 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 |