Redo registration process

This commit is contained in:
Matt Corallo 2014-05-27 20:40:39 +02:00
parent d21ab1f5e8
commit 753a950816
4 changed files with 145 additions and 128 deletions

View file

@ -87,6 +87,28 @@ window.textsecure.api = function() {
code = -1; code = -1;
var e = new Error(code); var e = new Error(code);
e.name = "HTTPError"; e.name = "HTTPError";
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.";
}
reject(e); reject(e);
} }
}); });
@ -108,12 +130,11 @@ window.textsecure.api = function() {
}; };
self.confirmCode = function(code, number, password, self.confirmCode = function(code, number, password,
signaling_key, registrationId, single_device, signaling_key, registrationId, single_device) {
success_callback, error_callback) {
var call = single_device ? 'accounts' : 'devices'; var call = single_device ? 'accounts' : 'devices';
var urlPrefix = single_device ? '/code/' : '/'; var urlPrefix = single_device ? '/code/' : '/';
doAjax({ return doAjax({
call : call, call : call,
httpType : 'PUT', httpType : 'PUT',
urlParameters : urlPrefix + code, urlParameters : urlPrefix + code,
@ -123,12 +144,6 @@ window.textsecure.api = function() {
supportsSms : false, supportsSms : false,
fetchesMessages : true, fetchesMessages : true,
registrationId : registrationId}, registrationId : registrationId},
}).then(function(response) {
if (success_callback !== undefined)
success_callback(response);
}).catch(function(code) {
if (error_callback !== undefined)
error_callback(code);
}); });
}; };

View file

@ -207,45 +207,41 @@ window.textsecure.utils = function() {
return [number.substr(1, 1), number.substr(2)]; //XXX return [number.substr(1, 1), number.substr(2)]; //XXX
} }
function numberValid(number) { function validateNumber(number, countryCode) {
return true; //XXX return isNumeric(number) && number.length > 3 && number.length < 11; //XXX
} }
function countryCodeValid(number) { function validateCountryCode(countryCode) {
return true; //XXX return isNumeric(countryCode) && countryCode.length < 4 && countryCode.length > 0;
} }
self.verifyNumber = function(number, countryCode) { self.verifyNumber = function(number, countryCode) {
//XXX: All verifyNumber stuff needs to match the server-side verification
var countryCodeValid = true; var countryCodeValid = true;
var numberValid = true; var numberValid = true;
if (countryCode !== undefined) { if (number.substr(0, 1) == '+') {
var match = countryCode.match(/[0-9]{3}-?[0-9]{3}/g) if (countryCode === undefined) {
if (match == null || match.length == 1 || match[0] == countryCode) {
countryCodeValid = false;
countryCode = '1'; // Continue testing number with a fake countryCode
}
}
if (!isNumeric(number)) {
if (countryCode !== undefined || !number.startsWith('+') || !isNumeric(number.substr(1))) {
numberValid = false;
number = '2222222222'; // Continue testing countryCode with a fake number
} else {
var numberCCPair = splitPrefixedNumber(number); var numberCCPair = splitPrefixedNumber(number);
if (numberCCPair != null) {
countryCode = numberCCPair[0]; countryCode = numberCCPair[0];
number = numberCCPair[1]; number = numberCCPair[1];
} } else
}
if (numberValid && !verifyNumber(number))
numberValid = false; numberValid = false;
if (countryCodeValid && !verifyCountryCode(countryCode)) } else
countryCodeValid = false; numberValid = false;
} else if (countryCode === undefined)
numberValid = false;
if (numberValid && !validateNumber(number, countryCode))
numberValid = false;
if (countryCode !== undefined)
countryCodeValid = validateCountryCode(countryCode);
if (!countryCodeValid || !numberValid) if (!countryCodeValid || !numberValid)
throw { countryCodeValid: countryCodeValid, numberValid: numberValid }; throw { countryCodeValid: countryCodeValid, numberValid: numberValid };
return '+' + country_code + number; return '+' + countryCode + number;
} }
self.unencodeNumber = function(number) { self.unencodeNumber = function(number) {
@ -615,6 +611,41 @@ window.textsecure.sendMessage = function() {
} }
}(); }();
window.textsecure.register = function() {
return function(number, verificationCode, singleDevice, stepDone) {
var signalingKey = textsecure.crypto.getRandomBytes(32 + 20);
textsecure.storage.putEncrypted('signaling_key', signalingKey);
var password = btoa(getString(textsecure.crypto.getRandomBytes(16)));
password = password.substring(0, password.length - 2);
textsecure.storage.putEncrypted("password", password);
var registrationId = new Uint16Array(textsecure.crypto.getRandomBytes(2))[0];
registrationId = registrationId & 0x3fff;
textsecure.storage.putUnencrypted("registrationId", registrationId);
return textsecure.api.confirmCode(number, verificationCode, password, signalingKey, registrationId, singleDevice).then(function(response) {
if (singleDevice)
response = 1;
var numberId = number + "." + response;
textsecure.storage.putUnencrypted("number_id", numberId);
stepDone(1);
if (!singleDevice) {
//TODO: Do things???
stepDone(2);
}
return textsecure.crypto.generateKeys().then(function(keys) {
stepDone(3);
return textsecure.api.registerKeys(keys).then(function() {
stepDone(4);
});
});
});
}
}();
function requestIdentityPrivKeyFromMasterDevice(number, identityKey) { function requestIdentityPrivKeyFromMasterDevice(number, identityKey) {
//TODO //TODO
} }

