@draftlab/auth 0.2.5 → 0.2.6
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/dist/provider/totp.d.ts +0 -8
- package/dist/provider/totp.js +6 -43
- package/dist/ui/select.js +20 -39
- package/dist/ui/totp.d.ts +0 -6
- package/dist/ui/totp.js +4 -57
- package/package.json +3 -3
package/dist/provider/totp.d.ts
CHANGED
|
@@ -47,14 +47,6 @@ interface TOTPProviderConfig {
|
|
|
47
47
|
* @param error - Optional error message to display
|
|
48
48
|
*/
|
|
49
49
|
register: (req: Request, qrCodeUrl: string, secret: string, backupCodes: string[], error?: string, email?: string) => Promise<Response>;
|
|
50
|
-
/**
|
|
51
|
-
* Custom verification handler that generates the UI for TOTP verification.
|
|
52
|
-
* Called when user needs to enter their TOTP code.
|
|
53
|
-
*
|
|
54
|
-
* @param req - The HTTP request object
|
|
55
|
-
* @param error - Optional error message to display
|
|
56
|
-
*/
|
|
57
|
-
verify: (req: Request, error?: string) => Promise<Response>;
|
|
58
50
|
/**
|
|
59
51
|
* Custom recovery handler that generates the UI for backup code entry.
|
|
60
52
|
* Called when user wants to use a recovery code instead of TOTP.
|
package/dist/provider/totp.js
CHANGED
|
@@ -42,9 +42,6 @@ const TOTPProvider = (config) => {
|
|
|
42
42
|
const saveTOTPData = async (userId, data) => {
|
|
43
43
|
await Storage.set(ctx.storage, totpKey(userId), data);
|
|
44
44
|
};
|
|
45
|
-
const deleteTOTPData = async (userId) => {
|
|
46
|
-
await Storage.remove(ctx.storage, totpKey(userId));
|
|
47
|
-
};
|
|
48
45
|
const generateBackupCodes = (count) => {
|
|
49
46
|
const codes = [];
|
|
50
47
|
for (let i = 0; i < count; i++) {
|
|
@@ -66,7 +63,7 @@ const TOTPProvider = (config) => {
|
|
|
66
63
|
routes.get("/register", async (c) => {
|
|
67
64
|
return ctx.forward(c, await config.register(c.request, "", "", []));
|
|
68
65
|
});
|
|
69
|
-
routes.post("/register
|
|
66
|
+
routes.post("/register", async (c) => {
|
|
70
67
|
const formData = await c.formData();
|
|
71
68
|
const email = formData.get("email")?.toString();
|
|
72
69
|
const action = formData.get("action")?.toString();
|
|
@@ -110,13 +107,13 @@ const TOTPProvider = (config) => {
|
|
|
110
107
|
routes.get("/authorize", async (c) => {
|
|
111
108
|
return ctx.forward(c, await config.authorize(c.request));
|
|
112
109
|
});
|
|
113
|
-
routes.post("/
|
|
110
|
+
routes.post("/authorize", async (c) => {
|
|
114
111
|
const formData = await c.formData();
|
|
115
112
|
const email = formData.get("email")?.toString();
|
|
116
113
|
const token = formData.get("token")?.toString();
|
|
117
|
-
if (!email || !token) return ctx.forward(c, await config.
|
|
114
|
+
if (!email || !token) return ctx.forward(c, await config.authorize(c.request, "Email and verification code are required"));
|
|
118
115
|
const totpData = await getTOTPData(email);
|
|
119
|
-
if (!totpData || !totpData.enabled) return ctx.forward(c, await config.
|
|
116
|
+
if (!totpData || !totpData.enabled) return ctx.forward(c, await config.authorize(c.request, "TOTP is not set up for this email"));
|
|
120
117
|
const totp = createTOTPInstance(totpData.secret, totpData.label || email);
|
|
121
118
|
const delta = totp.validate({
|
|
122
119
|
token,
|
|
@@ -126,12 +123,12 @@ const TOTPProvider = (config) => {
|
|
|
126
123
|
email,
|
|
127
124
|
method: "totp"
|
|
128
125
|
});
|
|
129
|
-
return ctx.forward(c, await config.
|
|
126
|
+
return ctx.forward(c, await config.authorize(c.request, "Invalid verification code"));
|
|
130
127
|
});
|
|
131
128
|
routes.get("/recovery", async (c) => {
|
|
132
129
|
return ctx.forward(c, await config.recovery(c.request));
|
|
133
130
|
});
|
|
134
|
-
routes.post("/recovery
|
|
131
|
+
routes.post("/recovery", async (c) => {
|
|
135
132
|
const formData = await c.formData();
|
|
136
133
|
const email = formData.get("email")?.toString();
|
|
137
134
|
const code = formData.get("code")?.toString()?.toUpperCase();
|
|
@@ -149,40 +146,6 @@ const TOTPProvider = (config) => {
|
|
|
149
146
|
}
|
|
150
147
|
return ctx.forward(c, await config.recovery(c.request, "Invalid or already used recovery code"));
|
|
151
148
|
});
|
|
152
|
-
routes.post("/disable", async (c) => {
|
|
153
|
-
const userId = c.query("userId");
|
|
154
|
-
if (!userId) return c.json({ error: "User ID is required" }, { status: 400 });
|
|
155
|
-
await deleteTOTPData(userId);
|
|
156
|
-
return c.json({
|
|
157
|
-
success: true,
|
|
158
|
-
message: "TOTP has been disabled"
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
routes.get("/status", async (c) => {
|
|
162
|
-
const userId = c.query("userId");
|
|
163
|
-
if (!userId) return c.json({ error: "User ID is required" }, { status: 400 });
|
|
164
|
-
const totpData = await getTOTPData(userId);
|
|
165
|
-
return c.json({
|
|
166
|
-
enabled: totpData?.enabled || false,
|
|
167
|
-
hasBackupCodes: (totpData?.backupCodes?.length || 0) > 0,
|
|
168
|
-
backupCodesCount: totpData?.backupCodes?.length || 0,
|
|
169
|
-
setupDate: totpData?.createdAt
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
routes.post("/regenerate-backup-codes", async (c) => {
|
|
173
|
-
const userId = c.query("userId");
|
|
174
|
-
if (!userId) return c.json({ error: "User ID is required" }, { status: 400 });
|
|
175
|
-
const totpData = await getTOTPData(userId);
|
|
176
|
-
if (!totpData || !totpData.enabled) return c.json({ error: "TOTP is not enabled for this user" }, { status: 400 });
|
|
177
|
-
const newBackupCodes = generateBackupCodes(backupCodesCount);
|
|
178
|
-
totpData.backupCodes = newBackupCodes;
|
|
179
|
-
await saveTOTPData(userId, totpData);
|
|
180
|
-
return c.json({
|
|
181
|
-
success: true,
|
|
182
|
-
backupCodes: newBackupCodes,
|
|
183
|
-
message: "New backup codes generated"
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
149
|
}
|
|
187
150
|
};
|
|
188
151
|
};
|
package/dist/ui/select.js
CHANGED
|
@@ -8,16 +8,7 @@ import { jsx, jsxs } from "preact/jsx-runtime";
|
|
|
8
8
|
*/
|
|
9
9
|
const PROVIDER_ICONS = {
|
|
10
10
|
github: ICON_GITHUB,
|
|
11
|
-
google: ICON_GOOGLE
|
|
12
|
-
code: () => /* @__PURE__ */ jsx("svg", {
|
|
13
|
-
width: "20",
|
|
14
|
-
height: "20",
|
|
15
|
-
viewBox: "0 0 52 52",
|
|
16
|
-
fill: "currentColor",
|
|
17
|
-
"aria-label": "Code authentication",
|
|
18
|
-
role: "img",
|
|
19
|
-
children: /* @__PURE__ */ jsx("path", { d: "M8.55,36.91A6.55,6.55,0,1,1,2,43.45,6.54,6.54,0,0,1,8.55,36.91Zm17.45,0a6.55,6.55,0,1,1-6.55,6.54A6.55,6.55,0,0,1,26,36.91Zm17.45,0a6.55,6.55,0,1,1-6.54,6.54A6.54,6.54,0,0,1,43.45,36.91ZM8.55,19.45A6.55,6.55,0,1,1,2,26,6.55,6.55,0,0,1,8.55,19.45Zm17.45,0A6.55,6.55,0,1,1,19.45,26,6.56,6.56,0,0,1,26,19.45Zm17.45,0A6.55,6.55,0,1,1,36.91,26,6.55,6.55,0,0,1,43.45,19.45ZM8.55,2A6.55,6.55,0,1,1,2,8.55,6.54,6.54,0,0,1,8.55,2ZM26,2a6.55,6.55,0,1,1-6.55,6.55A6.55,6.55,0,0,1,26,2ZM43.45,2a6.55,6.55,0,1,1-6.54,6.55A6.55,6.55,0,0,1,43.45,2Z" })
|
|
20
|
-
})
|
|
11
|
+
google: ICON_GOOGLE
|
|
21
12
|
};
|
|
22
13
|
/**
|
|
23
14
|
* Default display names for provider types
|
|
@@ -30,28 +21,6 @@ const DEFAULT_DISPLAYS = {
|
|
|
30
21
|
password: "Password"
|
|
31
22
|
};
|
|
32
23
|
/**
|
|
33
|
-
* Individual provider button component
|
|
34
|
-
*/
|
|
35
|
-
const ProviderButton = ({ providerKey, providerType, config, displays, buttonText }) => {
|
|
36
|
-
const displayName = config?.display || displays[providerType] || DEFAULT_DISPLAYS[providerType] || providerType;
|
|
37
|
-
const IconComponent = PROVIDER_ICONS[providerKey] || PROVIDER_ICONS[providerType];
|
|
38
|
-
return /* @__PURE__ */ jsxs("a", {
|
|
39
|
-
href: `./${providerKey}/authorize`,
|
|
40
|
-
"data-component": "button",
|
|
41
|
-
"data-color": "ghost",
|
|
42
|
-
"aria-label": `${buttonText} ${displayName}`,
|
|
43
|
-
children: [
|
|
44
|
-
IconComponent && /* @__PURE__ */ jsx("i", {
|
|
45
|
-
"data-slot": "icon",
|
|
46
|
-
children: /* @__PURE__ */ jsx(IconComponent, {})
|
|
47
|
-
}),
|
|
48
|
-
buttonText,
|
|
49
|
-
" ",
|
|
50
|
-
displayName
|
|
51
|
-
]
|
|
52
|
-
});
|
|
53
|
-
};
|
|
54
|
-
/**
|
|
55
24
|
* Main provider selection component
|
|
56
25
|
*/
|
|
57
26
|
const ProviderSelect = ({ providers, config = {}, theme }) => {
|
|
@@ -66,13 +35,25 @@ const ProviderSelect = ({ providers, config = {}, theme }) => {
|
|
|
66
35
|
title: "Sign In",
|
|
67
36
|
children: /* @__PURE__ */ jsx("div", {
|
|
68
37
|
"data-component": "form",
|
|
69
|
-
children: visibleProviders.map(([key, type]) =>
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
38
|
+
children: visibleProviders.map(([key, type]) => {
|
|
39
|
+
const displayName = config.providers?.[key]?.display || displays[type] || DEFAULT_DISPLAYS[type] || type;
|
|
40
|
+
const IconComponent = PROVIDER_ICONS[key] || PROVIDER_ICONS[type];
|
|
41
|
+
return /* @__PURE__ */ jsxs("a", {
|
|
42
|
+
href: `./${key}/authorize`,
|
|
43
|
+
"data-component": "button",
|
|
44
|
+
"data-color": "ghost",
|
|
45
|
+
"aria-label": `${buttonText} ${displayName}`,
|
|
46
|
+
children: [
|
|
47
|
+
IconComponent && /* @__PURE__ */ jsx("i", {
|
|
48
|
+
"data-slot": "icon",
|
|
49
|
+
children: /* @__PURE__ */ jsx(IconComponent, {})
|
|
50
|
+
}),
|
|
51
|
+
buttonText,
|
|
52
|
+
" ",
|
|
53
|
+
displayName
|
|
54
|
+
]
|
|
55
|
+
}, key);
|
|
56
|
+
})
|
|
76
57
|
})
|
|
77
58
|
});
|
|
78
59
|
};
|
package/dist/ui/totp.d.ts
CHANGED
|
@@ -6,12 +6,6 @@ import { TOTPProviderConfig } from "../provider/totp.js";
|
|
|
6
6
|
* Strongly typed copy text configuration for TOTP UI
|
|
7
7
|
*/
|
|
8
8
|
interface TOTPUICopy {
|
|
9
|
-
readonly setup_title: string;
|
|
10
|
-
readonly setup_description: string;
|
|
11
|
-
readonly verify_title: string;
|
|
12
|
-
readonly verify_description: string;
|
|
13
|
-
readonly recovery_title: string;
|
|
14
|
-
readonly recovery_description: string;
|
|
15
9
|
readonly setup_manual_entry: string;
|
|
16
10
|
readonly setup_backup_codes_title: string;
|
|
17
11
|
readonly setup_backup_codes_description: string;
|
package/dist/ui/totp.js
CHANGED
|
@@ -5,12 +5,6 @@ import QRCode from "qrcode";
|
|
|
5
5
|
|
|
6
6
|
//#region src/ui/totp.tsx
|
|
7
7
|
const DEFAULT_COPY = {
|
|
8
|
-
setup_title: "Set up Two-Factor Authentication",
|
|
9
|
-
setup_description: "Scan the QR code with your authenticator app, then enter the verification code.",
|
|
10
|
-
verify_title: "Enter authentication code",
|
|
11
|
-
verify_description: "Open your authenticator app and enter the current 6-digit code.",
|
|
12
|
-
recovery_title: "Use backup code",
|
|
13
|
-
recovery_description: "Enter one of your backup codes. Each code can only be used once.",
|
|
14
8
|
setup_manual_entry: "Can't scan? Enter this code manually:",
|
|
15
9
|
setup_backup_codes_title: "Save your backup codes",
|
|
16
10
|
setup_backup_codes_description: "Store these backup codes in a safe place. You can use them to access your account if you lose your device.",
|
|
@@ -54,7 +48,7 @@ const TOTPUI = (options = {}) => {
|
|
|
54
48
|
if (!qrCodeUrl) return /* @__PURE__ */ jsx(Layout, { children: /* @__PURE__ */ jsxs("form", {
|
|
55
49
|
"data-component": "form",
|
|
56
50
|
method: "post",
|
|
57
|
-
action: "./register
|
|
51
|
+
action: "./register",
|
|
58
52
|
children: [
|
|
59
53
|
/* @__PURE__ */ jsx(FormAlert, { message: error }),
|
|
60
54
|
/* @__PURE__ */ jsx("input", {
|
|
@@ -93,7 +87,7 @@ const TOTPUI = (options = {}) => {
|
|
|
93
87
|
return /* @__PURE__ */ jsx(Layout, { children: /* @__PURE__ */ jsxs("form", {
|
|
94
88
|
"data-component": "form",
|
|
95
89
|
method: "post",
|
|
96
|
-
action: "./register
|
|
90
|
+
action: "./register",
|
|
97
91
|
children: [
|
|
98
92
|
/* @__PURE__ */ jsx(FormAlert, { message: error }),
|
|
99
93
|
email && /* @__PURE__ */ jsx("input", {
|
|
@@ -169,7 +163,7 @@ const TOTPUI = (options = {}) => {
|
|
|
169
163
|
const renderAuthorize = (error) => /* @__PURE__ */ jsx(Layout, { children: /* @__PURE__ */ jsxs("form", {
|
|
170
164
|
"data-component": "form",
|
|
171
165
|
method: "post",
|
|
172
|
-
action: "./
|
|
166
|
+
action: "./authorize",
|
|
173
167
|
children: [
|
|
174
168
|
/* @__PURE__ */ jsx(FormAlert, { message: error }),
|
|
175
169
|
/* @__PURE__ */ jsx("input", {
|
|
@@ -215,55 +209,12 @@ const TOTPUI = (options = {}) => {
|
|
|
215
209
|
]
|
|
216
210
|
}) });
|
|
217
211
|
/**
|
|
218
|
-
* Renders the verification form
|
|
219
|
-
*/
|
|
220
|
-
const renderVerify = (error) => /* @__PURE__ */ jsx(Layout, { children: /* @__PURE__ */ jsxs("form", {
|
|
221
|
-
"data-component": "form",
|
|
222
|
-
method: "post",
|
|
223
|
-
action: "./verify",
|
|
224
|
-
children: [
|
|
225
|
-
/* @__PURE__ */ jsx(FormAlert, { message: error }),
|
|
226
|
-
/* @__PURE__ */ jsx("input", {
|
|
227
|
-
type: "email",
|
|
228
|
-
name: "email",
|
|
229
|
-
placeholder: "Email",
|
|
230
|
-
autoComplete: "email",
|
|
231
|
-
"data-component": "input",
|
|
232
|
-
required: true
|
|
233
|
-
}),
|
|
234
|
-
/* @__PURE__ */ jsx("input", {
|
|
235
|
-
type: "text",
|
|
236
|
-
name: "token",
|
|
237
|
-
placeholder: copy.input_token,
|
|
238
|
-
pattern: "[0-9]{6}",
|
|
239
|
-
maxLength: 6,
|
|
240
|
-
minLength: 6,
|
|
241
|
-
autoComplete: "one-time-code",
|
|
242
|
-
"data-component": "input",
|
|
243
|
-
required: true
|
|
244
|
-
}),
|
|
245
|
-
/* @__PURE__ */ jsx("button", {
|
|
246
|
-
type: "submit",
|
|
247
|
-
"data-component": "button",
|
|
248
|
-
children: copy.button_continue
|
|
249
|
-
}),
|
|
250
|
-
/* @__PURE__ */ jsx("div", {
|
|
251
|
-
"data-component": "form-footer",
|
|
252
|
-
children: /* @__PURE__ */ jsx("a", {
|
|
253
|
-
href: "./recovery",
|
|
254
|
-
"data-component": "link",
|
|
255
|
-
children: copy.link_use_recovery
|
|
256
|
-
})
|
|
257
|
-
})
|
|
258
|
-
]
|
|
259
|
-
}) });
|
|
260
|
-
/**
|
|
261
212
|
* Renders the recovery form
|
|
262
213
|
*/
|
|
263
214
|
const renderRecovery = (error) => /* @__PURE__ */ jsx(Layout, { children: /* @__PURE__ */ jsxs("form", {
|
|
264
215
|
"data-component": "form",
|
|
265
216
|
method: "post",
|
|
266
|
-
action: "./recovery
|
|
217
|
+
action: "./recovery",
|
|
267
218
|
children: [
|
|
268
219
|
/* @__PURE__ */ jsx(FormAlert, { message: error }),
|
|
269
220
|
/* @__PURE__ */ jsx("input", {
|
|
@@ -308,10 +259,6 @@ const TOTPUI = (options = {}) => {
|
|
|
308
259
|
const jsx$1 = await renderRegister(qrCodeUrl, secret, backupCodes, error, email);
|
|
309
260
|
return new Response(renderToHTML(jsx$1), { headers: { "Content-Type": "text/html" } });
|
|
310
261
|
},
|
|
311
|
-
verify: async (_req, error) => {
|
|
312
|
-
const jsx$1 = renderVerify(error);
|
|
313
|
-
return new Response(renderToHTML(jsx$1), { headers: { "Content-Type": "text/html" } });
|
|
314
|
-
},
|
|
315
262
|
recovery: async (_req, error) => {
|
|
316
263
|
const jsx$1 = renderRecovery(error);
|
|
317
264
|
return new Response(renderToHTML(jsx$1), { headers: { "Content-Type": "text/html" } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@draftlab/auth",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Core implementation for @draftlab/auth",
|
|
6
6
|
"author": "Matheus Pergoli",
|
|
@@ -60,10 +60,10 @@
|
|
|
60
60
|
"@standard-schema/spec": "^1.0.0",
|
|
61
61
|
"jose": "^6.0.12",
|
|
62
62
|
"otpauth": "^9.4.0",
|
|
63
|
-
"preact": "^10.
|
|
63
|
+
"preact": "^10.27.0",
|
|
64
64
|
"preact-render-to-string": "^6.5.13",
|
|
65
65
|
"qrcode": "^1.5.4",
|
|
66
|
-
"@draftlab/auth-router": "0.0.
|
|
66
|
+
"@draftlab/auth-router": "0.0.5"
|
|
67
67
|
},
|
|
68
68
|
"engines": {
|
|
69
69
|
"node": ">=18"
|