Module: Rets::Authentication

Included in:
Client
Defined in:
lib/rets/authentication.rb

Overview

Adapted from dbrain’s Net::HTTP::DigestAuth gem, and RETS4R auth in order to support RETS’ usage of digest authentication.

Instance Method Summary collapse

Instance Method Details

#build_auth(digest_authenticate, uri, nc = 0, method = "POST") ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rets/authentication.rb', line 5

def build_auth(digest_authenticate, uri, nc = 0, method = "POST")
  user     = CGI.unescape uri.user
  password = CGI.unescape uri.password

  digest_authenticate =~ /^(\w+) (.*)/

  params = {}
  $2.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 }

  cnonce = Digest::MD5.hexdigest "%x" % (Time.now.to_i + rand(65535))

  digest = calculate_digest(
    user, password, params['realm'], params['nonce'], method, uri.request_uri, params['qop'], cnonce, nc
  )

  header = [
    %Q(Digest username="#{user}"),
    %Q(realm="#{params['realm']}"),
    %Q(qop="#{params['qop']}"),
    %Q(uri="#{uri.request_uri}"),
    %Q(nonce="#{params['nonce']}"),
    %Q(nc=#{('%08x' % nc)}),
    %Q(cnonce="#{cnonce}"),
    %Q(response="#{digest}"),
    %Q(opaque="#{params['opaque']}"),
  ]

  header.join(", ")
end

#build_user_agent_auth(*args) ⇒ Object



54
55
56
# File 'lib/rets/authentication.rb', line 54

def build_user_agent_auth(*args)
  %Q(Digest "#{calculate_user_agent_digest(*args)}")
end

#calculate_digest(user, password, realm, nonce, method, uri, qop, cnonce, nc) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/rets/authentication.rb', line 35

def calculate_digest(user, password, realm, nonce, method, uri, qop, cnonce, nc)
  a1 = Digest::MD5.hexdigest "#{user}:#{realm}:#{password}"
  a2 = Digest::MD5.hexdigest "#{method}:#{uri}"

  if qop
    Digest::MD5.hexdigest("#{a1}:#{nonce}:#{'%08x' % nc}:#{cnonce}:#{qop}:#{a2}")
  else
    Digest::MD5.hexdigest("#{a1}:#{nonce}:#{a2}")
  end
end

#calculate_user_agent_digest(user_agent, user_agent_password, session_id, version) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/rets/authentication.rb', line 46

def calculate_user_agent_digest(user_agent, user_agent_password, session_id, version)
  product, _ = user_agent.split("/")

  a1 = Digest::MD5.hexdigest "#{product}:#{user_agent_password}"

  Digest::MD5.hexdigest "#{a1}::#{session_id}:#{version}"
end