View file

@ -14,16 +14,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
function updateCodeNumberColors() { function updateNumberColors() {
try { try {
textsecure.utils.verifyNumber($('#number').val(), $('#countrycode').val()); textsecure.utils.verifyNumber($('#number').val(), $('#countrycode').val());
$('#number').attr('style', ''); $('#number').attr('style', '');
$('#code').attr('style', ''); $('#countrycode').attr('style', '');
} catch (e) { } catch (e) {
if (e.countryCodeValid) if (e.countryCodeValid)
$('#code').attr('style', ''); $('#countrycode').attr('style', '');
else else
$('#code').attr('style', 'background-color:#ff6666;'); $('#countrycode').attr('style', 'background-color:#ff6666;');
if (e.numberValid) if (e.numberValid)
$('#number').attr('style', ''); $('#number').attr('style', '');
@ -32,15 +32,22 @@ function updateCodeNumberColors() {
} }
} }
$('#code').on('change', updateCodeNumberColors); $('#number').on('change', updateNumberColors);
$('#number').on('change', updateCodeNumberColors); $('#countrycode').on('change', updateNumberColors);
function isCodeValid() {
var code = $('#code');
return code.val().replace(/\D/g, '') == code.val() && code.val().length == 6;
}
$('#code').on('change', function() {
if (!isCodeValid())
$('#code').attr('style', 'background-color:#ff6666;');
else
$('#code').attr('style', '');
});
var single_device = false; var single_device = false;
var signaling_key = textsecure.crypto.getRandomBytes(32 + 20);
var password = btoa(getString(textsecure.crypto.getRandomBytes(16)));
password = password.substring(0, password.length - 2);
var registrationId = new Uint16Array(textsecure.crypto.getRandomBytes(2))[0];
registrationId = registrationId & 0x3fff;
$('#init-go-single-client').click(function() { $('#init-go-single-client').click(function() {
var number = textsecure.utils.verifyNumber($('#number').val(), $('#countrycode').val()); var number = textsecure.utils.verifyNumber($('#number').val(), $('#countrycode').val());
@ -61,8 +68,12 @@ $('#init-go-single-client').click(function() {
}); });
$('#init-go').click(function() { $('#init-go').click(function() {
if (codeMatches() && numberMatches()) { var number = textsecure.utils.verifyNumber($('#number').val(), $('#countrycode').val());
var number = "+" + $('#countrycode').val().replace(/\D/g, '') + $('#number').val().replace(/\D/g, ''); if (!isCodeValid()) {
updateCodeColor();
return;
}
$('#init-setup').hide(); $('#init-setup').hide();
$('#verify1done').html(''); $('#verify1done').html('');
@ -71,66 +82,26 @@ $('#init-go').click(function() {
$('#verify4done').html(''); $('#verify4done').html('');
$('#verify').show(); $('#verify').show();
textsecure.api.confirmCode($('#code').val(), number, password, signaling_key, registrationId, single_device, textsecure.register($('#code').val(), number, single_device, function(step) {
function(response) { switch(step) {
if (single_device) case 1:
response = 1;
var number_id = number + "." + response;
textsecure.storage.putEncrypted("password", password);
textsecure.storage.putEncrypted('signaling_key', signaling_key);
textsecure.storage.putUnencrypted("number_id", number_id);
textsecure.storage.putUnencrypted("registrationId", registrationId);
$('#verify1done').html('done'); $('#verify1done').html('done');
break;
var register_keys_func = function() { case 2:
$('#verify2done').html('done'); $('#verify2done').html('done');
textsecure.crypto.generateKeys().then(function(keys) { break;
case 3:
$('#verify3done').html('done'); $('#verify3done').html('done');
textsecure.api.registerKeys(keys, break;
function(response) { case 4:
$('#complete-number').html(number); $('#complete-number').html(number);
$('#verify').hide(); $('#verify').hide();
$('#setup-complete').show(); $('#setup-complete').show();
registrationDone(); registrationDone();
}, function(code) {
alert(code); //TODO
} }
);
});
}
if (!single_device) {
//TODO: Redo all this
/*getKeysForNumber(number).then(function(identityKey) {
textsecure.subscribeToPush(function(message) {
//TODO receive shared identity key
register_keys_func();
});
requestIdentityPrivKeyFromMasterDevice(number);
}).catch(function(error) { }).catch(function(error) {
alert(error); //TODO alert(error.human_error);
});*/ });
register_keys_func();
} else {
register_keys_func();
}
}, function(code) {
var error;
switch(code) {
case 403:
error = "Invalid code, please try again.";
break;
case -1:
error = "Error connecting to server, please check your network connection.";
break;
default:
error = "Unknown error, please try again later.";
console.log("Got error code " + code);
}
alert(error); //TODO
}
);
}
}); });
textsecure.registerOnLoadFunction(function() { textsecure.registerOnLoadFunction(function() {
@ -138,7 +109,7 @@ textsecure.registerOnLoadFunction(function() {
if (!isRegistrationDone()) { if (!isRegistrationDone()) {
$('#init-setup').show(); $('#init-setup').show();
} else { } else {
$('#complete-number').html(textsecure.storage.getUnencrypted("number_id").split(".")[0]); $('#complete-number').html(textsecure.storage.getUnencrypted("number_id").split(".")[0]);//TODO: no
$('#setup-complete').show(); $('#setup-complete').show();
} }
}); });

View file

@ -21,7 +21,7 @@
<div id="listener"></div> <div id="listener"></div>
<div id="log"></div> <div id="log"></div>
<h1>TextSecure <img src='icon.png' height='50px' width='50px'/></h1> <h1 style="font-size: 30pt;"><img src='icon.png' height='50px' width='50px'/> TextSecure</h1>
<div id="init-setup" style="display: none;"> <div id="init-setup" style="display: none;">
<h2>Welcome to TextSecure. To get started please get a 6-digit setup code from your phone and enter it below.</h2> <h2>Welcome to TextSecure. To get started please get a 6-digit setup code from your phone and enter it below.</h2>
Phone number (including country code): +<input type="text" size="2" id="countrycode" /><input type="text" size="8" id="number" /><br> Phone number (including country code): +<input type="text" size="2" id="countrycode" /><input type="text" size="8" id="number" /><br>