-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth-config.zod.ts
More file actions
187 lines (167 loc) · 7.59 KB
/
auth-config.zod.ts
File metadata and controls
187 lines (167 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
/**
* Better-Auth Configuration Protocol
*
* Defines the configuration required to initialize the Better-Auth kernel.
* Used in server-side configuration injection.
*/
export const AuthProviderConfigSchema = z.object({
id: z.string().describe('Provider ID (github, google)'),
clientId: z.string().describe('OAuth Client ID'),
clientSecret: z.string().describe('OAuth Client Secret'),
scope: z.array(z.string()).optional().describe('Requested permissions'),
});
export const AuthPluginConfigSchema = z.object({
organization: z.boolean().default(false).describe('Enable Organization/Teams support'),
twoFactor: z.boolean().default(false).describe('Enable 2FA'),
passkeys: z.boolean().default(false).describe('Enable Passkey support'),
magicLink: z.boolean().default(false).describe('Enable Magic Link login'),
});
/**
* Mutual TLS (mTLS) Configuration Schema
*
* Enables client certificate authentication for zero-trust architectures.
*/
export const MutualTLSConfigSchema = z.object({
/** Enable mutual TLS authentication */
enabled: z.boolean()
.default(false)
.describe('Enable mutual TLS authentication'),
/** Require client certificates for all connections */
clientCertRequired: z.boolean()
.default(false)
.describe('Require client certificates for all connections'),
/** PEM-encoded CA certificates or file paths for trust validation */
trustedCAs: z.array(z.string())
.describe('PEM-encoded CA certificates or file paths'),
/** Certificate Revocation List URL */
crlUrl: z.string()
.optional()
.describe('Certificate Revocation List (CRL) URL'),
/** Online Certificate Status Protocol URL */
ocspUrl: z.string()
.optional()
.describe('Online Certificate Status Protocol (OCSP) URL'),
/** Certificate validation strictness level */
certificateValidation: z.enum(['strict', 'relaxed', 'none'])
.describe('Certificate validation strictness level'),
/** Allowed Common Names on client certificates */
allowedCNs: z.array(z.string())
.optional()
.describe('Allowed Common Names (CN) on client certificates'),
/** Allowed Organizational Units on client certificates */
allowedOUs: z.array(z.string())
.optional()
.describe('Allowed Organizational Units (OU) on client certificates'),
/** Certificate pinning configuration */
pinning: z.object({
/** Enable certificate pinning */
enabled: z.boolean().describe('Enable certificate pinning'),
/** Array of pinned certificate hashes */
pins: z.array(z.string()).describe('Pinned certificate hashes'),
})
.optional()
.describe('Certificate pinning configuration'),
});
export type MutualTLSConfig = z.infer<typeof MutualTLSConfigSchema>;
/**
* Social / OAuth Provider Configuration
*
* Maps provider id → { clientId, clientSecret, ... }.
* Keys must match Better-Auth built-in provider names (google, github, etc.).
*/
export const SocialProviderConfigSchema = z.record(
z.string(),
z.object({
clientId: z.string().describe('OAuth Client ID'),
clientSecret: z.string().describe('OAuth Client Secret'),
enabled: z.boolean().optional().default(true).describe('Enable this provider'),
scope: z.array(z.string()).optional().describe('Additional OAuth scopes'),
}).catchall(z.unknown()),
).optional().describe(
'Social/OAuth provider map forwarded to better-auth socialProviders. ' +
'Keys are provider ids (google, github, apple, …).'
);
/**
* Email + Password Configuration
*/
export const EmailAndPasswordConfigSchema = z.object({
enabled: z.boolean().default(true).describe('Enable email/password auth'),
disableSignUp: z.boolean().optional().describe('Disable new user registration via email/password'),
requireEmailVerification: z.boolean().optional().describe(
'Require email verification before creating a session'
),
minPasswordLength: z.number().optional().describe('Minimum password length (default 8)'),
maxPasswordLength: z.number().optional().describe('Maximum password length (default 128)'),
resetPasswordTokenExpiresIn: z.number().optional().describe(
'Reset-password token TTL in seconds (default 3600)'
),
autoSignIn: z.boolean().optional().describe('Auto sign-in after sign-up (default true)'),
revokeSessionsOnPasswordReset: z.boolean().optional().describe(
'Revoke all other sessions on password reset'
),
}).optional().describe('Email and password authentication options forwarded to better-auth');
/**
* Email Verification Configuration
*/
export const EmailVerificationConfigSchema = z.object({
sendOnSignUp: z.boolean().optional().describe(
'Automatically send verification email after sign-up'
),
sendOnSignIn: z.boolean().optional().describe(
'Send verification email on sign-in when not yet verified'
),
autoSignInAfterVerification: z.boolean().optional().describe(
'Auto sign-in the user after email verification'
),
expiresIn: z.number().optional().describe(
'Verification token TTL in seconds (default 3600)'
),
}).optional().describe('Email verification options forwarded to better-auth');
/**
* Advanced / Low-level Better-Auth Options
*/
export const AdvancedAuthConfigSchema = z.object({
crossSubDomainCookies: z.object({
enabled: z.boolean().describe('Enable cross-subdomain cookies'),
additionalCookies: z.array(z.string()).optional().describe('Extra cookies shared across subdomains'),
domain: z.string().optional().describe(
'Cookie domain override — defaults to root domain derived from baseUrl'
),
}).optional().describe(
'Share auth cookies across subdomains (critical for *.example.com multi-tenant)'
),
useSecureCookies: z.boolean().optional().describe('Force Secure flag on cookies'),
disableCSRFCheck: z.boolean().optional().describe(
'⚠ Disable CSRF check — security risk, use with caution'
),
cookiePrefix: z.string().optional().describe('Prefix for auth cookie names'),
}).optional().describe('Advanced / low-level Better-Auth options');
export const AuthConfigSchema = z.object({
secret: z.string().optional().describe('Encryption secret'),
baseUrl: z.string().optional().describe('Base URL for auth routes'),
databaseUrl: z.string().optional().describe('Database connection string'),
providers: z.array(AuthProviderConfigSchema).optional(),
plugins: AuthPluginConfigSchema.optional(),
session: z.object({
expiresIn: z.number().default(60 * 60 * 24 * 7).describe('Session duration in seconds'),
updateAge: z.number().default(60 * 60 * 24).describe('Session update frequency'),
}).optional(),
trustedOrigins: z.array(z.string()).optional().describe(
'Trusted origins for CSRF protection. Supports wildcards (e.g. "https://*.example.com"). ' +
'The baseUrl origin is always trusted implicitly.'
),
socialProviders: SocialProviderConfigSchema,
emailAndPassword: EmailAndPasswordConfigSchema,
emailVerification: EmailVerificationConfigSchema,
advanced: AdvancedAuthConfigSchema,
mutualTls: MutualTLSConfigSchema.optional().describe('Mutual TLS (mTLS) configuration'),
}).catchall(z.unknown());
export type AuthProviderConfig = z.infer<typeof AuthProviderConfigSchema>;
export type AuthPluginConfig = z.infer<typeof AuthPluginConfigSchema>;
export type SocialProviderConfig = z.infer<typeof SocialProviderConfigSchema>;
export type EmailAndPasswordConfig = z.infer<typeof EmailAndPasswordConfigSchema>;
export type EmailVerificationConfig = z.infer<typeof EmailVerificationConfigSchema>;
export type AdvancedAuthConfig = z.infer<typeof AdvancedAuthConfigSchema>;
export type AuthConfig = z.infer<typeof AuthConfigSchema>;