add crypto to pinService and tests
This commit is contained in:
parent
0c0e11aad3
commit
65512bf1c2
4 changed files with 129 additions and 62 deletions
|
|
@ -15,7 +15,9 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc
|
||||||
$rootScope.fromEmailConfirmation = false;
|
$rootScope.fromEmailConfirmation = false;
|
||||||
}
|
}
|
||||||
Compatibility.check($scope);
|
Compatibility.check($scope);
|
||||||
$scope.hasPin = pinService.check();
|
pinService.check(function(err, value) {
|
||||||
|
$scope.hasPin = value;
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Object.defineProperty($scope,
|
Object.defineProperty($scope,
|
||||||
|
|
@ -23,24 +25,21 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc
|
||||||
get: function() {
|
get: function() {
|
||||||
return this._pin;
|
return this._pin;
|
||||||
},
|
},
|
||||||
set: function(newValue) {
|
set: function(newValue) {
|
||||||
console.log('[home.js:26]',newValue, this._pin); //TODO
|
this._pin = newValue;
|
||||||
this._pin = newValue;
|
if (newValue && newValue.length == 4) {
|
||||||
if (newValue && newValue.length == 4) {
|
$scope.openPin(newValue);
|
||||||
console.log('[home.js:26] INGRESANDO AUTOMATICAMENTE',newValue); //TODO
|
}
|
||||||
$scope.openPin(newValue);
|
if (!newValue) {
|
||||||
}
|
$scope.error = null;
|
||||||
if (!newValue) {
|
}
|
||||||
$scope.error = null;
|
},
|
||||||
}
|
enumerable: true,
|
||||||
},
|
configurable: true
|
||||||
enumerable: true,
|
|
||||||
configurable: true
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
$scope.done = function() {
|
$scope.done = function() {
|
||||||
$scope.hasPin = pinService.check();
|
|
||||||
$rootScope.starting = false;
|
$rootScope.starting = false;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
};
|
};
|
||||||
|
|
@ -49,8 +48,8 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc
|
||||||
$scope.$on("$destroy", function() {
|
$scope.$on("$destroy", function() {
|
||||||
var iden = $rootScope.iden;
|
var iden = $rootScope.iden;
|
||||||
if (iden) {
|
if (iden) {
|
||||||
iden.removeListener('newWallet', $scope.done );
|
iden.removeListener('newWallet', $scope.done);
|
||||||
iden.removeListener('noWallets', $scope.done );
|
iden.removeListener('noWallets', $scope.done);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -60,25 +59,26 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc
|
||||||
$scope.error = 'Please enter the required fields';
|
$scope.error = 'Please enter the required fields';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$scope.openPin(pin);
|
$scope.openPin(pin);
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.openPin = function(pin) {
|
$scope.openPin = function(pin) {
|
||||||
var credentials = pinService.get(parseInt(pin));
|
var credentials = pinService.get(pin, function(err, credentials) {
|
||||||
if (!credentials) {
|
if (err || !credentials) {
|
||||||
$scope.error = 'Wrong PIN';
|
$scope.error = 'Wrong PIN';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
$rootScope.starting = true;
|
||||||
$rootScope.starting = true;
|
$scope.open(credentials.email, credentials.password);
|
||||||
$scope.open(credentials.email, credentials.password);
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.createPin = function(form) {
|
$scope.createPin = function(form) {
|
||||||
if (form) {
|
if (!form) return;
|
||||||
pinService.save(form.repeatpin.$modelValue, $scope.email, $scope.password);
|
|
||||||
}
|
pinService.save(form.repeatpin.$modelValue, $scope.email, $scope.password, function(err) {
|
||||||
$scope.open($scope.email, $scope.password);
|
$scope.open($scope.email, $scope.password);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.openWithCredentials = function(form) {
|
$scope.openWithCredentials = function(form) {
|
||||||
|
|
@ -96,6 +96,15 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc
|
||||||
$scope.open(form.email.$modelValue, form.password.$modelValue);
|
$scope.open(form.email.$modelValue, form.password.$modelValue);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
$scope.pinLogout = function() {
|
||||||
|
pinService.clear(function() {
|
||||||
|
copay.logger.debug('PIN erased');
|
||||||
|
$scope.hasPin = null;
|
||||||
|
$scope.$digest();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
$scope.open = function(email, password) {
|
$scope.open = function(email, password) {
|
||||||
$rootScope.starting = true;
|
$rootScope.starting = true;
|
||||||
identityService.open(email, password, function(err, iden) {
|
identityService.open(email, password, function(err, iden) {
|
||||||
|
|
@ -104,7 +113,9 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc
|
||||||
copay.logger.warn(err);
|
copay.logger.warn(err);
|
||||||
if ((err.toString() || '').match('PNOTFOUND')) {
|
if ((err.toString() || '').match('PNOTFOUND')) {
|
||||||
$scope.error = 'Invalid email or password';
|
$scope.error = 'Invalid email or password';
|
||||||
pinService.clear();
|
pinService.clear(function() {
|
||||||
|
copay.logger.debug('PIN erased');
|
||||||
|
});
|
||||||
} else if ((err.toString() || '').match('Connection')) {
|
} else if ((err.toString() || '').match('Connection')) {
|
||||||
$scope.error = 'Could not connect to Insight Server';
|
$scope.error = 'Could not connect to Insight Server';
|
||||||
} else if ((err.toString() || '').match('Unable')) {
|
} else if ((err.toString() || '').match('Unable')) {
|
||||||
|
|
|
||||||
|
|
@ -1,38 +1,52 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
angular.module('copayApp.services')
|
angular.module('copayApp.services')
|
||||||
.factory('pinService', function($rootScope) {
|
.factory('pinService', function($rootScope, localstorageService) {
|
||||||
var root = {};
|
|
||||||
var storage = {
|
|
||||||
pinData: {
|
|
||||||
pin: 1234,
|
|
||||||
credentials: {
|
|
||||||
email: '4@queparece',
|
|
||||||
password: '1',
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
root.check = function() {
|
|
||||||
return storage.pinData ? true : false;
|
|
||||||
};
|
|
||||||
root.get = function(pin) {
|
|
||||||
var storedPin = storage.pinData.pin;
|
|
||||||
if (storedPin !== pin)
|
|
||||||
return;
|
|
||||||
|
|
||||||
return storage.pinData.credentials;
|
var KEY = 'pinDATA';
|
||||||
|
var SALT = '4gllotIKguqi0EkIslC0';
|
||||||
|
var ITER = 2000;
|
||||||
|
|
||||||
|
var ls = localstorageService;
|
||||||
|
var root = {};
|
||||||
|
|
||||||
|
root.check = function(cb) {
|
||||||
|
ls.getItem(KEY, function(err, value) {
|
||||||
|
return cb(err, value ? true : false);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
root.save = function(pin, email, password) {
|
|
||||||
storage.pinData = {
|
root.get = function(pin, cb) {
|
||||||
pin: pin,
|
ls.getItem(KEY, function(err, value) {
|
||||||
credentials: {
|
if (!value) return cb(null);
|
||||||
email: email,
|
var enc = value;
|
||||||
password: password
|
var data = copay.crypto.decrypt('' + parseInt(pin), enc);
|
||||||
|
var err = new Error('Could not decrypt');
|
||||||
|
if (data) {
|
||||||
|
var obj;
|
||||||
|
try {
|
||||||
|
obj = JSON.parse(data);
|
||||||
|
err = null;
|
||||||
|
} catch (e) {};
|
||||||
}
|
}
|
||||||
|
return cb(err, obj);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
root.save = function(pin, email, password, cb) {
|
||||||
|
var credentials = {
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
};
|
};
|
||||||
|
var enc = copay.crypto.encrypt('' + parseInt(pin), credentials, SALT, ITER);
|
||||||
|
ls.setItem(KEY, enc, function(err) {
|
||||||
|
return cb(err);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
root.clear = function(){
|
|
||||||
delete storage['pinData'];
|
root.clear = function(cb) {
|
||||||
|
ls.removeItem(KEY, cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -59,22 +59,22 @@ module.exports = {
|
||||||
/**
|
/**
|
||||||
* Encrypts symmetrically using a passphrase
|
* Encrypts symmetrically using a passphrase
|
||||||
*/
|
*/
|
||||||
encrypt: function(key, message) {
|
encrypt: function(key, message, salt, iter) {
|
||||||
if (!_.isString(message)) {
|
if (!_.isString(message)) {
|
||||||
message = JSON.stringify(message);
|
message = JSON.stringify(message);
|
||||||
}
|
}
|
||||||
sjcl.json.defaults.salt = defaultSalt;
|
sjcl.json.defaults.salt = salt || defaultSalt;
|
||||||
sjcl.json.defaults.iter = defaultIterations;
|
sjcl.json.defaults.iter = iter || defaultIterations;
|
||||||
return sjcl.encrypt(key, message);
|
return sjcl.encrypt(key, message);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrypts symmetrically using a passphrase
|
* Decrypts symmetrically using a passphrase
|
||||||
*/
|
*/
|
||||||
decrypt: function(key, cyphertext) {
|
decrypt: function(key, sjclEncryptedJson) {
|
||||||
var output = {};
|
var output = {};
|
||||||
try {
|
try {
|
||||||
return sjcl.decrypt(key, cyphertext);
|
return sjcl.decrypt(key, sjclEncryptedJson);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log.info('Decryption failed due to error: ' + e.message);
|
log.info('Decryption failed due to error: ' + e.message);
|
||||||
return null;
|
return null;
|
||||||
|
|
|
||||||
|
|
@ -102,6 +102,48 @@ describe("Angular services", function() {
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("Unit: identityService Service", function() {
|
||||||
|
it('should contain a identityService service', inject(function(identityService) {
|
||||||
|
expect(identityService).not.to.equal(null);
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Unit: pinService", function() {
|
||||||
|
it('should contain a pinService service', inject(function(pinService) {
|
||||||
|
expect(pinService).not.to.equal(null);
|
||||||
|
}));
|
||||||
|
it('should be able to check -> save -> get -> clear -> check', function(done) {
|
||||||
|
inject(function(pinService) {
|
||||||
|
pinService.save('123', 'user', 'pass', function(err) {
|
||||||
|
pinService.check(function(err, value) {
|
||||||
|
should.not.exist(err);
|
||||||
|
value.should.equal(true);
|
||||||
|
pinService.get('123', function(err, data) {
|
||||||
|
should.not.exist(err);
|
||||||
|
data.email.should.be.equal('user');
|
||||||
|
data.password.should.be.equal('pass');
|
||||||
|
pinService.clear(function(err) {
|
||||||
|
should.not.exist(err);
|
||||||
|
pinService.check(function(err, value) {
|
||||||
|
should.not.exist(err);
|
||||||
|
value.should.equal(false);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})
|
||||||
|
})
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Unit: localstorageService", function() {
|
||||||
|
it('should contain a localstorageService service', inject(function(localstorageService) {
|
||||||
|
expect(localstorageService).not.to.equal(null);
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
describe("Unit: Backup Service", function() {
|
describe("Unit: Backup Service", function() {
|
||||||
it('should contain a backup service', inject(function(backupService) {
|
it('should contain a backup service', inject(function(backupService) {
|
||||||
expect(backupService).not.to.equal(null);
|
expect(backupService).not.to.equal(null);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue