Class: Jabber::SASL::DigestMD5
- Defined in:
- lib/vendor/xmpp4r/lib/xmpp4r/sasl.rb
Overview
SASL DIGEST-MD5 authentication helper (RFC2831)
Instance Method Summary collapse
-
#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.
-
- #decode_challenge(challenge) ⇒ Object
-
#initialize(stream) ⇒ DigestMD5
constructor
Sends the wished auth mechanism and wait for a challenge.
Constructor Details
#initialize(stream) ⇒ DigestMD5
Sends the wished auth mechanism and wait for a challenge
(proceed with DigestMD5#auth)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/vendor/xmpp4r/lib/xmpp4r/sasl.rb', line 99 def initialize(stream) super challenge = {} error = nil @stream.send(generate_auth('DIGEST-MD5')) { |reply| if reply.name == 'challenge' and reply.namespace == NS_SASL challenge = decode_challenge(reply.text) 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
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/vendor/xmpp4r/lib/xmpp4r/sasl.rb', line 165 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 } response_text = response.collect { |k,v| "#{k}=#{v}" }.join(',') Jabber::debuglog("SASL DIGEST-MD5 response:\n#{response_text}") r = REXML::Element.new('response') r.add_namespace NS_SASL r.text = Base64::encode64(response_text).gsub(/\s/, '') success_already = false error = nil @stream.send(r) { |reply| if reply.name == 'success' success_already = true elsif reply.name != 'challenge' error = reply.first_element(nil).name end true } return if success_already 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 |
#decode_challenge(challenge) ⇒ Object
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/vendor/xmpp4r/lib/xmpp4r/sasl.rb', line 118 def decode_challenge(challenge) text = Base64::decode64(challenge) res = {} state = :key key = '' value = '' text.scan(/./) do |ch| if state == :key if ch == '=' state = :value else key += ch end elsif state == :value if ch == ',' res[key] = value key = '' value = '' state = :key elsif ch == '"' and value == '' state = :quote else value += ch end elsif state == :quote if ch == '"' state = :value else value += ch end end end res[key] = value unless key == '' Jabber::debuglog("SASL DIGEST-MD5 challenge:\n#{text.inspect}\n#{res.inspect}") res end |