Skip to content
Browse files

seperated authentication process from connection process for special …

…plugins
  • Loading branch information...
1 parent 5a15cef commit d020aebc315a815177c3a88d78ba645a4d8ed573 @dodo dodo committed Jul 30, 2011
Showing with 43 additions and 12 deletions.
  1. +43 −12 src/core.js
View
55 src/core.js
@@ -1434,9 +1434,11 @@ Strophe.Connection = function (service)
this.addTimeds = [];
this.addHandlers = [];
+ this._authentication = {};
this._idleTimeout = null;
this._disconnectTimeout = null;
+ this.do_authentication = true;
this.authenticated = false;
this.disconnecting = false;
this.connected = false;
@@ -1499,6 +1501,7 @@ Strophe.Connection.prototype = {
this.removeHandlers = [];
this.addTimeds = [];
this.addHandlers = [];
+ this._authentication = {};
this.authenticated = false;
this.disconnecting = false;
@@ -1591,8 +1594,16 @@ Strophe.Connection.prototype = {
* (Integer) hold - The optional HTTPBIND hold value. This is the
* number of connections the server will hold at one time. This
* should almost always be set to 1 (the default).
+ * (Boolean) authentication - The optional trigger for authentication
+ * process. When set to false a successful connection request will
+ * skip the authention process. The authentication process can
+ * triggered manually by the 'authenticate' method.
+ * (Function) _connect_cb - low level (xmpp) connect callback function.
+ * Useful for plugins with their own xmpp connect callback (when their)
+ * want to do something special).
*/
- connect: function (jid, pass, callback, wait, hold)
+ connect: function (jid, pass, callback, wait, hold,
+ authentication, _connect_cb)
{
this.jid = jid;
this.pass = pass;
@@ -1604,6 +1615,7 @@ Strophe.Connection.prototype = {
this.wait = wait || this.wait;
this.hold = hold || this.hold;
+ this.do_authentication = authentication || this.do_authentication;
// parse jid for domain and resource
this.domain = Strophe.getDomainFromJid(this.jid) || this.domain;
@@ -1622,6 +1634,7 @@ Strophe.Connection.prototype = {
this._changeConnectStatus(Strophe.Status.CONNECTING, null);
+ _connect_cb = _connect_cb || this._connect_cb;
this._requests.push(
new Strophe.Request(body.tree(),
this._onRequestStateChange.bind(
@@ -2550,8 +2563,11 @@ Strophe.Connection.prototype = {
*
* Parameters:
* (Strophe.Request) req - The current request.
+ * (Function) _callback - low level (xmpp) connect callback function.
+ * Useful for plugins with their own xmpp connect callback (when their)
+ * want to do something special).
*/
- _connect_cb: function (req)
+ _connect_cb: function (req, _callback)
{
Strophe.info("_connect_cb was called");
@@ -2598,25 +2614,26 @@ Strophe.Connection.prototype = {
var wait = bodyWrap.getAttribute('wait');
if (wait) { this.wait = parseInt(wait, 10); }
-
- var do_sasl_plain = false;
- var do_sasl_digest_md5 = false;
- var do_sasl_anonymous = false;
+//
+ this._authentication.sasl_plain = false;
+ this._authentication.sasl_digest_md5 = false;
+ this._authentication.sasl_anonymous = false;
var mechanisms = bodyWrap.getElementsByTagName("mechanism");
var i, mech, auth_str, hashed_auth_str;
if (mechanisms.length > 0) {
for (i = 0; i < mechanisms.length; i++) {
mech = Strophe.getText(mechanisms[i]);
if (mech == 'DIGEST-MD5') {
- do_sasl_digest_md5 = true;
+ this._authentication.sasl_digest_md5 = true;
} else if (mech == 'PLAIN') {
- do_sasl_plain = true;
+ this._authentication.sasl_plain = true;
} else if (mech == 'ANONYMOUS') {
- do_sasl_anonymous = true;
+ this._authentication.sasl_anonymous = true;
}
}
} else {
+ _callback = _callback || this._connect_cb;
// we didn't get stream:features yet, so we need wait for it
// by sending a blank poll request
var body = this._buildBody();
@@ -2628,9 +2645,23 @@ Strophe.Connection.prototype = {
this._throttledRequestHandler();
return;
}
+ if (this.do_authentication) this.authenticate();
+ },
+ /** Function: authenticate
+ * Set up authentication
+ *
+ * Contiunues the initial connection request by setting up authentication
+ * handlers and start the authentication process.
+ *
+ * SASL authentication will be attempted if available, otherwise
+ * the code will fall back to legacy authentication.
+ *
+ */
+ authenticate: function ()
+ {
if (Strophe.getNodeFromJid(this.jid) === null &&
- do_sasl_anonymous) {
+ this._authentication.sasl_anonymous) {
this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
this._sasl_success_handler = this._addSysHandler(
this._sasl_success_cb.bind(this), null,
@@ -2649,7 +2680,7 @@ Strophe.Connection.prototype = {
this._changeConnectStatus(Strophe.Status.CONNFAIL,
'x-strophe-bad-non-anon-jid');
this.disconnect();
- } else if (do_sasl_digest_md5) {
+ } else if (this._authentication.sasl_digest_md5) {
this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
this._sasl_challenge_handler = this._addSysHandler(
this._sasl_challenge1_cb.bind(this), null,
@@ -2662,7 +2693,7 @@ Strophe.Connection.prototype = {
xmlns: Strophe.NS.SASL,
mechanism: "DIGEST-MD5"
}).tree());
- } else if (do_sasl_plain) {
+ } else if (this._authentication.sasl_plain) {
// Build the plain auth string (barejid null
// username null password) and base 64 encoded.
auth_str = Strophe.getBareJidFromJid(this.jid);

0 comments on commit d020aeb

Please sign in to comment.
Something went wrong with that request. Please try again.