summary refs log tree commit diff
path: root/src/util/passwordStrength.ts
blob: 88155d82fa1c5b7aa37cae53d133925002d6bc80 (plain) (blame)
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
import "missing-native-js-functions";
import Config from "./Config";

const reNUMBER = /[0-9]/g;
const reUPPERCASELETTER = /[A-Z]/g;
const reSYMBOLS = /[A-Z,a-z,0-9]/g;

/*
 * https://en.wikipedia.org/wiki/Password_policy
 * password must meet following criteria, to be perfect:
 *  - min <n> chars
 *  - min <n> numbers
 *  - min <n> symbols
 *  - min <n> uppercase chars
 *
 * Returns: 0 > pw > 1
 */
export function check(password: string): number {
	const { pwMinLength, pwMinNumbers, pwMinUpperCase, pwMinSymbols } = Config.get().register.password;
	var strength = 0;

	// checks for total password len
	if (password.length >= pwMinLength - 1) {
		strength += 0.25;
	}

	// checks for amount of Numbers
	if (password.count(reNUMBER) >= pwMinNumbers - 1) {
		strength += 0.25;
	}

	// checks for amount of Uppercase Letters
	if (password.count(reUPPERCASELETTER) >= pwMinUpperCase - 1) {
		strength += 0.25;
	}

	// checks for amount of symbols
	if (password.replace(reSYMBOLS, "").length >= pwMinSymbols - 1) {
		strength += 0.25;
	}

	// checks if password only consists of numbers or only consists of chars
	if (password.length == password.count(reNUMBER) || password.length === password.count(reUPPERCASELETTER)) {
		strength = 0;
	}

	return strength;
}