Commits

Ivan Vučica committed eb8040b Merge

Merge

Comments (0)

Files changed (5)

zxmpp_authanonymous.js

 		{
 			
 			if(this.zxmpp.stream.features["urn:ietf:params:xml:ns:xmpp-sasl"] && 
-			   this.zxmpp.stream.features["urn:ietf:params:xml:ns:xmpp-sasl"]["mechanisms"]["ANONYMOUS"])
+			   this.zxmpp.stream.features["urn:ietf:params:xml:ns:xmpp-sasl"]["mechanisms"]["set"]["ANONYMOUS"])
 			{
 				this.sendAnonymousAuth("poll");
 			}
 	{
 		// ANONYMOUS should never have to handle a challenge.
 		// For now, we just won't respond to the challenge.
-		zxmppConsole.log("zxmpp::authPlain::handleChallenge: ignoring challenge!");
+		zxmppConsole.log("zxmpp::authAnonymous::handleChallenge: ignoring challenge!");
 	}
 	this.handleSuccess = function zxmppAuthAnonymous_handleSuccess(xml)
 	{
 	{
 		this.authSuccess = false;
 	}
-	this.sendAnonymousAuth = function zxmppAuthAnonymous_sendPlainAuth()
+	this.sendAnonymousAuth = function zxmppAuthAnonymous_sendAnonymousAuth()
 	{
 		// send authorization
 		var packet = new this.zxmpp.packet(this.zxmpp);

zxmpp_authdigestmd5.js

 			}
 			else
 			{
-				zxmppConsole.error("zxmpp::authDigestMD5::doStep(): plain authentication mechanism unsupported. giving up");
+				zxmppConsole.error("zxmpp::authDigestMD5::doStep(): digestmd5 authentication mechanism unsupported. giving up");
 
 				this.zxmpp.stream.terminate();
 	

zxmpp_authplain.js

 		{
 			
 			if(this.zxmpp.stream.features["urn:ietf:params:xml:ns:xmpp-sasl"] && 
-			   this.zxmpp.stream.features["urn:ietf:params:xml:ns:xmpp-sasl"]["mechanisms"]["PLAIN"])
+			   this.zxmpp.stream.features["urn:ietf:params:xml:ns:xmpp-sasl"]["mechanisms"]["set"]["PLAIN"])
 			{
 				this.sendPlainAuth("poll");
 			}
 		this.hasSentAuth=true;
 		packet.send();
 	}
-	this.authSucceeded = function zxmppAuthPLain_authSucceeded()
+	this.authSucceeded = function zxmppAuthPlain_authSucceeded()
 	{
 		return this.authSuccess;
 	}

zxmpp_stanzastreamfeatures.js

 	
 	this.parseMechanisms = function(xml)
 	{
-		var saslMechanisms = [];
+		var saslMechanismsList = [];
+		var saslMechanismsSet = {};
 		for(var i in xml.childNodes)
 		{
 			var child = xml.childNodes[i];
 			if(child.nodeName=="mechanism")
 			{
-				saslMechanisms[child.firstChild.nodeValue.toUpperCase()] = true;
+				var mechanism = child.firstChild.nodeValue.toUpperCase();
+
+				saslMechanismsList.push(mechanism);
+				saslMechanismsSet[mechanism] = true;
 			}
 		}
-		return saslMechanisms;
+
+		return {'list' : saslMechanismsList, 'set' : saslMechanismsSet};
 	}
 
 	this.toJSON = function()
 		
 		if(!this.auth)
 		{
+
+			var knownMechanisms = {
+				'ANONYMOUS' : this.zxmpp.authAnonymous,
+				'PLAIN' : this.zxmpp.authPlain,
+				'DIGEST-MD5' : this.zxmpp.authDigestMD5,
+			};
+
 			// Weighted, ordered list of supported mechanisms.
 			// 0 is the favorite mechanism,
 			// 1 is the less favored mechanism,
 			// etc.
-			// FIXME: server should be deciding the order of preference
-			// for mechanisms
 			var supportedMechanisms = new Array();
-			supportedMechanisms.push(["DIGEST-MD5", this.zxmpp.authDigestMD5]);
-			supportedMechanisms.push(["PLAIN", this.zxmpp.authPlain]);
-			supportedMechanisms.push(["ANONYMOUS", this.zxmpp.authAnonymous]);
+			if(this.zxmpp.stream.features["urn:ietf:params:xml:ns:xmpp-sasl"])
+			{
+				// server decides order of preference.
+				var mechanisms = this.zxmpp.stream.features["urn:ietf:params:xml:ns:xmpp-sasl"]["mechanisms"]["list"];
+				for (var mechanismId in mechanisms)
+				{
+					var mechanism = mechanisms[mechanismId];
+					if(knownMechanisms[mechanism])
+					{
+						supportedMechanisms.push([mechanism, knownMechanisms[mechanism]]);
+					}
+				}
+			}
+			else
+			{
+				// a default in case of some breakage.
+				// should never happen.
+				supportedMechanisms.push(["PLAIN", this.zxmpp.authPlain]);
+			}
 
+
+			// TODO: simplify.
+			// We already only permit server-supported mechanisms. We should easily
+			// be able to simply use the top mechanism.
 			var pickedMechanism = false;
 			for(var mechanismId = 0; mechanismId < supportedMechanisms.length; mechanismId++)
 			{
 				var mechanismClass = mechanism[1];
 
 				if(this.zxmpp.stream.features["urn:ietf:params:xml:ns:xmpp-sasl"] &&
-				   this.zxmpp.stream.features["urn:ietf:params:xml:ns:xmpp-sasl"]["mechanisms"][mechanismName])
+				   this.zxmpp.stream.features["urn:ietf:params:xml:ns:xmpp-sasl"]["mechanisms"]["set"][mechanismName])
 				{
 					pickedMechanism = mechanism;
 					break;