Class: Rack::SimpleAuth::HMAC::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/simple_auth/hmac/middleware.rb

Overview

Middleware class which represents the interface to the rack api via #call and checks if a request is hmac authorized.

Examples:

Basic Usage

"request_config = {
   'GET' => 'path',
   'POST' => 'params',
   'DELETE' => 'path',
   'PUT' => 'path',
   'PATCH' => 'path'
 }

 use Rack::SimpleAuth::HMAC::Middleware do |options|
   options.tolerance = 1500

   options.secret = 'test_secret'
   options.signature = 'test_signature'

   options.logpath = "#{File.expand_path('..', __FILE__)}/logs"
   options.request_config = request_config

   options.verbose = true
 end

 run Rack::Lobster.new"

Instance Method Summary collapse

Constructor Details

#initialize(app, &block) {|@config| ... } ⇒ Middleware

Constructor for Rack Middleware (passing the rack stack)

Parameters:

  • app (Rack Application)
    next middleware or rack app which gets called
  • block (Proc)
    the dsl block which will be yielded into the config object

Yields:

  • (@config)


39
40
41
42
43
# File 'lib/rack/simple_auth/hmac/middleware.rb', line 39

def initialize(app, &block)
  @app, @config = app, Config.new

  yield @config if block_given?
end

Instance Method Details

#call(env) ⇒ Object

Rack API Interface Method

Parameters:

  • env (Hash)
    Rack Env Hash which contains headers etc..


50
51
52
# File 'lib/rack/simple_auth/hmac/middleware.rb', line 50

def call(env)
  self.dup.call!(env)
end

#call!(env) ⇒ Object

call! Method

Using ! because this method isn’t a pure function Creating for example @request & @allowed_messages instance variables

Also this is a threadsafe approach for rack

Parameters:

  • env (Hash)
    Rack Env Hash which contains headers etc..


64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rack/simple_auth/hmac/middleware.rb', line 64

def call!(env)
  env = env.dup
  @request = Request.new(env, @config)

  if @request.valid?
    @app.call(env)
  else
    response = Response.new('Unauthorized', 401, 'Content-Type' => 'text/html')
    response.finish
  end
end