Class: SASL::DigestMD5

Inherits:
Mechanism show all
Defined in:
lib/sasl/digest_md5.rb

Overview

Instance Attribute Summary collapse

Attributes inherited from Mechanism

#mechanism, #preferences

Instance Method Summary collapse

Methods inherited from Mechanism

#failure?, #success?

Constructor Details

#initialize(*a) ⇒ DigestMD5

Returns a new instance of DigestMD5.



10
11
12
13
# File 'lib/sasl/digest_md5.rb', line 10

def initialize(*a)
  super
  @nonce_count = 0
end

Instance Attribute Details

#cnonce=(value) ⇒ Object (writeonly)

Sets the attribute cnonce

Parameters:

  • value

    the value to set the attribute cnonce to.



8
9
10
# File 'lib/sasl/digest_md5.rb', line 8

def cnonce=(value)
  @cnonce = value
end

Instance Method Details

#receive(message_name, content) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
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
63
64
65
66
# File 'lib/sasl/digest_md5.rb', line 25

def receive(message_name, content)
  if message_name == 'challenge'
    c = decode_challenge(content)

    unless c['rspauth']
      response = {}
      if defined?(@nonce) && response['nonce'].nil?
        # Could be reauth
      else
        # No reauth:
        @nonce_count = 0
      end
      @nonce ||= c['nonce']
      response['nonce'] = @nonce
      response['charset'] = 'utf-8'
      response['username'] = preferences.username
      response['realm'] = c['realm'] || preferences.realm
      @cnonce = generate_nonce unless defined? @cnonce
      response['cnonce'] = @cnonce
      @nc = next_nc
      response['nc'] = @nc
      @qop = c['qop'] || 'auth'
      response['qop'] = @qop
      response['digest-uri'] = preferences.digest_uri
      response['response'] = response_value(response['nonce'], response['nc'], response['cnonce'], response['qop'])
      ['response', encode_response(response)]
    else
      rspauth_expected = response_value(@nonce, @nc, @cnonce, @qop, '')
      p :rspauth_received=>c['rspauth'], :rspauth_expected=>rspauth_expected
      if c['rspauth'] == rspauth_expected
        ['response', nil]
      else
        # Bogus server?
        @state = :failure
        ['failure', nil]
      end
    end
  else
    # No challenge? Might be success or failure
    super
  end
end

#startObject



15
16
17
18
19
20
21
22
23
# File 'lib/sasl/digest_md5.rb', line 15

def start
  @state = nil
  unless defined? @nonce
    ['auth', nil]
  else
    # reauthentication
    receive('challenge', '')
  end
end