Class: Clearance::HttpAuth::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/clearance_http_auth/middleware.rb

Overview

A Rack middleware which intercepts requests to your application API (as defined in Configuration.api_formats) and performs a HTTP Basic Authentication via Rack::Auth::Basic.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



10
11
12
# File 'lib/clearance_http_auth/middleware.rb', line 10

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Wrap the application with a Rack::Auth::Basic block and set the env['clearance.current_user'] variable if the incoming request is targeting the API.



18
19
20
21
22
23
24
25
# File 'lib/clearance_http_auth/middleware.rb', line 18

def call(env)
  if targeting_api?(env)
    @app = Rack::Auth::Basic.new(@app) do |username, password|
      env['clearance.current_user'] = ::User.authenticate(username, password)
    end
  end
  @app.call(env)
end