@hamur/auth 0.0.0-stage → 0.0.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/LICENSE +22 -0
- package/README.md +14 -2
- package/dist/chunks/bun-index-0grce57w.js +47 -0
- package/dist/chunks/bun-index-nn9z2k40.js +92 -0
- package/dist/chunks/bun-index-pmpez2a8.js +10 -0
- package/dist/chunks/bun-index-r6757s88.js +206 -0
- package/dist/src/adapters/angular.js +63 -0
- package/dist/src/adapters/html.js +29 -0
- package/dist/src/adapters/preact.js +37 -0
- package/dist/src/adapters/react.js +32 -0
- package/dist/src/adapters/solid.js +45 -0
- package/dist/src/adapters/svelte.js +44 -0
- package/dist/src/adapters/vue.js +55 -0
- package/dist/src/index.js +688 -0
- package/dist/src/middleware.js +45 -0
- package/dist/src/route-middleware.js +42 -0
- package/dist/src/server/index.js +2971 -0
- package/dist/types/src/adapters/angular.d.ts +35 -0
- package/dist/types/src/adapters/html.d.ts +15 -0
- package/dist/types/src/adapters/preact.d.ts +19 -0
- package/dist/types/src/adapters/react.d.ts +19 -0
- package/dist/types/src/adapters/solid.d.ts +25 -0
- package/dist/types/src/adapters/svelte-session.d.ts +8 -0
- package/dist/types/src/adapters/svelte-webauthn.d.ts +8 -0
- package/dist/types/src/adapters/svelte.d.ts +5 -0
- package/dist/types/src/adapters/vue.d.ts +25 -0
- package/dist/types/src/build/access-tokens.d.ts +4 -0
- package/dist/types/src/build/client.d.ts +4 -0
- package/dist/types/src/build/dev-key.d.ts +6 -0
- package/dist/types/src/build/email.d.ts +4 -0
- package/dist/types/src/build/guards.d.ts +4 -0
- package/dist/types/src/build/module-entry.d.ts +3 -0
- package/dist/types/src/build/oauth.d.ts +8 -0
- package/dist/types/src/build/session.d.ts +6 -0
- package/dist/types/src/build/single-use.d.ts +15 -0
- package/dist/types/src/build/webauthn.d.ts +7 -0
- package/dist/types/src/client/webauthn.d.ts +37 -0
- package/dist/types/src/index.d.ts +43 -0
- package/dist/types/src/middleware.d.ts +4 -0
- package/dist/types/src/module.d.ts +3 -0
- package/dist/types/src/options.d.ts +4 -0
- package/dist/types/src/route-middleware.d.ts +2 -0
- package/dist/types/src/runtime/middleware/auth.d.ts +3 -0
- package/dist/types/src/runtime/middleware/guest.d.ts +3 -0
- package/dist/types/src/runtime/payload.d.ts +2 -0
- package/dist/types/src/runtime/plugin.d.ts +2 -0
- package/dist/types/src/runtime/store.d.ts +28 -0
- package/dist/types/src/runtime/targets.d.ts +4 -0
- package/dist/types/src/server/access-token.d.ts +27 -0
- package/dist/types/src/server/auth-link.d.ts +15 -0
- package/dist/types/src/server/door.d.ts +4 -0
- package/dist/types/src/server/endpoint.d.ts +8 -0
- package/dist/types/src/server/guard.d.ts +12 -0
- package/dist/types/src/server/hooks.d.ts +5 -0
- package/dist/types/src/server/index.d.ts +88 -0
- package/dist/types/src/server/oauth/atproto-dns.d.ts +1 -0
- package/dist/types/src/server/oauth/atproto-fetch.d.ts +13 -0
- package/dist/types/src/server/oauth/atproto-metadata.d.ts +1 -0
- package/dist/types/src/server/oauth/atproto.d.ts +36 -0
- package/dist/types/src/server/oauth/cookie.d.ts +17 -0
- package/dist/types/src/server/oauth/dpop.d.ts +4 -0
- package/dist/types/src/server/oauth/handler.d.ts +101 -0
- package/dist/types/src/server/oauth/oidc.d.ts +20 -0
- package/dist/types/src/server/oauth/providers/apple.d.ts +26 -0
- package/dist/types/src/server/oauth/providers/atlassian.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/auth0.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/authentik.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/azureb2c.d.ts +11 -0
- package/dist/types/src/server/oauth/providers/battledotnet.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/box.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/catalog.d.ts +12 -0
- package/dist/types/src/server/oauth/providers/cloudflare.d.ts +11 -0
- package/dist/types/src/server/oauth/providers/cognito.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/discord.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/dropbox.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/facebook.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/gitea.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/github.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/gitlab.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/google.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/heroku.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/hubspot.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/instagram.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/keycloak.d.ts +11 -0
- package/dist/types/src/server/oauth/providers/kick.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/line.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/linear.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/linkedin.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/livechat.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/mastodon.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/microsoft.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/okta.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/ory.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/osu.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/paypal.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/polar.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/preset.d.ts +74 -0
- package/dist/types/src/server/oauth/providers/riotgames.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/roblox.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/salesforce.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/seznam.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/shopifyCustomer.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/slack.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/spotify.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/steam.d.ts +23 -0
- package/dist/types/src/server/oauth/providers/strava.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/tiktok.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/twitch.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/vk.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/wechat.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/workos.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/x.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/xsuaa.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/yandex.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/zendesk.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/zitadel.d.ts +9 -0
- package/dist/types/src/server/personal.d.ts +5 -0
- package/dist/types/src/server/session.d.ts +20 -0
- package/dist/types/src/server/token-requests.d.ts +5 -0
- package/dist/types/src/server/token.d.ts +17 -0
- package/dist/types/src/server/totp.d.ts +28 -0
- package/dist/types/src/server/webauthn/cbor.d.ts +16 -0
- package/dist/types/src/server/webauthn/cose.d.ts +17 -0
- package/dist/types/src/server/webauthn/data.d.ts +20 -0
- package/dist/types/src/server/webauthn/handler.d.ts +63 -0
- package/dist/types/src/single-use.d.ts +17 -0
- package/dist/types/src/types.d.ts +88 -0
- package/package.json +155 -4
- package/src/adapters/AuthState.svelte +14 -0
- package/src/adapters/angular.ts +57 -0
- package/src/adapters/html.ts +39 -0
- package/src/adapters/preact.ts +53 -0
- package/src/adapters/react.ts +49 -0
- package/src/adapters/solid.ts +64 -0
- package/src/adapters/svelte-session.ts +21 -0
- package/src/adapters/svelte-webauthn.ts +20 -0
- package/src/adapters/svelte.ts +5 -0
- package/src/adapters/vue.ts +70 -0
- package/src/build/access-tokens.ts +44 -0
- package/src/build/client.ts +16 -0
- package/src/build/dev-key.ts +29 -0
- package/src/build/email.ts +32 -0
- package/src/build/guards.ts +12 -0
- package/src/build/module-entry.ts +8 -0
- package/src/build/oauth.ts +54 -0
- package/src/build/session.ts +54 -0
- package/src/build/single-use.ts +104 -0
- package/src/build/webauthn.ts +28 -0
- package/src/client/webauthn.ts +129 -0
- package/src/env.d.ts +84 -0
- package/src/index.ts +44 -0
- package/src/middleware.ts +9 -0
- package/src/module.ts +26 -0
- package/src/options.ts +241 -0
- package/src/route-middleware.ts +2 -0
- package/src/runtime/middleware/auth.ts +16 -0
- package/src/runtime/middleware/guest.ts +17 -0
- package/src/runtime/payload.ts +40 -0
- package/src/runtime/plugin.ts +12 -0
- package/src/runtime/store.ts +72 -0
- package/src/runtime/targets.ts +9 -0
- package/src/server/access-token.ts +130 -0
- package/src/server/auth-link.ts +95 -0
- package/src/server/door.ts +33 -0
- package/src/server/endpoint.ts +33 -0
- package/src/server/guard.ts +41 -0
- package/src/server/hooks.ts +13 -0
- package/src/server/index.ts +96 -0
- package/src/server/oauth/atproto-dns.ts +3 -0
- package/src/server/oauth/atproto-fetch.ts +67 -0
- package/src/server/oauth/atproto-metadata.ts +20 -0
- package/src/server/oauth/atproto.ts +292 -0
- package/src/server/oauth/cookie.ts +38 -0
- package/src/server/oauth/dpop.ts +32 -0
- package/src/server/oauth/handler.ts +377 -0
- package/src/server/oauth/oidc.ts +283 -0
- package/src/server/oauth/providers/apple.ts +108 -0
- package/src/server/oauth/providers/atlassian.ts +20 -0
- package/src/server/oauth/providers/auth0.ts +29 -0
- package/src/server/oauth/providers/authentik.ts +29 -0
- package/src/server/oauth/providers/azureb2c.ts +41 -0
- package/src/server/oauth/providers/battledotnet.ts +21 -0
- package/src/server/oauth/providers/box.ts +19 -0
- package/src/server/oauth/providers/catalog.ts +73 -0
- package/src/server/oauth/providers/cloudflare.ts +34 -0
- package/src/server/oauth/providers/cognito.ts +29 -0
- package/src/server/oauth/providers/discord.ts +19 -0
- package/src/server/oauth/providers/dropbox.ts +26 -0
- package/src/server/oauth/providers/facebook.ts +19 -0
- package/src/server/oauth/providers/gitea.ts +29 -0
- package/src/server/oauth/providers/github.ts +33 -0
- package/src/server/oauth/providers/gitlab.ts +29 -0
- package/src/server/oauth/providers/google.ts +19 -0
- package/src/server/oauth/providers/heroku.ts +28 -0
- package/src/server/oauth/providers/hubspot.ts +27 -0
- package/src/server/oauth/providers/instagram.ts +32 -0
- package/src/server/oauth/providers/keycloak.ts +31 -0
- package/src/server/oauth/providers/kick.ts +26 -0
- package/src/server/oauth/providers/line.ts +19 -0
- package/src/server/oauth/providers/linear.ts +34 -0
- package/src/server/oauth/providers/linkedin.ts +20 -0
- package/src/server/oauth/providers/livechat.ts +19 -0
- package/src/server/oauth/providers/mastodon.ts +29 -0
- package/src/server/oauth/providers/microsoft.ts +29 -0
- package/src/server/oauth/providers/okta.ts +29 -0
- package/src/server/oauth/providers/ory.ts +29 -0
- package/src/server/oauth/providers/osu.ts +19 -0
- package/src/server/oauth/providers/paypal.ts +19 -0
- package/src/server/oauth/providers/polar.ts +19 -0
- package/src/server/oauth/providers/preset.ts +206 -0
- package/src/server/oauth/providers/riotgames.ts +19 -0
- package/src/server/oauth/providers/roblox.ts +19 -0
- package/src/server/oauth/providers/salesforce.ts +29 -0
- package/src/server/oauth/providers/seznam.ts +19 -0
- package/src/server/oauth/providers/shopifyCustomer.ts +47 -0
- package/src/server/oauth/providers/slack.ts +27 -0
- package/src/server/oauth/providers/spotify.ts +19 -0
- package/src/server/oauth/providers/steam.ts +162 -0
- package/src/server/oauth/providers/strava.ts +20 -0
- package/src/server/oauth/providers/tiktok.ts +22 -0
- package/src/server/oauth/providers/twitch.ts +32 -0
- package/src/server/oauth/providers/vk.ts +36 -0
- package/src/server/oauth/providers/wechat.ts +39 -0
- package/src/server/oauth/providers/workos.ts +19 -0
- package/src/server/oauth/providers/x.ts +19 -0
- package/src/server/oauth/providers/xsuaa.ts +29 -0
- package/src/server/oauth/providers/yandex.ts +26 -0
- package/src/server/oauth/providers/zendesk.ts +29 -0
- package/src/server/oauth/providers/zitadel.ts +29 -0
- package/src/server/personal.ts +17 -0
- package/src/server/session.ts +150 -0
- package/src/server/token-requests.ts +13 -0
- package/src/server/token.ts +81 -0
- package/src/server/totp.ts +165 -0
- package/src/server/webauthn/cbor.ts +154 -0
- package/src/server/webauthn/cose.ts +95 -0
- package/src/server/webauthn/data.ts +50 -0
- package/src/server/webauthn/handler.ts +510 -0
- package/src/single-use.ts +41 -0
- package/src/types.ts +91 -0
|
@@ -0,0 +1,688 @@
|
|
|
1
|
+
// packages/auth/src/module.ts
|
|
2
|
+
import { defineModule } from "hamur/kit";
|
|
3
|
+
|
|
4
|
+
// packages/auth/src/build/access-tokens.ts
|
|
5
|
+
import { resolve } from "node:path";
|
|
6
|
+
import { DiagnosticError, addTemplate, hasModule } from "hamur/kit";
|
|
7
|
+
|
|
8
|
+
// packages/auth/src/build/module-entry.ts
|
|
9
|
+
import { dirname } from "node:path";
|
|
10
|
+
import { useInstalledModules } from "hamur/kit";
|
|
11
|
+
function moduleEntry(hamur, name, specifier) {
|
|
12
|
+
const file = useInstalledModules().find((module) => module.name === name)?.file;
|
|
13
|
+
return Bun.resolveSync(specifier, file === undefined ? hamur.options.rootDir : dirname(file));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// packages/auth/src/build/access-tokens.ts
|
|
17
|
+
var TABLE = {
|
|
18
|
+
table: "access_tokens",
|
|
19
|
+
columns: ["id", "user_id", "name", "token_hash", "scopes", "expires_at", "last_used_at", "created_at"]
|
|
20
|
+
};
|
|
21
|
+
function setupAccessTokens({ accessTokens }, hamur) {
|
|
22
|
+
let contents = `export const accessTokens = undefined;
|
|
23
|
+
`;
|
|
24
|
+
addTemplate({ filename: "auth/access-tokens", getContents: () => contents });
|
|
25
|
+
if (!accessTokens)
|
|
26
|
+
return;
|
|
27
|
+
let cli;
|
|
28
|
+
hamur.hooks.hook("modules:done", () => {
|
|
29
|
+
if (!hasModule("@hamur/db"))
|
|
30
|
+
throw new DiagnosticError("HAMUR_E5108", "@hamur/auth: auth.accessTokens needs @hamur/db in modules");
|
|
31
|
+
cli = moduleEntry(hamur, "db", "@hamur/db/cli");
|
|
32
|
+
const { rootDir } = hamur.options;
|
|
33
|
+
let user;
|
|
34
|
+
try {
|
|
35
|
+
user = Bun.resolveSync(resolve(rootDir, accessTokens.user), rootDir);
|
|
36
|
+
} catch {
|
|
37
|
+
throw new DiagnosticError("HAMUR_E5101", `Cannot find auth.accessTokens.user ${JSON.stringify(accessTokens.user)}`);
|
|
38
|
+
}
|
|
39
|
+
contents = `export const accessTokens = {
|
|
40
|
+
loadDb: () => import(${JSON.stringify(moduleEntry(hamur, "db", "@hamur/db/server"))}),
|
|
41
|
+
loadUser: () => import(${JSON.stringify(user)}),
|
|
42
|
+
};
|
|
43
|
+
`;
|
|
44
|
+
});
|
|
45
|
+
hamur.hooks.hook("build:before", async () => {
|
|
46
|
+
const { tableProblem } = await import(cli);
|
|
47
|
+
const problem = await tableProblem(hamur, TABLE);
|
|
48
|
+
if (problem)
|
|
49
|
+
throw new DiagnosticError("HAMUR_E5109", `@hamur/auth: ${problem}`);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// packages/auth/src/build/client.ts
|
|
54
|
+
import { addAppPlugin, addTemplate as addTemplate2, createResolver } from "hamur/kit";
|
|
55
|
+
function setupClient(options, hamur) {
|
|
56
|
+
const { loadStrategy } = options;
|
|
57
|
+
const fetchAfterMount = loadStrategy !== "none" && hamur.options.framework !== "html";
|
|
58
|
+
addTemplate2({
|
|
59
|
+
filename: "auth/client",
|
|
60
|
+
getContents: () => `export const loadStrategy = ${JSON.stringify(loadStrategy)};
|
|
61
|
+
export const fetchAfterMount = ${fetchAfterMount};
|
|
62
|
+
`
|
|
63
|
+
});
|
|
64
|
+
const r = createResolver(new URL("../../src/build/client.ts", import.meta.url).href);
|
|
65
|
+
addAppPlugin({ src: r.resolve("../runtime/payload.ts"), mode: "server" });
|
|
66
|
+
addAppPlugin({ src: r.resolve("../runtime/plugin.ts"), mode: "client" });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// packages/auth/src/build/email.ts
|
|
70
|
+
import { DiagnosticError as DiagnosticError3, addTemplate as addTemplate5, hasModule as hasModule2 } from "hamur/kit";
|
|
71
|
+
|
|
72
|
+
// packages/auth/src/build/single-use.ts
|
|
73
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
74
|
+
import { join } from "node:path";
|
|
75
|
+
import { DiagnosticError as DiagnosticError2, addTemplate as addTemplate4, hasStorageMount } from "hamur/kit";
|
|
76
|
+
|
|
77
|
+
// packages/auth/src/single-use.ts
|
|
78
|
+
var ATOMIC = ["redis", "db", "sqlite"];
|
|
79
|
+
var IN_PROCESS = ["memory", "fs"];
|
|
80
|
+
var ONE_PROCESS_PRESETS = ["bun", "node"];
|
|
81
|
+
var FIX = "mount storage.session on redis, db or sqlite";
|
|
82
|
+
var WHAT = {
|
|
83
|
+
webAuthnRegister: "challenges",
|
|
84
|
+
webAuthnAuthenticate: "challenges",
|
|
85
|
+
consumeAuthToken: "tokens",
|
|
86
|
+
verifyTotp: "TOTP codes"
|
|
87
|
+
};
|
|
88
|
+
function singleUseProblem(mount, at) {
|
|
89
|
+
const what = WHAT[at.name];
|
|
90
|
+
if (!mount)
|
|
91
|
+
return `@hamur/auth: ${at.name} needs the session storage mount for single-use ${what}; set storage.session`;
|
|
92
|
+
if (mount.encrypt)
|
|
93
|
+
return `@hamur/auth: single-use ${what} need an atomic increment; the "session" mount has encrypt, whose increment is not atomic; ${FIX}`;
|
|
94
|
+
if (ATOMIC.includes(mount.driver))
|
|
95
|
+
return;
|
|
96
|
+
if (IN_PROCESS.includes(mount.driver) && (at.dev || ONE_PROCESS_PRESETS.includes(at.preset)))
|
|
97
|
+
return;
|
|
98
|
+
return `@hamur/auth: single-use ${what} need an atomic increment; the "session" mount uses ${mount.driver} on the ${at.preset} preset; ${FIX}`;
|
|
99
|
+
}
|
|
100
|
+
function inProcessDriver(mount) {
|
|
101
|
+
return mount && !mount.encrypt && IN_PROCESS.includes(mount.driver) ? mount.driver : undefined;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// packages/auth/src/build/webauthn.ts
|
|
105
|
+
import { addTemplate as addTemplate3 } from "hamur/kit";
|
|
106
|
+
var SERVER = "@hamur/auth/server";
|
|
107
|
+
var HANDLERS = ["webAuthnRegister", "webAuthnAuthenticate"];
|
|
108
|
+
function usedHandlers(hamur) {
|
|
109
|
+
return HANDLERS.filter((name) => hamur.serverRoutes.some(({ fn }) => fn.handlerImport?.from === SERVER && fn.handlerImport.name === name));
|
|
110
|
+
}
|
|
111
|
+
function setupWebAuthn(options, hamur) {
|
|
112
|
+
addTemplate3({
|
|
113
|
+
filename: "auth/webauthn",
|
|
114
|
+
getContents: (ctx) => {
|
|
115
|
+
const value = {
|
|
116
|
+
...options.webauthn,
|
|
117
|
+
used: usedHandlers(ctx).length > 0,
|
|
118
|
+
listedHosts: Array.isArray(ctx.options.server.allowedHosts)
|
|
119
|
+
};
|
|
120
|
+
return `export const webauthn = ${JSON.stringify(value)};
|
|
121
|
+
`;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// packages/auth/src/build/single-use.ts
|
|
127
|
+
var SERVER2 = "@hamur/auth/server";
|
|
128
|
+
var SOURCE = /\.(?:[cm]?[jt]s|[jt]sx)$/;
|
|
129
|
+
var IGNORED_FILE = /\.(test|spec)\.[^/]+$|\.d\.[cm]?ts$/;
|
|
130
|
+
var NAMED_IMPORT = /^import\s+(?:[\w$]+\s*,\s*)?\{([^}]*)\}\s*from\s*"@hamur\/auth\/server"/gm;
|
|
131
|
+
var NAMESPACE_IMPORT = /^import\s+(?:[\w$]+\s*,\s*)?\*\s*as\s+([\w$]+)\s+from\s*"@hamur\/auth\/server"/gm;
|
|
132
|
+
function sourceFiles(dir) {
|
|
133
|
+
if (!existsSync(dir))
|
|
134
|
+
return [];
|
|
135
|
+
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
|
136
|
+
const path = join(dir, entry.name);
|
|
137
|
+
if (entry.isDirectory())
|
|
138
|
+
return entry.name === "node_modules" || entry.name.startsWith(".") ? [] : sourceFiles(path);
|
|
139
|
+
return SOURCE.test(entry.name) && !IGNORED_FILE.test(entry.name) ? [path] : [];
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
var TRANSPILERS = {
|
|
143
|
+
ts: new Bun.Transpiler({ loader: "ts" }),
|
|
144
|
+
tsx: new Bun.Transpiler({ loader: "tsx" }),
|
|
145
|
+
jsx: new Bun.Transpiler({ loader: "jsx" })
|
|
146
|
+
};
|
|
147
|
+
var IMPORTED = ["consumeAuthToken", "verifyTotp"];
|
|
148
|
+
function importsAuthName(name, text, kind = "ts") {
|
|
149
|
+
const transpiler = TRANSPILERS[kind];
|
|
150
|
+
let source;
|
|
151
|
+
try {
|
|
152
|
+
if (!transpiler.scanImports(text).some((entry) => entry.kind === "import-statement" && entry.path === SERVER2))
|
|
153
|
+
return false;
|
|
154
|
+
source = transpiler.transformSync(text);
|
|
155
|
+
} catch {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
const named = new RegExp(`^${name}\\b`);
|
|
159
|
+
for (const [, names] of source.matchAll(NAMED_IMPORT)) {
|
|
160
|
+
if (names.split(",").some((part) => named.test(part.trim())))
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
for (const [, space] of source.matchAll(NAMESPACE_IMPORT)) {
|
|
164
|
+
if (new RegExp(`\\b${space.replace(/\$/g, "\\$")}\\s*\\.\\s*${name}\\b`).test(source))
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
var loaderOf = (file) => file.endsWith("x") ? file.endsWith("tsx") ? "tsx" : "jsx" : "ts";
|
|
170
|
+
function serverImports(hamur) {
|
|
171
|
+
const dirs = [hamur.options.serverDir, ...hamur.options.layers.map((layer) => layer.serverDir)];
|
|
172
|
+
const files = dirs.flatMap((dir) => sourceFiles(dir).map((file) => ({ text: readFileSync(file, "utf8"), kind: loaderOf(file) })));
|
|
173
|
+
return (name) => files.some((file) => importsAuthName(name, file.text, file.kind));
|
|
174
|
+
}
|
|
175
|
+
function usedNames(hamur) {
|
|
176
|
+
return [...usedHandlers(hamur), ...IMPORTED.filter(serverImports(hamur))];
|
|
177
|
+
}
|
|
178
|
+
function sessionMount(hamur) {
|
|
179
|
+
const configured = hamur.options.storage.session;
|
|
180
|
+
if (configured) {
|
|
181
|
+
if (typeof configured.builtIn !== "string")
|
|
182
|
+
return null;
|
|
183
|
+
const options = configured.options ?? {};
|
|
184
|
+
return { driver: configured.builtIn, encrypt: options.encrypt !== undefined };
|
|
185
|
+
}
|
|
186
|
+
if (!hasStorageMount("session"))
|
|
187
|
+
return;
|
|
188
|
+
return {
|
|
189
|
+
driver: hamur.options.preset === "cloudflare" && !hamur.options.dev ? "cloudflare-kv" : "fs",
|
|
190
|
+
encrypt: false
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
function setupSingleUse(hamur) {
|
|
194
|
+
let inProcess;
|
|
195
|
+
addTemplate4({
|
|
196
|
+
filename: "auth/single-use",
|
|
197
|
+
getContents: (ctx) => `export const singleUse = ${JSON.stringify({ preset: ctx.options.preset, ...inProcess === undefined ? {} : { inProcess } })};
|
|
198
|
+
`
|
|
199
|
+
});
|
|
200
|
+
hamur.hooks.hook("build:before", () => {
|
|
201
|
+
inProcess = undefined;
|
|
202
|
+
const [name] = usedNames(hamur);
|
|
203
|
+
if (name === undefined)
|
|
204
|
+
return;
|
|
205
|
+
const mount = sessionMount(hamur);
|
|
206
|
+
if (mount === null)
|
|
207
|
+
return;
|
|
208
|
+
const problem = singleUseProblem(mount, { name, preset: hamur.options.preset, dev: hamur.options.dev });
|
|
209
|
+
if (problem)
|
|
210
|
+
throw new DiagnosticError2("HAMUR_E5103", problem);
|
|
211
|
+
inProcess = inProcessDriver(mount);
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// packages/auth/src/build/email.ts
|
|
216
|
+
function setupEmail(options, hamur) {
|
|
217
|
+
let mail;
|
|
218
|
+
hamur.hooks.hook("modules:done", () => {
|
|
219
|
+
if (!hasModule2("@hamur/mail"))
|
|
220
|
+
return;
|
|
221
|
+
mail = moduleEntry(hamur, "mail", "@hamur/mail/server");
|
|
222
|
+
});
|
|
223
|
+
addTemplate5({
|
|
224
|
+
filename: "auth/email",
|
|
225
|
+
getContents: (ctx) => [
|
|
226
|
+
`export const email = ${JSON.stringify(options.email)};`,
|
|
227
|
+
`export const siteUrl = ${JSON.stringify(ctx.options.site.url ?? null)};`,
|
|
228
|
+
...mail === undefined ? ["export const templates = [];", "export const loadMail = undefined;"] : [
|
|
229
|
+
`export { templates } from "#hamur/mail/config";`,
|
|
230
|
+
`export const loadMail = () => import(${JSON.stringify(mail)});`
|
|
231
|
+
],
|
|
232
|
+
""
|
|
233
|
+
].join(`
|
|
234
|
+
`)
|
|
235
|
+
});
|
|
236
|
+
hamur.hooks.hook("build:before", () => {
|
|
237
|
+
if (mail === undefined && serverImports(hamur)("sendAuthLink"))
|
|
238
|
+
throw new DiagnosticError3("HAMUR_E5107", "@hamur/auth: sendAuthLink needs @hamur/mail in modules");
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// packages/auth/src/build/guards.ts
|
|
243
|
+
import { addTemplate as addTemplate6 } from "hamur/kit";
|
|
244
|
+
function setupGuards(options, hamur) {
|
|
245
|
+
const { baseURL } = hamur.options.app;
|
|
246
|
+
addTemplate6({
|
|
247
|
+
filename: "auth/guards",
|
|
248
|
+
getContents: () => `export const redirect = ${JSON.stringify(options.redirect)};
|
|
249
|
+
export const baseURL = ${JSON.stringify(baseURL)};
|
|
250
|
+
`
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// packages/auth/src/build/oauth.ts
|
|
255
|
+
import { addRouteRules, addTemplate as addTemplate7, createResolver as createResolver2 } from "hamur/kit";
|
|
256
|
+
|
|
257
|
+
// packages/auth/src/server/oauth/providers/catalog.ts
|
|
258
|
+
var client = ["clientId", "clientSecret"];
|
|
259
|
+
var APPLE_ORIGIN = "https://appleid.apple.com";
|
|
260
|
+
var ATPROTO_METADATA_ROUTE = "/_hamur/auth/atproto/client-metadata.json";
|
|
261
|
+
var OAUTH_PROVIDERS = {
|
|
262
|
+
github: { label: "GitHub", secrets: client, keys: [] },
|
|
263
|
+
google: { label: "Google", secrets: client, keys: [] },
|
|
264
|
+
microsoft: { label: "Microsoft", secrets: client, keys: ["tenant"] },
|
|
265
|
+
apple: { label: "Apple", secrets: ["clientId", "privateKey"], keys: ["teamId", "keyId"] },
|
|
266
|
+
discord: { label: "Discord", secrets: client, keys: [] },
|
|
267
|
+
gitlab: { label: "GitLab", secrets: client, keys: ["baseURL"] },
|
|
268
|
+
steam: { label: "Steam", secrets: [], keys: [] },
|
|
269
|
+
atlassian: { label: "Atlassian", secrets: client, keys: [] },
|
|
270
|
+
auth0: { label: "Auth0", secrets: client, keys: ["domain"] },
|
|
271
|
+
authentik: { label: "Authentik", secrets: client, keys: ["domain"] },
|
|
272
|
+
battledotnet: { label: "BattleDotNet", secrets: client, keys: [] },
|
|
273
|
+
box: { label: "Box", secrets: client, keys: [] },
|
|
274
|
+
dropbox: { label: "Dropbox", secrets: client, keys: [] },
|
|
275
|
+
facebook: { label: "Facebook", secrets: client, keys: [] },
|
|
276
|
+
gitea: { label: "Gitea", secrets: client, keys: ["baseURL"] },
|
|
277
|
+
heroku: { label: "Heroku", secrets: client, keys: [] },
|
|
278
|
+
keycloak: { label: "Keycloak", secrets: client, keys: ["serverURL", "realm"] },
|
|
279
|
+
kick: { label: "Kick", secrets: client, keys: [] },
|
|
280
|
+
line: { label: "Line", secrets: client, keys: [] },
|
|
281
|
+
linear: { label: "Linear", secrets: client, keys: [] },
|
|
282
|
+
linkedin: { label: "LinkedIn", secrets: client, keys: [] },
|
|
283
|
+
mastodon: { label: "Mastodon", secrets: client, keys: ["baseURL"] },
|
|
284
|
+
okta: { label: "Okta", secrets: client, keys: ["domain"] },
|
|
285
|
+
ory: { label: "Ory", secrets: client, keys: ["domain"] },
|
|
286
|
+
osu: { label: "Osu", secrets: client, keys: [] },
|
|
287
|
+
paypal: { label: "PayPal", secrets: client, keys: [] },
|
|
288
|
+
polar: { label: "Polar", secrets: client, keys: [] },
|
|
289
|
+
riotgames: { label: "RiotGames", secrets: client, keys: [] },
|
|
290
|
+
roblox: { label: "Roblox", secrets: client, keys: [] },
|
|
291
|
+
salesforce: { label: "Salesforce", secrets: client, keys: ["baseURL"] },
|
|
292
|
+
seznam: { label: "Seznam", secrets: client, keys: [] },
|
|
293
|
+
slack: { label: "Slack", secrets: client, keys: [] },
|
|
294
|
+
spotify: { label: "Spotify", secrets: client, keys: [] },
|
|
295
|
+
twitch: { label: "Twitch", secrets: client, keys: [] },
|
|
296
|
+
x: { label: "X", secrets: client, keys: [] },
|
|
297
|
+
yandex: { label: "Yandex", secrets: client, keys: [] },
|
|
298
|
+
zendesk: { label: "Zendesk", secrets: client, keys: ["subdomain"] },
|
|
299
|
+
zitadel: { label: "Zitadel", secrets: client, keys: ["domain"] },
|
|
300
|
+
azureb2c: { label: "AzureB2C", secrets: client, keys: ["tenant", "policy"] },
|
|
301
|
+
cloudflare: { label: "Cloudflare", secrets: ["clientSecret"], keys: ["clientId", "teamName"] },
|
|
302
|
+
cognito: { label: "Cognito", secrets: client, keys: ["domain"] },
|
|
303
|
+
hubspot: { label: "HubSpot", secrets: client, keys: [] },
|
|
304
|
+
instagram: { label: "Instagram", secrets: client, keys: [] },
|
|
305
|
+
livechat: { label: "LiveChat", secrets: client, keys: [] },
|
|
306
|
+
shopifyCustomer: { label: "ShopifyCustomer", secrets: client, keys: ["shopId"] },
|
|
307
|
+
strava: { label: "Strava", secrets: client, keys: [] },
|
|
308
|
+
tiktok: { label: "TikTok", secrets: client, keys: [] },
|
|
309
|
+
vk: { label: "VK", secrets: ["clientId"], keys: [] },
|
|
310
|
+
wechat: { label: "WeChat", secrets: client, keys: [] },
|
|
311
|
+
workos: { label: "WorkOS", secrets: client, keys: [] },
|
|
312
|
+
xsuaa: { label: "Xsuaa", secrets: client, keys: ["domain"] }
|
|
313
|
+
};
|
|
314
|
+
function providerKeys(provider) {
|
|
315
|
+
const { secrets, keys } = OAUTH_PROVIDERS[provider];
|
|
316
|
+
return [...secrets, "redirectURL", ...keys];
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// packages/auth/src/build/oauth.ts
|
|
320
|
+
var SERVER3 = "@hamur/auth/server";
|
|
321
|
+
var DNS_PRESETS = ["bun", "node", "vercel", "netlify"];
|
|
322
|
+
var DNS_MODULE = createResolver2(new URL("../../src/build/oauth.ts", import.meta.url).href).resolve("../server/oauth/atproto-dns.ts");
|
|
323
|
+
var atprotoRoutes = (routes) => routes.filter(({ path, fn }) => fn.handlerImport?.from === SERVER3 && fn.handlerImport.name === "oauthAtproto" && !/[:*]/.test(path));
|
|
324
|
+
function keepAtprotoMetadata(routes) {
|
|
325
|
+
if (routes.some(({ fn }) => fn.handlerImport?.from === SERVER3 && fn.handlerImport.name === "oauthAtproto"))
|
|
326
|
+
return;
|
|
327
|
+
for (let at = routes.length - 1;at >= 0; at--) {
|
|
328
|
+
const { path, fn } = routes[at];
|
|
329
|
+
if (path === ATPROTO_METADATA_ROUTE && fn.export === "atprotoClientMetadata")
|
|
330
|
+
routes.splice(at, 1);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
function atprotoModule(hamur) {
|
|
334
|
+
const dns = hamur.options.dev || DNS_PRESETS.includes(hamur.options.preset);
|
|
335
|
+
const routes = atprotoRoutes(hamur.serverRoutes).map(({ path, fn }) => ` { path: ${JSON.stringify(path)}, load: () => import(${JSON.stringify(fn.file)}).then((module) => module[${JSON.stringify(fn.export)}]) },`);
|
|
336
|
+
return [
|
|
337
|
+
dns ? `export { hostFetch } from ${JSON.stringify(DNS_MODULE)};` : "export const hostFetch = undefined;",
|
|
338
|
+
routes.length ? `export const routes = [
|
|
339
|
+
${routes.join(`
|
|
340
|
+
`)}
|
|
341
|
+
];` : "export const routes = [];",
|
|
342
|
+
""
|
|
343
|
+
].join(`
|
|
344
|
+
`);
|
|
345
|
+
}
|
|
346
|
+
function setupOAuth(options, hamur) {
|
|
347
|
+
addTemplate7({ filename: "auth/oauth", getContents: () => `export const oauth = ${JSON.stringify(options.oauth)};
|
|
348
|
+
` });
|
|
349
|
+
addTemplate7({ filename: "auth/atproto", getContents: atprotoModule });
|
|
350
|
+
hamur.hooks.hook("server:extend", (routes) => {
|
|
351
|
+
keepAtprotoMetadata(routes);
|
|
352
|
+
for (const { path, fn } of routes) {
|
|
353
|
+
const helper = fn.handlerImport;
|
|
354
|
+
if (helper?.from !== SERVER3 || helper.name !== "oauthApple")
|
|
355
|
+
continue;
|
|
356
|
+
if (/[:*]/.test(path))
|
|
357
|
+
continue;
|
|
358
|
+
addRouteRules({ [path]: { originCheck: { allow: [APPLE_ORIGIN] } } });
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// packages/auth/src/build/session.ts
|
|
364
|
+
import {
|
|
365
|
+
DiagnosticError as DiagnosticError4,
|
|
366
|
+
addServer,
|
|
367
|
+
addTemplate as addTemplate8,
|
|
368
|
+
addTypeTemplate,
|
|
369
|
+
createResolver as createResolver3,
|
|
370
|
+
diagnostic as diagnostic2,
|
|
371
|
+
hasStorageMount as hasStorageMount2,
|
|
372
|
+
useLogger as useLogger2
|
|
373
|
+
} from "hamur/kit";
|
|
374
|
+
|
|
375
|
+
// packages/auth/src/build/dev-key.ts
|
|
376
|
+
import { diagnostic, useLogger, writeAppKey } from "hamur/kit";
|
|
377
|
+
function ignoredByGit(rootDir) {
|
|
378
|
+
try {
|
|
379
|
+
return Bun.spawnSync(["git", "check-ignore", "-q", ".env"], { cwd: rootDir, stdout: "ignore", stderr: "ignore" }).exitCode !== 1;
|
|
380
|
+
} catch {
|
|
381
|
+
return true;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
function ensureDevKey(rootDir) {
|
|
385
|
+
if (process.env.HAMUR_KEY)
|
|
386
|
+
return;
|
|
387
|
+
const written = writeAppKey(rootDir);
|
|
388
|
+
if (written.status === "present")
|
|
389
|
+
return;
|
|
390
|
+
process.env.HAMUR_KEY = written.key;
|
|
391
|
+
const log = useLogger("auth");
|
|
392
|
+
log.info("Wrote HAMUR_KEY to .env");
|
|
393
|
+
if (!ignoredByGit(rootDir))
|
|
394
|
+
log.warn(diagnostic("HAMUR_W5105", ".env holds HAMUR_KEY and is not ignored by git; add .env to .gitignore"));
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// packages/auth/src/build/session.ts
|
|
398
|
+
var SERVER4 = "@hamur/auth/server";
|
|
399
|
+
function unmountedStorage(name, preset) {
|
|
400
|
+
const hint = name === "session" && preset === "cloudflare" ? 'add a kv_namespaces entry with binding "SESSION" to the wrangler config, or set storage.session' : `set storage.${name}`;
|
|
401
|
+
return `auth.session.storage ${JSON.stringify(name)} has no mount on the ${preset} preset; ${hint}`;
|
|
402
|
+
}
|
|
403
|
+
function setupSession(options, hamur) {
|
|
404
|
+
const { session } = options;
|
|
405
|
+
const protection = hamur.options.build.importProtection;
|
|
406
|
+
const client = protection.client ??= {};
|
|
407
|
+
(client.deny ??= []).push(SERVER4);
|
|
408
|
+
if (typeof session.password === "string") {
|
|
409
|
+
useLogger2("auth").warn(diagnostic2("HAMUR_W5104", 'auth.session.password is a literal in the config file, which ends up in the repository; use env("NAME")'));
|
|
410
|
+
}
|
|
411
|
+
if (hamur.options.dev)
|
|
412
|
+
ensureDevKey(hamur.options.rootDir);
|
|
413
|
+
addTemplate8({ filename: "auth/options", getContents: () => `export const session = ${JSON.stringify(session)};
|
|
414
|
+
` });
|
|
415
|
+
addTypeTemplate({ filename: "auth.d.ts", getContents: () => `import type {} from "@hamur/auth";
|
|
416
|
+
export {};
|
|
417
|
+
` });
|
|
418
|
+
addServer(createResolver3(new URL("../../src/build/session.ts", import.meta.url).href).resolve("../server/door.ts"));
|
|
419
|
+
const { storage } = session;
|
|
420
|
+
if (storage !== undefined) {
|
|
421
|
+
hamur.hooks.hook("build:before", () => {
|
|
422
|
+
if (!hasStorageMount2(storage))
|
|
423
|
+
throw new DiagnosticError4("HAMUR_E5102", unmountedStorage(storage, hamur.options.preset));
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// packages/auth/src/runtime/targets.ts
|
|
429
|
+
function isAppPath(value) {
|
|
430
|
+
return typeof value === "string" && /^\/(?![/\\])/.test(value) && !/[\x00-\x1f\x7f]/.test(value);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// packages/auth/src/options.ts
|
|
434
|
+
import { DiagnosticError as DiagnosticError5, isEnvRef } from "hamur/kit";
|
|
435
|
+
var DEFAULT_MAX_AGE = 604800;
|
|
436
|
+
var TOP_KEYS = ["session", "loadStrategy", "redirect", "oauth", "webauthn", "email", "accessTokens"];
|
|
437
|
+
var WEBAUTHN_KEYS = ["rpID", "rpName", "origins"];
|
|
438
|
+
var EMAIL_KEYS = ["page", "template", "subject", "ttl"];
|
|
439
|
+
var EMAIL = {
|
|
440
|
+
"magic-link": { page: "/auth/magic-link", template: "AuthMagicLink", ttl: 900 },
|
|
441
|
+
"verify-email": { page: "/auth/verify-email", template: "AuthVerifyEmail", ttl: 86400 },
|
|
442
|
+
"password-reset": { page: "/auth/reset-password", template: "AuthPasswordReset", ttl: 3600 }
|
|
443
|
+
};
|
|
444
|
+
var HOST_NAME = /^(?=.{1,253}$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/;
|
|
445
|
+
var REDIRECT_KEYS = ["login", "home"];
|
|
446
|
+
var LOAD_STRATEGIES = ["server-first", "client-only", "none"];
|
|
447
|
+
var SESSION_KEYS = ["name", "password", "maxAge", "idleTimeout", "storage", "cookie"];
|
|
448
|
+
var COOKIE_KEYS = ["domain", "path", "httpOnly", "secure", "sameSite", "partitioned"];
|
|
449
|
+
var SAME_SITE = ["lax", "strict", "none"];
|
|
450
|
+
var TOKEN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
451
|
+
var MIN_PASSWORD = 32;
|
|
452
|
+
function fail(message) {
|
|
453
|
+
throw new DiagnosticError5("HAMUR_E5101", message);
|
|
454
|
+
}
|
|
455
|
+
function shown(value) {
|
|
456
|
+
if (Array.isArray(value))
|
|
457
|
+
return "an array";
|
|
458
|
+
if (typeof value === "function")
|
|
459
|
+
return "a function";
|
|
460
|
+
if (value === undefined)
|
|
461
|
+
return "undefined";
|
|
462
|
+
return JSON.stringify(value);
|
|
463
|
+
}
|
|
464
|
+
var isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
465
|
+
function onlyKeys(at, value, keys) {
|
|
466
|
+
for (const key of Object.keys(value))
|
|
467
|
+
if (!keys.includes(key))
|
|
468
|
+
fail(`${at} has no ${JSON.stringify(key)} (it takes: ${keys.join(", ")})`);
|
|
469
|
+
}
|
|
470
|
+
function seconds(at, value) {
|
|
471
|
+
if (value === undefined)
|
|
472
|
+
return;
|
|
473
|
+
if (!(typeof value === "number" && Number.isSafeInteger(value) && value >= 1))
|
|
474
|
+
fail(`${at} must be a positive integer of seconds (got ${shown(value)})`);
|
|
475
|
+
return value;
|
|
476
|
+
}
|
|
477
|
+
function resolveOAuth(input) {
|
|
478
|
+
if (input === undefined)
|
|
479
|
+
return {};
|
|
480
|
+
if (!isObject(input))
|
|
481
|
+
fail(`auth.oauth must be an object (got ${shown(input)})`);
|
|
482
|
+
onlyKeys("auth.oauth", input, Object.keys(OAUTH_PROVIDERS));
|
|
483
|
+
const resolved = {};
|
|
484
|
+
for (const [name, config] of Object.entries(input)) {
|
|
485
|
+
const at = `auth.oauth.${name}`;
|
|
486
|
+
if (!isObject(config))
|
|
487
|
+
fail(`${at} must be an object (got ${shown(config)})`);
|
|
488
|
+
const { secrets } = OAUTH_PROVIDERS[name];
|
|
489
|
+
onlyKeys(at, config, providerKeys(name));
|
|
490
|
+
const kept = {};
|
|
491
|
+
for (const [key, value] of Object.entries(config)) {
|
|
492
|
+
if (value === undefined || value === "")
|
|
493
|
+
continue;
|
|
494
|
+
kept[key] = value;
|
|
495
|
+
if (secrets.includes(key)) {
|
|
496
|
+
if (typeof value !== "string" && !isEnvRef(value))
|
|
497
|
+
fail(`${at}.${key} must be a string or env("NAME")`);
|
|
498
|
+
} else if (typeof value !== "string")
|
|
499
|
+
fail(`${at}.${key} must be a string (got ${shown(value)})`);
|
|
500
|
+
}
|
|
501
|
+
resolved[name] = kept;
|
|
502
|
+
}
|
|
503
|
+
return resolved;
|
|
504
|
+
}
|
|
505
|
+
function isOrigin(value) {
|
|
506
|
+
if (typeof value !== "string")
|
|
507
|
+
return false;
|
|
508
|
+
try {
|
|
509
|
+
const url = new URL(value);
|
|
510
|
+
return (url.protocol === "https:" || url.protocol === "http:") && url.origin === value;
|
|
511
|
+
} catch {
|
|
512
|
+
return false;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
function resolveWebAuthn(input) {
|
|
516
|
+
if (input === undefined)
|
|
517
|
+
return {};
|
|
518
|
+
if (!isObject(input))
|
|
519
|
+
fail(`auth.webauthn must be an object (got ${shown(input)})`);
|
|
520
|
+
onlyKeys("auth.webauthn", input, WEBAUTHN_KEYS);
|
|
521
|
+
const { rpID, rpName, origins } = input;
|
|
522
|
+
if (rpID !== undefined && !(typeof rpID === "string" && HOST_NAME.test(rpID)))
|
|
523
|
+
fail(`auth.webauthn.rpID must be a lowercase host name such as "example.com" (got ${shown(rpID)})`);
|
|
524
|
+
if (rpName !== undefined && !(typeof rpName === "string" && rpName !== ""))
|
|
525
|
+
fail(`auth.webauthn.rpName must be a non-empty string (got ${shown(rpName)})`);
|
|
526
|
+
if (origins !== undefined) {
|
|
527
|
+
if (!Array.isArray(origins) || !origins.length)
|
|
528
|
+
fail(`auth.webauthn.origins must be a non-empty array of origins such as "https://example.com" (got ${shown(origins)})`);
|
|
529
|
+
for (const origin of origins) {
|
|
530
|
+
if (!isOrigin(origin))
|
|
531
|
+
fail(`auth.webauthn.origins entry ${shown(origin)} must be an origin such as "https://example.com"`);
|
|
532
|
+
const host = new URL(origin).hostname;
|
|
533
|
+
if (typeof rpID === "string" && host !== rpID && !host.endsWith(`.${rpID}`))
|
|
534
|
+
fail(`auth.webauthn.rpID ${JSON.stringify(rpID)} is not the host or a parent domain of origin ${JSON.stringify(origin)}`);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
return {
|
|
538
|
+
...rpID === undefined ? {} : { rpID },
|
|
539
|
+
...rpName === undefined ? {} : { rpName },
|
|
540
|
+
...origins === undefined ? {} : { origins: [...origins] }
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
function resolveEmail(input) {
|
|
544
|
+
const email = input === undefined ? {} : input;
|
|
545
|
+
if (!isObject(email))
|
|
546
|
+
fail(`auth.email must be an object (got ${shown(email)})`);
|
|
547
|
+
onlyKeys("auth.email", email, Object.keys(EMAIL));
|
|
548
|
+
const resolved = structuredClone(EMAIL);
|
|
549
|
+
for (const purpose of Object.keys(EMAIL)) {
|
|
550
|
+
const given = email[purpose];
|
|
551
|
+
if (given === undefined)
|
|
552
|
+
continue;
|
|
553
|
+
const at = `auth.email.${purpose}`;
|
|
554
|
+
if (!isObject(given))
|
|
555
|
+
fail(`${at} must be an object (got ${shown(given)})`);
|
|
556
|
+
onlyKeys(at, given, EMAIL_KEYS);
|
|
557
|
+
const { page } = given;
|
|
558
|
+
if (page !== undefined) {
|
|
559
|
+
if (!isAppPath(page))
|
|
560
|
+
fail(`${at}.page must be an app path such as "/auth/magic-link" (got ${shown(page)})`);
|
|
561
|
+
if (/[?#]/.test(page))
|
|
562
|
+
fail(`${at}.page must be a path without a query or a hash (got ${shown(page)})`);
|
|
563
|
+
resolved[purpose].page = page;
|
|
564
|
+
}
|
|
565
|
+
for (const key of ["template", "subject"]) {
|
|
566
|
+
const value = given[key];
|
|
567
|
+
if (value === undefined)
|
|
568
|
+
continue;
|
|
569
|
+
if (typeof value !== "string" || value === "")
|
|
570
|
+
fail(`${at}.${key} must be a non-empty string (got ${shown(value)})`);
|
|
571
|
+
resolved[purpose][key] = value;
|
|
572
|
+
}
|
|
573
|
+
resolved[purpose].ttl = seconds(`${at}.ttl`, given.ttl) ?? resolved[purpose].ttl;
|
|
574
|
+
}
|
|
575
|
+
return resolved;
|
|
576
|
+
}
|
|
577
|
+
function resolveAccessTokens(input) {
|
|
578
|
+
if (input === undefined)
|
|
579
|
+
return;
|
|
580
|
+
if (!isObject(input))
|
|
581
|
+
fail(`auth.accessTokens must be an object (got ${shown(input)})`);
|
|
582
|
+
onlyKeys("auth.accessTokens", input, ["user"]);
|
|
583
|
+
const { user } = input;
|
|
584
|
+
if (typeof user !== "string" || user === "")
|
|
585
|
+
fail(`auth.accessTokens.user must be the path of a file (got ${shown(user)})`);
|
|
586
|
+
return { user };
|
|
587
|
+
}
|
|
588
|
+
function resolveOptions(input) {
|
|
589
|
+
if (input !== undefined && !isObject(input))
|
|
590
|
+
fail(`auth must be an object (got ${shown(input)})`);
|
|
591
|
+
const auth = input ?? {};
|
|
592
|
+
onlyKeys("auth", auth, TOP_KEYS);
|
|
593
|
+
const session = auth.session ?? {};
|
|
594
|
+
if (!isObject(session))
|
|
595
|
+
fail(`auth.session must be an object (got ${shown(session)})`);
|
|
596
|
+
onlyKeys("auth.session", session, SESSION_KEYS);
|
|
597
|
+
const { password } = session;
|
|
598
|
+
if (password !== undefined) {
|
|
599
|
+
const literal = typeof password === "string" && password.length >= MIN_PASSWORD;
|
|
600
|
+
if (!literal && !isEnvRef(password))
|
|
601
|
+
fail(`auth.session.password must be a string of at least ${MIN_PASSWORD} characters or env("NAME")`);
|
|
602
|
+
}
|
|
603
|
+
const maxAge = seconds("auth.session.maxAge", session.maxAge) ?? DEFAULT_MAX_AGE;
|
|
604
|
+
const idleTimeout = seconds("auth.session.idleTimeout", session.idleTimeout);
|
|
605
|
+
const storage = session.storage ?? false;
|
|
606
|
+
if (storage !== false && !(typeof storage === "string" && storage !== ""))
|
|
607
|
+
fail(`auth.session.storage must be a storage mount name or false (got ${shown(storage)})`);
|
|
608
|
+
const cookie = session.cookie ?? {};
|
|
609
|
+
if (!isObject(cookie))
|
|
610
|
+
fail(`auth.session.cookie must be an object (got ${shown(cookie)})`);
|
|
611
|
+
for (const key of ["maxAge", "expires"])
|
|
612
|
+
if (Object.hasOwn(cookie, key))
|
|
613
|
+
fail(`auth.session.cookie cannot set ${key}; use auth.session.maxAge`);
|
|
614
|
+
onlyKeys("auth.session.cookie", cookie, COOKIE_KEYS);
|
|
615
|
+
for (const key of ["path", "domain"]) {
|
|
616
|
+
if (cookie[key] !== undefined && typeof cookie[key] !== "string")
|
|
617
|
+
fail(`auth.session.cookie.${key} must be a string (got ${shown(cookie[key])})`);
|
|
618
|
+
}
|
|
619
|
+
for (const key of ["httpOnly", "secure", "partitioned"]) {
|
|
620
|
+
if (cookie[key] !== undefined && typeof cookie[key] !== "boolean")
|
|
621
|
+
fail(`auth.session.cookie.${key} must be true or false (got ${shown(cookie[key])})`);
|
|
622
|
+
}
|
|
623
|
+
const { sameSite } = cookie;
|
|
624
|
+
if (sameSite !== undefined && !SAME_SITE.includes(sameSite))
|
|
625
|
+
fail(`auth.session.cookie.sameSite must be "lax", "strict" or "none" (got ${shown(sameSite)})`);
|
|
626
|
+
if (sameSite === "none" && cookie.secure === false)
|
|
627
|
+
fail('auth.session.cookie.sameSite "none" needs secure: true; a browser drops a SameSite=None cookie without Secure');
|
|
628
|
+
const hostOnly = cookie.secure !== false && (cookie.path ?? "/") === "/" && cookie.domain === undefined;
|
|
629
|
+
const name = session.name ?? (hostOnly ? "__Host-hamur-auth" : "hamur-auth");
|
|
630
|
+
if (typeof name !== "string" || !TOKEN.test(name))
|
|
631
|
+
fail(`auth.session.name must be a cookie name (got ${shown(name)})`);
|
|
632
|
+
if (/^__host-/i.test(name) && !hostOnly)
|
|
633
|
+
fail(`auth.session.name ${JSON.stringify(name)} needs auth.session.cookie with secure, path "/" and no domain`);
|
|
634
|
+
const loadStrategy = auth.loadStrategy ?? "server-first";
|
|
635
|
+
if (!LOAD_STRATEGIES.includes(loadStrategy))
|
|
636
|
+
fail(`auth.loadStrategy must be "server-first", "client-only" or "none" (got ${shown(loadStrategy)})`);
|
|
637
|
+
const redirect = auth.redirect === undefined ? {} : auth.redirect;
|
|
638
|
+
if (!isObject(redirect))
|
|
639
|
+
fail(`auth.redirect must be an object (got ${shown(redirect)})`);
|
|
640
|
+
onlyKeys("auth.redirect", redirect, REDIRECT_KEYS);
|
|
641
|
+
const target = (key, fallback) => {
|
|
642
|
+
const value = redirect[key] === undefined ? fallback : redirect[key];
|
|
643
|
+
if (!isAppPath(value))
|
|
644
|
+
fail(`auth.redirect.${key} must be an app path such as ${JSON.stringify(fallback)} (got ${shown(value)})`);
|
|
645
|
+
if (/[?#]/.test(value))
|
|
646
|
+
fail(`auth.redirect.${key} must be a path without a query or a hash (got ${shown(value)})`);
|
|
647
|
+
return value;
|
|
648
|
+
};
|
|
649
|
+
const oauth = resolveOAuth(auth.oauth);
|
|
650
|
+
const webauthn = resolveWebAuthn(auth.webauthn);
|
|
651
|
+
const email = resolveEmail(auth.email);
|
|
652
|
+
const accessTokens = resolveAccessTokens(auth.accessTokens);
|
|
653
|
+
return {
|
|
654
|
+
redirect: { login: target("login", "/login"), home: target("home", "/") },
|
|
655
|
+
oauth,
|
|
656
|
+
webauthn,
|
|
657
|
+
email,
|
|
658
|
+
...accessTokens === undefined ? {} : { accessTokens },
|
|
659
|
+
loadStrategy,
|
|
660
|
+
session: {
|
|
661
|
+
name,
|
|
662
|
+
...password === undefined ? {} : { password },
|
|
663
|
+
maxAge,
|
|
664
|
+
...idleTimeout === undefined ? {} : { idleTimeout },
|
|
665
|
+
...storage === false ? {} : { storage },
|
|
666
|
+
cookie
|
|
667
|
+
}
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
// packages/auth/src/module.ts
|
|
672
|
+
var module_default = defineModule({
|
|
673
|
+
meta: { name: "auth", configKey: "auth" },
|
|
674
|
+
setup(input, hamur) {
|
|
675
|
+
const options = resolveOptions(input);
|
|
676
|
+
setupSession(options, hamur);
|
|
677
|
+
setupClient(options, hamur);
|
|
678
|
+
setupGuards(options, hamur);
|
|
679
|
+
setupOAuth(options, hamur);
|
|
680
|
+
setupWebAuthn(options, hamur);
|
|
681
|
+
setupSingleUse(hamur);
|
|
682
|
+
setupEmail(options, hamur);
|
|
683
|
+
setupAccessTokens(options, hamur);
|
|
684
|
+
}
|
|
685
|
+
});
|
|
686
|
+
export {
|
|
687
|
+
module_default as default
|
|
688
|
+
};
|