This commit is contained in:
JDonadio 2017-04-20 15:48:05 -03:00
commit dd154a3c4a
6 changed files with 172 additions and 100 deletions

View file

@ -5,14 +5,15 @@ angular.module('copayApp.controllers').controller('lockSetupController', functio
function init() { function init() {
$scope.options = [ $scope.options = [
{ {
method: null, method: 'none',
label: gettextCatalog.getString('Disabled'), label: gettextCatalog.getString('Disabled'),
disabled: false,
}, },
{ {
method: 'pin', method: 'pin',
label: gettextCatalog.getString('Lock by PIN'), label: gettextCatalog.getString('Lock by PIN'),
needsBackup: null, needsBackup: false,
disabled: null, disabled: false,
}, },
]; ];
@ -20,12 +21,12 @@ angular.module('copayApp.controllers').controller('lockSetupController', functio
$scope.options.push({ $scope.options.push({
method: 'fingerprint', method: 'fingerprint',
label: gettextCatalog.getString('Lock by Fingerprint'), label: gettextCatalog.getString('Lock by Fingerprint'),
needsBackup: null, needsBackup: false,
disabled: null, disabled: false,
}); });
} }
checkAndSelectOption(); initMethodSelector();
processWallets(); processWallets();
}; };
@ -33,21 +34,40 @@ angular.module('copayApp.controllers').controller('lockSetupController', functio
init(); init();
}); });
function checkAndSelectOption() { function getSavedMethod() {
var config = configService.getSync(); var config = configService.getSync();
var method = config.lock && config.lock.method; if (config.lock) return config.lock.method;
return 'none';
};
if (!method) { function initMethodSelector() {
$scope.currentOption = $scope.options[0]; function disable(method) {
$scope.options[1].disabled = false; lodash.find($scope.options, {
$scope.options[2].disabled = false; method: method
} else { }).disabled = true;
if (method == 'fingerprint') $scope.options[1].disabled = true; };
if (method == 'pin') $scope.options[2].disabled = true;
$scope.currentOption = lodash.find($scope.options, { var savedMethod = getSavedMethod();
'method': method
}); lodash.each($scope.options, function(o) {
o.disabled = false;
});
// HACK: Disable untill we allow to change between methods directly
if (fingerprintService.isAvailable()) {
switch (savedMethod) {
case 'pin':
disable('fingerprint');
break;
case 'fingerprint':
disable('pin');
break;
}
} }
$scope.currentOption = lodash.find($scope.options, {
method: savedMethod
});
$timeout(function() { $timeout(function() {
$scope.$apply(); $scope.$apply();
}); });
@ -87,42 +107,56 @@ angular.module('copayApp.controllers').controller('lockSetupController', functio
}; };
$scope.select = function(selectedMethod) { $scope.select = function(selectedMethod) {
var config = configService.getSync(); var savedMethod = getSavedMethod();
var savedMethod = config.lock && config.lock.method; if (savedMethod == selectedMethod) return;
if (!selectedMethod) { if (selectedMethod == 'none') {
if (!savedMethod) return; disableMethod(savedMethod);
if (savedMethod == 'pin') $state.transitionTo('tabs.pin', { } else {
fromSettings: true, enableMethod(selectedMethod);
locking: false, }
}); };
else saveConfig();
} else if (selectedMethod == 'pin') { function disableMethod(method) {
if (savedMethod == 'pin') return; switch (method) {
$state.transitionTo('tabs.pin', { case 'pin':
fromSettings: true, $state.transitionTo('tabs.pin', {
locking: savedMethod == 'pin' ? false : true action: 'disable'
}); });
} else if (selectedMethod == 'fingerprint') { break;
if (savedMethod == 'fingerprint') return; case 'fingerprint':
else saveConfig('fingerprint'); fingerprintService.check('unlockingApp', function(err) {
if (err) init();
else saveConfig('none');
});
break;
}
};
function enableMethod(method) {
switch (method) {
case 'pin':
$state.transitionTo('tabs.pin', {
action: 'setup'
});
break;
case 'fingerprint':
saveConfig('fingerprint');
break;
} }
$timeout(function() {
$scope.$apply();
});
}; };
function saveConfig(method) { function saveConfig(method) {
var opts = { var opts = {
lock: { lock: {
method: method || null, method: method,
value: null, value: null,
} }
}; };
configService.set(opts, function(err) { configService.set(opts, function(err) {
if (err) $log.debug(err); if (err) $log.debug(err);
checkAndSelectOption(); initMethodSelector();
}); });
}; };
}); });

View file

