From c29af5f658ad457e429d5acda62415487f4e22e2 Mon Sep 17 00:00:00 2001 From: JDonadio Date: Thu, 30 Mar 2017 11:31:23 -0300 Subject: [PATCH] attempts limit - remaining time --- src/js/controllers/pin.js | 97 +++++++++++++++++++++++++++++--- src/js/services/configService.js | 2 + src/sass/views/pin.scss | 5 +- www/views/pin.html | 9 ++- 4 files changed, 101 insertions(+), 12 deletions(-) diff --git a/src/js/controllers/pin.js b/src/js/controllers/pin.js index 411683e3e..9d78b9e65 100644 --- a/src/js/controllers/pin.js +++ b/src/js/controllers/pin.js @@ -1,22 +1,93 @@ 'use strict'; -angular.module('copayApp.controllers').controller('pinController', function($state, $stateParams, $ionicHistory, $timeout, $scope, $log, configService, appConfigService) { +angular.module('copayApp.controllers').controller('pinController', function($state, $interval, $stateParams, $ionicHistory, $timeout, $scope, $log, configService, appConfigService) { var PIN = 'pin'; + var ATTEPMPTS_LIMIT = 3; $scope.$on("$ionicView.beforeEnter", function(event) { $scope.currentPin = $scope.confirmPin = ''; $scope.fromSettings = $stateParams.fromSettings == 'true' ? true : false; $scope.locking = $stateParams.locking == 'true' ? true : false; - $scope.match = false; - $scope.error = false; + $scope.match = $scope.error = $scope.disableButtons = false; + $scope.currentAttempts = 0; $scope.appName = appConfigService.name; }); + $scope.$on("$ionicView.enter", function(event) { + configService.whenAvailable(function(config) { + $scope.bannedUntil = config.lock.bannedUntil || null; + if ($scope.bannedUntil) { + var now = Math.floor(Date.now() / 1000); + if (now < $scope.bannedUntil) { + $scope.error = $scope.disableButtons = true; + lockTimeControl($scope.bannedUntil); + } + } + }); + }); + + function checkAttempts() { + $scope.currentAttempts += 1; + $log.debug('Attempts to unlock:', $scope.currentAttempts); + if ($scope.currentAttempts === 3) { + $scope.currentAttempts = 0; + var limitTime = Math.floor(Date.now() / 1000) + 5 * 60; + 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); + }); + } + }; + + function lockTimeControl(limitTime) { + $scope.limitTimeExpired = false; + setExpirationTime(); + + var countDown = $interval(function() { + setExpirationTime(); + }, 1000); + + function setExpirationTime() { + var now = Math.floor(Date.now() / 1000); + if (now > limitTime) { + $scope.limitTimeExpired = true; + if (countDown) reset(); + } else { + $scope.disableButtons = true; + var totalSecs = limitTime - now; + var m = Math.floor(totalSecs / 60); + var s = totalSecs % 60; + $scope.expires = ('0' + m).slice(-2) + ":" + ('0' + s).slice(-2); + } + }; + + function reset() { + $scope.expires = $scope.error = $scope.disableButtons = null; + $scope.currentPin = $scope.confirmPin = ''; + $interval.cancel(countDown); + $timeout(function() { + $scope.$apply(); + }); + return; + }; + }; + $scope.getFilledClass = function(limit) { return $scope.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); $scope.error = false; @@ -30,6 +101,7 @@ angular.module('copayApp.controllers').controller('pinController', function($sta }; $scope.updatePin = function(value) { + if ($scope.disableButtons) return; $scope.error = false; if (value && !$scope.isComplete()) { $scope.currentPin = $scope.currentPin + value; @@ -46,11 +118,17 @@ angular.module('copayApp.controllers').controller('pinController', function($sta $scope.match = config.lock && config.lock.method == PIN && config.lock.value == $scope.currentPin ? true : false; if (!$scope.locking) { if ($scope.match) { - $scope.fromSettings ? saveSettings() : $scope.close(150); - $scope.error = false; + if ($scope.fromSettings) saveSettings(); + else { + saveSettings(PIN, $scope.currentPin); + $scope.error = false; + } } else { - $scope.confirmPin = $scope.currentPin = ''; - $scope.error = true; + $timeout(function() { + $scope.confirmPin = $scope.currentPin = ''; + $scope.error = true; + }, 200); + checkAttempts(); } } else { processCodes(); @@ -59,8 +137,8 @@ angular.module('copayApp.controllers').controller('pinController', function($sta function processCodes() { if (!$scope.confirmPin) { - $scope.confirmPin = $scope.currentPin; $timeout(function() { + $scope.confirmPin = $scope.currentPin; $scope.currentPin = ''; }, 200); } else { @@ -77,10 +155,13 @@ angular.module('copayApp.controllers').controller('pinController', function($sta }; function saveSettings(method, value) { + var config = configService.getSync(); var opts = { lock: { method: method || '', value: value || '', + bannedUntil: null, + attempts: config.lock.attempts + 1, } }; diff --git a/src/js/services/configService.js b/src/js/services/configService.js index b60e1cb13..3d979af7f 100644 --- a/src/js/services/configService.js +++ b/src/js/services/configService.js @@ -56,6 +56,8 @@ angular.module('copayApp.services').factory('configService', function(storageSer lock: { method: '', value: '', + bannedUntil: null, + attempts: null, }, // External services diff --git a/src/sass/views/pin.scss b/src/sass/views/pin.scss index 6f3733230..3d695f185 100644 --- a/src/sass/views/pin.scss +++ b/src/sass/views/pin.scss @@ -25,9 +25,11 @@ .block-text { align-items: center; background-color: #F1F1F1; - text-align: center; height: 30%; border-bottom: 1px solid #c5c5c5; + .message { + margin: auto; + } span { width: 60%; margin: 10% auto; @@ -88,5 +90,6 @@ } .error { color: #f13333; + max-width: 70%; } } diff --git a/www/views/pin.html b/www/views/pin.html index f1c4f6fa0..613bd3a66 100644 --- a/www/views/pin.html +++ b/www/views/pin.html @@ -5,9 +5,12 @@
- Please enter your mobile unlock code - Confirm your mobile unlock code - Incorrect code, try again. +
Please enter your unlock PIN
+
Confirm your unlock PIN
+
+
Incorrect PIN, try again.
+ +