2015-09-07 23:53:43 +02:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-05-04 08:34:13 +02:00
|
|
|
*/
|
|
|
|
|
2015-08-29 00:37:45 +02:00
|
|
|
var TextSecureServer = (function() {
|
2014-10-20 02:53:17 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-08-29 00:37:45 +02:00
|
|
|
// Promise-based async xhr routine
|
2015-03-25 01:04:41 +01:00
|
|
|
function ajax(url, options) {
|
2015-07-13 20:52:49 +02:00
|
|
|
return new Promise(function (resolve, reject) {
|
2015-09-17 08:45:01 +02:00
|
|
|
console.log(options.type, url);
|
2015-07-13 20:52:49 +02:00
|
|
|
var error = new Error(); // just in case, save stack here.
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open(options.type, url, true /*async*/);
|
2015-03-25 01:04:41 +01:00
|
|
|
|
2015-07-13 20:52:49 +02:00
|
|
|
if ( options.responseType ) {
|
|
|
|
xhr[ 'responseType' ] = options.responseType;
|
2015-03-25 01:04:41 +01:00
|
|
|
}
|
2015-07-13 20:52:49 +02:00
|
|
|
if (options.user && options.password) {
|
|
|
|
xhr.setRequestHeader("Authorization", "Basic " + btoa(getString(options.user) + ":" + getString(options.password)));
|
2015-03-25 01:04:41 +01:00
|
|
|
}
|
2015-07-13 20:52:49 +02:00
|
|
|
if (options.contentType) {
|
|
|
|
xhr.setRequestHeader( "Content-Type", options.contentType );
|
2015-03-25 01:04:41 +01:00
|
|
|
}
|
2015-07-13 20:52:49 +02:00
|
|
|
|
|
|
|
xhr.onload = function() {
|
|
|
|
var result = xhr.response;
|
|
|
|
if ( (!xhr.responseType || xhr.responseType === "text") &&
|
|
|
|
typeof xhr.responseText === "string" ) {
|
|
|
|
result = xhr.responseText;
|
|
|
|
}
|
|
|
|
if (options.dataType === 'json') {
|
|
|
|
try { result = JSON.parse(xhr.responseText + ''); } catch(e) {}
|
|
|
|
}
|
|
|
|
if ( 0 <= xhr.status && xhr.status < 400) {
|
2015-09-30 20:17:19 +02:00
|
|
|
console.log(options.type, url, xhr.status, 'Success');
|
2015-07-13 20:52:49 +02:00
|
|
|
resolve(result, xhr.status);
|
|
|
|
} else {
|
2015-09-30 20:17:19 +02:00
|
|
|
console.log(options.type, url, xhr.status, 'Error');
|
2015-07-13 20:52:49 +02:00
|
|
|
reject(HTTPError(xhr.status, result, error.stack));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.onerror = function() {
|
2015-09-30 20:17:19 +02:00
|
|
|
console.log(options.type, url, xhr.status, 'Error');
|
2015-07-13 20:52:49 +02:00
|
|
|
reject(HTTPError(xhr.status, null, error.stack));
|
|
|
|
};
|
|
|
|
xhr.send( options.data || null );
|
|
|
|
});
|
2015-04-30 21:13:04 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 20:28:47 +02:00
|
|
|
function HTTPError(code, response, stack) {
|
|
|
|
if (code > 999 || code < 100) {
|
|
|
|
code = -1;
|
|
|
|
}
|
2015-07-28 02:51:17 +02:00
|
|
|
var e = new Error();
|
2015-07-10 20:28:47 +02:00
|
|
|
e.name = 'HTTPError';
|
|
|
|
e.code = code;
|
2015-07-13 20:52:49 +02:00
|
|
|
e.stack = stack;
|
2015-07-10 20:28:47 +02:00
|
|
|
if (response) {
|
|
|
|
e.response = response;
|
|
|
|
}
|
|
|
|
return e;
|
2015-04-30 21:13:04 +02:00
|
|
|
}
|
2015-03-25 01:04:41 +01:00
|
|
|
|
2015-08-29 00:37:45 +02:00
|
|
|
var URL_CALLS = {
|
|
|
|
accounts : "/v1/accounts",
|
|
|
|
devices : "/v1/devices",
|
|
|
|
keys : "/v2/keys",
|
|
|
|
messages : "/v1/messages",
|
|
|
|
attachment : "/v1/attachments"
|
2014-10-20 02:53:17 +02:00
|
|
|
};
|
2014-03-22 23:45:01 +01:00
|
|
|
|
2015-08-29 00:37:45 +02:00
|
|
|
var attachment_id_regex = RegExp( "^https:\/\/.*\/(\\d+)\?");
|
2014-10-20 02:53:17 +02:00
|
|
|
|
2015-08-29 00:37:45 +02:00
|
|
|
function TextSecureServer(url, username, password) {
|
|
|
|
this.url = url;
|
|
|
|
this.username = username;
|
|
|
|
this.password = password;
|
|
|
|
}
|
2015-06-20 00:32:25 +02:00
|
|
|
|
2015-08-29 00:37:45 +02:00
|
|
|
TextSecureServer.prototype = {
|
|
|
|
constructor: TextSecureServer,
|
|
|
|
ajax: function(param) {
|
|
|
|
if (!param.urlParameters) {
|
|
|
|
param.urlParameters = '';
|
|
|
|
}
|
|
|
|
return ajax(this.url + URL_CALLS[param.call] + param.urlParameters, {
|
|
|
|
type : param.httpType,
|
|
|
|
data : param.jsonData && textsecure.utils.jsonThing(param.jsonData),
|
|
|
|
contentType : 'application/json; charset=utf-8',
|
|
|
|
dataType : 'json',
|
|
|
|
user : this.username,
|
|
|
|
password : this.password
|
|
|
|
}).catch(function(e) {
|
|
|
|
var code = e.code;
|
|
|
|
if (code === 200) {
|
|
|
|
// happens sometimes when we get no response
|
|
|
|
// (TODO: Fix server to return 204? instead)
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var message;
|
|
|
|
switch (code) {
|
|
|
|
case -1:
|
|
|
|
message = "Failed to connect to the server, please check your network connection.";
|
|
|
|
break;
|
|
|
|
case 413:
|
|
|
|
message = "Rate limit exceeded, please try again later.";
|
|
|
|
break;
|
|
|
|
case 403:
|
|
|
|
message = "Invalid code, please try again.";
|
|
|
|
break;
|
|
|
|
case 417:
|
|
|
|
// TODO: This shouldn't be a thing?, but its in the API doc?
|
|
|
|
message = "Number already registered.";
|
|
|
|
break;
|
|
|
|
case 401:
|
|
|
|
case 403:
|
|
|
|
message = "Invalid authentication, most likely someone re-registered and invalidated our registration.";
|
|
|
|
break;
|
|
|
|
case 404:
|
|
|
|
message = "Number is not registered with TextSecure.";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
message = "The server rejected our query, please file a bug report.";
|
|
|
|
}
|
|
|
|
e.message = message
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
requestVerificationSMS: function(number) {
|
|
|
|
return this.ajax({
|
|
|
|
call : 'accounts',
|
|
|
|
httpType : 'GET',
|
|
|
|
urlParameters : '/sms/code/' + number,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
requestVerificationVoice: function(number) {
|
|
|
|
return this.ajax({
|
|
|
|
call : 'accounts',
|
|
|
|
httpType : 'GET',
|
|
|
|
urlParameters : '/voice/code/' + number,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
confirmCode: function(number, code, password, signaling_key, registrationId, deviceName) {
|
2015-06-18 20:00:58 +02:00
|
|
|
var call = deviceName ? 'devices' : 'accounts';
|
|
|
|
var urlPrefix = deviceName ? '/' : '/code/';
|
|
|
|
|
|
|
|
var jsonData = {
|
|
|
|
signalingKey : btoa(getString(signaling_key)),
|
|
|
|
supportsSms : false,
|
|
|
|
fetchesMessages : true,
|
|
|
|
registrationId : registrationId,
|
|
|
|
};
|
|
|
|
if (deviceName) {
|
|
|
|
jsonData.name = deviceName;
|
|
|
|
}
|
2015-08-29 00:37:45 +02:00
|
|
|
this.username = number;
|
|
|
|
this.password = password;
|
|
|
|
return this.ajax({
|
2014-10-20 02:53:17 +02:00
|
|
|
call : call,
|
|
|
|
httpType : 'PUT',
|
|
|
|
urlParameters : urlPrefix + code,
|
2015-06-18 20:00:58 +02:00
|
|
|
jsonData : jsonData
|
2014-10-20 02:53:17 +02:00
|
|
|
});
|
2015-08-29 00:37:45 +02:00
|
|
|
},
|
|
|
|
getDevices: function(number) {
|
|
|
|
return this.ajax({
|
|
|
|
call : 'devices',
|
|
|
|
httpType : 'GET',
|
|
|
|
});
|
|
|
|
},
|
|
|
|
registerKeys: function(genKeys) {
|
|
|
|
var keys = {};
|
|
|
|
keys.identityKey = btoa(getString(genKeys.identityKey));
|
|
|
|
keys.signedPreKey = {keyId: genKeys.signedPreKey.keyId, publicKey: btoa(getString(genKeys.signedPreKey.publicKey)),
|
|
|
|
signature: btoa(getString(genKeys.signedPreKey.signature))};
|
2014-10-20 02:53:17 +02:00
|
|
|
|
2015-08-29 00:37:45 +02:00
|
|
|
keys.preKeys = [];
|
|
|
|
var j = 0;
|
|
|
|
for (var i in genKeys.preKeys)
|
|
|
|
keys.preKeys[j++] = {keyId: genKeys.preKeys[i].keyId, publicKey: btoa(getString(genKeys.preKeys[i].publicKey))};
|
2015-04-29 01:26:41 +02:00
|
|
|
|
2015-08-29 00:37:45 +02:00
|
|
|
//TODO: This is just to make the server happy (v2 clients should choke on publicKey),
|
|
|
|
// it needs removed before release
|
|
|
|
keys.lastResortKey = {keyId: 0x7fffFFFF, publicKey: btoa("42")};
|
2014-10-20 02:53:17 +02:00
|
|
|
|
2015-08-29 00:37:45 +02:00
|
|
|
return this.ajax({
|
|
|
|
call : 'keys',
|
|
|
|
httpType : 'PUT',
|
|
|
|
jsonData : keys,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getMyKeys: function(number, deviceId) {
|
|
|
|
return this.ajax({
|
|
|
|
call : 'keys',
|
|
|
|
httpType : 'GET',
|
|
|
|
}).then(function(res) {
|
|
|
|
return parseInt(res.count);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getKeysForNumber: function(number, deviceId) {
|
|
|
|
if (deviceId === undefined)
|
|
|
|
deviceId = "*";
|
2014-10-20 02:53:17 +02:00
|
|
|
|
2015-08-29 00:37:45 +02:00
|
|
|
return this.ajax({
|
|
|
|
call : 'keys',
|
|
|
|
httpType : 'GET',
|
|
|
|
urlParameters : "/" + number + "/" + deviceId,
|
|
|
|
}).then(function(res) {
|
|
|
|
var promises = [];
|
|
|
|
res.identityKey = StringView.base64ToBytes(res.identityKey);
|
|
|
|
for (var i = 0; i < res.devices.length; i++) {
|
|
|
|
res.devices[i].signedPreKey.publicKey = StringView.base64ToBytes(res.devices[i].signedPreKey.publicKey);
|
|
|
|
res.devices[i].signedPreKey.signature = StringView.base64ToBytes(res.devices[i].signedPreKey.signature);
|
|
|
|
res.devices[i].preKey.publicKey = StringView.base64ToBytes(res.devices[i].preKey.publicKey);
|
|
|
|
//TODO: Is this still needed?
|
|
|
|
//if (res.devices[i].keyId === undefined)
|
|
|
|
// res.devices[i].keyId = 0;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
sendMessages: function(destination, messageArray, legacy) {
|
|
|
|
//TODO: Do this conversion somewhere else?
|
|
|
|
for (var i = 0; i < messageArray.length; i++) {
|
|
|
|
messageArray[i].content = btoa(messageArray[i].content);
|
|
|
|
if (legacy) {
|
|
|
|
messageArray[i].body = messageArray[i].content;
|
|
|
|
delete messageArray[i].content;
|
|
|
|
}
|
2015-06-16 04:30:42 +02:00
|
|
|
}
|
2015-08-29 00:37:45 +02:00
|
|
|
var jsonData = { messages: messageArray };
|
|
|
|
jsonData.timestamp = messageArray[0].timestamp;
|
2014-10-20 02:53:17 +02:00
|
|
|
|
2015-08-29 00:37:45 +02:00
|
|
|
return this.ajax({
|
|
|
|
call : 'messages',
|
|
|
|
httpType : 'PUT',
|
|
|
|
urlParameters : '/' + destination,
|
|
|
|
jsonData : jsonData,
|
2014-10-20 02:53:17 +02:00
|
|
|
});
|
2015-08-29 00:37:45 +02:00
|
|
|
},
|
|
|
|
getAttachment: function(id) {
|
|
|
|
return this.ajax({
|
|
|
|
call : 'attachment',
|
|
|
|
httpType : 'GET',
|
|
|
|
urlParameters : '/' + id,
|
|
|
|
}).then(function(response) {
|
|
|
|
return ajax(response.location, {
|
|
|
|
type : "GET",
|
|
|
|
responseType: "arraybuffer",
|
|
|
|
contentType : "application/octet-stream"
|
|
|
|
});
|
2014-10-20 02:53:17 +02:00
|
|
|
});
|
2015-08-29 00:37:45 +02:00
|
|
|
},
|
|
|
|
putAttachment: function(encryptedBin) {
|
|
|
|
return this.ajax({
|
|
|
|
call : 'attachment',
|
|
|
|
httpType : 'GET',
|
|
|
|
}).then(function(response) {
|
|
|
|
return ajax(response.location, {
|
|
|
|
type : "PUT",
|
|
|
|
contentType : "application/octet-stream",
|
|
|
|
data : encryptedBin,
|
|
|
|
processData : false,
|
|
|
|
}).then(function() {
|
|
|
|
// Parse the id as a string from the location url
|
|
|
|
// (workaround for ids too large for Javascript numbers)
|
|
|
|
return response.location.match(attachment_id_regex)[1];
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getMessageSocket: function() {
|
|
|
|
return new WebSocket(
|
|
|
|
this.url.replace('https://', 'wss://')
|
|
|
|
.replace('http://', 'ws://')
|
|
|
|
+ '/v1/websocket/?login=' + encodeURIComponent(this.username)
|
|
|
|
+ '&password=' + encodeURIComponent(this.password)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
getProvisioningSocket: function () {
|
|
|
|
return new WebSocket(
|
|
|
|
this.url.replace('https://', 'wss://')
|
|
|
|
.replace('http://', 'ws://')
|
|
|
|
+ '/v1/websocket/provisioning/'
|
|
|
|
);
|
|
|
|
}
|
2014-10-20 02:53:17 +02:00
|
|
|
};
|
|
|
|
|
2015-08-29 00:37:45 +02:00
|
|
|
return TextSecureServer;
|
|
|
|
})();
|