Class: RETS4R::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/rets4r/auth.rb

Defined Under Namespace

Classes: ResponseHeader

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nc = 1) ⇒ Auth

Returns a new instance of Auth.


8
9
10
11
# File 'lib/rets4r/auth.rb', line 8

def initialize(nc = 1)
  self.nc = nc
  self.current_strategy = :none
end

Instance Attribute Details

#current_strategyObject

Returns the value of attribute current_strategy


5
6
7
# File 'lib/rets4r/auth.rb', line 5

def current_strategy
  @current_strategy
end

#methodObject

Returns the value of attribute method


5
6
7
# File 'lib/rets4r/auth.rb', line 5

def method
  @method
end

#ncObject

Returns the value of attribute nc


5
6
7
# File 'lib/rets4r/auth.rb', line 5

def nc
  @nc
end

#nonceObject

Returns the value of attribute nonce


5
6
7
# File 'lib/rets4r/auth.rb', line 5

def nonce
  @nonce
end

#opaqueObject

Returns the value of attribute opaque


5
6
7
# File 'lib/rets4r/auth.rb', line 5

def opaque
  @opaque
end

#passwordObject

Returns the value of attribute password


5
6
7
# File 'lib/rets4r/auth.rb', line 5

def password
  @password
end

#qopObject

Returns the value of attribute qop


5
6
7
# File 'lib/rets4r/auth.rb', line 5

def qop
  @qop
end

#realmObject

Returns the value of attribute realm


5
6
7
# File 'lib/rets4r/auth.rb', line 5

def realm
  @realm
end

#request_idObject

Returns the value of attribute request_id


5
6
7
# File 'lib/rets4r/auth.rb', line 5

def request_id
  @request_id
end

#uriObject

Returns the value of attribute uri


5
6
7
# File 'lib/rets4r/auth.rb', line 5

def uri
  @uri
end

#useragentObject

Returns the value of attribute useragent


5
6
7
# File 'lib/rets4r/auth.rb', line 5

def useragent
  @useragent
end

#usernameObject

Returns the value of attribute username


5
6
7
# File 'lib/rets4r/auth.rb', line 5

def username
  @username
end

Class Method Details

.authenticate(response, username, password, uri, method, requestId, useragent, nc = 0) ⇒ Object

:nodoc:


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rets4r/auth.rb', line 46

def self.authenticate(response, username, password, uri, method, requestId, useragent, nc = 0) # :nodoc:
  warn "#{caller.first}: warning: #{self.class}::authenticate is deprecated and will be removed by rets4r 2.0; use a RETS4R::Auth object instead"
  auth = new.tap do |a|
    a.username = username
    a.password = password
    a.uri = uri
    a.method = method
    a.request_id = requestId
    a.useragent = useragent
    a.nc = nc
  end

  auth.update_with_response response

  auth.to_s
end

.calculate_digest(username, password, realm, nonce, method, uri, qop = false, cnonce = false, nc = 0) ⇒ Object

:nodoc:


63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rets4r/auth.rb', line 63

def self.calculate_digest(username, password, realm, nonce, method, uri, qop = false, cnonce = false, nc = 0) # :nodoc:
  warn "#{caller.first}: warning: #{self.class}::authenticate is deprecated and will be removed by rets4r 2.0; use a RETS4R::Auth object instead"
  text = [hA1(username, realm, password), nonce]
  if (qop)
    throw ArgumentException, 'qop requires a cnonce to be provided.' unless cnonce

    text << '%08x' % nc << cnonce << qop
  end
  text << hA2(method, uri)

  return Digest::MD5.hexdigest text.join(':')
end

.cnonce(useragent, password, requestId, nonce) ⇒ Object

:nodoc:


76
77
78
79
# File 'lib/rets4r/auth.rb', line 76

def self.cnonce(useragent, password, requestId, nonce) # :nodoc:
  warn "#{caller.first}: warning: #{self.class}::cnonce is deprecated and will be removed by rets4r 2.0; use a RETS4R::Auth object instead"
  Digest::MD5.hexdigest("#{useragent}:#{password}:#{requestId}:#{nonce}")
end

.hA1(username, realm, password) ⇒ Object

:nodoc:


90
91
92
# File 'lib/rets4r/auth.rb', line 90

def self.hA1(username, realm, password) # :nodoc:
  Digest::MD5.hexdigest("#{username}:#{realm}:#{password}")
end

.hA2(method, uri) ⇒ Object

:nodoc:


86
87
88
# File 'lib/rets4r/auth.rb', line 86

def self.hA2(method, uri) # :nodoc:
  Digest::MD5.hexdigest("#{method}:#{uri}")
end

.parse_header(header) ⇒ Object

:nodoc:


81
82
83
84
# File 'lib/rets4r/auth.rb', line 81

def self.parse_header(header) # :nodoc:
  warn "#{caller.first}: warning: #{self.class}::parse_header is deprecated and will be removed by rets4r 2.0; use RETS4R::Auth::ResponseHeader.parse instead"
  RETS4R::Auth::ResponseHeader.parse(header).to_h
end

.request_idObject

:nodoc:


94
95
96
# File 'lib/rets4r/auth.rb', line 94

def self.request_id # :nodoc:
  Digest::MD5.hexdigest(Time.new.to_f.to_s)
end

Instance Method Details

#to_sObject


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

def to_s
  case current_strategy
  when :none
    nil
  when :basic
    to_basic_header
  when :digest
    to_digest_header
  end
end

#update(uri, method, requestId) ⇒ Object


13
14
15
16
17
18
# File 'lib/rets4r/auth.rb', line 13

def update(uri, method, requestId)
  self.uri = uri
  self.method = method
  self.request_id = requestId || self.class.request_id
  self.nc += 1
end

#update_with_response(response) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rets4r/auth.rb', line 20

def update_with_response(response)
  if response['www-authenticate'].nil? || response['www-authenticate'].empty?
    self.current_strategy = :none
    warn "Missing required header 'www-authenticate'. Got: #{response}"
  else
    header = Auth::ResponseHeader.parse(response['www-authenticate'])
    self.current_strategy = header.strategy
    self.realm = header.realm
    self.nonce = header.nonce
    self.qop = header.qop
    self.opaque = header.opaque
  end
  self
end