@pithy-sh/auth 0.1.2 → 0.1.3
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/admin/users.d.ts +155 -0
- package/dist/admin/users.d.ts.map +1 -0
- package/dist/admin/users.js +205 -0
- package/dist/audit/actions.d.ts +66 -0
- package/dist/audit/actions.d.ts.map +1 -0
- package/dist/audit/actions.js +66 -0
- package/dist/audit/emit.d.ts +101 -0
- package/dist/audit/emit.d.ts.map +1 -0
- package/dist/audit/emit.js +170 -0
- package/dist/capability.d.ts +60 -0
- package/dist/capability.d.ts.map +1 -0
- package/dist/capability.js +169 -0
- package/dist/client/api.d.ts +299 -0
- package/dist/client/api.d.ts.map +1 -0
- package/dist/client/api.js +385 -0
- package/dist/client/projection.d.ts +51 -0
- package/dist/client/projection.d.ts.map +1 -0
- package/dist/client/projection.js +0 -0
- package/dist/data/betterAuth.d.ts +75 -0
- package/dist/data/betterAuth.d.ts.map +1 -0
- package/dist/data/betterAuth.js +92 -0
- package/dist/data/device.d.ts +36 -0
- package/dist/data/device.d.ts.map +1 -0
- package/dist/data/device.js +33 -0
- package/dist/data/kitFields.d.ts +78 -0
- package/dist/data/kitFields.d.ts.map +1 -0
- package/dist/data/kitFields.js +77 -0
- package/dist/data/rotatedToken.d.ts +20 -0
- package/dist/data/rotatedToken.d.ts.map +1 -0
- package/dist/data/rotatedToken.js +22 -0
- package/dist/data/tables.d.ts +104 -0
- package/dist/data/tables.d.ts.map +1 -0
- package/dist/data/tables.js +30 -0
- package/dist/device/registry.d.ts +54 -0
- package/dist/device/registry.d.ts.map +1 -0
- package/dist/device/registry.js +85 -0
- package/dist/email/send.d.ts +38 -0
- package/dist/email/send.d.ts.map +1 -0
- package/dist/email/send.js +48 -0
- package/dist/http/adminRoutes.d.ts +9 -0
- package/dist/http/adminRoutes.d.ts.map +1 -0
- package/dist/http/adminRoutes.js +274 -0
- package/dist/http/baseUrl.d.ts +41 -0
- package/dist/http/baseUrl.d.ts.map +1 -0
- package/dist/http/baseUrl.js +96 -0
- package/dist/http/csrf.d.ts +18 -0
- package/dist/http/csrf.d.ts.map +1 -0
- package/dist/http/csrf.js +81 -0
- package/dist/http/devLoginRoute.d.ts +42 -0
- package/dist/http/devLoginRoute.d.ts.map +1 -0
- package/dist/http/devLoginRoute.js +112 -0
- package/dist/http/errors.d.ts +24 -0
- package/dist/http/errors.d.ts.map +1 -0
- package/dist/http/errors.js +67 -0
- package/dist/http/guards.d.ts +103 -0
- package/dist/http/guards.d.ts.map +1 -0
- package/dist/http/guards.js +148 -0
- package/dist/http/middleware.d.ts +17 -0
- package/dist/http/middleware.d.ts.map +1 -0
- package/dist/http/middleware.js +47 -0
- package/dist/http/rateLimit.d.ts +17 -0
- package/dist/http/rateLimit.d.ts.map +1 -0
- package/dist/http/rateLimit.js +31 -0
- package/dist/http/resolve.d.ts +9 -0
- package/dist/http/resolve.d.ts.map +1 -0
- package/dist/http/resolve.js +92 -0
- package/dist/http/responses.d.ts +152 -0
- package/dist/http/responses.d.ts.map +1 -0
- package/dist/http/responses.js +121 -0
- package/dist/http/routes.d.ts +34 -0
- package/dist/http/routes.d.ts.map +1 -0
- package/dist/http/routes.js +268 -0
- package/dist/http/schemas.d.ts +66 -0
- package/dist/http/schemas.d.ts.map +1 -0
- package/dist/http/schemas.js +59 -0
- package/dist/http/views.d.ts +53 -0
- package/dist/http/views.d.ts.map +1 -0
- package/dist/http/views.js +86 -0
- package/dist/i18n/errorCopy.d.ts +24 -0
- package/dist/i18n/errorCopy.d.ts.map +1 -0
- package/dist/i18n/errorCopy.es.d.ts +31 -0
- package/dist/i18n/errorCopy.es.d.ts.map +1 -0
- package/dist/i18n/errorCopy.es.js +33 -0
- package/dist/i18n/errorCopy.js +91 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/instance/auth.d.ts +1122 -0
- package/dist/instance/auth.d.ts.map +1 -0
- package/dist/instance/auth.js +176 -0
- package/dist/instance/plugins.d.ts +87 -0
- package/dist/instance/plugins.d.ts.map +1 -0
- package/dist/instance/plugins.js +116 -0
- package/dist/instance/providers.d.ts +117 -0
- package/dist/instance/providers.d.ts.map +1 -0
- package/dist/instance/providers.js +111 -0
- package/dist/instance/secrets.d.ts +163 -0
- package/dist/instance/secrets.d.ts.map +1 -0
- package/dist/instance/secrets.js +169 -0
- package/dist/migrations/0001_init.d.ts +33 -0
- package/dist/migrations/0001_init.d.ts.map +1 -0
- package/dist/migrations/0001_init.js +77 -0
- package/dist/migrations/pluginTables.d.ts +126 -0
- package/dist/migrations/pluginTables.d.ts.map +1 -0
- package/dist/migrations/pluginTables.js +233 -0
- package/dist/seeds/devSession.d.ts +124 -0
- package/dist/seeds/devSession.d.ts.map +1 -0
- package/dist/seeds/devSession.js +211 -0
- package/dist/seeds/example.d.ts +12 -0
- package/dist/seeds/example.d.ts.map +1 -0
- package/dist/seeds/example.js +30 -0
- package/dist/test-utils/liveApp.d.ts +227 -0
- package/dist/test-utils/liveApp.d.ts.map +1 -0
- package/dist/test-utils/liveApp.js +240 -0
- package/dist/token/rotation.d.ts +56 -0
- package/dist/token/rotation.d.ts.map +1 -0
- package/dist/token/rotation.js +68 -0
- package/dist/version.generated.d.ts +5 -0
- package/dist/version.generated.d.ts.map +1 -0
- package/dist/version.generated.js +7 -0
- package/package.json +24 -13
- package/src/version.generated.ts +1 -1
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type { SecretsStoreEnv } from "@pithy-sh/secrets/src/env/bindings";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
/**
|
|
4
|
+
* The secrets `@pithy-sh/auth` reads, declared as a minimal registry. The secret name is the join key:
|
|
5
|
+
* `secretsStore` resolves the same encrypted D1 row regardless of which capability's registry names it,
|
|
6
|
+
* so auth never needs the project-wide registry to read its own secrets.
|
|
7
|
+
*
|
|
8
|
+
* - `auth-session-secret` — the Better Auth signing/encryption secret. Signs session cookies and the
|
|
9
|
+
* bearer HMAC, and encrypts the JWKS private keys at rest. Rotatable (Better Auth accepts a version
|
|
10
|
+
* set so a rotation keeps prior signatures valid).
|
|
11
|
+
* - `auth-google-credentials` — the Google OAuth client id and secret together, as a typed JSON value.
|
|
12
|
+
* Both travel as one atomic secret so the credential pair is never split across config and store.
|
|
13
|
+
* Read only when the provider is enabled; rotated in Google Cloud Console, not on our schedule.
|
|
14
|
+
* - `auth-apple-credentials` — the Apple Sign-In Services id, client secret, and (optional) app bundle
|
|
15
|
+
* id, as a typed JSON value. Apple's client secret is an ES256 JWT minted from the `.p8` key that
|
|
16
|
+
* expires (max 6 months), so this one is rotatable.
|
|
17
|
+
* - `auth-facebook-credentials` — the Facebook Login app id and client secret, as a typed JSON value.
|
|
18
|
+
* Read only when the provider is enabled; rotated in the Meta app dashboard, not on our schedule.
|
|
19
|
+
* - `auth-github-credentials` — the GitHub OAuth app client id and secret, as a typed JSON value.
|
|
20
|
+
* Read only when the provider is enabled; rotated in GitHub Developer settings, not on our schedule.
|
|
21
|
+
*/
|
|
22
|
+
export declare const AUTH_SESSION_SECRET = "auth-session-secret";
|
|
23
|
+
export declare const AUTH_GOOGLE_CREDENTIALS = "auth-google-credentials";
|
|
24
|
+
export declare const AUTH_APPLE_CREDENTIALS = "auth-apple-credentials";
|
|
25
|
+
export declare const AUTH_FACEBOOK_CREDENTIALS = "auth-facebook-credentials";
|
|
26
|
+
export declare const AUTH_GITHUB_CREDENTIALS = "auth-github-credentials";
|
|
27
|
+
/** The Google OAuth credential pair, stored as one typed JSON secret. */
|
|
28
|
+
export declare const GoogleOAuthCredentials: z.ZodObject<{
|
|
29
|
+
clientId: z.ZodString;
|
|
30
|
+
clientSecret: z.ZodString;
|
|
31
|
+
}, z.core.$strip>;
|
|
32
|
+
export type GoogleOAuthCredentials = z.infer<typeof GoogleOAuthCredentials>;
|
|
33
|
+
/** The Apple Sign-In credentials, stored as one typed JSON secret. */
|
|
34
|
+
export declare const AppleOAuthCredentials: z.ZodObject<{
|
|
35
|
+
clientId: z.ZodString;
|
|
36
|
+
clientSecret: z.ZodString;
|
|
37
|
+
appBundleIdentifier: z.ZodOptional<z.ZodString>;
|
|
38
|
+
}, z.core.$strip>;
|
|
39
|
+
export type AppleOAuthCredentials = z.infer<typeof AppleOAuthCredentials>;
|
|
40
|
+
/** The Facebook Login credential pair, stored as one typed JSON secret. */
|
|
41
|
+
export declare const FacebookOAuthCredentials: z.ZodObject<{
|
|
42
|
+
clientId: z.ZodString;
|
|
43
|
+
clientSecret: z.ZodString;
|
|
44
|
+
}, z.core.$strip>;
|
|
45
|
+
export type FacebookOAuthCredentials = z.infer<typeof FacebookOAuthCredentials>;
|
|
46
|
+
/** The GitHub OAuth credential pair, stored as one typed JSON secret. */
|
|
47
|
+
export declare const GithubOAuthCredentials: z.ZodObject<{
|
|
48
|
+
clientId: z.ZodString;
|
|
49
|
+
clientSecret: z.ZodString;
|
|
50
|
+
}, z.core.$strip>;
|
|
51
|
+
export type GithubOAuthCredentials = z.infer<typeof GithubOAuthCredentials>;
|
|
52
|
+
export declare const authSecretsRegistry: {
|
|
53
|
+
readonly "auth-session-secret": {
|
|
54
|
+
readonly backend: "d1";
|
|
55
|
+
readonly scope: "environment";
|
|
56
|
+
readonly rotatable: true;
|
|
57
|
+
readonly valueType: "text";
|
|
58
|
+
readonly devValue: "random";
|
|
59
|
+
readonly origin: {
|
|
60
|
+
readonly kind: "minted";
|
|
61
|
+
readonly recipe: {
|
|
62
|
+
readonly kind: "random";
|
|
63
|
+
readonly bytes: 32;
|
|
64
|
+
readonly encoding: "base64url";
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
readonly rotation: {
|
|
68
|
+
readonly kind: "local";
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
readonly "auth-google-credentials": {
|
|
72
|
+
readonly backend: "d1";
|
|
73
|
+
readonly scope: "environment";
|
|
74
|
+
readonly rotatable: false;
|
|
75
|
+
readonly valueType: "json";
|
|
76
|
+
readonly schema: z.ZodObject<{
|
|
77
|
+
clientId: z.ZodString;
|
|
78
|
+
clientSecret: z.ZodString;
|
|
79
|
+
}, z.core.$strip>;
|
|
80
|
+
readonly origin: {
|
|
81
|
+
readonly kind: "obtained";
|
|
82
|
+
readonly issuer: "google";
|
|
83
|
+
readonly documentation: "https://console.cloud.google.com/apis/credentials";
|
|
84
|
+
};
|
|
85
|
+
readonly rotation: {
|
|
86
|
+
readonly kind: "manual";
|
|
87
|
+
readonly issuer: "google";
|
|
88
|
+
readonly documentation: "https://console.cloud.google.com/apis/credentials";
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
readonly "auth-apple-credentials": {
|
|
92
|
+
readonly backend: "d1";
|
|
93
|
+
readonly scope: "environment";
|
|
94
|
+
readonly rotatable: true;
|
|
95
|
+
readonly valueType: "json";
|
|
96
|
+
readonly schema: z.ZodObject<{
|
|
97
|
+
clientId: z.ZodString;
|
|
98
|
+
clientSecret: z.ZodString;
|
|
99
|
+
appBundleIdentifier: z.ZodOptional<z.ZodString>;
|
|
100
|
+
}, z.core.$strip>;
|
|
101
|
+
readonly origin: {
|
|
102
|
+
readonly kind: "obtained";
|
|
103
|
+
readonly issuer: "apple";
|
|
104
|
+
readonly documentation: "https://developer.apple.com/account/resources/authkeys/list";
|
|
105
|
+
};
|
|
106
|
+
readonly rotation: {
|
|
107
|
+
readonly kind: "manual";
|
|
108
|
+
readonly issuer: "apple";
|
|
109
|
+
readonly documentation: "https://developer.apple.com/account/resources/authkeys/list";
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
readonly "auth-facebook-credentials": {
|
|
113
|
+
readonly backend: "d1";
|
|
114
|
+
readonly scope: "environment";
|
|
115
|
+
readonly rotatable: false;
|
|
116
|
+
readonly valueType: "json";
|
|
117
|
+
readonly schema: z.ZodObject<{
|
|
118
|
+
clientId: z.ZodString;
|
|
119
|
+
clientSecret: z.ZodString;
|
|
120
|
+
}, z.core.$strip>;
|
|
121
|
+
readonly origin: {
|
|
122
|
+
readonly kind: "obtained";
|
|
123
|
+
readonly issuer: "facebook";
|
|
124
|
+
readonly documentation: "https://developers.facebook.com/apps/";
|
|
125
|
+
};
|
|
126
|
+
readonly rotation: {
|
|
127
|
+
readonly kind: "manual";
|
|
128
|
+
readonly issuer: "facebook";
|
|
129
|
+
readonly documentation: "https://developers.facebook.com/apps/";
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
readonly "auth-github-credentials": {
|
|
133
|
+
readonly backend: "d1";
|
|
134
|
+
readonly scope: "environment";
|
|
135
|
+
readonly rotatable: false;
|
|
136
|
+
readonly valueType: "json";
|
|
137
|
+
readonly schema: z.ZodObject<{
|
|
138
|
+
clientId: z.ZodString;
|
|
139
|
+
clientSecret: z.ZodString;
|
|
140
|
+
}, z.core.$strip>;
|
|
141
|
+
readonly origin: {
|
|
142
|
+
readonly kind: "obtained";
|
|
143
|
+
readonly issuer: "github";
|
|
144
|
+
readonly documentation: "https://github.com/settings/developers";
|
|
145
|
+
};
|
|
146
|
+
readonly rotation: {
|
|
147
|
+
readonly kind: "manual";
|
|
148
|
+
readonly issuer: "github";
|
|
149
|
+
readonly documentation: "https://github.com/settings/developers";
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
/** Resolve the Better Auth session secret for this invocation. */
|
|
154
|
+
export declare function resolveSessionSecret(env: SecretsStoreEnv): Promise<string>;
|
|
155
|
+
/** Resolve the Google OAuth credential pair for this invocation (only call when Google is enabled). */
|
|
156
|
+
export declare function resolveGoogleCredentials(env: SecretsStoreEnv): Promise<GoogleOAuthCredentials>;
|
|
157
|
+
/** Resolve the Apple Sign-In credentials for this invocation (only call when Apple is enabled). */
|
|
158
|
+
export declare function resolveAppleCredentials(env: SecretsStoreEnv): Promise<AppleOAuthCredentials>;
|
|
159
|
+
/** Resolve the Facebook Login credential pair for this invocation (only call when Facebook is enabled). */
|
|
160
|
+
export declare function resolveFacebookCredentials(env: SecretsStoreEnv): Promise<FacebookOAuthCredentials>;
|
|
161
|
+
/** Resolve the GitHub OAuth credential pair for this invocation (only call when GitHub is enabled). */
|
|
162
|
+
export declare function resolveGithubCredentials(env: SecretsStoreEnv): Promise<GithubOAuthCredentials>;
|
|
163
|
+
//# sourceMappingURL=secrets.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secrets.d.ts","sourceRoot":"","sources":["../../src/instance/secrets.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AAG1E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,mBAAmB,wBAAwB,CAAC;AACzD,eAAO,MAAM,uBAAuB,4BAA4B,CAAC;AACjE,eAAO,MAAM,sBAAsB,2BAA2B,CAAC;AAC/D,eAAO,MAAM,yBAAyB,8BAA8B,CAAC;AACrE,eAAO,MAAM,uBAAuB,4BAA4B,CAAC;AAEjE,yEAAyE;AACzE,eAAO,MAAM,sBAAsB;;;iBAWoE,CAAC;AACxG,MAAM,MAAM,sBAAsB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAE5E,sEAAsE;AACtE,eAAO,MAAM,qBAAqB;;;;iBAiB/B,CAAC;AACJ,MAAM,MAAM,qBAAqB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAE1E,2EAA2E;AAC3E,eAAO,MAAM,wBAAwB;;;iBAW6D,CAAC;AACnG,MAAM,MAAM,wBAAwB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,wBAAwB,CAAC,CAAC;AAEhF,yEAAyE;AACzE,eAAO,MAAM,sBAAsB;;;iBAW6D,CAAC;AACjG,MAAM,MAAM,sBAAsB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAe5E,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsD9B,CAAC;AAEH,kEAAkE;AAClE,wBAAsB,oBAAoB,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAGhF;AAED,uGAAuG;AACvG,wBAAsB,wBAAwB,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAGpG;AAED,mGAAmG;AACnG,wBAAsB,uBAAuB,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAGlG;AAED,2GAA2G;AAC3G,wBAAsB,0BAA0B,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,wBAAwB,CAAC,CAGxG;AAED,uGAAuG;AACvG,wBAAsB,wBAAwB,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAGpG"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { defineSecretRegistry } from "@pithy-sh/secrets/src/registry";
|
|
3
|
+
import { sharedSecretsStore } from "@pithy-sh/secrets/src/sharedSecretsStore";
|
|
4
|
+
//#region src/instance/secrets.ts
|
|
5
|
+
/**
|
|
6
|
+
* The secrets `@pithy-sh/auth` reads, declared as a minimal registry. The secret name is the join key:
|
|
7
|
+
* `secretsStore` resolves the same encrypted D1 row regardless of which capability's registry names it,
|
|
8
|
+
* so auth never needs the project-wide registry to read its own secrets.
|
|
9
|
+
*
|
|
10
|
+
* - `auth-session-secret` — the Better Auth signing/encryption secret. Signs session cookies and the
|
|
11
|
+
* bearer HMAC, and encrypts the JWKS private keys at rest. Rotatable (Better Auth accepts a version
|
|
12
|
+
* set so a rotation keeps prior signatures valid).
|
|
13
|
+
* - `auth-google-credentials` — the Google OAuth client id and secret together, as a typed JSON value.
|
|
14
|
+
* Both travel as one atomic secret so the credential pair is never split across config and store.
|
|
15
|
+
* Read only when the provider is enabled; rotated in Google Cloud Console, not on our schedule.
|
|
16
|
+
* - `auth-apple-credentials` — the Apple Sign-In Services id, client secret, and (optional) app bundle
|
|
17
|
+
* id, as a typed JSON value. Apple's client secret is an ES256 JWT minted from the `.p8` key that
|
|
18
|
+
* expires (max 6 months), so this one is rotatable.
|
|
19
|
+
* - `auth-facebook-credentials` — the Facebook Login app id and client secret, as a typed JSON value.
|
|
20
|
+
* Read only when the provider is enabled; rotated in the Meta app dashboard, not on our schedule.
|
|
21
|
+
* - `auth-github-credentials` — the GitHub OAuth app client id and secret, as a typed JSON value.
|
|
22
|
+
* Read only when the provider is enabled; rotated in GitHub Developer settings, not on our schedule.
|
|
23
|
+
*/
|
|
24
|
+
const AUTH_SESSION_SECRET = "auth-session-secret";
|
|
25
|
+
const AUTH_GOOGLE_CREDENTIALS = "auth-google-credentials";
|
|
26
|
+
const AUTH_APPLE_CREDENTIALS = "auth-apple-credentials";
|
|
27
|
+
const AUTH_FACEBOOK_CREDENTIALS = "auth-facebook-credentials";
|
|
28
|
+
const AUTH_GITHUB_CREDENTIALS = "auth-github-credentials";
|
|
29
|
+
/** The Google OAuth credential pair, stored as one typed JSON secret. */
|
|
30
|
+
const GoogleOAuthCredentials = z.object({
|
|
31
|
+
clientId: z.string().describe("The Google OAuth client id for this environment. Not secret on its own, but stored with the secret so the pair is atomic."),
|
|
32
|
+
clientSecret: z.string().describe("The Google OAuth client secret for this environment. Never committed, never an env literal.")
|
|
33
|
+
}).describe("A Google OAuth client credential pair (`clientId` + `clientSecret`) for one environment.");
|
|
34
|
+
/** The Apple Sign-In credentials, stored as one typed JSON secret. */
|
|
35
|
+
const AppleOAuthCredentials = z.object({
|
|
36
|
+
clientId: z.string().describe("The Apple Sign-In Services id (the web OAuth client id) for this environment."),
|
|
37
|
+
clientSecret: z.string().describe("The Apple client secret — an ES256 JWT minted from the `.p8` key. Expires (max 6 months), so rotate it on schedule."),
|
|
38
|
+
appBundleIdentifier: z.string().optional().describe("The native iOS app bundle id, used as the id-token audience for the native Sign-in-with-Apple flow. Optional.")
|
|
39
|
+
}).describe("An Apple Sign-In credential set (`clientId` + `clientSecret`, optional `appBundleIdentifier`) for one environment.");
|
|
40
|
+
/** The Facebook Login credential pair, stored as one typed JSON secret. */
|
|
41
|
+
const FacebookOAuthCredentials = z.object({
|
|
42
|
+
clientId: z.string().describe("The Facebook Login app id for this environment. Not secret on its own, but stored with the secret so the pair is atomic."),
|
|
43
|
+
clientSecret: z.string().describe("The Facebook Login app secret for this environment. Never committed, never an env literal.")
|
|
44
|
+
}).describe("A Facebook Login credential pair (`clientId` + `clientSecret`) for one environment.");
|
|
45
|
+
/** The GitHub OAuth credential pair, stored as one typed JSON secret. */
|
|
46
|
+
const GithubOAuthCredentials = z.object({
|
|
47
|
+
clientId: z.string().describe("The GitHub OAuth app client id for this environment. Not secret on its own, but stored with the secret so the pair is atomic."),
|
|
48
|
+
clientSecret: z.string().describe("The GitHub OAuth app client secret for this environment. Never committed, never an env literal.")
|
|
49
|
+
}).describe("A GitHub OAuth credential pair (`clientId` + `clientSecret`) for one environment.");
|
|
50
|
+
/**
|
|
51
|
+
* Where a human goes for each OAuth credential pair, and where the same human rotates it.
|
|
52
|
+
*
|
|
53
|
+
* **The settings page, not the product homepage.** The point is to end a search, not to start one — and
|
|
54
|
+
* these four are the case with no command and no possible one, so the link is the entire remedy the kit
|
|
55
|
+
* can offer. Named constants because origin and rotation are the same page for all four, and two copies
|
|
56
|
+
* of a URL is one that goes stale.
|
|
57
|
+
*/
|
|
58
|
+
const GOOGLE_CREDENTIALS_PAGE = "https://console.cloud.google.com/apis/credentials";
|
|
59
|
+
const APPLE_KEYS_PAGE = "https://developer.apple.com/account/resources/authkeys/list";
|
|
60
|
+
const FACEBOOK_APPS_PAGE = "https://developers.facebook.com/apps/";
|
|
61
|
+
const GITHUB_OAUTH_APPS_PAGE = "https://github.com/settings/developers";
|
|
62
|
+
const authSecretsRegistry = defineSecretRegistry({
|
|
63
|
+
[AUTH_SESSION_SECRET]: {
|
|
64
|
+
backend: "d1",
|
|
65
|
+
scope: "environment",
|
|
66
|
+
rotatable: true,
|
|
67
|
+
valueType: "text",
|
|
68
|
+
devValue: "random",
|
|
69
|
+
origin: {
|
|
70
|
+
kind: "minted",
|
|
71
|
+
recipe: {
|
|
72
|
+
kind: "random",
|
|
73
|
+
bytes: 32,
|
|
74
|
+
encoding: "base64url"
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
rotation: { kind: "local" }
|
|
78
|
+
},
|
|
79
|
+
[AUTH_GOOGLE_CREDENTIALS]: {
|
|
80
|
+
backend: "d1",
|
|
81
|
+
scope: "environment",
|
|
82
|
+
rotatable: false,
|
|
83
|
+
valueType: "json",
|
|
84
|
+
schema: GoogleOAuthCredentials,
|
|
85
|
+
origin: {
|
|
86
|
+
kind: "obtained",
|
|
87
|
+
issuer: "google",
|
|
88
|
+
documentation: GOOGLE_CREDENTIALS_PAGE
|
|
89
|
+
},
|
|
90
|
+
rotation: {
|
|
91
|
+
kind: "manual",
|
|
92
|
+
issuer: "google",
|
|
93
|
+
documentation: GOOGLE_CREDENTIALS_PAGE
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
[AUTH_APPLE_CREDENTIALS]: {
|
|
97
|
+
backend: "d1",
|
|
98
|
+
scope: "environment",
|
|
99
|
+
rotatable: true,
|
|
100
|
+
valueType: "json",
|
|
101
|
+
schema: AppleOAuthCredentials,
|
|
102
|
+
origin: {
|
|
103
|
+
kind: "obtained",
|
|
104
|
+
issuer: "apple",
|
|
105
|
+
documentation: APPLE_KEYS_PAGE
|
|
106
|
+
},
|
|
107
|
+
rotation: {
|
|
108
|
+
kind: "manual",
|
|
109
|
+
issuer: "apple",
|
|
110
|
+
documentation: APPLE_KEYS_PAGE
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
[AUTH_FACEBOOK_CREDENTIALS]: {
|
|
114
|
+
backend: "d1",
|
|
115
|
+
scope: "environment",
|
|
116
|
+
rotatable: false,
|
|
117
|
+
valueType: "json",
|
|
118
|
+
schema: FacebookOAuthCredentials,
|
|
119
|
+
origin: {
|
|
120
|
+
kind: "obtained",
|
|
121
|
+
issuer: "facebook",
|
|
122
|
+
documentation: FACEBOOK_APPS_PAGE
|
|
123
|
+
},
|
|
124
|
+
rotation: {
|
|
125
|
+
kind: "manual",
|
|
126
|
+
issuer: "facebook",
|
|
127
|
+
documentation: FACEBOOK_APPS_PAGE
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
[AUTH_GITHUB_CREDENTIALS]: {
|
|
131
|
+
backend: "d1",
|
|
132
|
+
scope: "environment",
|
|
133
|
+
rotatable: false,
|
|
134
|
+
valueType: "json",
|
|
135
|
+
schema: GithubOAuthCredentials,
|
|
136
|
+
origin: {
|
|
137
|
+
kind: "obtained",
|
|
138
|
+
issuer: "github",
|
|
139
|
+
documentation: GITHUB_OAUTH_APPS_PAGE
|
|
140
|
+
},
|
|
141
|
+
rotation: {
|
|
142
|
+
kind: "manual",
|
|
143
|
+
issuer: "github",
|
|
144
|
+
documentation: GITHUB_OAUTH_APPS_PAGE
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
/** Resolve the Better Auth session secret for this invocation. */
|
|
149
|
+
async function resolveSessionSecret(env) {
|
|
150
|
+
return (await sharedSecretsStore(env, authSecretsRegistry)).get(AUTH_SESSION_SECRET);
|
|
151
|
+
}
|
|
152
|
+
/** Resolve the Google OAuth credential pair for this invocation (only call when Google is enabled). */
|
|
153
|
+
async function resolveGoogleCredentials(env) {
|
|
154
|
+
return (await sharedSecretsStore(env, authSecretsRegistry)).get(AUTH_GOOGLE_CREDENTIALS);
|
|
155
|
+
}
|
|
156
|
+
/** Resolve the Apple Sign-In credentials for this invocation (only call when Apple is enabled). */
|
|
157
|
+
async function resolveAppleCredentials(env) {
|
|
158
|
+
return (await sharedSecretsStore(env, authSecretsRegistry)).get(AUTH_APPLE_CREDENTIALS);
|
|
159
|
+
}
|
|
160
|
+
/** Resolve the Facebook Login credential pair for this invocation (only call when Facebook is enabled). */
|
|
161
|
+
async function resolveFacebookCredentials(env) {
|
|
162
|
+
return (await sharedSecretsStore(env, authSecretsRegistry)).get(AUTH_FACEBOOK_CREDENTIALS);
|
|
163
|
+
}
|
|
164
|
+
/** Resolve the GitHub OAuth credential pair for this invocation (only call when GitHub is enabled). */
|
|
165
|
+
async function resolveGithubCredentials(env) {
|
|
166
|
+
return (await sharedSecretsStore(env, authSecretsRegistry)).get(AUTH_GITHUB_CREDENTIALS);
|
|
167
|
+
}
|
|
168
|
+
//#endregion
|
|
169
|
+
export { AUTH_APPLE_CREDENTIALS, AUTH_FACEBOOK_CREDENTIALS, AUTH_GITHUB_CREDENTIALS, AUTH_GOOGLE_CREDENTIALS, AUTH_SESSION_SECRET, AppleOAuthCredentials, FacebookOAuthCredentials, GithubOAuthCredentials, GoogleOAuthCredentials, authSecretsRegistry, resolveAppleCredentials, resolveFacebookCredentials, resolveGithubCredentials, resolveGoogleCredentials, resolveSessionSecret };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Migration } from "kysely/migration";
|
|
2
|
+
/**
|
|
3
|
+
* Create the auth tables in the app database, all prefixed `pithy_auth_` so they never clash with an
|
|
4
|
+
* adopter's own tables. Six are Better-Auth-managed (`users`, `sessions`, `accounts`,
|
|
5
|
+
* `verifications`, `jwks`, `rate_limit`); `devices` (the device registry) and `rotated_tokens` (the
|
|
6
|
+
* refresh-token reuse-detection ledger) are Pithy's own.
|
|
7
|
+
*
|
|
8
|
+
* Identifiers are declared in **camelCase**: the runner installs `CamelCasePlugin`, which snake-cases
|
|
9
|
+
* every identifier in the emitted DDL (CLAUDE.md §Data layer) — and the same plugin lets Better
|
|
10
|
+
* Auth's camelCase queries land on these snake_case columns. Better-Auth `date` columns are **text**
|
|
11
|
+
* (it stores ISO-8601 strings on SQLite; ISO sorts chronologically); Pithy's own device timestamps
|
|
12
|
+
* are **integer** ms-epoch. Foreign keys are omitted to match the repo convention (D1 does not enforce
|
|
13
|
+
* them without a per-connection PRAGMA); linkage is by indexed id columns.
|
|
14
|
+
*
|
|
15
|
+
* `down` is the tested inverse — indexes then tables, in reverse creation order (D1 has no
|
|
16
|
+
* transactional DDL, so order matters).
|
|
17
|
+
*
|
|
18
|
+
* **Amended in place on 2026-08-23** for `locale` on `pithy_auth_users` (pithy-sh/pithy#441), and the
|
|
19
|
+
* condition was checked rather than assumed, because CONTRIBUTING.md asks a later reader to check it:
|
|
20
|
+
*
|
|
21
|
+
* - `@pithy-sh/auth` is at `0.0.0` and `https://registry.npmjs.org/@pithy-sh/auth` is a 404. Nothing
|
|
22
|
+
* has been released, so no `0300_auth_0001_init` has run anywhere a chain would be replayed against.
|
|
23
|
+
* - `packages/cli/src/migrations/oneMigration.test.ts` gates that same condition repo-wide and is green,
|
|
24
|
+
* which is what keeps this an amendment rather than a `0002`.
|
|
25
|
+
*
|
|
26
|
+
* **The moment either stops being true this file is history, and the chain is append-only.** A version
|
|
27
|
+
* cut and the next column is a `0002`. Nothing about a tidy `0001` tells a reader which side of that
|
|
28
|
+
* line they are on — re-run the two checks, do not infer them.
|
|
29
|
+
*/
|
|
30
|
+
export declare const auth_0001_init: Migration;
|
|
31
|
+
/** The migration order for the auth namespace within the app database (library-before-app; auth is a wedge cap). */
|
|
32
|
+
export declare const AUTH_MIGRATION_ORDER = 300;
|
|
33
|
+
//# sourceMappingURL=0001_init.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"0001_init.d.ts","sourceRoot":"","sources":["../../src/migrations/0001_init.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,cAAc,EAAE,SA+L5B,CAAC;AAEF,oHAAoH;AACpH,eAAO,MAAM,oBAAoB,MAAM,CAAC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
//#region src/migrations/0001_init.ts
|
|
2
|
+
/**
|
|
3
|
+
* Create the auth tables in the app database, all prefixed `pithy_auth_` so they never clash with an
|
|
4
|
+
* adopter's own tables. Six are Better-Auth-managed (`users`, `sessions`, `accounts`,
|
|
5
|
+
* `verifications`, `jwks`, `rate_limit`); `devices` (the device registry) and `rotated_tokens` (the
|
|
6
|
+
* refresh-token reuse-detection ledger) are Pithy's own.
|
|
7
|
+
*
|
|
8
|
+
* Identifiers are declared in **camelCase**: the runner installs `CamelCasePlugin`, which snake-cases
|
|
9
|
+
* every identifier in the emitted DDL (CLAUDE.md §Data layer) — and the same plugin lets Better
|
|
10
|
+
* Auth's camelCase queries land on these snake_case columns. Better-Auth `date` columns are **text**
|
|
11
|
+
* (it stores ISO-8601 strings on SQLite; ISO sorts chronologically); Pithy's own device timestamps
|
|
12
|
+
* are **integer** ms-epoch. Foreign keys are omitted to match the repo convention (D1 does not enforce
|
|
13
|
+
* them without a per-connection PRAGMA); linkage is by indexed id columns.
|
|
14
|
+
*
|
|
15
|
+
* `down` is the tested inverse — indexes then tables, in reverse creation order (D1 has no
|
|
16
|
+
* transactional DDL, so order matters).
|
|
17
|
+
*
|
|
18
|
+
* **Amended in place on 2026-08-23** for `locale` on `pithy_auth_users` (pithy-sh/pithy#441), and the
|
|
19
|
+
* condition was checked rather than assumed, because CONTRIBUTING.md asks a later reader to check it:
|
|
20
|
+
*
|
|
21
|
+
* - `@pithy-sh/auth` is at `0.0.0` and `https://registry.npmjs.org/@pithy-sh/auth` is a 404. Nothing
|
|
22
|
+
* has been released, so no `0300_auth_0001_init` has run anywhere a chain would be replayed against.
|
|
23
|
+
* - `packages/cli/src/migrations/oneMigration.test.ts` gates that same condition repo-wide and is green,
|
|
24
|
+
* which is what keeps this an amendment rather than a `0002`.
|
|
25
|
+
*
|
|
26
|
+
* **The moment either stops being true this file is history, and the chain is append-only.** A version
|
|
27
|
+
* cut and the next column is a `0002`. Nothing about a tidy `0001` tells a reader which side of that
|
|
28
|
+
* line they are on — re-run the two checks, do not infer them.
|
|
29
|
+
*/
|
|
30
|
+
const auth_0001_init = {
|
|
31
|
+
up: async (db) => {
|
|
32
|
+
await db.schema.createTable("pithyAuthUsers").addColumn("id", "text", (c) => c.primaryKey()).addColumn("name", "text", (c) => c.notNull()).addColumn("email", "text", (c) => c.notNull().unique()).addColumn("emailVerified", "integer", (c) => c.notNull().defaultTo(0)).addColumn("image", "text").addColumn("locale", "text").addColumn("createdAt", "text", (c) => c.notNull()).addColumn("updatedAt", "text", (c) => c.notNull()).execute();
|
|
33
|
+
await db.schema.createTable("pithyAuthSessions").addColumn("id", "text", (c) => c.primaryKey()).addColumn("expiresAt", "text", (c) => c.notNull()).addColumn("token", "text", (c) => c.notNull().unique()).addColumn("createdAt", "text", (c) => c.notNull()).addColumn("updatedAt", "text", (c) => c.notNull()).addColumn("ipAddress", "text").addColumn("userAgent", "text").addColumn("userId", "text", (c) => c.notNull()).addColumn("deviceId", "text").addColumn("familyId", "text").execute();
|
|
34
|
+
await db.schema.createTable("pithyAuthAccounts").addColumn("id", "text", (c) => c.primaryKey()).addColumn("issuer", "text", (c) => c.notNull()).addColumn("accountId", "text", (c) => c.notNull()).addColumn("providerId", "text", (c) => c.notNull()).addColumn("userId", "text", (c) => c.notNull()).addColumn("accessToken", "text").addColumn("refreshToken", "text").addColumn("idToken", "text").addColumn("accessTokenExpiresAt", "text").addColumn("refreshTokenExpiresAt", "text").addColumn("scope", "text").addColumn("password", "text").addColumn("createdAt", "text", (c) => c.notNull()).addColumn("updatedAt", "text", (c) => c.notNull()).execute();
|
|
35
|
+
await db.schema.createTable("pithyAuthVerifications").addColumn("id", "text", (c) => c.primaryKey()).addColumn("identifier", "text", (c) => c.notNull()).addColumn("value", "text", (c) => c.notNull()).addColumn("expiresAt", "text", (c) => c.notNull()).addColumn("createdAt", "text", (c) => c.notNull()).addColumn("updatedAt", "text", (c) => c.notNull()).execute();
|
|
36
|
+
await db.schema.createTable("pithyAuthJwks").addColumn("id", "text", (c) => c.primaryKey()).addColumn("publicKey", "text", (c) => c.notNull()).addColumn("privateKey", "text", (c) => c.notNull()).addColumn("createdAt", "text", (c) => c.notNull()).addColumn("expiresAt", "text").addColumn("alg", "text").addColumn("crv", "text").execute();
|
|
37
|
+
await db.schema.createTable("pithyAuthRateLimit").addColumn("id", "text", (c) => c.primaryKey()).addColumn("key", "text", (c) => c.notNull().unique()).addColumn("count", "integer", (c) => c.notNull()).addColumn("lastRequest", "integer", (c) => c.notNull()).execute();
|
|
38
|
+
await db.schema.createTable("pithyAuthDevices").addColumn("id", "text", (c) => c.notNull()).addColumn("userId", "text", (c) => c.notNull()).addColumn("platform", "text", (c) => c.notNull()).addColumn("name", "text").addColumn("model", "text").addColumn("osVersion", "text").addColumn("appVersion", "text").addColumn("pushToken", "text").addColumn("lastIp", "text").addColumn("lastSeenAt", "integer", (c) => c.notNull()).addColumn("createdAt", "integer", (c) => c.notNull()).addPrimaryKeyConstraint("pithyAuthDevicesPk", ["userId", "id"]).execute();
|
|
39
|
+
await db.schema.createTable("pithyAuthRotatedTokens").addColumn("token", "text", (c) => c.primaryKey()).addColumn("familyId", "text", (c) => c.notNull()).addColumn("userId", "text", (c) => c.notNull()).addColumn("rotatedAt", "integer", (c) => c.notNull()).execute();
|
|
40
|
+
await db.schema.createIndex("pithyAuthSessionsUserIdIdx").on("pithyAuthSessions").column("userId").execute();
|
|
41
|
+
await db.schema.createIndex("pithyAuthSessionsDeviceIdIdx").on("pithyAuthSessions").column("deviceId").execute();
|
|
42
|
+
await db.schema.createIndex("pithyAuthAccountsUserIdIdx").on("pithyAuthAccounts").column("userId").execute();
|
|
43
|
+
await db.schema.createIndex("pithyAuthAccountsIssuerAccountIdIdx").on("pithyAuthAccounts").columns(["issuer", "accountId"]).unique().execute();
|
|
44
|
+
await db.schema.createIndex("pithyAuthVerificationsIdentifierIdx").on("pithyAuthVerifications").column("identifier").execute();
|
|
45
|
+
await db.schema.createIndex("pithyAuthRotatedTokensFamilyIdIdx").on("pithyAuthRotatedTokens").column("familyId").execute();
|
|
46
|
+
await db.schema.createIndex("pithyAuthRotatedTokensRotatedAtIdx").on("pithyAuthRotatedTokens").column("rotatedAt").execute();
|
|
47
|
+
await db.schema.createIndex("pithyAuthUsersCreatedAtIdx").on("pithyAuthUsers").columns(["createdAt", "id"]).execute();
|
|
48
|
+
await db.schema.createIndex("pithyAuthDevicesLastSeenAtIdx").on("pithyAuthDevices").columns([
|
|
49
|
+
"lastSeenAt",
|
|
50
|
+
"userId",
|
|
51
|
+
"id"
|
|
52
|
+
]).execute();
|
|
53
|
+
},
|
|
54
|
+
down: async (db) => {
|
|
55
|
+
await db.schema.dropIndex("pithyAuthDevicesLastSeenAtIdx").execute();
|
|
56
|
+
await db.schema.dropIndex("pithyAuthUsersCreatedAtIdx").execute();
|
|
57
|
+
await db.schema.dropIndex("pithyAuthRotatedTokensRotatedAtIdx").execute();
|
|
58
|
+
await db.schema.dropIndex("pithyAuthRotatedTokensFamilyIdIdx").execute();
|
|
59
|
+
await db.schema.dropIndex("pithyAuthVerificationsIdentifierIdx").execute();
|
|
60
|
+
await db.schema.dropIndex("pithyAuthAccountsIssuerAccountIdIdx").execute();
|
|
61
|
+
await db.schema.dropIndex("pithyAuthAccountsUserIdIdx").execute();
|
|
62
|
+
await db.schema.dropIndex("pithyAuthSessionsDeviceIdIdx").execute();
|
|
63
|
+
await db.schema.dropIndex("pithyAuthSessionsUserIdIdx").execute();
|
|
64
|
+
await db.schema.dropTable("pithyAuthRotatedTokens").execute();
|
|
65
|
+
await db.schema.dropTable("pithyAuthDevices").execute();
|
|
66
|
+
await db.schema.dropTable("pithyAuthRateLimit").execute();
|
|
67
|
+
await db.schema.dropTable("pithyAuthJwks").execute();
|
|
68
|
+
await db.schema.dropTable("pithyAuthVerifications").execute();
|
|
69
|
+
await db.schema.dropTable("pithyAuthAccounts").execute();
|
|
70
|
+
await db.schema.dropTable("pithyAuthSessions").execute();
|
|
71
|
+
await db.schema.dropTable("pithyAuthUsers").execute();
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
/** The migration order for the auth namespace within the app database (library-before-app; auth is a wedge cap). */
|
|
75
|
+
const AUTH_MIGRATION_ORDER = 300;
|
|
76
|
+
//#endregion
|
|
77
|
+
export { AUTH_MIGRATION_ORDER, auth_0001_init };
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { BetterAuthOptions, BetterAuthPlugin } from "better-auth";
|
|
2
|
+
import type { Migration } from "kysely/migration";
|
|
3
|
+
/**
|
|
4
|
+
* Deriving an adopter's Better Auth plugin tables, and creating them through `pithy migrate`.
|
|
5
|
+
*
|
|
6
|
+
* The kit's migration model is per-capability sets composed by `pithy migrate`, and until now there was
|
|
7
|
+
* no path for tables an **adopter** introduced through a capability's plugin. This is that path, and it
|
|
8
|
+
* works because the plugin list is in `pithy.config.ts` — the same file the CLI already imports to
|
|
9
|
+
* collect capabilities. The auth capability reads its own config, asks Better Auth what schema those
|
|
10
|
+
* plugins imply, and contributes one ordinary Kysely migration per plugin. Nothing new was added to the
|
|
11
|
+
* migration model; the capability simply declares more of it.
|
|
12
|
+
*
|
|
13
|
+
* **The diff is against a baseline, not against nothing.** `getSchema` answers with the *whole* schema
|
|
14
|
+
* for a set of options, so the same call is made twice — once with only the kit's own plugins, once
|
|
15
|
+
* with the kit's own plus one adopter plugin — and the difference is what that plugin brought. That is
|
|
16
|
+
* what keeps the derived migration from re-creating `pithy_auth_users` on every project.
|
|
17
|
+
*
|
|
18
|
+
* **A plugin brings two kinds of change and both matter.** `organization` adds three tables *and* an
|
|
19
|
+
* `activeOrganizationId` column to the session table the kit already owns; a create-table-only reading
|
|
20
|
+
* ships a schema where `setActive` fails on the first call. Both halves are derived here.
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* The Better Auth options that shape the schema, and only those. Model names, the session's extra
|
|
24
|
+
* fields, and the rate-limit table are all read by `getSchema`, so the baseline it computes has to
|
|
25
|
+
* carry the same ones `makeAuth` passes or the diff would report the kit's own tables as new.
|
|
26
|
+
*
|
|
27
|
+
* Kept beside `makeAuth` rather than inside it because the migration is built at `auth(config)` time,
|
|
28
|
+
* where the request-scoped deps (database, secret, base URL) do not exist yet — and none of them
|
|
29
|
+
* changes a column.
|
|
30
|
+
*/
|
|
31
|
+
export declare function authSchemaOptions(plugins: readonly BetterAuthPlugin[]): BetterAuthOptions;
|
|
32
|
+
/** SQLite storage for one Better Auth field type — the same two the kit's own tables use. */
|
|
33
|
+
export type PluginColumnType = "text" | "integer";
|
|
34
|
+
/** One column of a table a plugin introduced. */
|
|
35
|
+
export interface PluginTableColumn {
|
|
36
|
+
/** The camelCase column name; `CamelCasePlugin` snake-cases it in the emitted DDL. */
|
|
37
|
+
name: string;
|
|
38
|
+
/** Its SQLite storage. */
|
|
39
|
+
type: PluginColumnType;
|
|
40
|
+
/** Whether the column is `NOT NULL`. */
|
|
41
|
+
notNull: boolean;
|
|
42
|
+
/** Whether this is the table's `id` primary key. */
|
|
43
|
+
primaryKey: boolean;
|
|
44
|
+
/** Whether the column carries a `UNIQUE` constraint. */
|
|
45
|
+
unique: boolean;
|
|
46
|
+
/** Whether the plugin asked for an index on it. */
|
|
47
|
+
index: boolean;
|
|
48
|
+
}
|
|
49
|
+
/** A whole table a plugin introduced. */
|
|
50
|
+
export interface PluginTable {
|
|
51
|
+
/** The camelCase model name; `CamelCasePlugin` snake-cases it. */
|
|
52
|
+
name: string;
|
|
53
|
+
/** Its columns, `id` first. */
|
|
54
|
+
columns: PluginTableColumn[];
|
|
55
|
+
}
|
|
56
|
+
/** A column a plugin added to a table that already existed — the kit's own, or an earlier plugin's. */
|
|
57
|
+
export interface PluginColumn {
|
|
58
|
+
/** The table the column is added to. */
|
|
59
|
+
table: string;
|
|
60
|
+
/** The camelCase column name. */
|
|
61
|
+
name: string;
|
|
62
|
+
/** Its SQLite storage. */
|
|
63
|
+
type: PluginColumnType;
|
|
64
|
+
/** Always `false` — see {@link pluginSchemaDelta}. */
|
|
65
|
+
notNull: boolean;
|
|
66
|
+
/** Whether the plugin asked for an index on it. */
|
|
67
|
+
index: boolean;
|
|
68
|
+
}
|
|
69
|
+
/** Everything one plugin adds to the schema: whole tables, and columns on tables that already exist. */
|
|
70
|
+
export interface PluginSchemaDelta {
|
|
71
|
+
tables: PluginTable[];
|
|
72
|
+
columns: PluginColumn[];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* What one plugin adds to the auth schema, as a plan a migration can be built from.
|
|
76
|
+
*
|
|
77
|
+
* **Every added column is nullable, deliberately.** SQLite refuses `ALTER TABLE … ADD COLUMN … NOT NULL`
|
|
78
|
+
* without a constant default, and the table it is being added to is a live one that already has rows —
|
|
79
|
+
* so a plugin's `required` on a *new* column of an *existing* table cannot be honored by any migration,
|
|
80
|
+
* whatever it claims. Better Auth writes the value on every insert it makes, so the constraint is
|
|
81
|
+
* enforced where the plugin enforces it; the column simply does not also declare it. A column of a table
|
|
82
|
+
* the plugin creates itself is `NOT NULL` normally — there are no rows to contradict it.
|
|
83
|
+
*/
|
|
84
|
+
export declare function pluginSchemaDelta(plugin: BetterAuthPlugin): PluginSchemaDelta;
|
|
85
|
+
/**
|
|
86
|
+
* Build the Kysely migration for one plugin's delta. `down` is the exact inverse, and it runs the
|
|
87
|
+
* indexes off first: SQLite refuses to drop an indexed column.
|
|
88
|
+
*/
|
|
89
|
+
export declare function pluginMigration(delta: PluginSchemaDelta): Migration;
|
|
90
|
+
/**
|
|
91
|
+
* The migration key for a plugin, e.g. `two-factor` → `0002_plugin_two_factor`.
|
|
92
|
+
*
|
|
93
|
+
* `0002` for every plugin, and that is not laziness. The four-digit lead is what
|
|
94
|
+
* `createMigrationRegistry` demands of a local key; within the auth namespace these all sort after
|
|
95
|
+
* `0001_init` and among themselves by plugin id, and **the order between two plugins is arbitrary by
|
|
96
|
+
* construction** — no plugin's tables reference another's, since each is derived against the same
|
|
97
|
+
* baseline. Numbering them `0002`, `0003`, … in config order would make the key of an already-applied
|
|
98
|
+
* migration change the moment a plugin was inserted before it, which is the one thing a ledger cannot
|
|
99
|
+
* survive.
|
|
100
|
+
*/
|
|
101
|
+
export declare function pluginMigrationKey(id: string): string;
|
|
102
|
+
/** What an adopter's plugin list contributes to the auth capability, derived once. */
|
|
103
|
+
export interface AuthPluginPlan {
|
|
104
|
+
/** One migration per plugin that declares a schema, keyed by {@link pluginMigrationKey}. */
|
|
105
|
+
migrations: Record<string, Migration>;
|
|
106
|
+
/** One entry per plugin, whatever it declared — what `pithy doctor` reports. */
|
|
107
|
+
extensions: {
|
|
108
|
+
id: string;
|
|
109
|
+
tables: string[];
|
|
110
|
+
}[];
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Derive everything the auth capability needs from an adopter's plugin list, in one pass.
|
|
114
|
+
*
|
|
115
|
+
* Migrations: one per plugin that has a schema, keyed by {@link pluginMigrationKey}. A plugin that adds
|
|
116
|
+
* no tables and no columns gets no migration and no ledger row — a plugin that is only endpoints has
|
|
117
|
+
* nothing to create. Extensions: one per plugin regardless, because a plugin with no tables still adds
|
|
118
|
+
* routes and still must not be invisible.
|
|
119
|
+
*
|
|
120
|
+
* Two plugins that claim the same table, or a plugin that claims one of the kit's own, are refused
|
|
121
|
+
* here rather than at `pithy migrate`: the second `createTable` would fail mid-run against a database
|
|
122
|
+
* that has no transactional DDL, leaving the first plugin's tables half-created. Naming both plugins
|
|
123
|
+
* and the table is the whole remedy — one of them takes a `schema: { … modelName }` override.
|
|
124
|
+
*/
|
|
125
|
+
export declare function authPluginPlan(plugins: readonly BetterAuthPlugin[]): AuthPluginPlan;
|
|
126
|
+
//# sourceMappingURL=pluginTables.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pluginTables.d.ts","sourceRoot":"","sources":["../../src/migrations/pluginTables.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAGvE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAKlD;;;;;;;;;;;;;;;;;;GAkBG;AAEH;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,SAAS,gBAAgB,EAAE,GAAG,iBAAiB,CAuBzF;AAED,6FAA6F;AAC7F,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,SAAS,CAAC;AAElD,iDAAiD;AACjD,MAAM,WAAW,iBAAiB;IAChC,sFAAsF;IACtF,IAAI,EAAE,MAAM,CAAC;IACb,0BAA0B;IAC1B,IAAI,EAAE,gBAAgB,CAAC;IACvB,wCAAwC;IACxC,OAAO,EAAE,OAAO,CAAC;IACjB,oDAAoD;IACpD,UAAU,EAAE,OAAO,CAAC;IACpB,wDAAwD;IACxD,MAAM,EAAE,OAAO,CAAC;IAChB,mDAAmD;IACnD,KAAK,EAAE,OAAO,CAAC;CAChB;AAED,yCAAyC;AACzC,MAAM,WAAW,WAAW;IAC1B,kEAAkE;IAClE,IAAI,EAAE,MAAM,CAAC;IACb,+BAA+B;IAC/B,OAAO,EAAE,iBAAiB,EAAE,CAAC;CAC9B;AAED,uGAAuG;AACvG,MAAM,WAAW,YAAY;IAC3B,wCAAwC;IACxC,KAAK,EAAE,MAAM,CAAC;IACd,iCAAiC;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,0BAA0B;IAC1B,IAAI,EAAE,gBAAgB,CAAC;IACvB,sDAAsD;IACtD,OAAO,EAAE,OAAO,CAAC;IACjB,mDAAmD;IACnD,KAAK,EAAE,OAAO,CAAC;CAChB;AAED,wGAAwG;AACxG,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,WAAW,EAAE,CAAC;IACtB,OAAO,EAAE,YAAY,EAAE,CAAC;CACzB;AA0BD;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,gBAAgB,GAAG,iBAAiB,CA0C7E;AAOD;;;GAGG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,iBAAiB,GAAG,SAAS,CAoDnE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAKrD;AAED,sFAAsF;AACtF,MAAM,WAAW,cAAc;IAC7B,4FAA4F;IAC5F,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACtC,gFAAgF;IAChF,UAAU,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,EAAE,CAAA;KAAE,EAAE,CAAC;CAChD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,SAAS,gBAAgB,EAAE,GAAG,cAAc,CAwCnF"}
|