2014-05-14 20:58:12 +02:00
|
|
|
/* vim: ts=4:sw=4
|
|
|
|
*
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2014-05-17 07:53:58 +02:00
|
|
|
window.textsecure = window.textsecure || {};
|
2014-03-26 20:05:09 +01:00
|
|
|
|
2014-05-17 07:53:58 +02:00
|
|
|
window.textsecure.api = function() {
|
|
|
|
var self = {};
|
2014-03-26 20:05:09 +01:00
|
|
|
|
2014-05-17 07:53:58 +02:00
|
|
|
/************************************************
|
|
|
|
*** Utilities to communicate with the server ***
|
|
|
|
************************************************/
|
|
|
|
// WARNING: THIS SERVER LOGS KEY MATERIAL FOR TESTING
|
|
|
|
var URL_BASE = "http://sushiforeveryone.bluematt.me";
|
2014-03-22 23:45:01 +01:00
|
|
|
|
2014-05-17 07:53:58 +02:00
|
|
|
// This is the real server
|
|
|
|
//var URL_BASE = "https://textsecure-service.whispersystems.org";
|
|
|
|
|
|
|
|
var URL_CALLS = {};
|
|
|
|
URL_CALLS['accounts'] = "/v1/accounts";
|
|
|
|
URL_CALLS['devices'] = "/v1/devices";
|
|
|
|
URL_CALLS['keys'] = "/v1/keys";
|
|
|
|
URL_CALLS['push'] = "/v1/websocket";
|
|
|
|
URL_CALLS['messages'] = "/v1/messages";
|
|
|
|
URL_CALLS['attachment'] = "/v1/attachments";
|
2014-03-22 23:45:01 +01: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
|
|
|
|
* password: password to be sent in a basic auth headerA
|
|
|
|
* do_auth: alternative to user/password where user/password are figured out automagically
|
|
|
|
* jsonData: JSON data sent in the request body
|
|
|
|
*/
|
2014-05-13 10:40:29 +02:00
|
|
|
var doAjax = function(param) {
|
2014-03-22 23:45:01 +01:00
|
|
|
if (param.urlParameters === undefined)
|
|
|
|
param.urlParameters = "";
|
|
|
|
|
|
|
|
if (param.do_auth) {
|
2014-05-21 04:21:07 +02:00
|
|
|
param.user = textsecure.storage.getUnencrypted("number_id");
|
|
|
|
param.password = textsecure.storage.getEncrypted("password");
|
2014-03-22 23:45:01 +01:00
|
|
|
}
|
|
|
|
|
2014-05-13 10:40:29 +02:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
$.ajax(URL_BASE + URL_CALLS[param.call] + param.urlParameters, {
|
|
|
|
type : param.httpType,
|
2014-05-26 00:45:22 +02:00
|
|
|
data : param.jsonData && textsecure.utils.jsonThing(param.jsonData),
|
2014-05-13 10:40:29 +02:00
|
|
|
contentType : 'application/json; charset=utf-8',
|
|
|
|
dataType : 'json',
|
|
|
|
|
|
|
|
beforeSend : function(xhr) {
|
|
|
|
if (param.user !== undefined &&
|
|
|
|
param.password !== undefined)
|
|
|
|
xhr.setRequestHeader("Authorization", "Basic " + btoa(getString(param.user) + ":" + getString(param.password)));
|
|
|
|
},
|
|
|
|
|
|
|
|
success : function(response, textStatus, jqXHR) {
|
|
|
|
resolve(response);
|
|
|
|
},
|
|
|
|
|
|
|
|
error : function(jqXHR, textStatus, errorThrown) {
|
|
|
|
var code = jqXHR.status;
|
|
|
|
if (code == 200) {
|
|
|
|
// happens sometimes when we get no response
|
|
|
|
// (TODO: Fix server to return 204? instead)
|
|
|
|
resolve(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (code > 999 || code < 100)
|
|
|
|
code = -1;
|
|
|
|
var e = new Error(code);
|
|
|
|
e.name = "HTTPError";
|
2014-05-27 20:40:39 +02:00
|
|
|
switch (code) {
|
|
|
|
case -1:
|
|
|
|
e.human_error = "Failed to connect to the server, please check your network connection.";
|
|
|
|
break;
|
|
|
|
case 413:
|
|
|
|
e.human_error = "Rate limit exceeded, please try again later.";
|
|
|
|
break;
|
|
|
|
case 403:
|
|
|
|
e.human_error = "Invalid code, please try again.";
|
|
|
|
break;
|
|
|
|
case 417:
|
|
|
|
e.human_error = "Number already registered"; // TODO: This shouldn't be a thing?, but its in the API doc?
|
|
|
|
break;
|
|
|
|
case 401:
|
|
|
|
e.human_error = "Invalid authentication, most likely someone re-registered and invalidated our registration";
|
|
|
|
break;
|
|
|
|
case 404:
|
|
|
|
e.human_error = "Number is not registered with TextSecure..";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
e.human_error = "The server rejected our query, please file a bug report.";
|
|
|
|
}
|
2014-05-13 10:40:29 +02:00
|
|
|
reject(e);
|
2014-03-22 23:45:01 +01:00
|
|
|
}
|
2014-05-13 10:40:29 +02:00
|
|
|
});
|
2014-03-22 23:45:01 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-05-17 07:53:58 +02:00
|
|
|
self.requestVerificationCode = function(number, success_callback, error_callback) {
|
2014-05-13 10:40:29 +02:00
|
|
|
doAjax({
|
2014-03-22 23:45:01 +01:00
|
|
|
call : 'accounts',
|
|
|
|
httpType : 'GET',
|
|
|
|
urlParameters : '/sms/code/' + number,
|
2014-05-13 10:40:29 +02:00
|
|
|
}).then(function(response) {
|
|
|
|
if (success_callback !== undefined)
|
|
|
|
success_callback(response);
|
|
|
|
}).catch(function(code) {
|
|
|
|
if (error_callback !== undefined)
|
|
|
|
error_callback(code);
|
2014-03-22 23:45:01 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-05-17 07:53:58 +02:00
|
|
|
self.confirmCode = function(code, number, password,
|
2014-05-27 20:40:39 +02:00
|
|
|
signaling_key, registrationId, single_device) {
|
|
|
|
var call = single_device ? 'accounts' : 'devices';
|
|
|
|
var urlPrefix = single_device ? '/code/' : '/';
|
|
|
|
|
|
|
|
return doAjax({
|
|
|
|
call : call,
|
|
|
|
httpType : 'PUT',
|
|
|
|
urlParameters : urlPrefix + code,
|
|
|
|
user : number,
|
|
|
|
password : password,
|
|
|
|
jsonData : { signalingKey : btoa(getString(signaling_key)),
|
|
|
|
supportsSms : false,
|
|
|
|
fetchesMessages : true,
|
|
|
|
registrationId : registrationId},
|
|
|
|
});
|
2014-03-22 23:45:01 +01:00
|
|
|
};
|
|
|
|
|
2014-05-17 07:53:58 +02:00
|
|
|
self.registerKeys = function(keys, success_callback, error_callback) {
|
2014-03-23 18:19:53 +01:00
|
|
|
//TODO: Do this conversion somewhere else?
|
|
|
|
var identityKey = btoa(getString(keys.keys[0].identityKey));
|
|
|
|
for (var i = 0; i < keys.keys.length; i++)
|
|
|
|
keys.keys[i] = {keyId: i, publicKey: btoa(getString(keys.keys[i].publicKey)), identityKey: identityKey};
|
|
|
|
keys.lastResortKey = {keyId: keys.lastResortKey.keyId, publicKey: btoa(getString(keys.lastResortKey.publicKey)), identityKey: identityKey};
|
2014-05-13 10:40:29 +02:00
|
|
|
doAjax({
|
2014-03-22 23:45:01 +01:00
|
|
|
call : 'keys',
|
|
|
|
httpType : 'PUT',
|
|
|
|
do_auth : true,
|
|
|
|
jsonData : keys,
|
2014-05-13 10:40:29 +02:00
|
|
|
}).then(function(response) {
|
|
|
|
if (success_callback !== undefined)
|
|
|
|
success_callback(response);
|
|
|
|
}).catch(function(code) {
|
|
|
|
if (error_callback !== undefined)
|
|
|
|
error_callback(code);
|
2014-03-22 23:45:01 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-05-17 07:53:58 +02:00
|
|
|
self.getKeysForNumber = function(number) {
|
2014-05-13 21:15:45 +02:00
|
|
|
return doAjax({
|
2014-03-22 23:45:01 +01:00
|
|
|
call : 'keys',
|
|
|
|
httpType : 'GET',
|
|
|
|
do_auth : true,
|
2014-05-26 00:09:07 +02:00
|
|
|
urlParameters : "/" + number + "/*",
|
2014-05-13 10:40:29 +02:00
|
|
|
}).then(function(response) {
|
2014-05-13 21:15:45 +02:00
|
|
|
//TODO: Do this conversion somewhere else?
|
|
|
|
var res = response.keys;
|
|
|
|
for (var i = 0; i < res.length; i++) {
|
|
|
|
res[i].identityKey = base64DecToArr(res[i].identityKey);
|
|
|
|
res[i].publicKey = base64DecToArr(res[i].publicKey);
|
|
|
|
if (res[i].keyId === undefined)
|
|
|
|
res[i].keyId = 0;
|
|
|
|
}
|
|
|
|
return res;
|
2014-03-22 23:45:01 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-05-17 07:53:58 +02:00
|
|
|
self.sendMessages = function(destination, messageArray) {
|
2014-03-25 20:27:19 +01:00
|
|
|
//TODO: Do this conversion somewhere else?
|
|
|
|
for (var i = 0; i < messageArray.length; i++)
|
|
|
|
messageArray[i].body = btoa(messageArray[i].body);
|
|
|
|
var jsonData = { messages: messageArray };
|
|
|
|
if (messageArray[0].relay !== undefined)
|
|
|
|
jsonData.relay = messageArray[0].relay;
|
|
|
|
|
2014-05-13 10:40:29 +02:00
|
|
|
return doAjax({
|
2014-03-22 23:45:01 +01:00
|
|
|
call : 'messages',
|
2014-03-25 20:27:19 +01:00
|
|
|
httpType : 'PUT',
|
|
|
|
urlParameters : '/' + destination,
|
2014-03-22 23:45:01 +01:00
|
|
|
do_auth : true,
|
|
|
|
jsonData : jsonData,
|
|
|
|
});
|
2014-05-15 06:26:37 +02:00
|
|
|
};
|
|
|
|
|
2014-05-17 07:53:58 +02:00
|
|
|
self.getAttachment = function(id) {
|
2014-05-15 06:26:37 +02:00
|
|
|
return doAjax({
|
|
|
|
call : 'attachment',
|
|
|
|
httpType : 'GET',
|
|
|
|
urlParameters : '/' + id,
|
|
|
|
do_auth : true,
|
|
|
|
}).then(function(response) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
$.ajax(response.location, {
|
|
|
|
type : "GET",
|
|
|
|
xhrFields: {
|
|
|
|
responseType: "arraybuffer"
|
|
|
|
},
|
2014-05-15 07:02:15 +02:00
|
|
|
headers: {
|
2014-05-15 06:26:37 +02:00
|
|
|
"Content-Type": "application/octet-stream"
|
2014-05-15 07:02:15 +02:00
|
|
|
},
|
2014-05-15 06:26:37 +02:00
|
|
|
|
|
|
|
success : function(response, textStatus, jqXHR) {
|
|
|
|
resolve(response);
|
|
|
|
},
|
|
|
|
|
|
|
|
error : function(jqXHR, textStatus, errorThrown) {
|
|
|
|
var code = jqXHR.status;
|
|
|
|
if (code > 999 || code < 100)
|
|
|
|
code = -1;
|
|
|
|
var e = new Error(code);
|
|
|
|
e.name = "HTTPError";
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2014-05-17 07:53:58 +02:00
|
|
|
|
2014-05-17 22:56:08 +02:00
|
|
|
self.getWebsocket = function() {
|
2014-05-21 04:21:07 +02:00
|
|
|
var user = textsecure.storage.getUnencrypted("number_id");
|
|
|
|
var password = textsecure.storage.getEncrypted("password");
|
2014-05-17 22:56:08 +02:00
|
|
|
var URL = URL_BASE.replace(/^http/g, 'ws') + URL_CALLS['push'] + '/?';
|
|
|
|
var params = $.param({
|
|
|
|
user: '+' + getString(user).substring(1),
|
|
|
|
password: getString(password)
|
|
|
|
});
|
|
|
|
return new WebSocket(URL+params);
|
|
|
|
}
|
|
|
|
|
2014-05-17 07:53:58 +02:00
|
|
|
return self;
|
|
|
|
}();
|