Class: AgentXmpp::Xmpp::SASL::DigestMD5
- Defined in:
- lib/agent_xmpp/xmpp/sasl.rb
Overview
Instance Method Summary collapse
-
#auth(password) ⇒ Object
.….….….….….….….….….….….….….….….….….….….….….….….….….
-
#decode_challenge(challenge) ⇒ Object
.….….….….….….….….….….….….….….….….….….….….….….….….….
-
#initialize(stream) ⇒ DigestMD5
constructor
.….….….….….….….….….….….….….….….….….….….….….….….….….
Constructor Details
#initialize(stream) ⇒ DigestMD5
.….….….….….….….….….….….….….….….….….….….….….….….….….
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/agent_xmpp/xmpp/sasl.rb', line 102 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
.….….….….….….….….….….….….….….….….….….….….….….….….….
162 163 164 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 |
# File 'lib/agent_xmpp/xmpp/sasl.rb', line 162 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['authzid']) response.each do |key,value| unless %w(nc qop response charset).include? key response[key] = "\"#{value}\"" end end response_text = response.collect { |k,v| "#{k}=#{v}" }.join(',') 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) do |reply| if reply.name == 'success' success_already = true elsif reply.name != 'challenge' error = reply.first_element(nil).name end true end return if success_already raise error if error r.text = nil @stream.send(r) do |reply| if reply.name != 'success' error = reply.first_element(nil).name end true end raise error if error end |
#decode_challenge(challenge) ⇒ Object
.….….….….….….….….….….….….….….….….….….….….….….….….….
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/agent_xmpp/xmpp/sasl.rb', line 120 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 == '' res end |