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-11-25 21:07:23 +01:00
|
|
|
function validateResponse(response, schema) {
|
|
|
|
try {
|
|
|
|
for (var i in schema) {
|
|
|
|
switch (schema[i]) {
|
|
|
|
case 'object':
|
|
|
|
case 'string':
|
|
|
|
case 'number':
|
|
|
|
if (typeof response[i] !== schema[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-29 00:37:45 +02:00
|
|
|
// Promise-based async xhr routine
|
2015-10-11 04:07:00 +02:00
|
|
|
function promise_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 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-12-04 19:45:53 +01:00
|
|
|
xhr.setRequestHeader( 'X-Signal-Agent', 'OWD' );
|
|
|
|
|
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) {}
|
2015-11-25 21:07:23 +01:00
|
|
|
if (options.validateResponse) {
|
|
|
|
if (!validateResponse(result, options.validateResponse)) {
|
2015-11-28 01:17:37 +01:00
|
|
|
console.log(options.type, url, xhr.status, 'Error');
|
|
|
|
reject(HTTPError(xhr.status, result, options.stack));
|
2015-11-25 21:07:23 +01:00
|
|
|
}
|
|
|
|
}
|
2015-07-13 20:52:49 +02:00
|
|
|
}
|
|
|
|
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-10-11 21:11:56 +02:00
|
|
|
reject(HTTPError(xhr.status, result, options.stack));
|
2015-07-13 20:52:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.onerror = function() {
|
2015-09-30 20:17:19 +02:00
|
|
|
console.log(options.type, url, xhr.status, 'Error');
|
2015-10-11 21:11:56 +02:00
|
|
|
reject(HTTPError(xhr.status, null, options.stack));
|
2015-07-13 20:52:49 +02:00
|
|
|
};
|
|
|
|
xhr.send( options.data || null );
|
|
|
|
});
|
2015-04-30 21:13:04 +02:00
|
|
|
}
|
|
|
|
|
2015-10-11 04:07:00 +02:00
|
|
|
function ajax(url, options) {
|
2015-10-11 21:11:56 +02:00
|
|
|
options.stack = new Error().stack; // just in case, save stack here.
|
2015-10-11 04:07:00 +02:00
|
|
|
var count = 3;
|
|
|
|
|
|
|
|
function retry(e) {
|
|
|
|
if (e.name === 'HTTPError' && e.code === -1 && count > 0) {
|
|
|
|
count = count - 1;
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
setTimeout(function() {
|
|
|
|
resolve(promise_ajax(url, options).catch(retry));
|
|
|
|
}, 1000 );
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise_ajax(url, options).catch(retry);
|
|
|
|
}
|
|
|
|
|
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-11-24 21:40:00 +01:00
|
|
|
function TextSecureServer(url, username, password, attachment_server_url) {
|
2015-09-25 02:59:57 +02:00
|
|
|
if (typeof url !== 'string') {
|
|
|
|
throw new Error('Invalid server url');
|
|
|
|
}
|
2015-08-29 00:37:45 +02:00
|
|
|
this.url = url;
|
|
|
|
this.username = username;
|
|
|
|
this.password = password;
|
2015-11-24 21:40:00 +01:00
|
|
|
|
|
|
|
this.attachment_id_regex = RegExp("^https:\/\/.*\/(\\d+)\?");
|
|
|
|
if (attachment_server_url) {
|
|
|
|
// strip trailing /
|
|
|
|
attachment_server_url = attachment_server_url.replace(/\/$/,'');
|
|
|
|
// and escape
|
|
|
|
attachment_server_url = attachment_server_url.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
|
|
this.attachment_id_regex = RegExp( "^" + attachment_server_url + "\/(\\d+)\?");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-29 00:37:45 +02:00
|
|
|
}
|
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,
|
2015-11-25 21:07:23 +01:00
|
|
|
password : this.password,
|
|
|
|
validateResponse: param.validateResponse
|
2015-08-29 00:37:45 +02:00
|
|
|
}).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:
|
|
|
|
message = "Invalid authentication, most likely someone re-registered and invalidated our registration.";
|
|
|
|
break;
|
|
|
|
case 404:
|
2015-11-13 19:15:03 +01:00
|
|
|
message = "Number is not registered.";
|
2015-08-29 00:37:45 +02:00
|
|
|
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 jsonData = {
|
|
|
|
signalingKey : btoa(getString(signaling_key)),
|
|
|
|
supportsSms : false,
|
|
|
|
fetchesMessages : true,
|
|
|
|
registrationId : registrationId,
|
|
|
|
};
|
2015-11-25 21:07:23 +01:00
|
|
|
|
|
|
|
var call, urlPrefix, schema;
|
2015-06-18 20:00:58 +02:00
|
|
|
if (deviceName) {
|
|
|
|
jsonData.name = deviceName;
|
2015-11-25 21:07:23 +01:00
|
|
|
call = 'devices';
|
|
|
|
urlPrefix = '/';
|
|
|
|
schema = { deviceId: 'number' };
|
|
|
|
} else {
|
|
|
|
call = 'accounts';
|
|
|
|
urlPrefix = '/code/';
|
2015-06-18 20:00:58 +02:00
|
|
|
}
|
2015-11-25 21:07:23 +01:00
|
|
|
|
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-11-25 21:07:23 +01:00
|
|
|
jsonData : jsonData,
|
|
|
|
validateResponse : schema
|
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));
|
2015-10-11 00:12:06 +02:00
|
|
|
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;
|
2015-10-11 00:12:06 +02:00
|
|
|
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-11-04 19:31:50 +01:00
|
|
|
// This is just to make the server happy
|
|
|
|
// (v2 clients should choke on publicKey)
|
2015-08-29 00:37:45 +02:00
|
|
|
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',
|
2015-11-25 21:07:23 +01:00
|
|
|
validateResponse : {count: 'number'}
|
2015-08-29 00:37:45 +02:00
|
|
|
}).then(function(res) {
|
2015-11-25 21:07:23 +01:00
|
|
|
return res.count;
|
2015-08-29 00:37:45 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
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,
|
2015-11-25 21:07:23 +01:00
|
|
|
validateResponse : {identityKey: 'string', devices: 'object'}
|
2015-08-29 00:37:45 +02:00
|
|
|
}).then(function(res) {
|
2015-11-25 21:07:23 +01:00
|
|
|
if (res.devices.constructor !== Array) {
|
|
|
|
throw new Error("Invalid response");
|
|
|
|
}
|
2015-08-29 00:37:45 +02:00
|
|
|
res.identityKey = StringView.base64ToBytes(res.identityKey);
|
|
|
|
for (var i = 0; i < res.devices.length; i++) {
|
2015-11-25 21:07:23 +01:00
|
|
|
if ( !validateResponse(res.devices[i], {signedPreKey: 'object', preKey: 'object'}) ||
|
|
|
|
!validateResponse(res.devices[i].signedPreKey, {publicKey: 'string', signature: 'string'}) ||
|
|
|
|
!validateResponse(res.devices[i].preKey, {publicKey: 'string'})) {
|
|
|
|
throw new Error("Invalid response");
|
|
|
|
}
|
2015-08-29 00:37:45 +02: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);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
});
|
|
|
|
},
|
2015-11-17 21:00:41 +01:00
|
|
|
sendMessages: function(destination, messageArray, timestamp) {
|
|
|
|
var jsonData = { messages: messageArray, timestamp: 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,
|
2015-11-25 21:07:23 +01:00
|
|
|
validateResponse : {location: 'string'}
|
2015-08-29 00:37:45 +02:00
|
|
|
}).then(function(response) {
|
2015-11-24 21:40:00 +01:00
|
|
|
var match = response.location.match(this.attachment_id_regex);
|
|
|
|
if (!match) {
|
2015-12-08 01:52:45 +01:00
|
|
|
console.log('Invalid attachment url for incoming message', response.location);
|
|
|
|
throw new Error('Received invalid attachment url');
|
2015-11-24 21:40:00 +01:00
|
|
|
}
|
2015-08-29 00:37:45 +02:00
|
|
|
return ajax(response.location, {
|
|
|
|
type : "GET",
|
|
|
|
responseType: "arraybuffer",
|
|
|
|
contentType : "application/octet-stream"
|
|
|
|
});
|
2015-11-24 21:40:00 +01:00
|
|
|
}.bind(this));
|
2015-08-29 00:37:45 +02:00
|
|
|
},
|
|
|
|
putAttachment: function(encryptedBin) {
|
|
|
|
return this.ajax({
|
|
|
|
call : 'attachment',
|
|
|
|
httpType : 'GET',
|
|
|
|
}).then(function(response) {
|
2015-11-24 21:40:00 +01:00
|
|
|
// Extract the id as a string from the location url
|
|
|
|
// (workaround for ids too large for Javascript numbers)
|
|
|
|
var match = response.location.match(this.attachment_id_regex);
|
|
|
|
if (!match) {
|
2015-12-08 01:52:45 +01:00
|
|
|
console.log('Invalid attachment url for outgoing message', response.location);
|
|
|
|
throw new Error('Received invalid attachment url');
|
2015-11-24 21:40:00 +01:00
|
|
|
}
|
2015-08-29 00:37:45 +02:00
|
|
|
return ajax(response.location, {
|
|
|
|
type : "PUT",
|
|
|
|
contentType : "application/octet-stream",
|
|
|
|
data : encryptedBin,
|
|
|
|
processData : false,
|
|
|
|
}).then(function() {
|
2015-11-24 21:40:00 +01:00
|
|
|
return match[1];
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
2015-08-29 00:37:45 +02:00
|
|
|
},
|
|
|
|
getMessageSocket: function() {
|
|
|
|
return new WebSocket(
|
|
|
|
this.url.replace('https://', 'wss://')
|
|
|
|
.replace('http://', 'ws://')
|
|
|
|
+ '/v1/websocket/?login=' + encodeURIComponent(this.username)
|
|
|
|
+ '&password=' + encodeURIComponent(this.password)
|
2015-12-04 19:45:53 +01:00
|
|
|
+ '&agent=OWD'
|
2015-08-29 00:37:45 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
getProvisioningSocket: function () {
|
|
|
|
return new WebSocket(
|
|
|
|
this.url.replace('https://', 'wss://')
|
|
|
|
.replace('http://', 'ws://')
|
2015-12-04 19:45:53 +01:00
|
|
|
+ '/v1/websocket/provisioning/?agent=OWD'
|
2015-08-29 00:37:45 +02:00
|
|
|
);
|
|
|
|
}
|
2014-10-20 02:53:17 +02:00
|
|
|
};
|
|
|
|
|
2015-08-29 00:37:45 +02:00
|
|
|
return TextSecureServer;
|
|
|
|
})();
|