Class: Rack::Client::Auth::Basic

Inherits:
Object
  • Object
show all
Includes:
DualBand
Defined in:
lib/rack/client/middleware/auth/basic.rb

Defined Under Namespace

Classes: Challenge

Instance Method Summary collapse

Methods included from DualBand

#call

Constructor Details

#initialize(app, username, password, force = false) ⇒ Basic

Returns a new instance of Basic.



7
8
9
# File 'lib/rack/client/middleware/auth/basic.rb', line 7

def initialize(app, username, password, force = false)
  @app, @username, @password, @force = app, username, password, force
end

Instance Method Details

#async_call(env, &b) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rack/client/middleware/auth/basic.rb', line 25

def async_call(env, &b)
  return authorized_call(env, &b) if @force

  @app.call(env) do |response_parts|
    request   = Rack::Request.new(env)
    response  = Response.new(*response_parts)
    challenge = Basic::Challenge.new(request, response)

    if challenge.required? && (challenge.unspecified? || challenge.basic?)
      authorized_call(env, &b)
    else
      yield response.finish
    end
  end
end

#auth_headerObject



45
46
47
# File 'lib/rack/client/middleware/auth/basic.rb', line 45

def auth_header
  {'HTTP_AUTHORIZATION' => "Basic #{}"}
end

#authorized_call(env, &b) ⇒ Object



41
42
43
# File 'lib/rack/client/middleware/auth/basic.rb', line 41

def authorized_call(env, &b)
  @app.call(env.merge(auth_header), &b)
end

#encoded_loginObject



49
50
51
# File 'lib/rack/client/middleware/auth/basic.rb', line 49

def 
  ["#{@username}:#{@password}"].pack("m*").chomp
end

#sync_call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rack/client/middleware/auth/basic.rb', line 11

def sync_call(env)
  return authorized_call(env) if @force

  request   = Rack::Request.new(env)
  response  = Response.new(*@app.call(env))
  challenge = Basic::Challenge.new(request, response)

  if challenge.required? && (challenge.unspecified? || challenge.basic?)
    return authorized_call(env)
  end

  response.finish
end