Merge pull request #5910 from JDonadio/ref/pin-request

Request PIN to disable it
This commit is contained in:
Matias Alejo Garcia 2017-04-24 13:45:20 +02:00 committed by GitHub
commit aceef14276
7 changed files with 185 additions and 107 deletions

View File

@ -1,17 +1,19 @@
'use strict';
angular.module('copayApp.controllers').controller('lockSetupController', function($state, $scope, $timeout, $log, configService, popupService, gettextCatalog, appConfigService, fingerprintService, profileService, lodash) {
angular.module('copayApp.controllers').controller('lockSetupController', function($state, $scope, $timeout, $log, configService, gettextCatalog, fingerprintService, profileService, lodash) {
function init() {
$scope.options = [
{
method: null,
method: 'none',
label: gettextCatalog.getString('Disabled'),
disabled: false,
},
{
method: 'pin',
label: gettextCatalog.getString('Lock by PIN'),
needsBackup: null,
needsBackup: false,
disabled: false,
},
];
@ -19,16 +21,12 @@ angular.module('copayApp.controllers').controller('lockSetupController', functio
$scope.options.push({
method: 'fingerprint',
label: gettextCatalog.getString('Lock by Fingerprint'),
needsBackup: null,
needsBackup: false,
disabled: false,
});
}
var config = configService.getSync();
var method = config.lock && config.lock.method;
if (!method) $scope.currentOption = $scope.options[0];
else $scope.currentOption = lodash.find($scope.options, {
'method': method
});
initMethodSelector();
processWallets();
};
@ -36,6 +34,45 @@ angular.module('copayApp.controllers').controller('lockSetupController', functio
init();
});
function getSavedMethod() {
var config = configService.getSync();
if (config.lock) return config.lock.method;
return 'none';
};
function initMethodSelector() {
function disable(method) {
lodash.find($scope.options, {
method: method
}).disabled = true;
};
var savedMethod = getSavedMethod();
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() {
$scope.$apply();
});
};
function processWallets() {
var wallets = profileService.getWallets();
var singleLivenetWallet = wallets.length == 1 && wallets[0].network == 'livenet' && wallets[0].needsBackup;
@ -70,52 +107,56 @@ angular.module('copayApp.controllers').controller('lockSetupController', functio
};
$scope.select = function(selectedMethod) {
var config = configService.getSync();
var savedMethod = config.lock && config.lock.method;
var savedMethod = getSavedMethod();
if (savedMethod == selectedMethod) return;
if (!selectedMethod)
saveConfig();
else if (selectedMethod == 'fingerprint') {
if (savedMethod == 'pin') {
askForDisablePin(function(disablePin) {
if (disablePin) saveConfig('fingerprint');
else init();
});
} else saveConfig('fingerprint');
} else if (selectedMethod == 'pin') {
if (savedMethod == 'pin') return;
$state.transitionTo('tabs.pin', {
fromSettings: true,
locking: savedMethod == 'pin' ? false : true
});
if (selectedMethod == 'none') {
disableMethod(savedMethod);
} else {
enableMethod(selectedMethod);
}
$timeout(function() {
$scope.$apply();
});
};
function askForDisablePin(cb) {
var message = gettextCatalog.getString('{{appName}} startup is locked by PIN. Are you sure you want to disable it?', {
appName: appConfigService.nameCase
});
var okText = gettextCatalog.getString('Continue');
var cancelText = gettextCatalog.getString('Cancel');
popupService.showConfirm(null, message, okText, cancelText, function(ok) {
if (!ok) return cb(false);
return cb(true);
});
function disableMethod(method) {
switch (method) {
case 'pin':
$state.transitionTo('tabs.pin', {
action: 'disable'
});
break;
case '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;
}
};
function saveConfig(method) {
var opts = {
lock: {
method: method || null,
method: method,
value: null,
}
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
initMethodSelector();
});
};
});

View File

@ -3,11 +3,11 @@
angular.module('copayApp.controllers').controller('pinController', function($state, $interval, $stateParams, $ionicHistory, $timeout, $scope, $log, configService, appConfigService) {
var ATTEMPT_LIMIT = 3;
var ATTEMPT_LOCK_OUT_TIME = 5 * 60;
var currentPin;
$scope.$on("$ionicView.beforeEnter", function(event) {
$scope.currentPin = $scope.confirmPin = '';
$scope.fromSettings = $stateParams.fromSettings == 'true' ? true : false;
$scope.locking = $stateParams.locking == 'true' ? true : false;
currentPin = $scope.confirmPin = '';
$scope.action = $stateParams.action;
$scope.match = $scope.error = $scope.disableButtons = false;
$scope.currentAttempts = 0;
$scope.appName = appConfigService.name;
@ -27,31 +27,23 @@ 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() {
$scope.currentAttempts += 1;
$log.debug('Attempts to unlock:', $scope.currentAttempts);
if ($scope.currentAttempts === ATTEMPT_LIMIT) {
$scope.currentAttempts = 0;
var limitTime = Math.floor(Date.now() / 1000) + ATTEMPT_LOCK_OUT_TIME;
var config = configService.getSync();
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);
});
var bannedUntil = Math.floor(Date.now() / 1000) + ATTEMPT_LOCK_OUT_TIME;
saveFailedAttempt(bannedUntil);
}
};
function lockTimeControl(limitTime) {
$scope.limitTimeExpired = false;
function lockTimeControl(bannedUntil) {
setExpirationTime();
var countDown = $interval(function() {
@ -60,12 +52,11 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
function setExpirationTime() {
var now = Math.floor(Date.now() / 1000);
if (now > limitTime) {
$scope.limitTimeExpired = true;
if (now > bannedUntil) {
if (countDown) reset();
} else {
$scope.disableButtons = true;
var totalSecs = limitTime - now;
var totalSecs = bannedUntil - now;
var m = Math.floor(totalSecs / 60);
var s = totalSecs % 60;
$scope.expires = ('0' + m).slice(-2) + ":" + ('0' + s).slice(-2);
@ -74,7 +65,7 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
function reset() {
$scope.expires = $scope.error = $scope.disableButtons = null;
$scope.currentPin = $scope.confirmPin = '';
currentPin = $scope.confirmPin = '';
$interval.cancel(countDown);
$timeout(function() {
$scope.$apply();
@ -84,20 +75,20 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
};
$scope.getFilledClass = function(limit) {
return $scope.currentPin.length >= limit ? 'filled-' + $scope.appName : null;
return currentPin.length >= limit ? 'filled-' + $scope.appName : null;
};
$scope.delete = function() {
if ($scope.disableButtons) return;
if ($scope.currentPin.length > 0) {
$scope.currentPin = $scope.currentPin.substring(0, $scope.currentPin.length - 1);
if (currentPin.length > 0) {
currentPin = currentPin.substring(0, currentPin.length - 1);
$scope.error = false;
$scope.updatePin();
}
};
$scope.isComplete = function() {
if ($scope.currentPin.length < 4) return false;
if (currentPin.length < 4) return false;
else return true;
};
@ -105,7 +96,7 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
if ($scope.disableButtons) return;
$scope.error = false;
if (value && !$scope.isComplete()) {
$scope.currentPin = $scope.currentPin + value;
currentPin = currentPin + value;
$timeout(function() {
$scope.$apply();
});
@ -113,40 +104,57 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
$scope.save();
};
function isMatch(pin) {
var config = configService.getSync();
return config.lock.value == pin;
};
$scope.save = function() {
if (!$scope.isComplete()) return;
var config = configService.getSync();
$scope.match = config.lock && config.lock.method == 'pin' && config.lock.value == $scope.currentPin ? true : false;
if (!$scope.locking) {
if ($scope.match) {
if ($scope.fromSettings) saveSettings();
else {
saveSettings('pin', $scope.currentPin);
$scope.error = false;
var savedMethod = getSavedMethod();
switch ($scope.action) {
case 'setup':
applyAndCheckPin();
break;
case 'disable':
if (isMatch(currentPin)) {
deletePin();
} else {
showError();
checkAttempts();
}
} else {
$timeout(function() {
$scope.confirmPin = $scope.currentPin = '';
$scope.error = true;
}, 200);
break;
case 'check':
if (isMatch(currentPin)) return $scope.close();
showError();
checkAttempts();
}
} else {
processCodes();
break;
}
};
function processCodes() {
function showError() {
$timeout(function() {
$scope.confirmPin = currentPin = '';
$scope.error = true;
}, 200);
$timeout(function() {
$scope.$apply();
});
};
function applyAndCheckPin() {
if (!$scope.confirmPin) {
$timeout(function() {
$scope.confirmPin = $scope.currentPin;
$scope.currentPin = '';
$scope.confirmPin = currentPin;
currentPin = '';
}, 200);
} else {
if ($scope.confirmPin == $scope.currentPin)
saveSettings('pin', $scope.confirmPin);
if ($scope.confirmPin == currentPin)
savePin($scope.confirmPin);
else {
$scope.confirmPin = $scope.currentPin = '';
$scope.confirmPin = currentPin = '';
$scope.error = true;
}
}
@ -155,15 +163,12 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
});
};
function saveSettings(method, value) {
var config = configService.getSync();
var attempts = config.lock && config.lock.attempts ? config.lock.attempts : 0;
function deletePin() {
var opts = {
lock: {
method: method || null,
value: value || null,
method: 'none',
value: null,
bannedUntil: null,
attempts: attempts + 1,
}
};
@ -173,6 +178,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(bannedUntil);
});
};
$scope.close = function(delay) {
$timeout(function() {
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;
configService.whenAvailable(function(config) {
$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', {
url: '/pin/',
url: '/pin/:action',
controller: 'pinController',
templateUrl: 'views/pin.html',
})
@ -473,7 +473,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
}
})
.state('tabs.pin', {
url: '/pin/:fromSettings/:locking',
url: '/pin/:action',
views: {
'tab-settings@tabs': {
controller: 'pinController',
@ -1214,7 +1214,9 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
function goTo(nextView) {
nextView = nextView || defaultView;
$state.transitionTo(nextView).then(function() {
$state.transitionTo(nextView, {
action: 'check'
}).then(function() {
if (nextView == 'lockedView')
$ionicHistory.clearHistory();
});
@ -1230,7 +1232,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
fingerprintService.check('unlockingApp', function(err) {
if (err)
goTo('lockedView');
if ($ionicHistory.currentStateName() == 'lockedView' || !onResume)
else if ($ionicHistory.currentStateName() == 'lockedView' || !onResume)
goTo('tabs.home');
});
} else if (lockMethod == 'pin') {

View File

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

View File

@ -6,8 +6,8 @@
</ion-nav-bar>
<ion-content>
<ion-radio ng-repeat="opt in options" ng-value="opt" ng-model="currentOption" ng-click="select(opt.method)" ng-disabled="opt.needsBackup">
<span ng-class="{'disabled': opt.needsBackup}">{{opt.label}}</span>
<ion-radio ng-repeat="opt in options" ng-value="opt" ng-model="currentOption" ng-click="select(opt.method)" ng-disabled="opt.needsBackup || opt.disabled">
<span ng-class="{'disabled': opt.needsBackup || opt.disabled}">{{opt.label}}</span>
</ion-radio>
<div class="assertive" style="text-align: center; margin: 4rem" ng-if="errorMsg">

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-back-button>
</ion-nav-back-button>