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"

Class Method Summary collapse

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)


56
57
58
59
60
# File 'lib/rack/simple_auth/hmac/middleware.rb', line 56

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

  yield @config if block_given?
end

Class Method Details

.method_missing(name, *args) ⇒ Object

Throw NoMethodError and give hint if method who was called is Rack::SimpleAuth::Middleware.call

Parameters:

  • name (Symbol)
  • args (Array)

Raises:

  • (NoMethodError)

    if the method isn’t defined and outputs additional hint for calling Rack::SimpleAuth::Middleware.call



41
42
43
44
45
46
47
48
# File 'lib/rack/simple_auth/hmac/middleware.rb', line 41

def self.method_missing(name, *args)
  msg = "Did you try to use HMAC Middleware as Rack Application via 'run'?\n" if name.eql?(:call)
  msg << "method: #{name}\n"
  msg << "args: #{args.inspect}\n" unless name.eql?(:call)
  msg << "on: #{self}"

  fail NoMethodError, msg
end

Instance Method Details

#call(env) ⇒ Object

call Method for Rack Middleware/Application

Parameters:

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


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rack/simple_auth/hmac/middleware.rb', line 67

def call(env)
  @request = Rack::Request.new(env)
  @allowed_messages = allowed_messages

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