Commit 9bf00241 authored by JC Brand's avatar JC Brand

Updates #721 Restore sessions when logging in anonymously

together with keepalive.
parent 9a09409e
# Changelog # Changelog
## 2.0.2 (Unreleased)
- #721 keepalive not working with anonymous authentication [jcbrand]
## 2.0.1 (2016-11-07) ## 2.0.1 (2016-11-07)
- #203 New configuration setting [muc_domain](https://conversejs.org/docs/html/configuration.html#muc_domain) [jcbrand] - #203 New configuration setting [muc_domain](https://conversejs.org/docs/html/configuration.html#muc_domain) [jcbrand]
- #705 White content after submitting password on chatrooms [jcbrand] - #705 White content after submitting password on chatrooms [jcbrand]
......
...@@ -46,13 +46,11 @@ ...@@ -46,13 +46,11 @@
})); }));
it("needs jid, rid and sid values when not using keepalive", mock.initConverse(function (converse) { it("needs jid, rid and sid values when not using keepalive", mock.initConverse(function (converse) {
var authentication = converse.authentication;
var jid = converse.jid; var jid = converse.jid;
delete converse.jid; delete converse.jid;
converse.authentication = "prebind"; converse.authentication = "prebind";
expect(converse.logIn.bind(converse)).toThrow( expect(converse.logIn.bind(converse)).toThrow(
new Error("attemptPreboundSession: If you use prebind and not keepalive, then you MUST supply JID, RID and SID values")); new Error("attemptPreboundSession: If you use prebind and not keepalive, then you MUST supply JID, RID and SID values or a prebind_url."));
converse.authentication= authentication;
converse.bosh_service_url = undefined; converse.bosh_service_url = undefined;
converse.jid = jid; converse.jid = jid;
})); }));
......
...@@ -1757,31 +1757,30 @@ ...@@ -1757,31 +1757,30 @@
}; };
this.startNewBOSHSession = function () { this.startNewBOSHSession = function () {
var that = this;
$.ajax({ $.ajax({
url: this.prebind_url, url: this.prebind_url,
type: 'GET', type: 'GET',
dataType: "json", dataType: "json",
success: function (response) { success: function (response) {
this.connection.attach( that.connection.attach(
response.jid, response.jid,
response.sid, response.sid,
response.rid, response.rid,
this.onConnectStatusChanged that.onConnectStatusChanged
); );
}.bind(this), },
error: function (response) { error: function (response) {
delete this.connection; delete that.connection;
this.emit('noResumeableSession'); that.emit('noResumeableSession');
}.bind(this) }
}); });
}; };
this.attemptPreboundSession = function (tokens) { this.attemptPreboundSession = function (tokens) {
/* Handle session resumption or initialization when prebind is being used. /* Handle session resumption or initialization when prebind is being used.
*/ */
if (this.jid && this.sid && this.rid) { if (this.keepalive) {
return this.connection.attach(this.jid, this.sid, this.rid, this.onConnectStatusChanged);
} else if (this.keepalive) {
if (!this.jid) { if (!this.jid) {
throw new Error("attemptPreboundSession: when using 'keepalive' with 'prebind, "+ throw new Error("attemptPreboundSession: when using 'keepalive' with 'prebind, "+
"you must supply the JID of the current user."); "you must supply the JID of the current user.");
...@@ -1792,18 +1791,16 @@ ...@@ -1792,18 +1791,16 @@
this.log("Could not restore session for jid: "+this.jid+" Error message: "+e.message); this.log("Could not restore session for jid: "+this.jid+" Error message: "+e.message);
this.clearSession(); // If there's a roster, we want to clear it (see #555) this.clearSession(); // If there's a roster, we want to clear it (see #555)
} }
} else {
throw new Error("attemptPreboundSession: If you use prebind and not keepalive, "+
"then you MUST supply JID, RID and SID values");
} }
// We haven't been able to attach yet. Let's see if there
// is a prebind_url, otherwise there's nothing with which // No keepalive, or session resumption has failed.
// we can attach. if (this.jid && this.sid && this.rid) {
if (this.prebind_url) { return this.connection.attach(this.jid, this.sid, this.rid, this.onConnectStatusChanged);
this.startNewBOSHSession(); } else if (this.prebind_url) {
return this.startNewBOSHSession();
} else { } else {
delete this.connection; throw new Error("attemptPreboundSession: If you use prebind and not keepalive, "+
this.emit('noResumeableSession'); "then you MUST supply JID, RID and SID values or a prebind_url.");
} }
}; };
...@@ -1844,7 +1841,7 @@ ...@@ -1844,7 +1841,7 @@
} }
}; };
this.attemptNonPreboundSession = function () { this.attemptNonPreboundSession = function (credentials) {
/* Handle session resumption or initialization when prebind is not being used. /* Handle session resumption or initialization when prebind is not being used.
* *
* Two potential options exist and are handled in this method: * Two potential options exist and are handled in this method:
...@@ -1860,7 +1857,11 @@ ...@@ -1860,7 +1857,11 @@
} }
} }
if (this.auto_login) { if (this.auto_login) {
if (this.credentials_url) { if (credentials) {
// When credentials are passed in, they override prebinding
// or credentials fetching via HTTP
this.autoLogin(credentials);
} else if (this.credentials_url) {
this.fetchLoginCredentials().done(this.autoLogin.bind(this)); this.fetchLoginCredentials().done(this.autoLogin.bind(this));
} else if (!this.jid) { } else if (!this.jid) {
throw new Error( throw new Error(
...@@ -1870,24 +1871,19 @@ ...@@ -1870,24 +1871,19 @@
"username and password can be fetched (via credentials_url)." "username and password can be fetched (via credentials_url)."
); );
} else { } else {
// Probably ANONYMOUS login
this.autoLogin(); this.autoLogin();
} }
} }
}; };
this.logIn = function (credentials) { this.logIn = function (credentials) {
if (credentials || this.authentication === converse.ANONYMOUS) { // We now try to resume or automatically set up a new session.
// When credentials are passed in, they override prebinding // Otherwise the user will be shown a login form.
// or credentials fetching via HTTP if (this.authentication === converse.PREBIND) {
this.autoLogin(credentials); this.attemptPreboundSession();
} else { } else {
// We now try to resume or automatically set up a new session. this.attemptNonPreboundSession(credentials);
// Otherwise the user will be shown a login form.
if (this.authentication === converse.PREBIND) {
this.attemptPreboundSession();
} else {
this.attemptNonPreboundSession();
}
} }
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment