Class: Polar::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/polar/authentication.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, api_key, secret_key, &failed_handler) ⇒ Authentication

Returns a new instance of Authentication.



7
8
9
10
11
12
13
14
# File 'lib/polar/authentication.rb', line 7

def initialize(app, api_key, secret_key, &failed_handler)
  @app = app
  @api_key = api_key
  @secret_key = secret_key
  @signature_calculator = SignatureCalculator.new(@secret_key)
  @required_keys = %w{user session_key ss expires}.collect { |e| @api_key + "_" + e } << @api_key
  @failed_handler = block_given? ? failed_handler : proc { [401, {"Content-Type" => "text/plain"}, ["Unauthorized!"]] }
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/polar/authentication.rb', line 16

def call(env)
  request = Rack::Request.new(env)
  if %r{^/people/(?<person_id>\d+)} =~ request.path_info
    cookies = request.cookies
    if valid?(cookies) && cookies["#{@api_key}_user"] == person_id
      @app.call(env)
    else
      @failed_handler.call(env)
    end
  else
    @app.call(env)
  end
end