Commit 62d59ff3 authored by JC Brand's avatar JC Brand

Refactor some OTR code into a Backbone model and adapt tests.

parent 398142c7
......@@ -262,20 +262,6 @@
}
};
this.getSessionPassphrase = function () {
if (this.prebind) {
var key = hex_sha1(this.connection.jid),
pass = window.sessionStorage[key];
if (typeof pass === 'undefined') {
pass = Math.floor(Math.random()*4294967295).toString();
window.sessionStorage[key] = pass;
}
return pass;
} else {
return this.connection.pass;
}
};
this.getVCard = function (jid, callback, errback) {
if (!this.use_vcards) {
if (callback) {
......@@ -561,6 +547,44 @@
// Backbone Models and Views
// -------------------------
this.OTR = Backbone.Model.extend({
// A model for managing OTR settings.
getSessionPassphrase: function () {
if (converse.prebind) {
var key = hex_sha1(converse.connection.jid),
pass = window.sessionStorage[key];
if (typeof pass === 'undefined') {
pass = Math.floor(Math.random()*4294967295).toString();
window.sessionStorage[key] = pass;
}
return pass;
} else {
return converse.connection.pass;
}
},
generatePrivateKey: function (callback, instance_tag) {
var key = new DSA();
var jid = converse.connection.jid;
if (converse.cache_otr_key) {
var cipher = CryptoJS.lib.PasswordBasedCipher;
var pass = this.getSessionPassphrase();
if (typeof pass !== "undefined") {
// Encrypt the key and set in sessionStorage. Also store instance tag.
window.sessionStorage[hex_sha1(jid+'priv_key')] =
cipher.encrypt(CryptoJS.algo.AES, key.packPrivate(), pass).toString();
window.sessionStorage[hex_sha1(jid+'instance_tag')] = instance_tag;
window.sessionStorage[hex_sha1(jid+'pass_check')] =
cipher.encrypt(CryptoJS.algo.AES, 'match', pass).toString();
}
}
callback({
'key': key,
'instance_tag': instance_tag
});
}
});
this.Message = Backbone.Model.extend();
this.Messages = Backbone.Collection.extend({
......@@ -581,35 +605,15 @@
}
},
generatePrivateKey: function (callback, instance_tag) {
var cipher = CryptoJS.lib.PasswordBasedCipher;
var key = new DSA();
if (converse.cache_otr_key) {
pass = converse.getSessionPassphrase();
if (typeof pass !== "undefined") {
// Encrypt the key and set in sessionStorage. Also store instance tag.
window.sessionStorage[hex_sha1(this.id+'priv_key')] =
cipher.encrypt(CryptoJS.algo.AES, key.packPrivate(), pass).toString();
window.sessionStorage[hex_sha1(this.id+'instance_tag')] = instance_tag;
this.save({'pass_check': cipher.encrypt(CryptoJS.algo.AES, 'match', pass).toString()});
}
}
this.trigger('showHelpMessages', [__('Private key generated.')], null, false);
callback({
'key': key,
'instance_tag': instance_tag
});
},
getSession: function (callback) {
var cipher = CryptoJS.lib.PasswordBasedCipher;
var result, pass, instance_tag, saved_key;
var result, pass, instance_tag, saved_key, pass_check;
if (converse.cache_otr_key) {
pass = converse.getSessionPassphrase();
pass = converse.otr.getSessionPassphrase();
if (typeof pass !== "undefined") {
instance_tag = window.sessionStorage[hex_sha1(this.id+'instance_tag')];
saved_key = window.sessionStorage[hex_sha1(this.id+'priv_key')];
var pass_check = this.get('pass_check');
pass_check = window.sessionStorage[hex_sha1(this.connection.jid+'pass_check')];
if (saved_key && instance_tag && typeof pass_check !== 'undefined') {
var decrypted = cipher.decrypt(CryptoJS.algo.AES, saved_key, pass);
var key = DSA.parsePrivate(decrypted.toString(CryptoJS.enc.Latin1));
......@@ -633,7 +637,7 @@
null,
true // show spinner
);
setTimeout($.proxy(this.generatePrivateKey, this), 500, callback, instance_tag);
setTimeout($.proxy(converse.otr.generatePrivateKey, this), 500, callback, instance_tag);
},
updateOTRStatus: function (state) {
......@@ -3561,6 +3565,7 @@
this.chatboxes = new this.ChatBoxes();
this.chatboxesview = new this.ChatBoxesView({model: this.chatboxes});
this.controlboxtoggle = new this.ControlBoxToggle();
this.otr = new this.OTR();
if ((this.prebind) && (!this.connection)) {
if ((!this.jid) || (!this.sid) || (!this.rid) || (!this.bosh_service_url)) {
......
......@@ -7,7 +7,7 @@
}
);
} (this, function (mock, utils) {
return describe("Converse", $.proxy(function(mock, utils) {
return describe("The OTR module", $.proxy(function(mock, utils) {
beforeEach($.proxy(function () {
window.localStorage.clear();
......@@ -20,14 +20,14 @@
// stored in session storage.
this.prebind = false;
this.connection.pass = 's3cr3t!';
expect(this.getSessionPassphrase()).toBe(this.connection.pass);
expect(this.otr.getSessionPassphrase()).toBe(this.connection.pass);
expect(window.sessionStorage.length).toBe(0);
expect(window.localStorage.length).toBe(0);
// With prebind, a random passphrase is generated and stored in
// session storage.
this.prebind = true;
pp = this.getSessionPassphrase();
pp = this.otr.getSessionPassphrase();
expect(pp).not.toBe(this.connection.pass);
expect(window.sessionStorage.length).toBe(1);
expect(window.localStorage.length).toBe(0);
......@@ -36,6 +36,5 @@
// Clean up
this.prebind = false;
}, converse));
}, converse, mock, utils));
}));
......@@ -111,10 +111,18 @@ require([
testing: true
}, function (converse) {
window.converse = converse;
window.crypto = {
getRandomValues: function (buf) {
var i;
for (i=0, len=buf.length; i<len; i++) {
buf[i] = Math.floor(Math.random()*256);
}
}
};
require([
"jasmine-console-reporter",
"jasmine-junit-reporter",
"spec/converse",
"spec/otr",
"spec/eventemitter",
"spec/controlbox",
"spec/chatbox",
......
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