Class: Jabber::SASL::DigestMD5

Inherits:
Base
  • Object
show all
Defined in:
lib/xmpp4r/sasl.rb

Overview

SASL DIGEST-MD5 authentication helper (RFC2831)

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ DigestMD5

Sends the wished auth mechanism and wait for a challenge

(proceed with DigestMD5#auth)



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/xmpp4r/sasl.rb', line 74

def initialize(stream)
  super

  challenge = {}
  error = nil
  @stream.send(generate_auth('DIGEST-MD5')) { |reply|
    if reply.name == 'challenge' and reply.namespace == NS_SASL
      challenge_text = Base64::decode64(reply.text)
      challenge_text.split(/,/).each { |s|
        key, value = s.split(/=/, 2)
        value.sub!(/^"/, '')
        value.sub!(/"$/, '')
        challenge[key] = value
      }
    else
      error = reply.first_element(nil).name
    end
    true
  }
  raise error if error

  @nonce = challenge['nonce']
  @realm = challenge['realm']
end

Instance Method Details

#auth(password) ⇒ Object

  • Send a response

  • Wait for the server’s challenge (which aren’t checked)

  • Send a blind response to the server’s challenge



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/xmpp4r/sasl.rb', line 103

def auth(password)
  response = {}
  response['nonce'] = @nonce
  response['charset'] = 'utf-8'
  response['username'] = @stream.jid.node
  response['realm'] = @realm || @stream.jid.domain
  response['cnonce'] = generate_nonce
  response['nc'] = '00000001'
  response['qop'] = 'auth'
  response['digest-uri'] = "xmpp/#{@stream.jid.domain}"
  response['response'] = response_value(@stream.jid.node, @stream.jid.domain, response['digest-uri'], password, @nonce, response['cnonce'], response['qop'])
  response.each { |key,value|
    unless %w(nc qop response charset).include? key
      response[key] = "\"#{value}\""
    end
  }

  r = REXML::Element.new('response')
  r.add_namespace NS_SASL
  r.text = Base64::encode64(response.collect { |k,v| "#{k}=#{v}" }.join(',')).gsub(/\s/, '')
  error = nil
  @stream.send(r) { |reply|
    if reply.name != 'challenge'
      error = reply.first_element(nil).name
    end
    true
  }
  
  raise error if error

  # TODO: check the challenge from the server

  r.text = nil
  @stream.send(r) { |reply|
    if reply.name != 'success'
      error = reply.first_element(nil).name
    end
    true
  }
  
  raise error if error
end