xmpp/sasl.go
changeset 147 d7679d991b17
parent 145 21a390dd3506
child 150 fa7f6ff10c67
equal deleted inserted replaced
146:aa9a0ae8f875 147:d7679d991b17
    22 			digestMd5 = true
    22 			digestMd5 = true
    23 		}
    23 		}
    24 	}
    24 	}
    25 
    25 
    26 	if digestMd5 {
    26 	if digestMd5 {
    27 		auth := &auth{XMLName: xml.Name{Space: NsSASL, Local: "auth"}, Mechanism: "DIGEST-MD5"}
    27 		auth := &auth{XMLName: xml.Name{Space: NsSASL, Local: "auth"},
       
    28 			Mechanism: "DIGEST-MD5"}
    28 		cl.sendXml <- auth
    29 		cl.sendXml <- auth
    29 	}
    30 	}
    30 }
    31 }
    31 
    32 
    32 func (cl *Client) handleSasl(srv *auth) {
    33 func (cl *Client) handleSasl(srv *auth) {
   122 	}
   123 	}
   123 
   124 
   124 	// Encode the map and send it.
   125 	// Encode the map and send it.
   125 	clStr := packSasl(clMap)
   126 	clStr := packSasl(clMap)
   126 	b64 := base64.StdEncoding
   127 	b64 := base64.StdEncoding
   127 	clObj := &auth{XMLName: xml.Name{Space: NsSASL, Local: "response"}, Chardata: b64.EncodeToString([]byte(clStr))}
   128 	clObj := &auth{XMLName: xml.Name{Space: NsSASL, Local: "response"},
       
   129 		Chardata: b64.EncodeToString([]byte(clStr))}
   128 	cl.sendXml <- clObj
   130 	cl.sendXml <- clObj
   129 }
   131 }
   130 
   132 
   131 func (cl *Client) saslDigest2(srvMap map[string]string) {
   133 func (cl *Client) saslDigest2(srvMap map[string]string) {
   132 	if cl.saslExpected == srvMap["rspauth"] {
   134 	if cl.saslExpected == srvMap["rspauth"] {
   166 }
   168 }
   167 
   169 
   168 // Computes the response string for digest authentication.
   170 // Computes the response string for digest authentication.
   169 func saslDigestResponse(username, realm, passwd, nonce, cnonceStr,
   171 func saslDigestResponse(username, realm, passwd, nonce, cnonceStr,
   170 	authenticate, digestUri, nonceCountStr string) string {
   172 	authenticate, digestUri, nonceCountStr string) string {
   171 	h := func(text string) []byte {
   173 	h := func(text string) string {
   172 		h := md5.New()
   174 		h := md5.New()
   173 		h.Write([]byte(text))
   175 		h.Write([]byte(text))
   174 		return h.Sum(nil)
   176 		return string(h.Sum(nil))
   175 	}
   177 	}
   176 	hex := func(bytes []byte) string {
   178 	hex := func(input string) string {
   177 		return fmt.Sprintf("%x", bytes)
   179 		return fmt.Sprintf("%x", input)
   178 	}
   180 	}
   179 	kd := func(secret, data string) []byte {
   181 	kd := func(secret, data string) string {
   180 		return h(secret + ":" + data)
   182 		return h(secret + ":" + data)
   181 	}
   183 	}
   182 
   184 
   183 	a1 := string(h(username+":"+realm+":"+passwd)) + ":" +
   185 	a1 := h(username+":"+realm+":"+passwd) + ":" +
   184 		nonce + ":" + cnonceStr
   186 		nonce + ":" + cnonceStr
   185 	a2 := authenticate + ":" + digestUri
   187 	a2 := authenticate + ":" + digestUri
   186 	response := hex(kd(hex(h(a1)), nonce+":"+
   188 	response := hex(kd(hex(h(a1)), nonce+":"+
   187 		nonceCountStr+":"+cnonceStr+":auth:"+
   189 		nonceCountStr+":"+cnonceStr+":auth:"+
   188 		hex(h(a2))))
   190 		hex(h(a2))))