@dax-crafta/auth 1.3.0 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/utils/mfa.js +38 -0
- package/src/utils/password-policy.js +60 -0
package/package.json
CHANGED
package/src/utils/mfa.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const speakeasy = require('speakeasy');
|
|
2
|
+
const qrcode = require('qrcode');
|
|
3
|
+
|
|
4
|
+
class MFAService {
|
|
5
|
+
async generateSecret(label, issuer = 'App') {
|
|
6
|
+
const secret = speakeasy.generateSecret({
|
|
7
|
+
length: 20,
|
|
8
|
+
name: label,
|
|
9
|
+
issuer
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const qrCodeUrl = await qrcode.toDataURL(secret.otpauth_url);
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
secret: secret.base32,
|
|
16
|
+
qrCode: qrCodeUrl
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
verifyToken(token, secret) {
|
|
21
|
+
return speakeasy.totp.verify({
|
|
22
|
+
secret,
|
|
23
|
+
encoding: 'base32',
|
|
24
|
+
token,
|
|
25
|
+
window: 1
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
generateBackupCodes() {
|
|
30
|
+
const codes = [];
|
|
31
|
+
for (let i = 0; i < 10; i++) {
|
|
32
|
+
codes.push(speakeasy.generateSecret({ length: 10 }).base32);
|
|
33
|
+
}
|
|
34
|
+
return codes;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = MFAService;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const zxcvbn = require('zxcvbn');
|
|
2
|
+
|
|
3
|
+
class PasswordPolicy {
|
|
4
|
+
constructor(config = {}) {
|
|
5
|
+
this.config = {
|
|
6
|
+
minLength: config.minLength || 8,
|
|
7
|
+
requireUppercase: config.requireUppercase !== false,
|
|
8
|
+
requireLowercase: config.requireLowercase !== false,
|
|
9
|
+
requireNumbers: config.requireNumbers !== false,
|
|
10
|
+
requireSpecialChars: config.requireSpecialChars !== false,
|
|
11
|
+
passwordHistory: config.passwordHistory || 3,
|
|
12
|
+
expiryDays: config.expiryDays || 90,
|
|
13
|
+
minStrength: config.minStrength || 3
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
validate(password, userInfo = {}) {
|
|
18
|
+
const errors = [];
|
|
19
|
+
|
|
20
|
+
if (password.length < this.config.minLength) {
|
|
21
|
+
errors.push(`Password must be at least ${this.config.minLength} characters long`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (this.config.requireUppercase && !/[A-Z]/.test(password)) {
|
|
25
|
+
errors.push('Password must contain at least one uppercase letter');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (this.config.requireLowercase && !/[a-z]/.test(password)) {
|
|
29
|
+
errors.push('Password must contain at least one lowercase letter');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (this.config.requireNumbers && !/\d/.test(password)) {
|
|
33
|
+
errors.push('Password must contain at least one number');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (this.config.requireSpecialChars && !/[!@#$%^&*(),.?":{}|<>]/.test(password)) {
|
|
37
|
+
errors.push('Password must contain at least one special character');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const strength = zxcvbn(password, [userInfo.email, userInfo.name]);
|
|
41
|
+
if (strength.score < this.config.minStrength) {
|
|
42
|
+
errors.push('Password is too weak. Please choose a stronger password');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
isValid: errors.length === 0,
|
|
47
|
+
errors,
|
|
48
|
+
strength: strength.score
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
isExpired(lastPasswordChange) {
|
|
53
|
+
if (!this.config.expiryDays) return false;
|
|
54
|
+
|
|
55
|
+
const expiryDate = new Date(lastPasswordChange.getTime() + this.config.expiryDays * 24 * 60 * 60 * 1000);
|
|
56
|
+
return expiryDate < new Date();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
module.exports = PasswordPolicy;
|