MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / resolveAuthProviders

Function resolveAuthProviders

apps/host-selfhost/src/auth/index.ts:44–91  ·  view source on GitHub ↗
(
  dbHandle: SelfHostDbHandle,
)

Source from the content-addressed store, hash-verified

42}
43
44export const resolveAuthProviders = async (
45 dbHandle: SelfHostDbHandle,
46): Promise<ResolvedAuthProviders> => {
47 const betterAuth = await buildBetterAuth(dbHandle.client);
48 const betterAuthLayer = Layer.succeed(BetterAuth)(betterAuth);
49
50 // The consent redirect from Better Auth's authorize only carries the opaque
51 // client_id; look the registered client_name up (its adapter sees the
52 // just-written DCR row) so the approval screen reads "Connect Codex?" not a
53 // random id. Self-declared at open DCR — a label, not a trust signal.
54 const lookupClientName = async (clientId: string): Promise<string | null> => {
55 const ctx = await betterAuth.auth.$context;
56 const app = await ctx.adapter.findOne<{ name?: string | null }>({
57 model: "oauthApplication",
58 where: [{ field: "clientId", value: clientId }],
59 });
60 return app?.name ?? null;
61 };
62
63 // Force the MCP approval screen: inject `prompt=consent` on every MCP
64 // authorize so a connecting client is gated on /mcp-consent rather than
65 // silently granted a token (see ./force-mcp-consent), and enrich the
66 // resulting consent redirect with the registered client name.
67 const config = loadConfig();
68 const authHandler = async (request: Request): Promise<Response> => {
69 const response = await betterAuth.handler(withForcedMcpConsent(request));
70 // Turn Better Auth's bare 403 "Invalid origin" into a setup instruction —
71 // on a fresh deploy it almost always means the public URL needs configuring.
72 const friendlier = await rewriteInvalidOrigin(request, response, config.webBaseUrl);
73 if (friendlier) return friendlier;
74 if (response.status !== 302) return response;
75 const clientId = consentRedirectClientId(response.headers.get("location"));
76 if (!clientId) return response;
77 const name = await lookupClientName(clientId);
78 if (!name) return response;
79 // Preserve the rest of the response — notably the signed consent cookie.
80 const headers = new Headers(response.headers);
81 headers.set("location", withClientName(response.headers.get("location")!, name));
82 return new Response(null, { status: 302, headers });
83 };
84
85 return {
86 identityLayer: betterAuthIdentityLayer.pipe(Layer.provide(betterAuthLayer)),
87 memberDirectoryLayer: betterAuthMemberDirectoryLayer.pipe(Layer.provide(betterAuthLayer)),
88 authHandler,
89 betterAuth,
90 };
91};

Callers 1

makeSelfHostAppFunction · 0.90

Calls 2

buildBetterAuthFunction · 0.90
loadConfigFunction · 0.90

Tested by

no test coverage detected