/
PasswordStrength.vue
208 lines (181 loc) · 5.74 KB
/
PasswordStrength.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (C) 2024 Storj Labs, Inc.
// See LICENSE for copying information.
<template>
<v-card
class="positioning mt-n2"
width="100%"
position="absolute"
elevation="12"
variant="elevated"
>
<v-card-item>
<p class="font-weight-bold mt-1 mb-3">Password strength</p>
<p class="text-body-2 font-weight-bold mb-2" :style="strengthLabelColor">{{ passwordStrength }}</p>
<v-progress-linear :model-value="barWidth" :color="passwordStrengthColor" rounded="lg" />
</v-card-item>
<v-card-item>
<p class="text-body-2">Your password should contain:</p>
<v-checkbox
tabindex="-1"
class="no-pointer-events text-body-2"
:model-value="isPasswordLengthAcceptable"
color="success"
density="compact"
hide-details
>
<template #label>
<p class="text-body-2">Between {{ passMinLength }} and {{ passMaxLength }} Latin characters</p>
</template>
</v-checkbox>
<p class="text-body-2 mt-2">Its nice to have:</p>
<v-checkbox
tabindex="-1"
class="no-pointer-events text-body-2"
:model-value="hasLowerAndUpperCaseLetters"
color="success"
density="compact"
hide-details
>
<template #label>
<p class="text-body-2">Upper and lowercase letters</p>
</template>
</v-checkbox>
<v-checkbox
tabindex="-1"
class="no-pointer-events text-body-2 mt-n2"
:model-value="hasSpecialCharacter"
color="success"
density="compact"
hide-details
>
<template #label>
<p class="text-body-2">At least one special character</p>
</template>
</v-checkbox>
<p class="text-caption mt-2">
Avoid using a password that you use on other websites or that might be easily guessed by someone else.
</p>
</v-card-item>
</v-card>
</template>
<script setup lang="ts">
import { computed } from 'vue';
import {
VCard,
VCardItem,
VProgressLinear,
VCheckbox,
} from 'vuetify/components';
import { useConfigStore } from '@/store/modules/configStore';
const configStore = useConfigStore();
const PASSWORD_STRENGTH = {
veryStrong: 'Very Strong',
strong: 'Strong',
good: 'Good',
weak: 'Weak',
};
const PASSWORD_STRENGTH_COLORS = {
[PASSWORD_STRENGTH.good]: '#ffa500',
[PASSWORD_STRENGTH.strong]: '#00AC26',
[PASSWORD_STRENGTH.veryStrong]: '#00AC26',
default: '#ff0000',
};
const BAR_WIDTH = {
[PASSWORD_STRENGTH.weak]: '25',
[PASSWORD_STRENGTH.good]: '50',
[PASSWORD_STRENGTH.strong]: '75',
[PASSWORD_STRENGTH.veryStrong]: '100',
default: '0',
};
const props = defineProps<{
password: string;
}>();
/**
* Returns the maximum password length from the store.
*/
const passMaxLength = computed((): number => {
return configStore.state.config.passwordMaximumLength;
});
/**
* Returns the minimum password length from the store.
*/
const passMinLength = computed((): number => {
return configStore.state.config.passwordMinimumLength;
});
const isPasswordLengthAcceptable = computed((): boolean => {
return props.password.length <= passMaxLength.value
&& props.password.length >= passMinLength.value;
});
/**
* Returns password strength label depends on score.
*/
const passwordStrength = computed((): string => {
if (props.password.length < passMinLength.value) {
return `Use ${passMinLength.value} or more characters`;
}
if (props.password.length > passMaxLength.value) {
return `Use ${passMaxLength.value} or fewer characters`;
}
const score = scorePassword();
if (score > 90) {
return PASSWORD_STRENGTH.veryStrong;
}
if (score > 70) {
return PASSWORD_STRENGTH.strong;
}
if (score > 45) {
return PASSWORD_STRENGTH.good;
}
return PASSWORD_STRENGTH.weak;
});
/**
* Color for indicator between red as weak and green as strong password.
*/
const passwordStrengthColor = computed((): string => {
return PASSWORD_STRENGTH_COLORS[passwordStrength.value] || PASSWORD_STRENGTH_COLORS.default;
});
/**
* Fills password strength indicator bar.
*/
const barWidth = computed((): string => {
return BAR_WIDTH[passwordStrength.value] || BAR_WIDTH.default;
});
const strengthLabelColor = computed((): { color: string } => {
return { color: passwordStrengthColor.value };
});
const hasLowerAndUpperCaseLetters = computed((): boolean => {
return /[a-z]/.test(props.password) && /[A-Z]/.test(props.password);
});
const hasSpecialCharacter = computed((): boolean => {
return /\W/.test(props.password);
});
/**
* Returns password strength score depends on length, case variations and special characters.
*/
function scorePassword(): number {
const password = props.password;
let score = 0;
const letters: number[] = [];
for (let i = 0; i < password.length; i++) {
letters[password[i]] = (letters[password[i]] || 0) + 1;
score += 5 / letters[password[i]];
}
const variations: boolean[] = [
/\d/.test(password),
/[a-z]/.test(password),
/[A-Z]/.test(password),
/\W/.test(password),
];
let variationCount = 0;
variations.forEach((check) => {
variationCount += check ? 1 : 0;
});
score += variationCount * 10;
return score;
}
</script>
<style scoped lang="scss">
.positioning {
z-index: 1;
}
</style>