From 4bb31f8e71c666fa5f6fdb718d24bc657469bc91 Mon Sep 17 00:00:00 2001 From: Juan Lago Date: Tue, 11 Aug 2020 11:48:02 +0200 Subject: [PATCH] Implemented custom password level check based on regular expressions --- src/components/password/Password.d.ts | 2 + src/components/password/Password.vue | 73 +++++++++++++-------------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/components/password/Password.d.ts b/src/components/password/Password.d.ts index 366ad6a0f..e63b691ed 100755 --- a/src/components/password/Password.d.ts +++ b/src/components/password/Password.d.ts @@ -3,6 +3,8 @@ import Vue from 'vue'; export declare class Password extends Vue { value?: string; promptLabel?: string; + mediumCheckExpr?: string; + strongCheckExpr?: string; weakLabel?: string; mediumLabel?: string; strongLabel?: string; diff --git a/src/components/password/Password.vue b/src/components/password/Password.vue index 9dfec64cf..893da7caf 100755 --- a/src/components/password/Password.vue +++ b/src/components/password/Password.vue @@ -12,6 +12,14 @@ export default { type: String, default: 'Enter a password' }, + mediumCheckExpr: { + type: String, + default: '^(((?=.*[a-z])(?=.*[A-Z]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[A-Z])(?=.*[0-9])))(?=.{6,})' // eslint-disable-line + }, + strongCheckExpr: { + type: String, + default: '^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})' // eslint-disable-line + }, weakLabel: { type: String, default: 'Weak' @@ -32,34 +40,24 @@ export default { panel: null, meter: null, info: null, + mediumCheckRegExp: null, + strongCheckRegExp: null, + mounted() { + this.mediumCheckRegExp = new RegExp(this.mediumCheckExpr); + this.strongCheckRegExp = new RegExp(this.strongCheckExpr); + }, methods: { testStrength(str) { - let grade = 0; - let val; + let level = 0; - val = str.match('[0-9]'); - grade += this.normalize(val ? val.length : 1/4, 1) * 25; + if (this.strongCheckRegExp.test(str)) + level = 3; + else if (this.mediumCheckRegExp.test(str)) + level = 2; + else if (str.length) + level = 1; - val = str.match('[a-zA-Z]'); - grade += this.normalize(val ? val.length : 1/2, 3) * 10; - - val = str.match('[!@#$%^&*?_~.,;=]'); - grade += this.normalize(val ? val.length : 1/6, 1) * 35; - - val = str.match('[A-Z]'); - grade += this.normalize(val ? val.length : 1/6, 1) * 30; - - grade *= str.length / 8; - - return grade > 100 ? 100 : grade; - }, - normalize(x, y) { - let diff = x - y; - - if(diff <= 0) - return x / y; - else - return 1 + 0.5 * (x / (x + y/4)); + return level; }, createPanel() { this.panel = document.createElement('div'); @@ -119,25 +117,26 @@ export default { let label = null; let meterPos = null; - if (value.length === 0) { - label = this.promptLabel; - meterPos = '0px 0px'; - } - else { - let score = this.testStrength(value); - - if (score < 30) { + switch (this.testStrength(value)) { + case 1: label = this.weakLabel; meterPos = '0px -10px'; - } - else if (score >= 30 && score < 80) { + break; + + case 2: label = this.mediumLabel; meterPos = '0px -20px'; - } - else if (score >= 80) { + break; + + case 3: label = this.strongLabel; meterPos = '0px -30px'; - } + break; + + default: + label = this.promptLabel; + meterPos = '0px 0px'; + break; } vm.meter.style.backgroundPosition = meterPos;