@dax-crafta/auth 1.3.1 → 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/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;
|