Wallet/src/js/controllers/pin.js

181 lines
5.1 KiB
JavaScript
Raw Normal View History

2017-03-29 11:30:16 -03:00
'use strict';
2017-03-30 11:31:23 -03:00
angular.module('copayApp.controllers').controller('pinController', function($state, $interval, $stateParams, $ionicHistory, $timeout, $scope, $log, configService, appConfigService) {
2017-04-04 18:06:11 -03:00
var ATTEMPT_LIMIT = 3;
var ATTEMPT_LOCK_OUT_TIME = 5 * 60;
2017-03-29 11:30:16 -03:00
$scope.$on("$ionicView.beforeEnter", function(event) {
$scope.currentPin = $scope.confirmPin = '';
$scope.fromSettings = $stateParams.fromSettings == 'true' ? true : false;
$scope.locking = $stateParams.locking == 'true' ? true : false;
2017-03-30 11:31:23 -03:00
$scope.match = $scope.error = $scope.disableButtons = false;
$scope.currentAttempts = 0;
2017-03-29 11:30:16 -03:00
$scope.appName = appConfigService.name;
});
2017-03-30 11:31:23 -03:00
$scope.$on("$ionicView.enter", function(event) {
configService.whenAvailable(function(config) {
2017-04-18 10:18:55 -03:00
if (!config.lock) return;
2017-03-30 11:31:23 -03:00
$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);
2017-04-04 18:06:11 -03:00
if ($scope.currentAttempts === ATTEMPT_LIMIT) {
2017-03-30 11:31:23 -03:00
$scope.currentAttempts = 0;
2017-04-04 18:06:11 -03:00
var limitTime = Math.floor(Date.now() / 1000) + ATTEMPT_LOCK_OUT_TIME;
2017-03-30 11:31:23 -03:00
var config = configService.getSync();
var opts = {
lock: {
2017-04-04 18:06:11 -03:00
method: 'pin',
2017-03-30 11:31:23 -03:00
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;
};
};
2017-03-29 11:30:16 -03:00
$scope.getFilledClass = function(limit) {
return $scope.currentPin.length >= limit ? 'filled-' + $scope.appName : null;
};
$scope.delete = function() {
2017-03-30 11:31:23 -03:00
if ($scope.disableButtons) return;
2017-03-29 11:30:16 -03:00
if ($scope.currentPin.length > 0) {
$scope.currentPin = $scope.currentPin.substring(0, $scope.currentPin.length - 1);
$scope.error = false;
$scope.updatePin();
}
};
$scope.isComplete = function() {
if ($scope.currentPin.length < 4) return false;
else return true;
};
$scope.updatePin = function(value) {
2017-03-30 11:31:23 -03:00
if ($scope.disableButtons) return;
2017-03-29 11:30:16 -03:00
$scope.error = false;
if (value && !$scope.isComplete()) {
$scope.currentPin = $scope.currentPin + value;
$timeout(function() {
$scope.$apply();
});
}
$scope.save();
};
$scope.save = function() {
if (!$scope.isComplete()) return;
var config = configService.getSync();
2017-04-04 18:06:11 -03:00
$scope.match = config.lock && config.lock.method == 'pin' && config.lock.value == $scope.currentPin ? true : false;
2017-03-29 11:30:16 -03:00
if (!$scope.locking) {
if ($scope.match) {
2017-03-30 11:31:23 -03:00
if ($scope.fromSettings) saveSettings();
else {
2017-04-04 18:06:11 -03:00
saveSettings('pin', $scope.currentPin);
2017-03-30 11:31:23 -03:00
$scope.error = false;
}
2017-03-29 11:30:16 -03:00
} else {
2017-03-30 11:31:23 -03:00
$timeout(function() {
$scope.confirmPin = $scope.currentPin = '';
$scope.error = true;
}, 200);
checkAttempts();
2017-03-29 11:30:16 -03:00
}
} else {
processCodes();
}
};
function processCodes() {
if (!$scope.confirmPin) {
$timeout(function() {
2017-03-30 11:31:23 -03:00
$scope.confirmPin = $scope.currentPin;
2017-03-29 11:30:16 -03:00
$scope.currentPin = '';
}, 200);
} else {
if ($scope.confirmPin == $scope.currentPin)
2017-04-04 18:06:11 -03:00
saveSettings('pin', $scope.confirmPin);
2017-03-29 11:30:16 -03:00
else {
$scope.confirmPin = $scope.currentPin = '';
$scope.error = true;
}
}
$timeout(function() {
$scope.$apply();
});
};
function saveSettings(method, value) {
2017-03-30 11:31:23 -03:00
var config = configService.getSync();
2017-04-18 10:18:55 -03:00
var attempts = config.lock && config.lock.attempts ? config.lock.attempts : 0;
2017-03-29 11:30:16 -03:00
var opts = {
lock: {
method: method || '',
value: value || '',
2017-03-30 11:31:23 -03:00
bannedUntil: null,
2017-04-18 10:18:55 -03:00
attempts: attempts + 1,
2017-03-29 11:30:16 -03:00
}
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
$scope.close();
});
};
$scope.close = function(delay) {
$timeout(function() {
$ionicHistory.viewHistory().backView ? $ionicHistory.goBack() : $state.go('tabs.home');
}, delay || 1);
};
});