Repository navigation
Expand file tree
/
Copy pathauth.ts
More file actions
106 lines (93 loc) · 2.72 KB
/
Copy pathauth.ts
File metadata and controls
106 lines (93 loc) · 2.72 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
import { env } from "cloudflare:workers";
import { mcp } from "@better-auth/mcp";
import { betterAuth } from "better-auth";
import { jwt } from "better-auth/plugins";
export const BETTER_AUTH_SECRET =
(env as unknown as { BETTER_AUTH_SECRET?: string })?.BETTER_AUTH_SECRET ||
"codingcat-mcp-oauth2-secret-secure-random-2026-key-better-auth";
export const MCP_DEFAULT_SCOPES = [
"read:content",
"search",
"mcp:tools",
"agent",
] as const;
export const DEFAULT_MCP_USER = {
id: "user_codingcat_explorer",
name: "CodingCat Dev Explorer",
email: "explorer@codingcat.dev",
};
export const DEFAULT_MCP_SESSION = {
id: "sess_codingcat_default",
token: "cc_mcp_session_token",
userId: "user_codingcat_explorer",
};
/**
* Computes base64url(SHA-256(token)) matching Better Auth's token store format.
*/
export async function hashToken(token: string): Promise<string> {
const hash = await crypto.subtle.digest(
"SHA-256",
new TextEncoder().encode(token),
);
return btoa(String.fromCharCode(...new Uint8Array(hash)))
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+$/, "");
}
/**
* Ensures a default guest/explorer user and active session exist in Cloudflare D1
* for automated MCP server account linking.
*/
export async function ensureDefaultUserAndSession(db: D1Database) {
const now = Date.now();
await db
.prepare(
`INSERT OR IGNORE INTO user (id, name, email, emailVerified, createdAt, updatedAt)
VALUES ('user_codingcat_explorer', 'CodingCat Dev Explorer', 'explorer@codingcat.dev', 1, ?, ?)`,
)
.bind(now, now)
.run();
await db
.prepare(
`INSERT OR IGNORE INTO session (id, expiresAt, token, createdAt, updatedAt, userId)
VALUES ('sess_codingcat_default', 2147483647000, 'cc_mcp_session_token', ?, ?, 'user_codingcat_explorer')`,
)
.bind(now, now)
.run();
}
let cachedAuth: any = null;
let cachedOrigin = "";
/**
* Creates or retrieves the Better Auth instance configured for
* Model Context Protocol (MCP) and OAuth 2.1 authorization server
* backed by Cloudflare D1.
*/
export function getAuth(origin: string, d1?: D1Database) {
const db = d1 ?? (env as unknown as { DB?: D1Database })?.DB;
if (!db && cachedAuth && cachedOrigin === origin) {
return cachedAuth;
}
const instance = betterAuth({
baseURL: origin,
basePath: "",
secret: BETTER_AUTH_SECRET,
...(db ? { database: db } : {}),
plugins: [
jwt(),
mcp({
resource: `${origin}/mcp`,
loginPage: "/sign-in",
consentPage: "/oauth2/consent",
allowDynamicClientRegistration: true,
allowUnauthenticatedClientRegistration: true,
clientRegistrationRequirePKCE: false,
scopes: [...MCP_DEFAULT_SCOPES],
}),
],
});
if (!db) {
cachedAuth = instance;
cachedOrigin = origin;
}
return instance;
}