Class: Request::DigestAuth

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

Overview

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) ⇒ DigestAuth

Initializes a DigestAuth.

Parameters:

  • app

    The Faraday app.

  • user

    A String with the user to authentication the connection.

  • password

    A String with the password to authentication the connection.



31
32
33
34
# File 'lib/faraday/request/digest_authentication.rb', line 31

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

Instance Method Details

#call(env) ⇒ Object

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.

Parameters:

  • env

    A Hash with the request environment.

Returns:

  • a Faraday::Response.



43
44
45
46
47
48
49
# File 'lib/faraday/request/digest_authentication.rb', line 43

def call(env)
  response = handshake(env)
  return response unless response.status == 401

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

#handshake(env) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sends the the request with an empry body.

Parameters:

  • env

    A Hash with the request environment.

Returns:

  • a Faraday::Response.



57
58
59
60
61
62
# File 'lib/faraday/request/digest_authentication.rb', line 57

def handshake(env)
  env_without_body = env.dup
  env_without_body.delete(:body)

  @app.call(env_without_body)
end

#header(response) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds the authorization header with the authentication data.

Parameters:

  • response

    A Faraday::Response with the authenticate headers.

Returns:

  • a String with the DigestAuth header.



69
70
71
72
73
74
75
76
77
78
# File 'lib/faraday/request/digest_authentication.rb', line 69

def header(response)
  uri = response.env[:url]
  uri.user = @user
  uri.password = @password

  realm = response.headers['www-authenticate']
  method = response.env[:method].to_s.upcase

  Net::HTTP::DigestAuth.new.auth_header(uri, realm, method)
end