Skip to content
Snippets Groups Projects

Resolve "System should block the login option for defined period of time, when user enter wrong local device PIN more than 3 attempts"

@@ -544,17 +544,77 @@ function encryptMessage(message, password, label) {
//*********************************************************************************
function decryptMessage(message, password) {
const secret = pvutils.stringToArrayBuffer(password);
const buffer = decodePEM(message);
const asn1 = asn1js.fromBER(buffer);
const content = new pkijs.ContentInfo({schema: asn1.result});
const enveloped = new pkijs.EnvelopedData({schema: content.content});
return enveloped.decrypt(0, {preDefinedData: secret}).then(result => {
return pvutils.arrayBufferToString(result);
}).catch(() => {
throw("Wrong pincode")
})
if (canTryPincode()) {
const secret = pvutils.stringToArrayBuffer(password);
const buffer = decodePEM(message);
const asn1 = asn1js.fromBER(buffer);
const content = new pkijs.ContentInfo({schema: asn1.result});
const enveloped = new pkijs.EnvelopedData({schema: content.content});
return enveloped.decrypt(0, {preDefinedData: secret}).then(result => {
return pvutils.arrayBufferToString(result);
}).catch(() => {
return Promise.reject(failPincodeAttempt(password));
});
} else {
return Promise.reject(getTimeLeftInLocalStorage());
}
}
//*********************************************************************************
function getBlockFinishTimeInLocalStorage() {
return localStorage.getItem("blockFinishTime") || getCurrentTime();
}
function getCurrentTime() {
return Math.floor(new Date().getTime() / 1000);
}
function getTimeLeftInLocalStorage() {
const blockFinishTime = getBlockFinishTimeInLocalStorage();
const timeNow = getCurrentTime();
const seconds = (blockFinishTime - timeNow) % 60;
let minutes = Math.floor((blockFinishTime - timeNow) / 60);
minutes %= 60;
const left = "Your identity has been blocked. Try again in " + minutes + " minutes and " + seconds + " seconds.";
return left;
}
function failPincodeAttempt(password) {
let message = "Wrong pincode";
if (password !== '00000000') {
let attempt = localStorage.getItem("attempt") || 1;
attempt = parseInt(attempt);
if (attempt === 9) {
const identitiesTemp = listIdentitiesFromLocalStorage();
for(let i in identitiesTemp) {
destroyIdentityFromLocalStorage(i);
}
message = "9 failed attempts. Identity is revoked!";
localStorage.removeItem("attempt");
} else if (attempt % 3 === 0) {
const timeNow = getCurrentTime();
const blockFinishTime = timeNow + 300;
localStorage.setItem("blockFinishTime", blockFinishTime);
localStorage.setItem("attempt", attempt + 1);
message = "3 failed attempts. Identity is blocked!";
} else {
localStorage.setItem("attempt", attempt + 1);
}
}
return message;
}
function canTryPincode() {
const timeNow = getCurrentTime();
const blockFinishTime = localStorage.getItem("blockFinishTime") || timeNow;
if (blockFinishTime <= timeNow) {
localStorage.removeItem("blockFinishTime");
return true;
} else {
return false;
}
}
//*********************************************************************************
@@ -1407,6 +1467,7 @@ function loadIdentityInternal(identityKey, pinCode) {
"status": "Can not load identity"
});
}
localStorage.removeItem("attempt");
const copiedIdentity = JSON.parse(JSON.stringify(loadedIdentity));
window.loadedIdentities[identityKey] = loadedIdentity;
Loading