Class: Authie::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/authie/session.rb

Defined Under Namespace

Classes: BrowserMismatch, ExpiredSession, HostMismatch, InactiveSession, ValidityError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, session) ⇒ Authie::Session

Initialize a new session object

Parameters:

  • controller (ActionController::Base)

    any controller

  • session (Authie::SessionModel)

    an Authie session model instance



43
44
45
46
# File 'lib/authie/session.rb', line 43

def initialize(controller, session)
  @controller = controller
  @session = session
end

Instance Attribute Details

#sessionAuthie::SessionModel (readonly)

The underlying session model instance



13
14
15
# File 'lib/authie/session.rb', line 13

def session
  @session
end

Class Method Details

.get_session(controller) ⇒ Authie::Session

Lookup a session for a given controller and return the session object.

Parameters:

  • controller (ActionController::Base)

Returns:



267
268
269
270
271
272
273
274
275
276
# File 'lib/authie/session.rb', line 267

def get_session(controller)
  cookies = controller.send(:cookies)
  return nil if cookies[:user_session].blank?

  session = SessionModel.find_session_by_token(cookies[:user_session])
  return nil if session.blank?

  session.temporary_token = cookies[:user_session]
  new(controller, session)
end

.start(controller, user:, persistent: false, see_password: false, **params) ⇒ Authie::Session

Create a new session within the given controller for the

Parameters:

  • controller (ActionController::Base)
  • user (ActiveRecord::Base)

    user

  • persistent (Boolean) (defaults to: false)

    create a persistent session

Returns:



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/authie/session.rb', line 243

def start(controller, user:, persistent: false, see_password: false, **params)
  cookies = controller.send(:cookies)
  SessionModel.active.where(browser_id: cookies[:browser_id]).each(&:invalidate!)

  session = SessionModel.new(params)
  session.user = user
  session.browser_id = cookies[:browser_id]
  session. = Time.now
  session. = controller.request.ip
  session. = Authie.config.lookup_ip_country(session.)
  session.host = controller.request.host
  session.user_agent = controller.request.user_agent
  session.expires_at = Time.now + Authie.config.persistent_session_length if persistent
  session.password_seen_at = Time.now if see_password
  session.save!

  new(controller, session).start
end

Instance Method Details

#invalidateAuthie::Session Also known as: invalidate!

Invalidates the current session by marking it inactive and removing the current cookie.

Returns:



80
81
82
83
84
# File 'lib/authie/session.rb', line 80

def invalidate
  @session.invalidate!
  cookies.delete(:user_session)
  self
end

#mark_as_two_factored(skip: nil) ⇒ Authie::Session Also known as: mark_as_two_factored!

Mark this request as two factored by setting the time and the current IP address.

Returns:



128
129
130
131
132
133
134
135
136
# File 'lib/authie/session.rb', line 128

def mark_as_two_factored(skip: nil)
  @session.two_factored_at = Time.now
  @session.two_factored_ip = @controller.request.ip
  @session.two_factored_ip_country = Authie.config.lookup_ip_country(@controller.request.ip)
  @session.skip_two_factor = skip unless skip.nil?
  @session.save!
  Authie.notify(:mark_as_two_factor, session: self)
  self
end

#persistAuthie::Session Also known as: persist!

Mark the current session as persistent. Will set the expiry time of the underlying session and update the cookie.

Returns:



69
70
71
72
73
74
# File 'lib/authie/session.rb', line 69

def persist
  @session.expires_at = Authie.config.persistent_session_length.from_now
  @session.save!
  set_cookie
  self
end

#reset_tokenAuthie::Session

Resets the token for the currently active session to a new string

Returns:



152
153
154
155
156
# File 'lib/authie/session.rb', line 152

def reset_token
  @session.reset_token
  set_cookie
  self
end

#see_passwordAuthie::Session Also known as: see_password!

Mark the session’s password as seen at the current time

Returns:



116
117
118
119
120
121
# File 'lib/authie/session.rb', line 116

def see_password
  @session.password_seen_at = Time.now
  @session.save!
  Authie.notify(:see_password, session: self)
  self
end

#startAuthie::Session

Starts a new session by setting the cookie. This should be invoked whenever a new session begins. It usually does not need to be called directly as it will be taken care of by the class-level start method.

Returns:



143
144
145
146
147
# File 'lib/authie/session.rb', line 143

def start
  set_cookie
  Authie.notify(:session_start, session: self)
  self
end

#touchAuthie::Session Also known as: touch!

Touches the current session to ensure it is currently valid and to update attributes which should be updatd on each request. This will raise the same errors as the #validate method. It will set the last activity time, IP and path as well as incrementing the request counter.

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/authie/session.rb', line 97

def touch
  @session.last_activity_at = Time.now
  if @controller.request.ip != @session.last_activity_ip
    @session.last_activity_ip_country = Authie.config.lookup_ip_country(@controller.request.ip)
  end
  @session.last_activity_ip = @controller.request.ip

  @session.last_activity_path = @controller.request.path
  @session.requests += 1
  extend_session_expiry_if_appropriate
  @session.save!
  Authie.notify(:touch, session: self)
  self
end

#validateAuthie::Session Also known as: check_security!

Validate that the session is valid and raise and error if not

Returns:



55
56
57
58
59
60
61
62
# File 'lib/authie/session.rb', line 55

def validate
  validate_browser_id
  validate_active
  validate_expiry
  validate_inactivity
  validate_host
  self
end