Some number verification refactor stuff

This commit is contained in:
Matt Corallo 2014-05-26 00:09:07 +02:00
parent d387cd22f5
commit 18f1eed70f
3 changed files with 99 additions and 54 deletions

View file

@ -157,7 +157,7 @@ window.textsecure.api = function() {
call : 'keys',
httpType : 'GET',
do_auth : true,
urlParameters : "/" + getNumberFromString(number) + "/*",
urlParameters : "/" + number + "/*",
}).then(function(response) {
//TODO: Do this conversion somewhere else?
var res = response.keys;

View file

@ -190,14 +190,65 @@ window.textsecure.protos = function() {
return self;
}();
// Number formatting
function getNumberFromString(string) {
return string.split(".")[0];
}
// Number formatting utils
window.textsecure.utils = function() {
var self = {};
function verifyNumber(string) {
//TODO: fancy country-code guessing and number verification
return getEncodedNumber(string.trim());
function isNumeric(string) {
return string.replace(/\D/g, '') === string;
}
function splitPrefixedNumber(number) {
// number == "+CCNumber"
return [number.substr(1, 1), number.substr(2)]; //XXX
}
function numberValid(number) {
return true; //XXX
}
function countryCodeValid(number) {
return true; //XXX
}
self.verifyNumber = function(number, countryCode) {
var countryCodeValid = true;
var numberValid = true;
if (countryCode !== undefined) {
var match = countryCode.match(/[0-9]{3}-?[0-9]{3}/g)
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);
countryCode = numberCCPair[0];
number = numberCCPair[1];
}
}
if (numberValid && !verifyNumber(number))
numberValid = false;
if (countryCodeValid && !verifyCountryCode(countryCode))
countryCodeValid = false;
if (!countryCodeValid || !numberValid)
throw { countryCodeValid: countryCodeValid, numberValid: numberValid };
return '+' + country_code + number;
}
self.unencodeNumber(number) {
return string.split(".")[0];
}
return self;
}
/************************************************
@ -281,18 +332,18 @@ window.textsecure.storage = function() {
return textsecure.storage.getEncrypted("deviceObject" + encodedNumber);
}
self.getDeviceIdListFromNumber = function(number) {
return textsecure.storage.getEncrypted("deviceIdList" + getNumberFromString(number), []);
var getDeviceIdListFromNumber = function(number) {
return textsecure.storage.getEncrypted("deviceIdList" + number, []);
}
self.addDeviceIdForNumber = function(number, deviceId) {
var deviceIdList = this.getDeviceIdListFromNumber(getNumberFromString(number));
var addDeviceIdForNumber = function(number, deviceId) {
var deviceIdList = getDeviceIdListFromNumber(number);
for (var i = 0; i < deviceIdList.length; i++) {
if (deviceIdList[i] == deviceId)
return;
}
deviceIdList[deviceIdList.length] = deviceId;
textsecure.storage.putEncrypted("deviceIdList" + getNumberFromString(number), deviceIdList);
textsecure.storage.putEncrypted("deviceIdList" + number, deviceIdList);
}
var getDeviceId = function(encodedNumber) {
@ -317,14 +368,14 @@ window.textsecure.storage = function() {
existing[key] = deviceObject[key];
}
textsecure.storage.putEncrypted("deviceObject" + deviceObject.encodedNumber, existing);
this.addDeviceIdForNumber(deviceObject.encodedNumber, getDeviceId(deviceObject.encodedNumber));
addDeviceIdForNumber(textsecure.utils.unencodeNumber(deviceObject.encodedNumber), getDeviceId(deviceObject.encodedNumber));
}
self.getDeviceObjectListFromNumber = function(number) {
var deviceObjectList = [];
var deviceIdList = this.getDeviceIdListFromNumber(number);
var deviceIdList = getDeviceIdListFromNumber(number);
for (var i = 0; i < deviceIdList.length; i++)
deviceObjectList[deviceObjectList.length] = this.getDeviceObject(getNumberFromString(number) + "." + deviceIdList[i]);
deviceObjectList[deviceObjectList.length] = self.getDeviceObject(number + "." + deviceIdList[i]);
return deviceObjectList;
}
@ -560,7 +611,6 @@ window.textsecure.sendMessage = function() {
}();
function requestIdentityPrivKeyFromMasterDevice(number, identityKey) {
sendMessageToDevices([textsecure.storage.devices.getDeviceObject(getNumberFromString(number)) + ".1"],
{message: "Identity Key request"}, function() {}, function() {});//TODO
//TODO
}

View file

@ -14,29 +14,26 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
function codeMatches() {
var match = $('#code').val().match(/[0-9]{3}-?[0-9]{3}/g)
return match != null && match.length == 1 && match[0] == $('#code').val();
}
function numberMatches() {
var country_code = $('#countrycode').val().replace(/\D/g, '');
return $('#number').val().replace(/\D/g, '').length > 5 && country_code.length > 0 && country_code.length < 4;
}
$('#code').on('change', function() {
if (!codeMatches())
$('#code').attr('style', 'background-color:#ff6666;');
else
$('#code').attr('style', '');
});
$('#number').on('change', function() {//TODO
if (!numberMatches())
$('#number').attr('style', 'background-color:#ff6666;');
else
function updateCodeNumberColors() {
try {
textsecure.utils.verifyNumber($('#number').val(), $('#countrycode').val());
$('#number').attr('style', '');
});
$('#code').attr('style', '');
} catch (e) {
if (e.countryCodeValid)
$('#code').attr('style', '');
else
$('#code').attr('style', 'background-color:#ff6666;');
if (e.numberValid)
$('#number').attr('style', '');
else
$('#number').attr('style', 'background-color:#ff6666;');
}
}
$('#code').on('change', updateCodeNumberColors);
$('#number').on('change', updateCodeNumberColors);
var single_device = false;
var signaling_key = textsecure.crypto.getRandomBytes(32 + 20);
@ -46,23 +43,21 @@ var registrationId = new Uint16Array(textsecure.crypto.getRandomBytes(2))[0];
registrationId = registrationId & 0x3fff;
$('#init-go-single-client').click(function() {
if (numberMatches()) {
var number = "+" + $('#countrycode').val().replace(/\D/g, '') + $('#number').val().replace(/\D/g, '');
var number = textsecure.utils.verifyNumber($('#number').val(), $('#countrycode').val());
$('#init-go').html('Setup');
$('#countrycode').prop('disabled', 'disabled');
$('#number').prop('disabled', 'disabled');
$('#init-go-single-client').prop('disabled', 'disabled');
$('#init-go').html('Setup');
$('#countrycode').prop('disabled', 'disabled');
$('#number').prop('disabled', 'disabled');
$('#init-go-single-client').prop('disabled', 'disabled');
single_device = true;
single_device = true;
textsecure.api.requestVerificationCode(number,
function(response) { },
function(code) {
alert("Failed to send key?" + code); //TODO
}
);
}
textsecure.api.requestVerificationCode(number,
function(response) { },
function(code) {
alert("Failed to send key?" + code); //TODO
}
);
});
$('#init-go').click(function() {