@ -3,11 +3,11 @@
angular.module('copayApp.controllers').controller('pinController', function($state, $interval, $stateParams, $ionicHistory, $timeout, $scope, $log, configService, appConfigService) { angular.module('copayApp.controllers').controller('pinController', function($state, $interval, $stateParams, $ionicHistory, $timeout, $scope, $log, configService, appConfigService) {
var ATTEMPT_LIMIT = 3; var ATTEMPT_LIMIT = 3;
var ATTEMPT_LOCK_OUT_TIME = 5 * 60; var ATTEMPT_LOCK_OUT_TIME = 5 * 60;
var currentPin;
$scope.$on("$ionicView.beforeEnter", function(event) { $scope.$on("$ionicView.beforeEnter", function(event) {
$scope.currentPin = $scope.confirmPin = ''; currentPin = $scope.confirmPin = '';
$scope.fromSettings = $stateParams.fromSettings == 'true' ? true : false; $scope.action = $stateParams.action;
$scope.locking = $stateParams.locking == 'true' ? true : false;
$scope.match = $scope.error = $scope.disableButtons = false; $scope.match = $scope.error = $scope.disableButtons = false;
$scope.currentAttempts = 0; $scope.currentAttempts = 0;
$scope.appName = appConfigService.name; $scope.appName = appConfigService.name;
@ -27,26 +27,19 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
}); });
}); });
function getSavedMethod() {
var config = configService.getSync();
if (config.lock) return config.lock.method;
return 'none';
};
function checkAttempts() { function checkAttempts() {
$scope.currentAttempts += 1; $scope.currentAttempts += 1;
$log.debug('Attempts to unlock:', $scope.currentAttempts); $log.debug('Attempts to unlock:', $scope.currentAttempts);
if ($scope.currentAttempts === ATTEMPT_LIMIT) { if ($scope.currentAttempts === ATTEMPT_LIMIT) {
$scope.currentAttempts = 0; $scope.currentAttempts = 0;
var limitTime = Math.floor(Date.now() / 1000) + ATTEMPT_LOCK_OUT_TIME; var limitTime = Math.floor(Date.now() / 1000) + ATTEMPT_LOCK_OUT_TIME;
var config = configService.getSync(); saveFailedAttempt(limitTime);
var opts = {
lock: {
method: 'pin',
value: config.lock.value,
bannedUntil: limitTime,
attempts: config.lock.attempts + 1,
}
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
lockTimeControl(limitTime);
});
} }
}; };
@ -74,7 +67,7 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
function reset() { function reset() {
$scope.expires = $scope.error = $scope.disableButtons = null; $scope.expires = $scope.error = $scope.disableButtons = null;
$scope.currentPin = $scope.confirmPin = ''; currentPin = $scope.confirmPin = '';
$interval.cancel(countDown); $interval.cancel(countDown);
$timeout(function() { $timeout(function() {
$scope.$apply(); $scope.$apply();
@ -84,20 +77,20 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
}; };
$scope.getFilledClass = function(limit) { $scope.getFilledClass = function(limit) {
return $scope.currentPin.length >= limit ? 'filled-' + $scope.appName : null; return currentPin.length >= limit ? 'filled-' + $scope.appName : null;
}; };
$scope.delete = function() { $scope.delete = function() {
if ($scope.disableButtons) return; if ($scope.disableButtons) return;
if ($scope.currentPin.length > 0) { if (currentPin.length > 0) {
$scope.currentPin = $scope.currentPin.substring(0, $scope.currentPin.length - 1); currentPin = currentPin.substring(0, currentPin.length - 1);
$scope.error = false; $scope.error = false;
$scope.updatePin(); $scope.updatePin();
} }
}; };
$scope.isComplete = function() { $scope.isComplete = function() {
if ($scope.currentPin.length < 4) return false; if (currentPin.length < 4) return false;
else return true; else return true;
}; };
@ -105,7 +98,7 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
if ($scope.disableButtons) return; if ($scope.disableButtons) return;
$scope.error = false; $scope.error = false;
if (value && !$scope.isComplete()) { if (value && !$scope.isComplete()) {
$scope.currentPin = $scope.currentPin + value; currentPin = currentPin + value;
$timeout(function() { $timeout(function() {
$scope.$apply(); $scope.$apply();
}); });
@ -113,40 +106,56 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
$scope.save(); $scope.save();
}; };
function isMatch(pin) {
var config = configService.getSync();
return config.lock.value == pin;
};
$scope.save = function() { $scope.save = function() {
if (!$scope.isComplete()) return; if (!$scope.isComplete()) return;
var config = configService.getSync(); var savedMethod = getSavedMethod();
$scope.match = config.lock && config.lock.method == 'pin' && config.lock.value == $scope.currentPin ? true : false;
if (!$scope.locking) { switch ($scope.action) {
if ($scope.match) { case 'setup':
if ($scope.fromSettings) saveSettings(); applyAndCheckPin();
else { break;
saveSettings('pin', $scope.currentPin); case 'disable':
$scope.error = false; if (isMatch(currentPin)) {
deletePin();
} else {
showError();
checkAttempts();
} }
} else { break;
$timeout(function() { case 'check':
$scope.confirmPin = $scope.currentPin = ''; if (isMatch(currentPin)) return $scope.close();
$scope.error = true; showError();
}, 200); break;
checkAttempts();
}
} else {
processCodes();
} }
}; };
function processCodes() { function showError() {
$timeout(function() {
$scope.confirmPin = currentPin = '';
$scope.error = true;
}, 200);
$timeout(function() {
$scope.$apply();
});
};
function applyAndCheckPin() {
if (!$scope.confirmPin) { if (!$scope.confirmPin) {
$timeout(function() { $timeout(function() {
$scope.confirmPin = $scope.currentPin; $scope.confirmPin = currentPin;
$scope.currentPin = ''; currentPin = '';
}, 200); }, 200);
} else { } else {
if ($scope.confirmPin == $scope.currentPin) if ($scope.confirmPin == currentPin)
saveSettings('pin', $scope.confirmPin); savePin($scope.confirmPin);
else { else {
$scope.confirmPin = $scope.currentPin = ''; $scope.confirmPin = currentPin = '';
$scope.error = true; $scope.error = true;
} }
} }
@ -155,15 +164,12 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
}); });
}; };
function saveSettings(method, value) { function deletePin() {
var config = configService.getSync();
var attempts = config.lock && config.lock.attempts ? config.lock.attempts : 0;
var opts = { var opts = {
lock: { lock: {
method: method || null, method: 'none',
value: value || null, value: null,
bannedUntil: null, bannedUntil: null,
attempts: attempts + 1,
} }
}; };
@ -173,6 +179,34 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
}); });
}; };
function savePin(value) {
var opts = {
lock: {
method: 'pin',
value: value,
bannedUntil: null,
}
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
$scope.close();
});
};
function saveFailedAttempt(bannedUntil) {
var opts = {
lock: {
bannedUntil: bannedUntil,
}
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
lockTimeControl(limitTime);
});
};
$scope.close = function(delay) { $scope.close = function(delay) {
$timeout(function() { $timeout(function() {
var shouldReturn = $ionicHistory.viewHistory().backView && $ionicHistory.viewHistory().backView.stateName != 'starting'; var shouldReturn = $ionicHistory.viewHistory().backView && $ionicHistory.viewHistory().backView.stateName != 'starting';

View file

@ -55,7 +55,10 @@ angular.module('copayApp.controllers').controller('tabSettingsController', funct
$scope.appName = appConfigService.nameCase; $scope.appName = appConfigService.nameCase;
configService.whenAvailable(function(config) { configService.whenAvailable(function(config) {
$scope.locked = config.lock && config.lock.method; $scope.locked = config.lock && config.lock.method;
$scope.method = $scope.locked ? config.lock.method.charAt(0).toUpperCase() + config.lock.method.slice(1) : gettextCatalog.getString('Disabled'); if (!$scope.locked || $scope.locked == 'none')
$scope.method = gettextCatalog.getString('Disabled');
else
$scope.method = $scope.locked.charAt(0).toUpperCase() + config.lock.method.slice(1);
}); });
}); });

View file

@ -126,7 +126,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
*/ */
.state('pin', { .state('pin', {
url: '/pin/', url: '/pin/:action',
controller: 'pinController', controller: 'pinController',
templateUrl: 'views/pin.html', templateUrl: 'views/pin.html',
}) })
@ -473,7 +473,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
} }
}) })
.state('tabs.pin', { .state('tabs.pin', {
url: '/pin/:fromSettings/:locking', url: '/pin/:action',
views: { views: {
'tab-settings@tabs': { 'tab-settings@tabs': {
controller: 'pinController', controller: 'pinController',
@ -1214,7 +1214,9 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
function goTo(nextView) { function goTo(nextView) {
nextView = nextView || defaultView; nextView = nextView || defaultView;
$state.transitionTo(nextView).then(function() { $state.transitionTo(nextView, {
action: 'check'
}).then(function() {
if (nextView == 'lockedView') if (nextView == 'lockedView')
$ionicHistory.clearHistory(); $ionicHistory.clearHistory();
}); });
@ -1230,7 +1232,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
fingerprintService.check('unlockingApp', function(err) { fingerprintService.check('unlockingApp', function(err) {
if (err) if (err)
goTo('lockedView'); goTo('lockedView');
if ($ionicHistory.currentStateName() == 'lockedView' || !onResume) else if ($ionicHistory.currentStateName() == 'lockedView' || !onResume)
goTo('tabs.home'); goTo('tabs.home');
}); });
} else if (lockMethod == 'pin') { } else if (lockMethod == 'pin') {

View file

@ -57,7 +57,6 @@ angular.module('copayApp.services').factory('configService', function(storageSer
method: null, method: null,
value: null, value: null,
bannedUntil: null, bannedUntil: null,
attempts: null,
}, },
// External services // External services

View file

@ -1,4 +1,4 @@
<ion-view id="pin" hide-tabs hide-back-button="!fromSettings"> <ion-view id="pin" hide-tabs hide-back-button="action == 'check'">
<ion-nav-bar class="bar-clear"> <ion-nav-bar class="bar-clear">
<ion-nav-back-button> <ion-nav-back-button>
</ion-nav-back-button> </ion-nav-back-button>