auth-verify 1.8.0 → 1.10.0
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/auth-verify.client.js +193 -0
- package/index.js +7 -2
- package/package.json +5 -2
- package/readme.md +1212 -793
- package/src/crypto/index.js +64 -0
- package/src/jwt/index.js +16 -0
- package/src/oauth/index.js +204 -0
- package/src/otp/index.js +84 -54
- package/tests/cryptomanager.test.js +77 -0
- package/tests/jwtmanager.test.js +29 -2
- package/authverify.client.js +0 -174
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
window.AuthVerify = class AuthVerify {
|
|
2
|
+
constructor(options = {}){
|
|
3
|
+
this.apiBase = options.apiBase || 'http://localhost:3000';
|
|
4
|
+
this.qrContainer = options.qrEl || null;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// Fetch QR code from backend and display
|
|
8
|
+
post(url){
|
|
9
|
+
this.fetchPostUrl = url;
|
|
10
|
+
return this;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get(url){
|
|
14
|
+
this.fetchGetUrl = url;
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async qr() {
|
|
19
|
+
if (!this.qrContainer) return;
|
|
20
|
+
try {
|
|
21
|
+
const res = await fetch(`${this.apiBase}${this.fetchGetUrl}`);
|
|
22
|
+
const data = await res.json();
|
|
23
|
+
if (data.qr) {
|
|
24
|
+
this.qrContainer.src = data.qr;
|
|
25
|
+
} else {
|
|
26
|
+
this.showResponse('No QR received');
|
|
27
|
+
}
|
|
28
|
+
} catch (err) {
|
|
29
|
+
console.error(err);
|
|
30
|
+
this.showResponse('Error fetching QR');
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
showResponse(msg){
|
|
35
|
+
console.log("[AuthVerify]", msg);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async data(payload){
|
|
39
|
+
try {
|
|
40
|
+
const res = await fetch(`${this.apiBase}${this.fetchPostUrl}`, {
|
|
41
|
+
method: 'POST',
|
|
42
|
+
headers: { 'Content-Type': 'application/json' },
|
|
43
|
+
body: JSON.stringify(payload)
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const data = await res.json();
|
|
47
|
+
|
|
48
|
+
// if backend returned jwt we store it but still return whole data
|
|
49
|
+
if (data.token) {
|
|
50
|
+
this.jwt = data.token;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return data;
|
|
54
|
+
|
|
55
|
+
} catch(err){
|
|
56
|
+
console.error(err);
|
|
57
|
+
return { error: true, message: err.message };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
header(){
|
|
62
|
+
if(!this.jwt) return {};
|
|
63
|
+
return {
|
|
64
|
+
Authorization: `Bearer ${this.jwt}`
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async verify(code){
|
|
69
|
+
return this.data({code});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// -----------------------------
|
|
73
|
+
// Helper: decode Base64URL to Uint8Array
|
|
74
|
+
// -----------------------------
|
|
75
|
+
base64urlToUint8Array(base64url) {
|
|
76
|
+
if (!base64url) throw new Error("Missing Base64URL data");
|
|
77
|
+
let base64 = base64url.replace(/-/g, '+').replace(/_/g, '/');
|
|
78
|
+
while (base64.length % 4) base64 += '=';
|
|
79
|
+
const str = atob(base64);
|
|
80
|
+
return new Uint8Array([...str].map(c => c.charCodeAt(0)));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// start(route){
|
|
84
|
+
// this.startRegisterApi = route;
|
|
85
|
+
// return this;
|
|
86
|
+
// }
|
|
87
|
+
|
|
88
|
+
// finish(route){
|
|
89
|
+
// this.finishRegisterApi = route;
|
|
90
|
+
// return this;
|
|
91
|
+
// }
|
|
92
|
+
|
|
93
|
+
// // -----------------------------
|
|
94
|
+
// // REGISTER PASSKEY (full flow)
|
|
95
|
+
// // -----------------------------
|
|
96
|
+
// async registerPasskey(user) {
|
|
97
|
+
// try {
|
|
98
|
+
// // 1️⃣ Get registration options from backend
|
|
99
|
+
// const publicKey = await this.post(`${this.startRegisterApi}`).data({user});
|
|
100
|
+
|
|
101
|
+
// // 2️⃣ Decode challenge & user.id automatically
|
|
102
|
+
// publicKey.challenge = this.base64urlToUint8Array(publicKey.challenge);
|
|
103
|
+
// publicKey.user.id = this.base64urlToUint8Array(publicKey.user.id);
|
|
104
|
+
|
|
105
|
+
// // 3️⃣ Ask browser to create credential
|
|
106
|
+
// const credential = await navigator.credentials.create({ publicKey });
|
|
107
|
+
|
|
108
|
+
// // 4️⃣ Convert ArrayBuffers to base64
|
|
109
|
+
// const data = {
|
|
110
|
+
// id: credential.id,
|
|
111
|
+
// rawId: btoa(String.fromCharCode(...new Uint8Array(credential.rawId))),
|
|
112
|
+
// type: credential.type,
|
|
113
|
+
// response: {
|
|
114
|
+
// clientDataJSON: btoa(String.fromCharCode(...new Uint8Array(credential.response.clientDataJSON))),
|
|
115
|
+
// attestationObject: btoa(String.fromCharCode(...new Uint8Array(credential.response.attestationObject))),
|
|
116
|
+
// },
|
|
117
|
+
// };
|
|
118
|
+
|
|
119
|
+
// // 5️⃣ Send credential to backend to finish registration
|
|
120
|
+
// const result = await this.post(`${this.finishRegisterApi}`).data(data);
|
|
121
|
+
|
|
122
|
+
// return result;
|
|
123
|
+
|
|
124
|
+
// } catch (err) {
|
|
125
|
+
// console.error("[AuthVerify registerPasskey]", err);
|
|
126
|
+
// return { error: true, message: err.message };
|
|
127
|
+
// }
|
|
128
|
+
// }
|
|
129
|
+
|
|
130
|
+
// // -----------------------------
|
|
131
|
+
// // LOGIN / AUTHENTICATE PASSKEY
|
|
132
|
+
// // -----------------------------
|
|
133
|
+
// async loginPasskey(user) {
|
|
134
|
+
// try {
|
|
135
|
+
// // 1️⃣ Get assertion options (challenge) from backend
|
|
136
|
+
// const publicKey = await this.post(`${this.startRegisterApi}`).data({ user, login: true });
|
|
137
|
+
|
|
138
|
+
// // 2️⃣ Decode Base64URL fields
|
|
139
|
+
// publicKey.challenge = this.base64urlToUint8Array(publicKey.challenge);
|
|
140
|
+
// publicKey.allowCredentials = publicKey.allowCredentials.map(cred => ({
|
|
141
|
+
// ...cred,
|
|
142
|
+
// id: this.base64urlToUint8Array(cred.id)
|
|
143
|
+
// }));
|
|
144
|
+
|
|
145
|
+
// // 3️⃣ Ask browser to get credential
|
|
146
|
+
// const credential = await navigator.credentials.get({ publicKey });
|
|
147
|
+
|
|
148
|
+
// // 4️⃣ Convert ArrayBuffers to Base64
|
|
149
|
+
// const data = {
|
|
150
|
+
// id: credential.id,
|
|
151
|
+
// rawId: btoa(String.fromCharCode(...new Uint8Array(credential.rawId))),
|
|
152
|
+
// type: credential.type,
|
|
153
|
+
// response: {
|
|
154
|
+
// clientDataJSON: btoa(String.fromCharCode(...new Uint8Array(credential.response.clientDataJSON))),
|
|
155
|
+
// authenticatorData: btoa(String.fromCharCode(...new Uint8Array(credential.response.authenticatorData))),
|
|
156
|
+
// signature: btoa(String.fromCharCode(...new Uint8Array(credential.response.signature))),
|
|
157
|
+
// userHandle: credential.response.userHandle
|
|
158
|
+
// ? btoa(String.fromCharCode(...new Uint8Array(credential.response.userHandle)))
|
|
159
|
+
// : null,
|
|
160
|
+
// },
|
|
161
|
+
// };
|
|
162
|
+
|
|
163
|
+
// // 5️⃣ Send assertion to backend for verification
|
|
164
|
+
// const result = await this.post(`${this.finishRegisterApi}`).data(data);
|
|
165
|
+
|
|
166
|
+
// return result;
|
|
167
|
+
|
|
168
|
+
// } catch (err) {
|
|
169
|
+
// console.error("[AuthVerify loginPasskey]", err);
|
|
170
|
+
// return { error: true, message: err.message };
|
|
171
|
+
// }
|
|
172
|
+
// }
|
|
173
|
+
|
|
174
|
+
async issue(publicKey){
|
|
175
|
+
publicKey.challenge = this.base64urlToUint8Array(publicKey.challenge);
|
|
176
|
+
publicKey.user.id = this.base64urlToUint8Array(publicKey.user.id);
|
|
177
|
+
|
|
178
|
+
const credential = await navigator.credentials.create({ publicKey });
|
|
179
|
+
|
|
180
|
+
const data = {
|
|
181
|
+
id: credential.id,
|
|
182
|
+
rawId: btoa(String.fromCharCode(...new Uint8Array(credential.rawId))),
|
|
183
|
+
type: credential.type,
|
|
184
|
+
response: {
|
|
185
|
+
clientDataJSON: btoa(String.fromCharCode(...new Uint8Array(credential.response.clientDataJSON))),
|
|
186
|
+
attestationObject: btoa(String.fromCharCode(...new Uint8Array(credential.response.attestationObject))),
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
return data;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
}
|
package/index.js
CHANGED
|
@@ -5,6 +5,7 @@ const OAuthManager = require("./src/oauth");
|
|
|
5
5
|
const TOTPManager = require("./src/totp");
|
|
6
6
|
const PasskeyManager = require("./src/passkey");
|
|
7
7
|
const MagicLinkManager = require("./src/magiclink");
|
|
8
|
+
const CryptoManager = require("./src/crypto");
|
|
8
9
|
|
|
9
10
|
class AuthVerify {
|
|
10
11
|
constructor(options = {}) {
|
|
@@ -26,7 +27,10 @@ class AuthVerify {
|
|
|
26
27
|
passExp = "2m",
|
|
27
28
|
mlSecret = "ml_secret",
|
|
28
29
|
mlExpiry = "5m",
|
|
29
|
-
appUrl = "https://yourapp.com"
|
|
30
|
+
appUrl = "https://yourapp.com",
|
|
31
|
+
hashAlg = "pbkdf2",
|
|
32
|
+
iterations = 100000,
|
|
33
|
+
keyLen = 64
|
|
30
34
|
} = options;
|
|
31
35
|
|
|
32
36
|
// ✅ Ensure cookieName and secret always exist
|
|
@@ -54,7 +58,8 @@ class AuthVerify {
|
|
|
54
58
|
this.senders = new Map();
|
|
55
59
|
|
|
56
60
|
this.passkey = new PasskeyManager({rpName, storeTokens, saveBy, passExp});
|
|
57
|
-
this.magic = new MagicLinkManager({mlSecret, mlExpiry, appUrl, storeTokens})
|
|
61
|
+
this.magic = new MagicLinkManager({mlSecret, mlExpiry, appUrl, storeTokens});
|
|
62
|
+
this.crypto = new CryptoManager({hashAlg, iterations, keyLen});
|
|
58
63
|
}
|
|
59
64
|
|
|
60
65
|
// --- Session helpers ---
|
package/package.json
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"uuid": "^9.0.1"
|
|
13
13
|
},
|
|
14
14
|
"name": "auth-verify",
|
|
15
|
-
"version": "1.
|
|
15
|
+
"version": "1.10.0",
|
|
16
16
|
"description": "A simple Node.js library for sending and verifying OTP via email, SMS and Telegram bot. And generating TOTP codes and QR codes. And handling JWT with Cookies. And also handling passwordless logins with passkeys/webauth. And handling magiclink passwordless logins",
|
|
17
17
|
"main": "index.js",
|
|
18
18
|
"scripts": {
|
|
@@ -48,7 +48,10 @@
|
|
|
48
48
|
"signin",
|
|
49
49
|
"webauthn",
|
|
50
50
|
"passkey",
|
|
51
|
-
"passwordless"
|
|
51
|
+
"passwordless",
|
|
52
|
+
"magiclink",
|
|
53
|
+
"hash",
|
|
54
|
+
"password_hashing"
|
|
52
55
|
],
|
|
53
56
|
"author": "Jahongir Sobirov",
|
|
54
57
|
"license": "MIT",
|