Class: Rack::Bacoo::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-bacoo.rb

Constant Summary collapse

DEFAULTS =
{
  cookie_attributes: {},
  password_cost: BCrypt::Engine.cost,
  paths: [Regexp.new(".*")],
  realm: nil
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, config) ⇒ Middleware

Returns a new instance of Middleware.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rack-bacoo.rb', line 23

def initialize(app, config)
  config = DEFAULTS.merge(config)

  @app = app
  @paths, @realm = config.fetch_values(:paths, :realm)
  @authenticator = Authenticator.new(
    cookie_attributes: CookieAttributesParser.call(config.fetch(:cookie_attributes)),
    cookie_encryptor: Encryptor.new(config.fetch(:encrypt_cookie_with)),
    password_cost: config.fetch(:password_cost),
    users: config.fetch(:users)
  )
end

Instance Attribute Details

#realmObject

Returns the value of attribute realm.



21
22
23
# File 'lib/rack-bacoo.rb', line 21

def realm
  @realm
end

Instance Method Details

#call(env) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rack-bacoo.rb', line 36

def call(env)
  @authenticator.with_env(env) do
    actual_path = Utils.clean_path_info(Utils.unescape_path(env["PATH_INFO"]))
    return @app.call(env) if @paths.none? { _1.match? actual_path }
    @authenticator.cookie_authenticated { |credentials| return call_app(credentials, env) }
    return unauthorized unless @authenticator.basic_auth_provided?
    return bad_request unless @authenticator.valid_basic_auth?
    @authenticator.basic_authenticated { |credentials| return call_app(credentials, env) }

    unauthorized
  end
end