Class: Faraday::Request::DigestAuth

Inherits:
Middleware
  • Object
show all
Defined in:
lib/faraday/request/digestauth.rb

Overview

Public: A Faraday middleware to use digest authentication. Since order of middlewares do care, it should be the first one of the Request middlewares in order to work properly (due to how digest authentication works).

If some requests using the connection don't need to use digest auth you don't have to worry, the middleware will do nothing.

It uses Net::HTTP::DigestAuth to generate the authorization header but it should work with any adapter.

Examples:

connection = Faraday.new(...) do |connection|
connection.request :digest, USER, PASSWORD
end

# You can also use it later with a connection:
connection.digest_auth('USER', 'PASSWORD')

Instance Method Summary collapse

Constructor Details

#initialize(app, user, password, opts = {}) ⇒ DigestAuth

Public: Initializes a DigestAuth.

app - The Faraday app. user - A String with the user to authentication the connection. password - A String with the password to authentication the connection. opts - A hash with options - keep_body_on_handshake: if set to truthy, will also send the original request body



33
34
35
36
37
38
# File 'lib/faraday/request/digestauth.rb', line 33

def initialize(app, user, password, opts = {})
  super(app)
  @user = user
  @password = password
  @opts = opts
end

Instance Method Details

#call(env) ⇒ Object

Public: Sends a first request with an empty body to get the authentication headers and then send the same request with the body and authorization header.

env - A Hash with the request environment.

Returns a Faraday::Response.



47
48
49
50
51
52
53
54
55
56
# File 'lib/faraday/request/digestauth.rb', line 47

def call(env)
  response = handshake(env)
  return response unless response.status == 401
  unless response.headers['www-authenticate'] =~ /Digest +[^\s]+/
    return response
  end

  env[:request_headers]['Authorization'] = header(response)
  @app.call(env)
end