Class: Requests::DigestAuth
- Inherits:
-
Object
- Object
- Requests::DigestAuth
- Includes:
- AuthBase
- Defined in:
- lib/requests/auth.rb
Instance Method Summary collapse
- #call(hdrs, meth: nil, url: nil, prev_resp: nil) ⇒ Object
-
#initialize(user, pass) ⇒ DigestAuth
constructor
A new instance of DigestAuth.
Constructor Details
#initialize(user, pass) ⇒ DigestAuth
Returns a new instance of DigestAuth.
34 35 36 37 |
# File 'lib/requests/auth.rb', line 34 def initialize(user, pass) @user = user @pass = pass end |
Instance Method Details
#call(hdrs, meth: nil, url: nil, prev_resp: nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/requests/auth.rb', line 38 def call(hdrs, meth: nil, url: nil, prev_resp: nil) return unless prev_resp wa = prev_resp.headers['www-authenticate'] return unless wa && wa =~ /\ADigest/ pr = parse_challenge(wa) realm = pr['realm'] nonce = pr['nonce'] qop = pr['qop'] path = URI.parse(url).request_uri ha1 = Digest::MD5.hexdigest("#{@user}:#{realm}:#{@pass}") ha2 = Digest::MD5.hexdigest("#{meth}:#{path}") if qop nc = '00000001' cnonce = SecureRandom.hex(8) response = Digest::MD5.hexdigest("#{ha1}:#{nonce}:#{nc}:#{cnonce}:#{qop}:#{ha2}") hdrs['Authorization'] = "Digest username=\"#{@user}\", realm=\"#{realm}\", nonce=\"#{nonce}\", " \ "uri=\"#{path}\", qop=#{qop}, nc=#{nc}, cnonce=\"#{cnonce}\", response=\"#{response}\"" else response = Digest::MD5.hexdigest("#{ha1}:#{nonce}:#{ha2}") hdrs['Authorization'] = "Digest username=\"#{@user}\", realm=\"#{realm}\", nonce=\"#{nonce}\", " \ "uri=\"#{path}\", response=\"#{response}\"" end end |