Class: Authie::ControllerDelegate

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

Overview

The controller delegate implements methods that can be used by a controller. These are then extended into controllers as needed (see ControllerExtension).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Authie::ControllerDelegate

Parameters:

  • controller (ActionController::Base)


16
17
18
19
# File 'lib/authie/controller_delegate.rb', line 16

def initialize(controller)
  @controller = controller
  @touch_auth_session_enabled = true
end

Instance Attribute Details

#touch_auth_session_enabledObject

Returns the value of attribute touch_auth_session_enabled.



12
13
14
# File 'lib/authie/controller_delegate.rb', line 12

def touch_auth_session_enabled
  @touch_auth_session_enabled
end

Instance Method Details

#auth_sessionAuthie::Session

Return an auth session that has been found in the current cookies.

Returns:



110
111
112
113
114
# File 'lib/authie/controller_delegate.rb', line 110

def auth_session
  return @auth_session if instance_variable_defined?('@auth_session')

  @auth_session = Authie::Session.get_session(@controller)
end

#create_auth_session(user, **kwargs) ⇒ Authie::Session?

Create a new session for the given user. If nil is provided as a user, the existing session will be invalidated.

Returns:



76
77
78
79
80
81
82
83
# File 'lib/authie/controller_delegate.rb', line 76

def create_auth_session(user, **kwargs)
  if user.nil?
    invalidate_auth_session
    return nil
  end

  @auth_session = Authie::Session.start(@controller, user: user, **kwargs)
end

#current_userActiveRecord::Base?

Return the user for the currently logged in user or nil if no user is logged in

Returns:

  • (ActiveRecord::Base, nil)


66
67
68
69
70
# File 'lib/authie/controller_delegate.rb', line 66

def current_user
  return nil unless logged_in?

  auth_session.session.user
end

#invalidate_auth_sessionBoolean

Invalidate the existing auth session if one exists. Return true if a sesion has been invalidated otherwise return false.

Returns:

  • (Boolean)


89
90
91
92
93
94
95
# File 'lib/authie/controller_delegate.rb', line 89

def invalidate_auth_session
  return false unless logged_in?

  auth_session.invalidate
  @auth_session = nil
  true
end

#logged_in?Boolean

Is anyone currently logged in? Return true if there is an auth session present.

Note: this does not check the validatity of the session. You must always ensure that the ‘validate` or `touch` method is invoked to ensure that the session that has been found is active.

Returns:

  • (Boolean)


103
104
105
# File 'lib/authie/controller_delegate.rb', line 103

def logged_in?
  auth_session.is_a?(Session)
end

#set_browser_idString

Sets a browser ID. This must be performed on any page request where AUthie will be used. It should be triggered before any other Authie provided methods. This will ensure that the given browser ID is unique.

Returns:

  • (String)

    the generated browser ID



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/authie/controller_delegate.rb', line 26

def set_browser_id
  until cookies[Authie.config.browser_id_cookie_name]
    proposed_browser_id = SecureRandom.uuid
    next if Authie::SessionModel.where(browser_id: proposed_browser_id).exists?

    cookies[Authie.config.browser_id_cookie_name] = {
      value: proposed_browser_id,
      expires: 5.years.from_now,
      httponly: true,
      secure: @controller.request.ssl?
    }
    Authie.notify(:set_browser_id,
                  browser_id: proposed_browser_id,
                  controller: @controller)
  end
  proposed_browser_id
end

#touch_auth_sessionAuthie::Session, false

Touch the session to update details on the latest activity.

Returns:



57
58
59
60
61
# File 'lib/authie/controller_delegate.rb', line 57

def touch_auth_session
  yield if block_given?
ensure
  auth_session.touch if @touch_auth_session_enabled && logged_in?
end

#validate_auth_sessionAuthie::Session, false

Validate the auth session to ensure that it is current validate and raise an error if it is not suitable for use.

Returns:



48
49
50
51
52
# File 'lib/authie/controller_delegate.rb', line 48

def validate_auth_session
  return false unless logged_in?

  auth_session.validate
end