2014-10-20 02:53:17 +02:00
|
|
|
/* vim: ts=4:sw=4:expandtab
|
2014-05-14 20:58:12 +02:00
|
|
|
*
|
2014-05-04 08:34:13 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-04-30 23:10:04 +02:00
|
|
|
TextSecureServer = function () {
|
2014-10-20 02:53:17 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var self = {};
|
|
|
|
|
|
|
|
/************************************************
|
|
|
|
*** Utilities to communicate with the server ***
|
|
|
|
************************************************/
|
|
|
|
// Staging server
|
|
|
|
var URL_BASE = "https://textsecure-service-staging.whispersystems.org";
|
|
|
|
self.relay = "textsecure-service-staging.whispersystems.org";
|
2014-11-09 11:17:26 +01:00
|
|
|
var ATTACHMENT_HOST = "whispersystems-textsecure-attachments-staging.s3.amazonaws.com";
|
2014-10-20 02:53:17 +02:00
|
|
|
|
|
|
|
// This is the real server
|
|
|
|
//var URL_BASE = "https://textsecure-service.whispersystems.org";
|
|
|
|
|
|
|
|
var URL_CALLS = {};
|
2014-11-09 11:17:26 +01:00
|
|
|
URL_CALLS.accounts = "/v1/accounts";
|
|
|
|
URL_CALLS.devices = "/v1/devices";
|
|
|
|
URL_CALLS.keys = "/v2/keys";
|
|
|
|
URL_CALLS.push = "/v1/websocket";
|
2015-01-23 22:19:29 +01:00
|
|
|
URL_CALLS.temp_push = "/v1/websocket/provisioning";
|
2014-11-09 11:17:26 +01:00
|
|
|
URL_CALLS.messages = "/v1/messages";
|
|
|
|
URL_CALLS.attachment = "/v1/attachments";
|
2014-10-20 02:53:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* REQUIRED PARAMS:
|
|
|
|
* call: URL_CALLS entry
|
|
|
|
* httpType: POST/GET/PUT/etc
|
|
|
|
* OPTIONAL PARAMS:
|
|
|
|
* success_callback: function(response object) called on success
|
|
|
|
* error_callback: function(http status code = -1 or != 200) called on failure
|
|
|
|
* urlParameters: crap appended to the url (probably including a leading /)
|
|
|
|
* user: user name to be sent in a basic auth header
|
2015-06-18 20:00:58 +02:00
|
|
|
* password: password to be sent in a basic auth header
|
2014-10-20 02:53:17 +02:00
|
|
|
* do_auth: alternative to user/password where user/password are figured out automagically
|
|
|
|
* jsonData: JSON data sent in the request body
|
|
|
|
*/
|
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) {
|
|
|
|
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) {
|
|
|
|
resolve(result, xhr.status);
|
|
|
|
} else {
|
|
|
|
reject(HTTPError(xhr.status, result, error.stack));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.onerror = function() {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
var e = new Error(message);
|
|
|
|
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
|
|
|
|
2014-11-09 11:17:26 +01:00
|
|
|
var doAjax = function (param) {
|
|
|
|
if (param.urlParameters === undefined) {
|
2014-10-20 02:53:17 +02:00
|
|
|
param.urlParameters = "";
|
2014-11-09 11:17:26 +01:00
|
|
|
}
|
2014-10-20 02:53:17 +02:00
|
|
|
|
|
|
|
if (param.do_auth) {
|
2015-03-24 23:48:59 +01:00
|
|
|
param.user = textsecure.storage.user.getNumber() + "." + textsecure.storage.user.getDeviceId();
|
2015-03-25 00:20:36 +01:00
|
|
|
param.password = textsecure.storage.get("password");
|
2014-10-20 02:53:17 +02:00
|
|
|
}
|
|
|
|
|
2015-07-13 20:52:49 +02:00
|
|
|
return ajax(URL_BASE + URL_CALLS[param.call] + param.urlParameters, {
|
2014-10-20 02:53:17 +02:00
|
|
|
type : param.httpType,
|
|
|
|
data : param.jsonData && textsecure.utils.jsonThing(param.jsonData),
|
|
|
|
contentType : 'application/json; charset=utf-8',
|
|
|
|
dataType : 'json',
|
2015-03-25 01:04:41 +01:00
|
|
|
user : param.user,
|
2015-07-13 20:52:49 +02:00
|
|
|
password : param.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;
|
2014-10-20 02:53:17 +02:00
|
|
|
});
|
|
|
|
};
|
2014-03-22 23:45:01 +01:00
|
|
|
|
2014-10-14 09:41:18 +02:00
|
|
|
function requestVerificationCode(number, transport) {
|
2014-10-20 02:53:17 +02:00
|
|
|
return doAjax({
|
|
|
|
call : 'accounts',
|
|
|
|
httpType : 'GET',
|
|
|
|
urlParameters : '/' + transport + '/code/' + number,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
self.requestVerificationSMS = function(number) {
|
2014-10-14 09:41:18 +02:00
|
|
|
return requestVerificationCode(number, 'sms');
|
|
|
|
};
|
2014-10-20 02:53:17 +02:00
|
|
|
self.requestVerificationVoice = function(number) {
|
2014-10-14 09:41:18 +02:00
|
|
|
return requestVerificationCode(number, 'voice');
|
2014-10-20 02:53:17 +02:00
|
|
|
};
|
|
|
|
|
2015-06-20 00:32:25 +02:00
|
|
|
self.getDevices = function(number) {
|
|
|
|
return doAjax({
|
|
|
|
call : 'devices',
|
|
|
|
httpType : 'GET',
|
|
|
|
do_auth : true
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-10-20 02:53:17 +02:00
|
|
|
self.confirmCode = function(number, code, password,
|
2015-06-18 20:00:58 +02:00
|
|
|
signaling_key, registrationId, deviceName) {
|
|
|
|
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;
|
|
|
|
}
|
2014-10-20 02:53:17 +02:00
|
|
|
return doAjax({
|
|
|
|
call : call,
|
|
|
|
httpType : 'PUT',
|
|
|
|
urlParameters : urlPrefix + code,
|
|
|
|
user : number,
|
|
|
|
password : password,
|
2015-06-18 20:00:58 +02:00
|
|
|
jsonData : jsonData
|
2014-10-20 02:53:17 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
self.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))};
|
|
|
|
|
|
|
|
keys.preKeys = [];
|
|
|
|
var j = 0;
|
|
|
|
for (var i in genKeys.preKeys)
|
2015-04-10 20:47:06 +02:00
|
|
|
keys.preKeys[j++] = {keyId: genKeys.preKeys[i].keyId, publicKey: btoa(getString(genKeys.preKeys[i].publicKey))};
|
2014-10-20 02:53:17 +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")};
|
|
|
|
|
|
|
|
return doAjax({
|
|
|
|
call : 'keys',
|
|
|
|
httpType : 'PUT',
|
|
|
|
do_auth : true,
|
|
|
|
jsonData : keys,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-04-29 01:26:41 +02:00
|
|
|
self.getMyKeys = function(number, deviceId) {
|
|
|
|
return doAjax({
|
|
|
|
call : 'keys',
|
|
|
|
httpType : 'GET',
|
|
|
|
do_auth : true,
|
|
|
|
}).then(function(res) {
|
|
|
|
return parseInt(res.count);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-10-20 02:53:17 +02:00
|
|
|
self.getKeysForNumber = function(number, deviceId) {
|
|
|
|
if (deviceId === undefined)
|
|
|
|
deviceId = "*";
|
|
|
|
|
|
|
|
return doAjax({
|
|
|
|
call : 'keys',
|
|
|
|
httpType : 'GET',
|
|
|
|
do_auth : true,
|
|
|
|
urlParameters : "/" + number + "/" + deviceId,
|
|
|
|
}).then(function(res) {
|
|
|
|
var promises = [];
|
2014-11-23 05:36:57 +01:00
|
|
|
res.identityKey = StringView.base64ToBytes(res.identityKey);
|
2014-10-20 02:53:17 +02:00
|
|
|
for (var i = 0; i < res.devices.length; i++) {
|
2014-11-23 05:36:57 +01:00
|
|
|
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);
|
2014-10-20 02:53:17 +02:00
|
|
|
//TODO: Is this still needed?
|
|
|
|
//if (res.devices[i].keyId === undefined)
|
|
|
|
// res.devices[i].keyId = 0;
|
|
|
|
}
|
2015-01-16 08:52:50 +01:00
|
|
|
return res;
|
2014-10-20 02:53:17 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-06-16 04:30:42 +02:00
|
|
|
self.sendMessages = function(destination, messageArray, legacy) {
|
2014-10-20 02:53:17 +02:00
|
|
|
//TODO: Do this conversion somewhere else?
|
2015-06-16 04:30:42 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-10-20 02:53:17 +02:00
|
|
|
var jsonData = { messages: messageArray };
|
|
|
|
if (messageArray[0].relay !== undefined)
|
|
|
|
jsonData.relay = messageArray[0].relay;
|
2014-12-22 06:01:21 +01:00
|
|
|
jsonData.timestamp = messageArray[0].timestamp;
|
2014-10-20 02:53:17 +02:00
|
|
|
|
|
|
|
return doAjax({
|
|
|
|
call : 'messages',
|
|
|
|
httpType : 'PUT',
|
|
|
|
urlParameters : '/' + destination,
|
|
|
|
do_auth : true,
|
|
|
|
jsonData : jsonData,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
self.getAttachment = function(id) {
|
|
|
|
return doAjax({
|
|
|
|
call : 'attachment',
|
|
|
|
httpType : 'GET',
|
|
|
|
urlParameters : '/' + id,
|
|
|
|
do_auth : true,
|
|
|
|
}).then(function(response) {
|
2015-07-13 20:52:49 +02:00
|
|
|
return ajax(response.location, {
|
|
|
|
type : "GET",
|
|
|
|
responseType: "arraybuffer",
|
|
|
|
contentType : "application/octet-stream"
|
2014-10-20 02:53:17 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-10-26 02:01:33 +01:00
|
|
|
var id_regex = RegExp( "^https:\/\/" + ATTACHMENT_HOST + "\/(\\d+)\?");
|
2014-10-20 02:53:17 +02:00
|
|
|
self.putAttachment = function(encryptedBin) {
|
|
|
|
return doAjax({
|
2014-10-26 03:48:54 +01:00
|
|
|
call : 'attachment',
|
|
|
|
httpType : 'GET',
|
|
|
|
do_auth : true,
|
2014-10-20 02:53:17 +02:00
|
|
|
}).then(function(response) {
|
2015-07-13 20:52:49 +02:00
|
|
|
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(id_regex)[1];
|
2014-10-20 02:53:17 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var getWebsocket = function(url, auth, reconnectTimeout) {
|
|
|
|
var URL = URL_BASE.replace(/^http/g, 'ws') + url + '/?';
|
2014-11-28 07:03:42 +01:00
|
|
|
var params = '';
|
2014-10-20 02:53:17 +02:00
|
|
|
if (auth) {
|
2015-03-24 23:48:59 +01:00
|
|
|
var user = textsecure.storage.user.getNumber() + "." + textsecure.storage.user.getDeviceId();
|
2015-03-25 00:20:36 +01:00
|
|
|
var password = textsecure.storage.get("password");
|
2015-03-25 01:04:41 +01:00
|
|
|
var params = 'login=%2B' + encodeURIComponent(user.substring(1)) + '&password=' + encodeURIComponent(password);
|
2014-11-28 07:03:42 +01:00
|
|
|
}
|
2015-04-30 23:29:10 +02:00
|
|
|
return TextSecureWebSocket(URL+params)
|
2014-10-20 02:53:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self.getMessageWebsocket = function() {
|
2014-10-21 23:26:14 +02:00
|
|
|
return getWebsocket(URL_CALLS['push'], true, 1000);
|
2014-10-20 02:53:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self.getTempWebsocket = function() {
|
2015-01-19 21:23:25 +01:00
|
|
|
return getWebsocket(URL_CALLS['temp_push'], false, 1000);
|
2014-10-20 02:53:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
2014-05-17 07:53:58 +02:00
|
|
|
}();
|