-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathauth.ts
More file actions
449 lines (412 loc) · 14.4 KB
/
Copy pathauth.ts
File metadata and controls
449 lines (412 loc) · 14.4 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import { TRIAL_ANONYMOUS_USER_ID } from '@simple-agent-manager/shared';
import { betterAuth } from 'better-auth';
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
import { ne } from 'drizzle-orm';
import { drizzle } from 'drizzle-orm/d1';
import * as v from 'valibot';
import * as schema from './db/schema';
import type { Env } from './env';
import { createModuleLogger } from './lib/logger';
import { readResponseJson } from './lib/runtime-validation';
import { getBetterAuthSecret } from './lib/secrets';
import {
getGitHubOAuthConfig,
getGitLabOAuthConfig,
getGoogleLoginOAuthConfig,
} from './services/platform-config';
import { isSignupApprovalRequired } from './services/signup-approval';
const log = createModuleLogger('auth');
/**
* Atomic, race-free login-time superadmin self-heal.
*
* Promotes the logging-in user to superadmin/active IFF they are the only real
* (non-internal) user on the deployment AND no superadmin exists — i.e. a fresh or
* sentinel-orphaned fork. Every guard lives in the WHERE clause so the statement is
* a single atomic UPDATE with no read-modify-write race; concurrent logins of the
* same user are idempotent (the second is a no-op).
*
* Numbered params: ?1 = current user id (referenced twice), ?2 = sentinel id.
* Guards: (a) skip already-superadmin rows; (d) never auto-elevate a suspended
* account; status!='system' never mutates the sentinel; (b) the current user is the
* only non-internal user; (c) no non-system superadmin exists anywhere.
*
* Drizzle cannot express the correlated-subquery WHERE, so this is issued via the
* raw D1 binding (prepare/bind/run) — the same mechanism used by
* services/scheduler-state-sync.ts and services/github-trigger-handler.ts.
*/
const LOGIN_SELF_HEAL_SQL = `
UPDATE users
SET role = 'superadmin', status = 'active'
WHERE id = ?1
AND role != 'superadmin'
AND status != 'system'
AND status != 'suspended'
AND (
SELECT COUNT(*) FROM users u2
WHERE u2.id != ?1
AND u2.status != 'system'
AND u2.id != ?2
) = 0
AND (
SELECT COUNT(*) FROM users u3
WHERE u3.role = 'superadmin'
AND u3.status != 'system'
) = 0
`;
interface GitHubEmailResponse {
email: string;
primary: boolean;
verified: boolean;
}
const GITHUB_API_VERSION = '2022-11-28';
const GITHUB_TOKEN_URL = 'https://github.com/login/oauth/access_token';
const githubUserSchema = v.object({
id: v.union([v.number(), v.string()]),
login: v.optional(v.nullable(v.string())),
name: v.optional(v.nullable(v.string())),
email: v.optional(v.nullable(v.string())),
avatar_url: v.optional(v.nullable(v.string())),
});
const githubEmailSchema = v.object({
email: v.string(),
primary: v.boolean(),
verified: v.boolean(),
});
const githubRefreshTokenSuccessSchema = v.object({
access_token: v.string(),
refresh_token: v.optional(v.string()),
expires_in: v.optional(v.number()),
refresh_token_expires_in: v.optional(v.number()),
scope: v.optional(v.string()),
token_type: v.optional(v.string()),
id_token: v.optional(v.string()),
});
const githubRefreshTokenErrorSchema = v.object({
error: v.string(),
error_description: v.optional(v.string()),
});
function expiresAtFromSeconds(seconds: number | undefined): Date | undefined {
if (typeof seconds !== 'number' || !Number.isFinite(seconds)) {
return undefined;
}
return new Date(Date.now() + seconds * 1000);
}
export async function refreshGitHubAccessToken(env: Env, refreshToken: string) {
const githubOAuth = await getGitHubOAuthConfig(env);
if (!githubOAuth) {
throw new Error('GitHub OAuth is not configured');
}
const requestBody = new URLSearchParams();
requestBody.set('client_id', githubOAuth.clientId);
requestBody.set('client_secret', githubOAuth.clientSecret);
requestBody.set('grant_type', 'refresh_token');
requestBody.set('refresh_token', refreshToken);
const response = await fetch(GITHUB_TOKEN_URL, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'SAM-Auth',
},
body: requestBody,
});
const body = await response.json().catch(() => null);
if (!response.ok) {
log.warn('github_oauth_refresh_failed', { status: response.status });
throw new Error('GitHub OAuth refresh failed');
}
const errorResult = v.safeParse(githubRefreshTokenErrorSchema, body);
if (errorResult.success) {
log.warn('github_oauth_refresh_error_body', {
status: response.status,
error: errorResult.output.error,
});
throw new Error('GitHub OAuth refresh failed');
}
const data = v.parse(githubRefreshTokenSuccessSchema, body);
return {
accessToken: data.access_token,
refreshToken: data.refresh_token,
accessTokenExpiresAt: expiresAtFromSeconds(data.expires_in),
refreshTokenExpiresAt: expiresAtFromSeconds(data.refresh_token_expires_in),
scopes: data.scope ? data.scope.split(/[,\s]+/).filter(Boolean) : [],
tokenType: data.token_type,
idToken: data.id_token,
raw: data,
};
}
function githubApiHeaders(accessToken: string): HeadersInit {
return {
Authorization: `Bearer ${accessToken}`,
Accept: 'application/vnd.github+json',
'User-Agent': 'SAM-Auth',
'X-GitHub-Api-Version': GITHUB_API_VERSION,
};
}
function normalizeEmail(email: string | null | undefined): string | null {
if (!email) {
return null;
}
const trimmed = email.trim();
return trimmed.length > 0 ? trimmed : null;
}
export function selectPrimaryGitHubEmail(
userEmail: string | null | undefined,
emails: GitHubEmailResponse[] | null | undefined
): string | null {
const normalizedUserEmail = normalizeEmail(userEmail);
const normalizedEmails = (emails || [])
.map((entry) => ({
email: normalizeEmail(entry.email),
primary: Boolean(entry.primary),
verified: Boolean(entry.verified),
}))
.filter((entry): entry is { email: string; primary: boolean; verified: boolean } =>
Boolean(entry.email)
);
const verifiedPrimary = normalizedEmails.find((entry) => entry.primary && entry.verified);
if (verifiedPrimary) {
return verifiedPrimary.email;
}
const primary = normalizedEmails.find((entry) => entry.primary);
if (primary) {
return primary.email;
}
return normalizedUserEmail;
}
/**
* Create BetterAuth instance with Cloudflare D1 + KV configuration.
*/
export async function createAuth(env: Env) {
const db = drizzle(env.DATABASE, { schema });
// Sentinel id is env-overridable; fall back to the shared constant.
const sentinelId = env.TRIAL_ANONYMOUS_USER_ID ?? TRIAL_ANONYMOUS_USER_ID;
const githubOAuth = await getGitHubOAuthConfig(env);
const googleOAuth = await getGoogleLoginOAuthConfig(env);
const gitlabOAuth = await getGitLabOAuthConfig(env);
const socialProviders: Record<string, unknown> = {};
const trustedProviders: string[] = [];
if (githubOAuth) {
trustedProviders.push('github');
socialProviders.github = {
clientId: githubOAuth.clientId,
clientSecret: githubOAuth.clientSecret,
scope: ['read:user', 'user:email', 'read:org'],
refreshAccessToken: (refreshToken: string) => refreshGitHubAccessToken(env, refreshToken),
// Ensure existing linked users are refreshed with latest provider profile data on sign-in.
overrideUserInfoOnSignIn: true,
// Custom getUserInfo to ensure we persist the account's primary email when available.
getUserInfo: async (token: { accessToken?: string }) => {
const accessToken = token.accessToken;
if (!accessToken) {
log.error('missing_github_access_token');
return null;
}
const userRes = await fetch('https://api.github.com/user', {
headers: githubApiHeaders(accessToken),
});
if (!userRes.ok) {
log.error('github_user_fetch_failed', { status: userRes.status });
return null;
}
const user = await readResponseJson(userRes, githubUserSchema, 'github.user');
let email = normalizeEmail(user.email);
// Resolve the user's primary email from /user/emails.
// OAuth apps need user:email scope. GitHub Apps need "Email addresses" user permission.
try {
const emailsRes = await fetch('https://api.github.com/user/emails', {
headers: githubApiHeaders(accessToken),
});
if (emailsRes.ok) {
const emailsData = await readResponseJson(
emailsRes,
v.array(githubEmailSchema),
'github.user_emails'
);
email = selectPrimaryGitHubEmail(email, emailsData);
} else {
const errorBody = await emailsRes.text();
if (emailsRes.status === 403 || emailsRes.status === 404) {
log.error('github_emails_unavailable', {
status: emailsRes.status,
hint: 'Ensure GitHub App user permission "Email addresses" is read-only or OAuth app has user:email scope',
responseBody: errorBody,
});
} else {
log.error('github_emails_fetch_failed', {
status: emailsRes.status,
responseBody: errorBody,
});
}
}
} catch (err) {
log.error('github_emails_fetch_exception', {
error: err instanceof Error ? err.message : String(err),
});
}
// Last resort: use GitHub noreply email
if (!email && user.login && user.id) {
email = `${user.id}+${user.login}@users.noreply.github.com`;
}
if (!email) {
return null;
}
return {
user: {
id: String(user.id),
email,
name: (user.name || user.login || '').trim(),
image: user.avatar_url || undefined,
emailVerified: true,
},
data: {
githubId: String(user.id),
avatarUrl: user.avatar_url || undefined,
},
};
},
};
}
if (googleOAuth) {
trustedProviders.push('google');
socialProviders.google = {
clientId: googleOAuth.clientId,
clientSecret: googleOAuth.clientSecret,
scope: ['openid', 'email', 'profile'],
overrideUserInfoOnSignIn: true,
};
}
if (gitlabOAuth) {
trustedProviders.push('gitlab');
socialProviders.gitlab = {
clientId: gitlabOAuth.clientId,
clientSecret: gitlabOAuth.clientSecret,
issuer: gitlabOAuth.host,
scope: ['read_user', 'api', 'read_repository', 'write_repository'],
overrideUserInfoOnSignIn: true,
};
}
return betterAuth({
database: drizzleAdapter(db, {
provider: 'sqlite',
usePlural: true,
}),
basePath: '/api/auth',
baseURL: `https://api.${env.BASE_DOMAIN}`,
secret: getBetterAuthSecret(env),
trustedOrigins: [
`https://app.${env.BASE_DOMAIN}`,
`https://api.${env.BASE_DOMAIN}`,
// Allow localhost only in development (BASE_DOMAIN contains 'localhost' or is empty)
...(!env.BASE_DOMAIN || env.BASE_DOMAIN.includes('localhost')
? ['http://localhost:5173', 'http://localhost:3000']
: []),
],
session: {
cookieCache: {
enabled: true,
maxAge: 5 * 60, // 5 minutes
},
},
socialProviders,
user: {
additionalFields: {
githubId: {
type: 'string',
required: false,
},
avatarUrl: {
type: 'string',
required: false,
},
role: {
type: 'string',
required: false,
defaultValue: 'user',
input: false,
},
status: {
type: 'string',
required: false,
defaultValue: 'active',
input: false,
},
},
},
databaseHooks: {
user: {
create: {
before: async (user) => {
if (!(await isSignupApprovalRequired(env))) {
// Open registration — defaults are fine (role='user', status='active')
return { data: user };
}
// Check if this is the first user (auto-superadmin)
const hookDb = drizzle(env.DATABASE, { schema });
const existing = await hookDb
.select({ id: schema.users.id })
.from(schema.users)
.where(ne(schema.users.id, sentinelId))
.limit(1)
.all();
if (existing.length === 0) {
return {
data: { ...user, role: 'superadmin', status: 'active' },
};
}
// Subsequent users need approval
return {
data: { ...user, role: 'user', status: 'pending' },
};
},
},
},
session: {
create: {
// Login-time superadmin self-heal. Fires on EVERY session creation —
// OAuth (GitHub), token-login, and device-flow all route through
// internalAdapter.createSession -> createWithHooks, which runs this
// session.create.after hook. Promotes the sole real user to
// superadmin/active on a sentinel-orphaned or fresh deployment (see
// LOGIN_SELF_HEAL_SQL). The data migration is the deploy-time guarantee:
// it heals the known orphaned victim at migration time regardless of
// whether that user ever signs in again.
//
// The try/catch is LOAD-BEARING: better-auth awaits session.create.after
// hooks before returning the login response, so an uncaught throw would
// surface as a 500 and break login. Swallow + log so login always succeeds.
after: async (session) => {
const userId = session.userId;
if (!userId) {
return;
}
try {
const result = await env.DATABASE.prepare(LOGIN_SELF_HEAL_SQL)
.bind(userId, sentinelId)
.run();
if (result.meta.changes > 0) {
log.info('login_self_heal.promoted', { userId });
}
} catch (err) {
log.error('login_self_heal.failed', {
userId,
error: err instanceof Error ? err.message : String(err),
});
}
},
},
},
},
account: {
encryptOAuthTokens: true,
accountLinking: {
enabled: true,
trustedProviders,
},
},
});
}
/**
* Type for the auth instance.
*/
export type Auth = Awaited<ReturnType<typeof createAuth>>;