Class: BasicAuth::Middleware

Inherits:
Rack::Auth::Basic
  • Object
show all
Defined in:
lib/basic_auth/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Override initialize to allow middleware options. IE:

use BasicAuth::Middleware, passwordfile: "htpasswd.txt"


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

def initialize(app, options={})
  @app, @options = app, options
  @passwordfile = options[:passwordfile] || "config/htpasswd"
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/basic_auth/middleware.rb', line 14

def call(env)
  path = Rack::Request.new(env).path
  matcher = Matcher.new(path, @options)
  return @app.call(env) unless matcher.match? # passthrough if route doesnt match

  htpasswd = Htpasswd.new(@passwordfile)
  authorized = htpasswd.call(env)
  if authorized
    @app.call(env)
  else
    unauthorized # from Rack::Auth::Basic
  end
end