Class: Authentication::Logic::ControllerAdapters::RackAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/auth/logic/controller_adapters/rack_adapter.rb

Overview

Adapter for auth-logic to make it function as a Rack middleware. First you’ll have write your own Rack adapter where you have to set your cookie domain.

class YourRackAdapter < Authentication::Logic::ControllerAdapters::RackAdapter
  def cookie_domain
    'your_cookie_domain_here.com'
  end
end

Next you need to set up a rack middleware like this:

class Authentication::LogicMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    YourRackAdapter.new(env)
    @app.call(env)
  end
end

And that is all! Now just load this middleware into rack:

use Authentication::LogicMiddleware

Authentication::Logic will expect a User and a UserSession object to be present:

class UserSession < Authentication::Logic::Session::Base
  # Authentication::Logic options go here
end

class User < ApplicationRecord
  acts_as_authentic
end

Constant Summary

Constants inherited from AbstractAdapter

AbstractAdapter::ENV_SESSION_OPTIONS, AbstractAdapter::E_COOKIE_DOMAIN_ADAPTER

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#controller

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#authenticate_with_http_basic, #cookie_domain, #last_request_update_allowed?, #params, #renew_session_id, #request, #request_content_type, #respond_to_missing?, #responds_to_single_access_allowed?, #session, #single_access_allowed?

Constructor Details

#initialize(env) ⇒ RackAdapter

Returns a new instance of RackAdapter.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/auth/logic/controller_adapters/rack_adapter.rb', line 43

def initialize(env)
  # We use the Rack::Request object as the controller object.
  # For this to work, we have to add some glue.
  request = Rack::Request.new(env)

  request.instance_eval do
    def request
      self
    end

    def remote_ip
      ip
    end
  end

  super(request)
  Authentication::Logic::Session::Base.controller = self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Authentication::Logic::ControllerAdapters::AbstractAdapter

Instance Method Details

#cookiesObject

Rack Requests stores cookies with not just the value, but also with flags and expire information in the hash. Authentication::Logic does not like this, so we drop everything except the cookie value.



65
66
67
68
69
70
# File 'lib/auth/logic/controller_adapters/rack_adapter.rb', line 65

def cookies
  controller
    .cookies
    .map { |key, value_hash| { key => value_hash[:value] } }
    .inject(:merge) || {}
end