Class: EY::ApiHMAC::ApiAuth::Server

Inherits:
LookupServer show all
Defined in:
lib/ey_api_hmac/api_auth.rb

Overview

Server middleware to validate requests

Initialize with a class that responds_to :find_by_auth_id, :id, :auth_key

Sets env to the id of the object returned by class.find_by_auth_id(auth_id)

Instance Method Summary collapse

Methods inherited from LookupServer

#call

Constructor Details

#initialize(app, klass) ⇒ Server

Returns a new instance of Server.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ey_api_hmac/api_auth.rb', line 37

def initialize(app, klass)
  unless klass.respond_to?(:find_by_auth_id)
    raise ArgumentError, "EY::ApiHMAC::ApiAuth::Server class must respond to find_by_auth_id"
  end

  lookup = Proc.new do |env, auth_id|
    if consumer = klass.find_by_auth_id(auth_id)
      env[CONSUMER] = consumer.id
      consumer.auth_key
    else
      raise HmacAuthFail
    end
  end

  super(app, &lookup)
end