@slotform/auth 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +26 -2
- package/dist/ability/index.d.ts +31 -0
- package/dist/ability/index.d.ts.map +1 -0
- package/dist/ability/index.js +64 -0
- package/dist/ability/index.js.map +1 -0
- package/dist/access.d.ts +19 -0
- package/dist/access.d.ts.map +1 -0
- package/dist/access.js +33 -0
- package/dist/access.js.map +1 -0
- package/dist/callback-stubs.d.ts +15 -0
- package/dist/callback-stubs.d.ts.map +1 -0
- package/dist/callback-stubs.js +28 -0
- package/dist/callback-stubs.js.map +1 -0
- package/dist/client.d.ts +38 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +37 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +190 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +451 -0
- package/dist/index.js.map +1 -0
- package/dist/infer.d.ts +31 -0
- package/dist/infer.d.ts.map +1 -0
- package/dist/infer.js +2 -0
- package/dist/infer.js.map +1 -0
- package/dist/schema/devices.d.ts +404 -0
- package/dist/schema/devices.d.ts.map +1 -0
- package/dist/schema/devices.js +45 -0
- package/dist/schema/devices.js.map +1 -0
- package/dist/schema/index.d.ts +679 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +94 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/schema/oauth.d.ts +2062 -0
- package/dist/schema/oauth.d.ts.map +1 -0
- package/dist/schema/oauth.js +245 -0
- package/dist/schema/oauth.js.map +1 -0
- package/dist/schema/organization.d.ts +388 -0
- package/dist/schema/organization.d.ts.map +1 -0
- package/dist/schema/organization.js +75 -0
- package/dist/schema/organization.js.map +1 -0
- package/dist/schema/passkey.d.ts +215 -0
- package/dist/schema/passkey.d.ts.map +1 -0
- package/dist/schema/passkey.js +29 -0
- package/dist/schema/passkey.js.map +1 -0
- package/dist/schema/sets.d.ts +3715 -0
- package/dist/schema/sets.d.ts.map +1 -0
- package/dist/schema/sets.js +48 -0
- package/dist/schema/sets.js.map +1 -0
- package/dist/scope.d.ts +36 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scope.js +59 -0
- package/dist/scope.js.map +1 -0
- package/dist/screens-fixtures.d.ts +148 -0
- package/dist/screens-fixtures.d.ts.map +1 -0
- package/dist/screens-fixtures.js +122 -0
- package/dist/screens-fixtures.js.map +1 -0
- package/dist/screens-session.d.ts +4 -0
- package/dist/screens-session.d.ts.map +1 -0
- package/dist/screens-session.js +24 -0
- package/dist/screens-session.js.map +1 -0
- package/dist/screens.d.ts +3 -0
- package/dist/screens.d.ts.map +1 -0
- package/dist/screens.js +8 -0
- package/dist/screens.js.map +1 -0
- package/dist/serve.d.ts +3 -0
- package/dist/serve.d.ts.map +1 -0
- package/dist/serve.js +44 -0
- package/dist/serve.js.map +1 -0
- package/dist/types.d.ts +152 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +215 -0
- package/dist/types.js.map +1 -0
- package/dist/worker/callback-url.d.ts +6 -0
- package/dist/worker/callback-url.d.ts.map +1 -0
- package/dist/worker/callback-url.js +57 -0
- package/dist/worker/callback-url.js.map +1 -0
- package/dist/worker/cimd-transport.d.ts +2 -0
- package/dist/worker/cimd-transport.d.ts.map +1 -0
- package/dist/worker/cimd-transport.js +86 -0
- package/dist/worker/cimd-transport.js.map +1 -0
- package/dist/worker/devices.d.ts +6 -0
- package/dist/worker/devices.d.ts.map +1 -0
- package/dist/worker/devices.js +70 -0
- package/dist/worker/devices.js.map +1 -0
- package/dist/worker/email-key.d.ts +2 -0
- package/dist/worker/email-key.d.ts.map +1 -0
- package/dist/worker/email-key.js +20 -0
- package/dist/worker/email-key.js.map +1 -0
- package/dist/worker/index.d.ts +156 -0
- package/dist/worker/index.d.ts.map +1 -0
- package/dist/worker/index.js +507 -0
- package/dist/worker/index.js.map +1 -0
- package/dist/worker/listener.d.ts +12 -0
- package/dist/worker/listener.d.ts.map +1 -0
- package/dist/worker/listener.js +37 -0
- package/dist/worker/listener.js.map +1 -0
- package/dist/worker/oauth.d.ts +95 -0
- package/dist/worker/oauth.d.ts.map +1 -0
- package/dist/worker/oauth.js +456 -0
- package/dist/worker/oauth.js.map +1 -0
- package/dist/worker/outputs.d.ts +75 -0
- package/dist/worker/outputs.d.ts.map +1 -0
- package/dist/worker/outputs.js +7 -0
- package/dist/worker/outputs.js.map +1 -0
- package/dist/worker/procedures.d.ts +360 -0
- package/dist/worker/procedures.d.ts.map +1 -0
- package/dist/worker/procedures.js +464 -0
- package/dist/worker/procedures.js.map +1 -0
- package/dist/worker/rate-limit.d.ts +7 -0
- package/dist/worker/rate-limit.d.ts.map +1 -0
- package/dist/worker/rate-limit.js +49 -0
- package/dist/worker/rate-limit.js.map +1 -0
- package/dist/worker/reserved-slugs.d.ts +4 -0
- package/dist/worker/reserved-slugs.d.ts.map +1 -0
- package/dist/worker/reserved-slugs.js +30 -0
- package/dist/worker/reserved-slugs.js.map +1 -0
- package/dist/worker/session-ended.d.ts +6 -0
- package/dist/worker/session-ended.d.ts.map +1 -0
- package/dist/worker/session-ended.js +10 -0
- package/dist/worker/session-ended.js.map +1 -0
- package/dist/worker/sign-up-policy.d.ts +20 -0
- package/dist/worker/sign-up-policy.d.ts.map +1 -0
- package/dist/worker/sign-up-policy.js +65 -0
- package/dist/worker/sign-up-policy.js.map +1 -0
- package/dist/worker/tenancy.d.ts +23 -0
- package/dist/worker/tenancy.d.ts.map +1 -0
- package/dist/worker/tenancy.js +133 -0
- package/dist/worker/tenancy.js.map +1 -0
- package/guide/abilities.md +55 -0
- package/guide/add-a-role.md +33 -0
- package/guide/callbacks.md +97 -0
- package/guide/config.md +84 -0
- package/guide/devices.md +85 -0
- package/guide/mcp-oauth.md +97 -0
- package/guide/organizations.md +114 -0
- package/guide/protect-by-scope.md +29 -0
- package/guide/scopes.md +87 -0
- package/guide/sign-in.md +127 -0
- package/package.json +155 -3
- package/src/ability/index.ts +174 -0
- package/src/access.ts +45 -0
- package/src/callback-stubs.ts +45 -0
- package/src/client.ts +119 -0
- package/src/expo.tsx +275 -0
- package/src/index.ts +574 -0
- package/src/infer.ts +44 -0
- package/src/schema/devices.ts +54 -0
- package/src/schema/index.ts +128 -0
- package/src/schema/oauth.ts +320 -0
- package/src/schema/organization.ts +104 -0
- package/src/schema/passkey.ts +38 -0
- package/src/schema/sets.ts +73 -0
- package/src/scope.ts +135 -0
- package/src/screens-fixtures.ts +149 -0
- package/src/screens-session.ts +26 -0
- package/src/screens.ts +10 -0
- package/src/serve.ts +49 -0
- package/src/types.ts +313 -0
- package/src/worker/callback-url.ts +65 -0
- package/src/worker/cimd-transport.ts +115 -0
- package/src/worker/devices.ts +83 -0
- package/src/worker/email-key.ts +21 -0
- package/src/worker/index.ts +941 -0
- package/src/worker/listener.ts +55 -0
- package/src/worker/oauth.ts +709 -0
- package/src/worker/outputs.ts +102 -0
- package/src/worker/procedures.ts +758 -0
- package/src/worker/rate-limit.ts +64 -0
- package/src/worker/reserved-slugs.ts +39 -0
- package/src/worker/session-ended.ts +14 -0
- package/src/worker/sign-up-policy.ts +88 -0
- package/src/worker/tenancy.ts +229 -0
- package/templates/callbacks.ts +40 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const AUTH_PREFIX = "/api/auth";
|
|
3
|
+
export declare const OAUTH_DISCOVERY_PREFIXES: readonly ["/.well-known/oauth-authorization-server", "/.well-known/oauth-protected-resource"];
|
|
4
|
+
export declare const CLIENT_IP_HEADER = "cf-connecting-ip";
|
|
5
|
+
export declare const authOptionsSchema: z.ZodObject<{
|
|
6
|
+
cookies: z.ZodOptional<z.ZodObject<{
|
|
7
|
+
prefix: z.ZodOptional<z.ZodString>;
|
|
8
|
+
domain: z.ZodOptional<z.ZodString>;
|
|
9
|
+
}, z.core.$strip>>;
|
|
10
|
+
session: z.ZodOptional<z.ZodObject<{
|
|
11
|
+
expiresIn: z.ZodOptional<z.ZodNumber>;
|
|
12
|
+
updateAge: z.ZodOptional<z.ZodNumber>;
|
|
13
|
+
freshAge: z.ZodOptional<z.ZodNumber>;
|
|
14
|
+
}, z.core.$strip>>;
|
|
15
|
+
user: z.ZodOptional<z.ZodObject<{
|
|
16
|
+
deleteUser: z.ZodOptional<z.ZodBoolean>;
|
|
17
|
+
}, z.core.$strip>>;
|
|
18
|
+
organization: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodObject<{
|
|
19
|
+
ac: z.ZodOptional<z.ZodUnknown>;
|
|
20
|
+
roles: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
21
|
+
}, z.core.$strip>]>>;
|
|
22
|
+
emailOtp: z.ZodDefault<z.ZodBoolean>;
|
|
23
|
+
magicLink: z.ZodDefault<z.ZodBoolean>;
|
|
24
|
+
socialProviders: z.ZodOptional<z.ZodObject<{
|
|
25
|
+
google: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodObject<{
|
|
26
|
+
clientIdVar: z.ZodOptional<z.ZodString>;
|
|
27
|
+
clientSecretVar: z.ZodOptional<z.ZodString>;
|
|
28
|
+
}, z.core.$strip>]>>;
|
|
29
|
+
apple: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodObject<{
|
|
30
|
+
clientIdVar: z.ZodOptional<z.ZodString>;
|
|
31
|
+
clientSecretVar: z.ZodOptional<z.ZodString>;
|
|
32
|
+
appBundleIdentifier: z.ZodOptional<z.ZodString>;
|
|
33
|
+
}, z.core.$strip>]>>;
|
|
34
|
+
}, z.core.$strip>>;
|
|
35
|
+
expo: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodObject<{
|
|
36
|
+
scheme: z.ZodString;
|
|
37
|
+
}, z.core.$strip>]>>;
|
|
38
|
+
passkey: z.ZodDefault<z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodObject<{
|
|
39
|
+
rpID: z.ZodOptional<z.ZodString>;
|
|
40
|
+
rpName: z.ZodOptional<z.ZodString>;
|
|
41
|
+
origin: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>>;
|
|
42
|
+
authenticatorSelection: z.ZodOptional<z.ZodObject<{
|
|
43
|
+
authenticatorAttachment: z.ZodOptional<z.ZodEnum<{
|
|
44
|
+
"cross-platform": "cross-platform";
|
|
45
|
+
platform: "platform";
|
|
46
|
+
}>>;
|
|
47
|
+
requireResidentKey: z.ZodOptional<z.ZodBoolean>;
|
|
48
|
+
residentKey: z.ZodOptional<z.ZodEnum<{
|
|
49
|
+
discouraged: "discouraged";
|
|
50
|
+
preferred: "preferred";
|
|
51
|
+
required: "required";
|
|
52
|
+
}>>;
|
|
53
|
+
userVerification: z.ZodOptional<z.ZodEnum<{
|
|
54
|
+
discouraged: "discouraged";
|
|
55
|
+
preferred: "preferred";
|
|
56
|
+
required: "required";
|
|
57
|
+
}>>;
|
|
58
|
+
}, z.core.$strip>>;
|
|
59
|
+
}, z.core.$strip>]>>;
|
|
60
|
+
mcp: z.ZodDefault<z.ZodBoolean>;
|
|
61
|
+
devices: z.ZodDefault<z.ZodBoolean>;
|
|
62
|
+
signUp: z.ZodDefault<z.ZodEnum<{
|
|
63
|
+
first: "first";
|
|
64
|
+
invite: "invite";
|
|
65
|
+
open: "open";
|
|
66
|
+
}>>;
|
|
67
|
+
secretVar: z.ZodDefault<z.ZodString>;
|
|
68
|
+
appUrlVar: z.ZodDefault<z.ZodString>;
|
|
69
|
+
rateLimiter: z.ZodDefault<z.ZodObject<{
|
|
70
|
+
ip: z.ZodDefault<z.ZodObject<{
|
|
71
|
+
binding: z.ZodDefault<z.ZodString>;
|
|
72
|
+
limit: z.ZodDefault<z.ZodNumber>;
|
|
73
|
+
period: z.ZodDefault<z.ZodNumber>;
|
|
74
|
+
}, z.core.$strip>>;
|
|
75
|
+
email: z.ZodDefault<z.ZodObject<{
|
|
76
|
+
binding: z.ZodDefault<z.ZodString>;
|
|
77
|
+
limit: z.ZodDefault<z.ZodNumber>;
|
|
78
|
+
period: z.ZodDefault<z.ZodNumber>;
|
|
79
|
+
}, z.core.$strip>>;
|
|
80
|
+
agent: z.ZodDefault<z.ZodObject<{
|
|
81
|
+
binding: z.ZodDefault<z.ZodString>;
|
|
82
|
+
limit: z.ZodDefault<z.ZodNumber>;
|
|
83
|
+
period: z.ZodDefault<z.ZodNumber>;
|
|
84
|
+
}, z.core.$strip>>;
|
|
85
|
+
}, z.core.$strip>>;
|
|
86
|
+
}, z.core.$strip>;
|
|
87
|
+
export type AuthOptions = z.input<typeof authOptionsSchema>;
|
|
88
|
+
export type ResolvedAuthOptions = z.output<typeof authOptionsSchema>;
|
|
89
|
+
export type SocialProviderName = "google" | "apple";
|
|
90
|
+
export interface ResolvedSocialProvider {
|
|
91
|
+
clientIdVar: string;
|
|
92
|
+
clientSecretVar: string;
|
|
93
|
+
appBundleIdentifier?: string;
|
|
94
|
+
}
|
|
95
|
+
export declare function resolveSocialProviders(input: ResolvedAuthOptions["socialProviders"]): Partial<Record<SocialProviderName, ResolvedSocialProvider>>;
|
|
96
|
+
export interface AuthUser {
|
|
97
|
+
id: string;
|
|
98
|
+
email: string;
|
|
99
|
+
emailVerified: boolean;
|
|
100
|
+
name: string;
|
|
101
|
+
image?: string | null;
|
|
102
|
+
createdAt: Date;
|
|
103
|
+
updatedAt: Date;
|
|
104
|
+
}
|
|
105
|
+
export type OtpType = "sign-in" | "email-verification" | "forget-password" | "change-email";
|
|
106
|
+
export interface AuthCallbackPayloads<TEnv = unknown> {
|
|
107
|
+
sendOTP: {
|
|
108
|
+
email: string;
|
|
109
|
+
code: string;
|
|
110
|
+
type: OtpType;
|
|
111
|
+
env: TEnv;
|
|
112
|
+
};
|
|
113
|
+
sendMagicLink: {
|
|
114
|
+
email: string;
|
|
115
|
+
url: string;
|
|
116
|
+
token: string;
|
|
117
|
+
env: TEnv;
|
|
118
|
+
};
|
|
119
|
+
sendInvitation: {
|
|
120
|
+
invitationId: string;
|
|
121
|
+
email: string;
|
|
122
|
+
role: string;
|
|
123
|
+
organization: {
|
|
124
|
+
id: string;
|
|
125
|
+
name: string;
|
|
126
|
+
slug: string;
|
|
127
|
+
};
|
|
128
|
+
inviter: {
|
|
129
|
+
id: string;
|
|
130
|
+
name: string;
|
|
131
|
+
email: string;
|
|
132
|
+
};
|
|
133
|
+
env: TEnv;
|
|
134
|
+
};
|
|
135
|
+
beforeDelete: {
|
|
136
|
+
user: AuthUser;
|
|
137
|
+
request?: Request;
|
|
138
|
+
env: TEnv;
|
|
139
|
+
};
|
|
140
|
+
sendDeleteVerification: {
|
|
141
|
+
user: AuthUser;
|
|
142
|
+
url: string;
|
|
143
|
+
token: string;
|
|
144
|
+
env: TEnv;
|
|
145
|
+
};
|
|
146
|
+
generateOTP: {
|
|
147
|
+
email: string;
|
|
148
|
+
type: OtpType;
|
|
149
|
+
env: TEnv;
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB,eAAO,MAAM,WAAW,cAAc,CAAC;AAGvC,eAAO,MAAM,wBAAwB,YACpC,yCAAyC,EACzC,uCAAuC,CAC9B,CAAC;AAIX,eAAO,MAAM,gBAAgB,qBAAqB,CAAC;AAiFnD,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuG3B,CAAC;AAGJ,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAG5D,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,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AC,IAAI,EAAE,MAAM,CAAA;SAAE,CAAC;QACzD,OAAO,EAAE;YAAE,EAAE,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SAAE,CAAC;QACrD,GAAG,EAAE,IAAI,CAAC;KACV,CAAC;IACF,YAAY,EAAE;QAAE,IAAI,EAAE,QAAQ,CAAC;QAAC,OAAO,CAAC,EAAE,OAAO,CAAC;QAAC,GAAG,EAAE,IAAI,CAAA;KAAE,CAAC;IAC/D,sBAAsB,EAAE;QACvB,IAAI,EAAE,QAAQ,CAAC;QACf,GAAG,EAAE,MAAM,CAAC;QACZ,KAAK,EAAE,MAAM,CAAC;QACd,GAAG,EAAE,IAAI,CAAC;KACV,CAAC;IACF,WAAW,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,OAAO,CAAC;QAAC,GAAG,EAAE,IAAI,CAAA;KAAE,CAAC;CACzD"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
// The path better-auth mounts under inside the worker. Shared so the runtime
|
|
3
|
+
// and the codegen contribution to `api.slots.routePrefixes` can never drift.
|
|
4
|
+
export const AUTH_PREFIX = "/api/auth";
|
|
5
|
+
// Discovery prefixes of the authorization server and the protected resource: docs/architecture/plugins/auth.md, "MCP OAuth".
|
|
6
|
+
export const OAUTH_DISCOVERY_PREFIXES = [
|
|
7
|
+
"/.well-known/oauth-authorization-server",
|
|
8
|
+
"/.well-known/oauth-protected-resource",
|
|
9
|
+
];
|
|
10
|
+
// The header Cloudflare sets to the client's address. It is unspoofable behind
|
|
11
|
+
// the edge, which `X-Forwarded-For` is not.
|
|
12
|
+
export const CLIENT_IP_HEADER = "cf-connecting-ip";
|
|
13
|
+
// Limiter defaults and the 10 or 60 s period: docs/architecture/plugins/auth.md, "Rate limiting".
|
|
14
|
+
// The agent limiter is one bucket per grant, bound only while `mcp` is on.
|
|
15
|
+
const RATE_LIMITER_DEFAULTS = {
|
|
16
|
+
ip: { binding: "RATE_LIMITER_IP", limit: 100, period: 60 },
|
|
17
|
+
email: { binding: "RATE_LIMITER_EMAIL", limit: 3, period: 60 },
|
|
18
|
+
agent: { binding: "RATE_LIMITER_AGENT", limit: 120, period: 60 },
|
|
19
|
+
};
|
|
20
|
+
function rateLimiterSchema(defaults) {
|
|
21
|
+
return z
|
|
22
|
+
.object({
|
|
23
|
+
binding: z.string().default(defaults.binding),
|
|
24
|
+
limit: z
|
|
25
|
+
.number()
|
|
26
|
+
.positive({
|
|
27
|
+
error: "auth: rateLimiter.limit must be a positive number",
|
|
28
|
+
})
|
|
29
|
+
.default(defaults.limit),
|
|
30
|
+
period: z
|
|
31
|
+
.number()
|
|
32
|
+
.positive({
|
|
33
|
+
error: "auth: rateLimiter.period must be a positive number",
|
|
34
|
+
})
|
|
35
|
+
.default(defaults.period),
|
|
36
|
+
})
|
|
37
|
+
.default(defaults);
|
|
38
|
+
}
|
|
39
|
+
const organizationObjectSchema = z.object({
|
|
40
|
+
ac: z.unknown().optional(),
|
|
41
|
+
roles: z.record(z.string(), z.unknown()).optional(),
|
|
42
|
+
});
|
|
43
|
+
const socialProviderConfigSchema = z.object({
|
|
44
|
+
clientIdVar: z.string().optional(),
|
|
45
|
+
clientSecretVar: z.string().optional(),
|
|
46
|
+
});
|
|
47
|
+
const appleProviderConfigSchema = socialProviderConfigSchema.extend({
|
|
48
|
+
// Native (Expo) Apple Sign-In issues the ID token to the app bundle id;
|
|
49
|
+
// Better Auth needs it to validate native tokens. Optional — the
|
|
50
|
+
// browser-redirect OAuth flow doesn't require it.
|
|
51
|
+
appBundleIdentifier: z.string().optional(),
|
|
52
|
+
});
|
|
53
|
+
const passkeyObjectSchema = z.object({
|
|
54
|
+
// The WebAuthn relying-party id: the domain credentials are bound to.
|
|
55
|
+
// Defaults to `app.domain`, a registrable suffix of every derived origin.
|
|
56
|
+
rpID: z.string().optional(),
|
|
57
|
+
// The name the authenticator shows the user. Defaults to `app.name`.
|
|
58
|
+
rpName: z.string().optional(),
|
|
59
|
+
// Origins a ceremony may come from. Defaults to the worker's resolved
|
|
60
|
+
// production CORS list.
|
|
61
|
+
origin: z.union([z.string(), z.array(z.string())]).optional(),
|
|
62
|
+
// Passed through to the registration options; better-auth's defaults are
|
|
63
|
+
// `residentKey: "preferred"` and `userVerification: "preferred"`.
|
|
64
|
+
authenticatorSelection: z
|
|
65
|
+
.object({
|
|
66
|
+
authenticatorAttachment: z
|
|
67
|
+
.enum(["platform", "cross-platform"])
|
|
68
|
+
.optional(),
|
|
69
|
+
requireResidentKey: z.boolean().optional(),
|
|
70
|
+
residentKey: z.enum(["discouraged", "preferred", "required"]).optional(),
|
|
71
|
+
userVerification: z
|
|
72
|
+
.enum(["discouraged", "preferred", "required"])
|
|
73
|
+
.optional(),
|
|
74
|
+
})
|
|
75
|
+
.optional(),
|
|
76
|
+
});
|
|
77
|
+
export const authOptionsSchema = z
|
|
78
|
+
.object({
|
|
79
|
+
cookies: z
|
|
80
|
+
.object({
|
|
81
|
+
prefix: z.string().optional(),
|
|
82
|
+
domain: z.string().optional(),
|
|
83
|
+
})
|
|
84
|
+
.optional(),
|
|
85
|
+
session: z
|
|
86
|
+
.object({
|
|
87
|
+
expiresIn: z
|
|
88
|
+
.number()
|
|
89
|
+
.positive({
|
|
90
|
+
error: "auth: session.expiresIn must be a positive number",
|
|
91
|
+
})
|
|
92
|
+
.optional(),
|
|
93
|
+
updateAge: z.number().optional(),
|
|
94
|
+
// How recently the session must have been created for better-auth to
|
|
95
|
+
// treat it as fresh; account deletion without email confirmation needs
|
|
96
|
+
// a fresh session. 0 disables the check entirely, so any stolen
|
|
97
|
+
// session cookie of any age can delete the account. Passwordless
|
|
98
|
+
// consumers should implement the `sendDeleteVerification` callback
|
|
99
|
+
// (email confirmation, no freshness requirement) instead of 0.
|
|
100
|
+
freshAge: z
|
|
101
|
+
.number()
|
|
102
|
+
.nonnegative({
|
|
103
|
+
error: "auth: session.freshAge must be zero or a positive number",
|
|
104
|
+
})
|
|
105
|
+
.optional(),
|
|
106
|
+
})
|
|
107
|
+
.optional(),
|
|
108
|
+
user: z
|
|
109
|
+
.object({
|
|
110
|
+
// Account deletion (App Store 5.1.1(v) requires it for a native app).
|
|
111
|
+
// Off unless asked for: the endpoint destroys rows. The consumer's
|
|
112
|
+
// `beforeDelete` callback vetoes or cleans up. With the
|
|
113
|
+
// `sendDeleteVerification` callback implemented, deletion goes
|
|
114
|
+
// through an emailed confirmation link; without it, a session older
|
|
115
|
+
// than `session.freshAge` is refused.
|
|
116
|
+
deleteUser: z.boolean().optional(),
|
|
117
|
+
})
|
|
118
|
+
.optional(),
|
|
119
|
+
organization: z.union([z.boolean(), organizationObjectSchema]).optional(),
|
|
120
|
+
// Email one-time-password sign-in. On by default; OAuth-only consumers set
|
|
121
|
+
// `false` to drop the email-OTP plugin and its required callback file.
|
|
122
|
+
emailOtp: z.boolean().default(true),
|
|
123
|
+
// Magic-link sign-in. Off by default; on, the consumer's `sendMagicLink`
|
|
124
|
+
// callback is required. The token lives 5 minutes, is stored hashed and
|
|
125
|
+
// is spent on its first verification; an unknown address signs up.
|
|
126
|
+
magicLink: z.boolean().default(false),
|
|
127
|
+
// OAuth social providers. `true` enables a provider with conventional env
|
|
128
|
+
// var names (e.g. GOOGLE_CLIENT_ID / GOOGLE_CLIENT_SECRET); an object
|
|
129
|
+
// overrides the var names. The runtime reads credentials from env at request
|
|
130
|
+
// time — only the var names are baked into the generated worker.
|
|
131
|
+
socialProviders: z
|
|
132
|
+
.object({
|
|
133
|
+
google: z.union([z.boolean(), socialProviderConfigSchema]).optional(),
|
|
134
|
+
apple: z.union([z.boolean(), appleProviderConfigSchema]).optional(),
|
|
135
|
+
})
|
|
136
|
+
.optional(),
|
|
137
|
+
// Native (Expo) consumer flag: docs/architecture/plugins/auth.md, "Native client".
|
|
138
|
+
expo: z
|
|
139
|
+
.union([z.boolean(), z.object({ scheme: z.string().min(1) })])
|
|
140
|
+
.optional(),
|
|
141
|
+
// Passkey (WebAuthn) sign-in through `@better-auth/passkey`. Off by
|
|
142
|
+
// default; `{}` enables it with every field derived. The consumer migrates
|
|
143
|
+
// the `passkey` table by re-exporting `@slotform/auth/schema/passkey`.
|
|
144
|
+
passkey: z.union([z.literal(false), passkeyObjectSchema]).default(false),
|
|
145
|
+
// An OAuth authorization server for MCP clients (`@better-auth/mcp`): an
|
|
146
|
+
// agent connects by signing in, never by a pasted secret. Needs
|
|
147
|
+
// `organization`; the consumer migrates the OAuth and JWT tables by
|
|
148
|
+
// re-exporting `@slotform/auth/schema/oauth`.
|
|
149
|
+
mcp: z.boolean().default(false),
|
|
150
|
+
// Device credentials: long-lived, named, revocable bearer keys a signed-in
|
|
151
|
+
// member makes for a machine that has no browser (an agent daemon, a sync
|
|
152
|
+
// client). A key acts as its member on the API, `/ws` and the server's
|
|
153
|
+
// own routes. The consumer migrates the `apikey` table by re-exporting
|
|
154
|
+
// `@slotform/auth/schema/devices`.
|
|
155
|
+
devices: z.boolean().default(false),
|
|
156
|
+
// Who may create an account, on every way in (code, link, OAuth). `open`:
|
|
157
|
+
// anyone. `invite`: only an address holding a pending organization
|
|
158
|
+
// invitation. `first`: the first account, then as `invite`: a single-owner
|
|
159
|
+
// app. Both closed modes need `organization`: the invitations are its.
|
|
160
|
+
signUp: z.enum(["open", "invite", "first"]).default("open"),
|
|
161
|
+
secretVar: z.string().default("AUTH_SECRET"),
|
|
162
|
+
appUrlVar: z.string().default("APP_URL"),
|
|
163
|
+
rateLimiter: z
|
|
164
|
+
.object({
|
|
165
|
+
ip: rateLimiterSchema(RATE_LIMITER_DEFAULTS.ip),
|
|
166
|
+
email: rateLimiterSchema(RATE_LIMITER_DEFAULTS.email),
|
|
167
|
+
agent: rateLimiterSchema(RATE_LIMITER_DEFAULTS.agent),
|
|
168
|
+
})
|
|
169
|
+
.default(RATE_LIMITER_DEFAULTS),
|
|
170
|
+
})
|
|
171
|
+
.refine((options) => options.signUp === "open" || options.organization, {
|
|
172
|
+
error: 'auth: `signUp: "invite"` and `"first"` need `organization`: the invitations are its.',
|
|
173
|
+
path: ["signUp"],
|
|
174
|
+
})
|
|
175
|
+
.refine((options) => !options.mcp || options.organization, {
|
|
176
|
+
error: "auth: `mcp` needs `organization`: an agent's grant is to one organization.",
|
|
177
|
+
path: ["mcp"],
|
|
178
|
+
});
|
|
179
|
+
const CONVENTIONAL_PROVIDER_VARS = {
|
|
180
|
+
google: {
|
|
181
|
+
clientIdVar: "GOOGLE_CLIENT_ID",
|
|
182
|
+
clientSecretVar: "GOOGLE_CLIENT_SECRET",
|
|
183
|
+
},
|
|
184
|
+
apple: {
|
|
185
|
+
clientIdVar: "APPLE_CLIENT_ID",
|
|
186
|
+
clientSecretVar: "APPLE_CLIENT_SECRET",
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
function resolveProvider(cfg, conventional) {
|
|
190
|
+
if (!cfg)
|
|
191
|
+
return undefined;
|
|
192
|
+
const overrides = cfg === true ? undefined : cfg;
|
|
193
|
+
const resolved = {
|
|
194
|
+
clientIdVar: overrides?.clientIdVar ?? conventional.clientIdVar,
|
|
195
|
+
clientSecretVar: overrides?.clientSecretVar ?? conventional.clientSecretVar,
|
|
196
|
+
};
|
|
197
|
+
if (overrides?.appBundleIdentifier) {
|
|
198
|
+
resolved.appBundleIdentifier = overrides.appBundleIdentifier;
|
|
199
|
+
}
|
|
200
|
+
return resolved;
|
|
201
|
+
}
|
|
202
|
+
// Normalize `socialProviders` into env-var references: docs/architecture/plugins/auth.md, "Social providers".
|
|
203
|
+
export function resolveSocialProviders(input) {
|
|
204
|
+
const out = {};
|
|
205
|
+
if (!input)
|
|
206
|
+
return out;
|
|
207
|
+
const google = resolveProvider(input.google, CONVENTIONAL_PROVIDER_VARS.google);
|
|
208
|
+
if (google)
|
|
209
|
+
out.google = google;
|
|
210
|
+
const apple = resolveProvider(input.apple, CONVENTIONAL_PROVIDER_VARS.apple);
|
|
211
|
+
if (apple)
|
|
212
|
+
out.apple = apple;
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,6EAA6E;AAC7E,6EAA6E;AAC7E,MAAM,CAAC,MAAM,WAAW,GAAG,WAAW,CAAC;AAEvC,6HAA6H;AAC7H,MAAM,CAAC,MAAM,wBAAwB,GAAG;IACvC,yCAAyC;IACzC,uCAAuC;CAC9B,CAAC;AAEX,+EAA+E;AAC/E,4CAA4C;AAC5C,MAAM,CAAC,MAAM,gBAAgB,GAAG,kBAAkB,CAAC;AAQnD,kGAAkG;AAClG,2EAA2E;AAC3E,MAAM,qBAAqB,GAGvB;IACH,EAAE,EAAE,EAAE,OAAO,EAAE,iBAAiB,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE;IAC1D,KAAK,EAAE,EAAE,OAAO,EAAE,oBAAoB,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE;IAC9D,KAAK,EAAE,EAAE,OAAO,EAAE,oBAAoB,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE;CAChE,CAAC;AAEF,SAAS,iBAAiB,CAAC,QAA2B;IACrD,OAAO,CAAC;SACN,MAAM,CAAC;QACP,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC;QAC7C,KAAK,EAAE,CAAC;aACN,MAAM,EAAE;aACR,QAAQ,CAAC;YACT,KAAK,EAAE,mDAAmD;SAC1D,CAAC;aACD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC;QACzB,MAAM,EAAE,CAAC;aACP,MAAM,EAAE;aACR,QAAQ,CAAC;YACT,KAAK,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
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function isSafeCallbackURL(value: string, trustedOrigins: readonly string[]): boolean;
|
|
2
|
+
declare const REDIRECT_FIELDS: readonly ["callbackURL", "newUserCallbackURL", "errorCallbackURL"];
|
|
3
|
+
export type RedirectInput = Partial<Record<(typeof REDIRECT_FIELDS)[number], string | undefined>>;
|
|
4
|
+
export declare function unsafeCallbackField(input: RedirectInput, trustedOrigins: readonly string[]): (typeof REDIRECT_FIELDS)[number] | undefined;
|
|
5
|
+
export {};
|
|
6
|
+
//# sourceMappingURL=callback-url.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"callback-url.d.ts","sourceRoot":"","sources":["../../src/worker/callback-url.ts"],"names":[],"mappings":"AAyBA,wBAAgB,iBAAiB,CAChC,KAAK,EAAE,MAAM,EACb,cAAc,EAAE,SAAS,MAAM,EAAE,GAC/B,OAAO,CAcT;AAED,QAAA,MAAM,eAAe,YACpB,aAAa,EACb,oBAAoB,EACpB,kBAAkB,CACT,CAAC;AAEX,MAAM,MAAM,aAAa,GAAG,OAAO,CAClC,MAAM,CAAC,CAAC,OAAO,eAAe,CAAC,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,CAC5D,CAAC;AAIF,wBAAgB,mBAAmB,CAClC,KAAK,EAAE,aAAa,EACpB,cAAc,EAAE,SAAS,MAAM,EAAE,GAC/B,CAAC,OAAO,eAAe,CAAC,CAAC,MAAM,CAAC,GAAG,SAAS,CAK9C"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Where a sign-in link may send the member: docs/architecture/plugins/auth.md, "Procedures".
|
|
2
|
+
// A control character or backslash lets a browser read a path as another
|
|
3
|
+
// host (`/\evil.com`, a tab inside `//`), so none is accepted anywhere.
|
|
4
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: that is the point.
|
|
5
|
+
const UNSAFE = /[\u0000-\u001f\u007f\\]/;
|
|
6
|
+
function schemeOf(pattern) {
|
|
7
|
+
const match = /^([a-z][a-z0-9+.-]*):\/\/\*?$/i.exec(pattern);
|
|
8
|
+
return match ? `${match[1]?.toLowerCase()}:` : undefined;
|
|
9
|
+
}
|
|
10
|
+
function matchesOrigin(url, pattern) {
|
|
11
|
+
const scheme = schemeOf(pattern);
|
|
12
|
+
if (scheme)
|
|
13
|
+
return url.protocol === scheme;
|
|
14
|
+
if (url.protocol !== "http:" && url.protocol !== "https:")
|
|
15
|
+
return false;
|
|
16
|
+
if (!pattern.includes("*"))
|
|
17
|
+
return url.origin === pattern.toLowerCase();
|
|
18
|
+
const source = pattern
|
|
19
|
+
.toLowerCase()
|
|
20
|
+
.split("*")
|
|
21
|
+
.map((part) => part.replace(/[.+?^${}()|[\]\\]/g, "\\$&"))
|
|
22
|
+
.join("[^/@?#:]+");
|
|
23
|
+
return new RegExp(`^${source}$`).test(url.origin);
|
|
24
|
+
}
|
|
25
|
+
export function isSafeCallbackURL(value, trustedOrigins) {
|
|
26
|
+
if (UNSAFE.test(value))
|
|
27
|
+
return false;
|
|
28
|
+
if (value.startsWith("/"))
|
|
29
|
+
return !value.startsWith("//");
|
|
30
|
+
let url;
|
|
31
|
+
try {
|
|
32
|
+
url = new URL(value);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
// A userinfo (`https://app.example.com@evil.com`) is how a lookalike
|
|
38
|
+
// passes a prefix check; the origin of a parsed URL already ignores it,
|
|
39
|
+
// but a link that carries one is never one an app means to send.
|
|
40
|
+
if (url.username || url.password)
|
|
41
|
+
return false;
|
|
42
|
+
return trustedOrigins.some((pattern) => matchesOrigin(url, pattern));
|
|
43
|
+
}
|
|
44
|
+
const REDIRECT_FIELDS = [
|
|
45
|
+
"callbackURL",
|
|
46
|
+
"newUserCallbackURL",
|
|
47
|
+
"errorCallbackURL",
|
|
48
|
+
];
|
|
49
|
+
// The fields of an input that carry a redirect, by name; the first one that
|
|
50
|
+
// is not safe, so the refusal can name it.
|
|
51
|
+
export function unsafeCallbackField(input, trustedOrigins) {
|
|
52
|
+
return REDIRECT_FIELDS.find((field) => {
|
|
53
|
+
const value = input[field];
|
|
54
|
+
return value !== undefined && !isSafeCallbackURL(value, trustedOrigins);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=callback-url.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"callback-url.js","sourceRoot":"","sources":["../../src/worker/callback-url.ts"],"names":[],"mappings":"AAAA,6FAA6F;AAE7F,yEAAyE;AACzE,wEAAwE;AACxE,8EAA8E;AAC9E,MAAM,MAAM,GAAG,yBAAyB,CAAC;AAEzC,SAAS,QAAQ,CAAC,OAAe;IAChC,MAAM,KAAK,GAAG,gCAAgC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC7D,OAAO,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC;AAC1D,CAAC;AAED,SAAS,aAAa,CAAC,GAAQ,EAAE,OAAe;IAC/C,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;IACjC,IAAI,MAAM;QAAE,OAAO,GAAG,CAAC,QAAQ,KAAK,MAAM,CAAC;IAC3C,IAAI,GAAG,CAAC,QAAQ,KAAK,OAAO,IAAI,GAAG,CAAC,QAAQ,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IACxE,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,OAAO,GAAG,CAAC,MAAM,KAAK,OAAO,CAAC,WAAW,EAAE,CAAC;IACxE,MAAM,MAAM,GAAG,OAAO;SACpB,WAAW,EAAE;SACb,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,oBAAoB,EAAE,MAAM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|
1
|
+
{"version":3,"file":"cimd-transport.d.ts","sourceRoot":"","sources":["../../src/worker/cimd-transport.ts"],"names":[],"mappings":"AAwEA,wBAAsB,2BAA2B,CAChD,KAAK,EAAE,WAAW,GAAG,GAAG,EACxB,IAAI,CAAC,EAAE,WAAW,GAChB,OAAO,CAAC,QAAQ,CAAC,CAuCnB"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { isPublicRoutableHost } from "@better-auth/core/utils/host";
|
|
2
|
+
// Fetch transport `@better-auth/cimd` requires on a Worker: docs/architecture/plugins/auth.md, "MCP OAuth".
|
|
3
|
+
const DOH_ENDPOINT = "https://cloudflare-dns.com/dns-query";
|
|
4
|
+
const TYPE_A = 1;
|
|
5
|
+
const TYPE_AAAA = 28;
|
|
6
|
+
// RFC 6761 and RFC 6762 special-use names, and the names no public resolver
|
|
7
|
+
// answers: a host equal to one or below it is refused before any lookup.
|
|
8
|
+
const SPECIAL_USE_NAMES = [
|
|
9
|
+
"localhost",
|
|
10
|
+
"local",
|
|
11
|
+
"test",
|
|
12
|
+
"invalid",
|
|
13
|
+
"example",
|
|
14
|
+
"internal",
|
|
15
|
+
"onion",
|
|
16
|
+
"home.arpa",
|
|
17
|
+
];
|
|
18
|
+
// The URL parser reduces every numeric host form (decimal, octal, hex) to
|
|
19
|
+
// dotted decimal, and an IPv6 literal keeps its brackets.
|
|
20
|
+
function isIpLiteral(hostname) {
|
|
21
|
+
return hostname.startsWith("[") || /^\d{1,3}(\.\d{1,3}){3}$/.test(hostname);
|
|
22
|
+
}
|
|
23
|
+
function isSpecialUseName(hostname) {
|
|
24
|
+
const name = hostname.replace(/\.$/, "");
|
|
25
|
+
return SPECIAL_USE_NAMES.some((special) => name === special || name.endsWith(`.${special}`));
|
|
26
|
+
}
|
|
27
|
+
// The A or AAAA addresses one DoH query answers, refusing a failed request and
|
|
28
|
+
// any status but NOERROR. Records of other types (a CNAME chain's links) are
|
|
29
|
+
// not judged: only the addresses the chain ends in are.
|
|
30
|
+
async function resolveAddresses(hostname, type, signal) {
|
|
31
|
+
const url = new URL(DOH_ENDPOINT);
|
|
32
|
+
url.searchParams.set("name", hostname);
|
|
33
|
+
url.searchParams.set("type", type === TYPE_A ? "A" : "AAAA");
|
|
34
|
+
const response = await globalThis.fetch(url, {
|
|
35
|
+
headers: { accept: "application/dns-json" },
|
|
36
|
+
signal,
|
|
37
|
+
});
|
|
38
|
+
if (!response.ok) {
|
|
39
|
+
throw new TypeError(`DNS lookup of the metadata host failed (${type})`);
|
|
40
|
+
}
|
|
41
|
+
// The DoH JSON answer is read as the shape `DohAnswer` names; a field of
|
|
42
|
+
// another type fails the `typeof` checks below.
|
|
43
|
+
const body = (await response.json());
|
|
44
|
+
if (body.Status !== 0) {
|
|
45
|
+
throw new TypeError(`DNS lookup of the metadata host answered status ${body.Status}`);
|
|
46
|
+
}
|
|
47
|
+
return (body.Answer ?? []).flatMap((record) => record.type === type && typeof record.data === "string"
|
|
48
|
+
? [record.data]
|
|
49
|
+
: []);
|
|
50
|
+
}
|
|
51
|
+
export async function fetchClientMetadataResource(input, init) {
|
|
52
|
+
const request = new Request(input, init);
|
|
53
|
+
const url = new URL(request.url);
|
|
54
|
+
if (url.protocol !== "https:") {
|
|
55
|
+
throw new TypeError("CIMD transport requires an HTTPS URL");
|
|
56
|
+
}
|
|
57
|
+
if (request.method !== "GET" && request.method !== "HEAD") {
|
|
58
|
+
throw new TypeError("CIMD transport supports only GET and HEAD");
|
|
59
|
+
}
|
|
60
|
+
if (isIpLiteral(url.hostname)) {
|
|
61
|
+
throw new TypeError("CIMD transport refuses an IP literal host");
|
|
62
|
+
}
|
|
63
|
+
if (isSpecialUseName(url.hostname)) {
|
|
64
|
+
throw new TypeError("CIMD transport refuses a special-use host name");
|
|
65
|
+
}
|
|
66
|
+
const signal = init?.signal ?? (input instanceof Request ? input.signal : undefined);
|
|
67
|
+
const [v4, v6] = await Promise.all([
|
|
68
|
+
resolveAddresses(url.hostname, TYPE_A, signal),
|
|
69
|
+
resolveAddresses(url.hostname, TYPE_AAAA, signal),
|
|
70
|
+
]);
|
|
71
|
+
const addresses = [...v4, ...v6];
|
|
72
|
+
if (addresses.length === 0) {
|
|
73
|
+
throw new TypeError("metadata hostname returned no DNS addresses");
|
|
74
|
+
}
|
|
75
|
+
if (!addresses.every((address) => isPublicRoutableHost(address))) {
|
|
76
|
+
throw new TypeError("metadata hostname must resolve only to public-routable addresses");
|
|
77
|
+
}
|
|
78
|
+
// A redirect comes back as the 302 it is: the caller refuses it.
|
|
79
|
+
return globalThis.fetch(url, {
|
|
80
|
+
method: request.method,
|
|
81
|
+
headers: request.headers,
|
|
82
|
+
redirect: "manual",
|
|
83
|
+
signal,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=cimd-transport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cimd-transport.js","sourceRoot":"","sources":["../../src/worker/cimd-transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAEpE,4GAA4G;AAE5G,MAAM,YAAY,GAAG,sCAAsC,CAAC;AAC5D,MAAM,MAAM,GAAG,CAAC,CAAC;AACjB,MAAM,SAAS,GAAG,EAAE,CAAC;AAErB,4EAA4E;AAC5E,yEAAyE;AACzE,MAAM,iBAAiB,GAAG;IACzB,WAAW;IACX,OAAO;IACP,MAAM;IACN,SAAS;IACT,SAAS;IACT,UAAU;IACV,OAAO;IACP,WAAW;CACX,CAAC;AAEF,0EAA0E;AAC1E,0DAA0D;AAC1D,SAAS,WAAW,CAAC,QAAgB;IACpC,OAAO,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,yBAAyB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AAC7E,CAAC;AAED,SAAS,gBAAgB,CAAC,QAAgB;IACzC,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC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CAAC,GAAG,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;IACjC,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC5B,MAAM,IAAI,SAAS,CAAC,6CAA6C,CAAC,CAAC;IACpE,CAAC;IACD,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC;QAClE,MAAM,IAAI,SAAS,CAClB,kEAAkE,CAClE,CAAC;IACH,CAAC;IAED,iEAAiE;IACjE,OAAO,UAAU,CAAC,KAAK,CAAC,GAAG,EAAE;QAC5B,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,QAAQ,EAAE,QAAQ;QAClB,MAAM;KACN,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { BetterAuthPlugin } from "better-auth";
|
|
2
|
+
export declare const DEVICE_NAME_MAX = 40;
|
|
3
|
+
export declare const DEVICE_EXPIRES_MAX_DAYS = 365;
|
|
4
|
+
export declare function deviceCredential(headers: Headers | undefined | null): string | null;
|
|
5
|
+
export declare function devicePlugins(): BetterAuthPlugin[];
|
|
6
|
+
//# sourceMappingURL=devices.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"devices.d.ts","sourceRoot":"","sources":["../../src/worker/devices.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAYpD,eAAO,MAAM,eAAe,KAAK,CAAC;AAIlC,eAAO,MAAM,uBAAuB,MAAM,CAAC;AAM3C,wBAAgB,gBAAgB,CAC/B,OAAO,EAAE,OAAO,GAAG,SAAS,GAAG,IAAI,GACjC,MAAM,GAAG,IAAI,CAGf;AAkCD,wBAAgB,aAAa,IAAI,gBAAgB,EAAE,CAoBlD"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { apiKey } from "@better-auth/api-key";
|
|
2
|
+
import { APIError, createAuthMiddleware } from "better-auth/api";
|
|
3
|
+
import { DEVICE_KEY_LENGTH, DEVICE_KEY_PREFIX } from "./outputs.js";
|
|
4
|
+
// Device credentials: docs/architecture/plugins/auth.md, "Devices".
|
|
5
|
+
// Calls one key may make in a window, enforced in the database row, so it
|
|
6
|
+
// holds on Node as on Cloudflare. Pinned, not options: a daemon that batches
|
|
7
|
+
// stays far under it, and one that loops is the case it exists for.
|
|
8
|
+
const RATE_LIMIT = { timeWindow: 60_000, maxRequests: 600 };
|
|
9
|
+
// Longest name a member can give a device.
|
|
10
|
+
export const DEVICE_NAME_MAX = 40;
|
|
11
|
+
// Furthest a key's expiry may be set, in days; a key made without one does
|
|
12
|
+
// not expire.
|
|
13
|
+
export const DEVICE_EXPIRES_MAX_DAYS = 365;
|
|
14
|
+
const BEARER = new RegExp(`^bearer\\s+(${DEVICE_KEY_PREFIX}\\S+)$`, "i");
|
|
15
|
+
// The device key the request carries, or null. Anything else in the header is
|
|
16
|
+
// not ours and is left to the plugins that read it.
|
|
17
|
+
export function deviceCredential(headers) {
|
|
18
|
+
const value = headers?.get("authorization")?.trim();
|
|
19
|
+
return (value && BEARER.exec(value)?.[1]) || null;
|
|
20
|
+
}
|
|
21
|
+
// Auth endpoints a device key may reach: docs/architecture/plugins/auth.md, "Devices".
|
|
22
|
+
const DEVICE_ENDPOINTS = new Set([
|
|
23
|
+
"/get-session",
|
|
24
|
+
"/organization/list",
|
|
25
|
+
"/organization/get-full-organization",
|
|
26
|
+
"/organization/get-active-member",
|
|
27
|
+
"/organization/list-members",
|
|
28
|
+
"/organization/has-permission",
|
|
29
|
+
]);
|
|
30
|
+
const DEVICE_REFUSED = "DEVICE_CREDENTIAL_REFUSED";
|
|
31
|
+
// Registered before the key plugin, so it refuses before a key is spent.
|
|
32
|
+
const guard = {
|
|
33
|
+
id: "device-guard",
|
|
34
|
+
hooks: {
|
|
35
|
+
before: [
|
|
36
|
+
{
|
|
37
|
+
matcher: (ctx) => deviceCredential(ctx.headers) !== null &&
|
|
38
|
+
!DEVICE_ENDPOINTS.has(ctx.path ?? ""),
|
|
39
|
+
handler: createAuthMiddleware(async () => {
|
|
40
|
+
throw new APIError("FORBIDDEN", {
|
|
41
|
+
code: DEVICE_REFUSED,
|
|
42
|
+
message: "A device credential cannot call this endpoint.",
|
|
43
|
+
});
|
|
44
|
+
}),
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
export function devicePlugins() {
|
|
50
|
+
return [
|
|
51
|
+
guard,
|
|
52
|
+
apiKey({
|
|
53
|
+
defaultPrefix: DEVICE_KEY_PREFIX,
|
|
54
|
+
defaultKeyLength: DEVICE_KEY_LENGTH,
|
|
55
|
+
requireName: true,
|
|
56
|
+
maximumNameLength: DEVICE_NAME_MAX,
|
|
57
|
+
enableMetadata: false,
|
|
58
|
+
// The key stands in for the session on `get-session`.
|
|
59
|
+
enableSessionForAPIKeys: true,
|
|
60
|
+
customAPIKeyGetter: (ctx) => deviceCredential(ctx.headers),
|
|
61
|
+
rateLimit: { enabled: true, ...RATE_LIMIT },
|
|
62
|
+
keyExpiration: {
|
|
63
|
+
defaultExpiresIn: null,
|
|
64
|
+
minExpiresIn: 1,
|
|
65
|
+
maxExpiresIn: DEVICE_EXPIRES_MAX_DAYS,
|
|
66
|
+
},
|
|
67
|
+
}),
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=devices.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"devices.js","sourceRoot":"","sources":["../../src/worker/devices.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAE9C,OAAO,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AACjE,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE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