Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -1,7 +1,13 @@
|
||||
import { createHash, createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
||||
import { createHash, createHmac, randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
|
||||
|
||||
import { Prisma } from "../generated/prisma/client/client.js";
|
||||
import { getDbClient } from "./client.server.js";
|
||||
import type { Prisma } from "./prisma-browser.js";
|
||||
import { runWithRlsContext } from "./rls-context.server.js";
|
||||
import {
|
||||
resolveMcpOAuthAccessTokenRlsIdentity,
|
||||
resolveMcpOAuthAuthorizationCodeRlsIdentity,
|
||||
resolveMcpOAuthRefreshTokenRlsIdentity,
|
||||
} from "./rls-resolvers.server.js";
|
||||
|
||||
const DEFAULT_TOKEN_HASH_SECRET = "mcp-oauth-token-hash-v1";
|
||||
const CLIENT_ID_PREFIX = "hgomc_";
|
||||
@@ -48,6 +54,15 @@ export type McpOAuthConsumedAuthorizationCode = {
|
||||
codeChallengeMethod: string;
|
||||
};
|
||||
|
||||
export type ConsumeMcpOAuthAuthorizationCodeInput = {
|
||||
code: string;
|
||||
secret: string | undefined | null;
|
||||
clientId: string;
|
||||
redirectUri: string;
|
||||
codeVerifier: string;
|
||||
now?: Date;
|
||||
};
|
||||
|
||||
export type McpOAuthTokenPairInput = {
|
||||
clientId: string;
|
||||
userId: string;
|
||||
@@ -89,6 +104,13 @@ export type McpOAuthRefreshTokenRecord = {
|
||||
expiresAt: Date;
|
||||
};
|
||||
|
||||
export type ConsumeMcpOAuthRefreshTokenInput = {
|
||||
token: string;
|
||||
secret: string | undefined | null;
|
||||
clientId: string;
|
||||
now?: Date;
|
||||
};
|
||||
|
||||
export function normalizeMcpOAuthTokenHashSecret(secret: string | undefined | null): string {
|
||||
const normalized = secret?.trim();
|
||||
return normalized || DEFAULT_TOKEN_HASH_SECRET;
|
||||
@@ -111,17 +133,32 @@ export async function registerMcpOAuthClient(input: {
|
||||
contacts?: readonly string[];
|
||||
metadata?: Prisma.InputJsonValue;
|
||||
}): Promise<McpOAuthClientSummary> {
|
||||
const client = await getDbClient().mcpOAuthClient.create({
|
||||
data: {
|
||||
clientId: `${CLIENT_ID_PREFIX}${randomToken(24)}`,
|
||||
clientName: input.clientName,
|
||||
redirectUris: [...input.redirectUris] as Prisma.InputJsonArray,
|
||||
scopes: [...input.scopes] as Prisma.InputJsonArray,
|
||||
grantTypes: [...(input.grantTypes ?? ["authorization_code", "refresh_token"])] as Prisma.InputJsonArray,
|
||||
contacts: input.contacts ? ([...input.contacts] as Prisma.InputJsonArray) : undefined,
|
||||
metadata: input.metadata,
|
||||
},
|
||||
});
|
||||
const id = randomUUID();
|
||||
const clientId = `${CLIENT_ID_PREFIX}${randomToken(24)}`;
|
||||
const redirectUris = JSON.stringify([...input.redirectUris]);
|
||||
const grantTypes = JSON.stringify([...(input.grantTypes ?? ["authorization_code", "refresh_token"])]);
|
||||
const scopes = JSON.stringify([...input.scopes]);
|
||||
const contacts = input.contacts ? JSON.stringify([...input.contacts]) : null;
|
||||
const metadata = input.metadata === undefined ? null : JSON.stringify(input.metadata);
|
||||
const clients = await getDbClient().$queryRaw<
|
||||
Array<{
|
||||
id: string;
|
||||
clientId: string;
|
||||
clientName: string;
|
||||
redirectUris: Prisma.JsonValue;
|
||||
scopes: Prisma.JsonValue;
|
||||
active: boolean;
|
||||
}>
|
||||
>(Prisma.sql`
|
||||
SELECT * FROM public.hivegrid_register_mcp_oauth_client(
|
||||
${id}, ${clientId}, ${input.clientName}, ${redirectUris}::jsonb,
|
||||
${grantTypes}::jsonb, ${scopes}::jsonb, ${contacts}::jsonb, ${metadata}::jsonb
|
||||
)
|
||||
`);
|
||||
if (clients.length !== 1) {
|
||||
throw new Error("MCP OAuth client registration capability did not return exactly one row");
|
||||
}
|
||||
const client = clients[0];
|
||||
return toClientSummary(client);
|
||||
}
|
||||
|
||||
@@ -152,22 +189,33 @@ export async function createMcpOAuthAuthorizationCode(input: McpOAuthAuthorizati
|
||||
return { code, expiresAt };
|
||||
}
|
||||
|
||||
export async function consumeMcpOAuthAuthorizationCode(
|
||||
code: string,
|
||||
secret: string | undefined | null,
|
||||
now = new Date(),
|
||||
): Promise<McpOAuthConsumedAuthorizationCode | null> {
|
||||
export async function consumeMcpOAuthAuthorizationCode(input: ConsumeMcpOAuthAuthorizationCodeInput): Promise<McpOAuthConsumedAuthorizationCode | null> {
|
||||
const now = input.now ?? new Date();
|
||||
const db = getDbClient();
|
||||
const codeHash = hashMcpOAuthToken(code, secret);
|
||||
const codeHash = hashMcpOAuthToken(input.code, input.secret);
|
||||
const row = await db.mcpOAuthAuthorizationCode.findUnique({
|
||||
where: { codeHash },
|
||||
include: { client: true },
|
||||
});
|
||||
if (!row || row.consumedAt || row.expiresAt.getTime() <= now.getTime() || !row.client.active) {
|
||||
if (
|
||||
!row ||
|
||||
row.consumedAt ||
|
||||
row.expiresAt.getTime() <= now.getTime() ||
|
||||
!row.client.active ||
|
||||
row.client.clientId !== input.clientId ||
|
||||
row.redirectUri !== input.redirectUri ||
|
||||
!verifyMcpOAuthPkceS256(input.codeVerifier, row.codeChallenge)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const update = await db.mcpOAuthAuthorizationCode.updateMany({
|
||||
where: { id: row.id, consumedAt: null, expiresAt: { gt: now } },
|
||||
where: {
|
||||
id: row.id,
|
||||
clientId: row.clientId,
|
||||
redirectUri: input.redirectUri,
|
||||
consumedAt: null,
|
||||
expiresAt: { gt: now },
|
||||
},
|
||||
data: { consumedAt: now },
|
||||
});
|
||||
if (update.count !== 1) {
|
||||
@@ -227,19 +275,16 @@ export async function issueMcpOAuthTokenPair(input: McpOAuthTokenPairInput): Pro
|
||||
};
|
||||
}
|
||||
|
||||
export async function consumeMcpOAuthRefreshToken(
|
||||
token: string,
|
||||
secret: string | undefined | null,
|
||||
now = new Date(),
|
||||
): Promise<McpOAuthRefreshTokenRecord | null> {
|
||||
export async function consumeMcpOAuthRefreshToken(input: ConsumeMcpOAuthRefreshTokenInput): Promise<McpOAuthRefreshTokenRecord | null> {
|
||||
const now = input.now ?? new Date();
|
||||
const db = getDbClient();
|
||||
const tokenHash = hashMcpOAuthToken(token, secret);
|
||||
const tokenHash = hashMcpOAuthToken(input.token, input.secret);
|
||||
const row = await db.mcpOAuthRefreshToken.findUnique({ where: { tokenHash }, include: { client: true } });
|
||||
if (!row || row.revokedAt || row.rotatedAt || row.expiresAt.getTime() <= now.getTime() || !row.client.active) {
|
||||
if (!row || row.revokedAt || row.rotatedAt || row.expiresAt.getTime() <= now.getTime() || !row.client.active || row.client.clientId !== input.clientId) {
|
||||
return null;
|
||||
}
|
||||
const update = await db.mcpOAuthRefreshToken.updateMany({
|
||||
where: { id: row.id, revokedAt: null, rotatedAt: null, expiresAt: { gt: now } },
|
||||
where: { id: row.id, clientId: row.clientId, revokedAt: null, rotatedAt: null, expiresAt: { gt: now } },
|
||||
data: { rotatedAt: now },
|
||||
});
|
||||
if (update.count !== 1) {
|
||||
@@ -261,8 +306,9 @@ export async function findActiveMcpOAuthAccessToken(
|
||||
token: string,
|
||||
secret: string | undefined | null,
|
||||
now = new Date(),
|
||||
client = getDbClient()
|
||||
): Promise<McpOAuthAccessTokenPrincipal | null> {
|
||||
const row = await getDbClient().mcpOAuthAccessToken.findUnique({
|
||||
const row = await client.mcpOAuthAccessToken.findUnique({
|
||||
where: { tokenHash: hashMcpOAuthToken(token, secret) },
|
||||
include: { client: true },
|
||||
});
|
||||
@@ -282,6 +328,45 @@ export async function findActiveMcpOAuthAccessToken(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the opaque credential to its tenant without exposing the token row,
|
||||
* then performs the complete lookup under the matching RLS identity.
|
||||
*/
|
||||
export async function findActiveMcpOAuthAccessTokenWithRlsBootstrap(
|
||||
token: string,
|
||||
secret: string | undefined | null,
|
||||
now = new Date(),
|
||||
client = getDbClient()
|
||||
): Promise<McpOAuthAccessTokenPrincipal | null> {
|
||||
const tokenHash = hashMcpOAuthToken(token, secret);
|
||||
const identity = await resolveMcpOAuthAccessTokenRlsIdentity(tokenHash, client);
|
||||
if (!identity) return null;
|
||||
return runWithRlsContext({ tenantId: identity.tenantId, userId: identity.userId }, () => findActiveMcpOAuthAccessToken(token, secret, now, client));
|
||||
}
|
||||
|
||||
export async function consumeMcpOAuthAuthorizationCodeWithRlsBootstrap(
|
||||
input: ConsumeMcpOAuthAuthorizationCodeInput
|
||||
): Promise<McpOAuthConsumedAuthorizationCode | null> {
|
||||
const codeHash = hashMcpOAuthToken(input.code, input.secret);
|
||||
const identity = await resolveMcpOAuthAuthorizationCodeRlsIdentity(codeHash);
|
||||
if (!identity) return null;
|
||||
return runWithRlsContext({ tenantId: identity.tenantId, userId: identity.userId }, () => consumeMcpOAuthAuthorizationCode(input));
|
||||
}
|
||||
|
||||
export async function consumeMcpOAuthRefreshTokenWithRlsBootstrap(input: ConsumeMcpOAuthRefreshTokenInput): Promise<McpOAuthRefreshTokenRecord | null> {
|
||||
const tokenHash = hashMcpOAuthToken(input.token, input.secret);
|
||||
const identity = await resolveMcpOAuthRefreshTokenRlsIdentity(tokenHash);
|
||||
if (!identity) return null;
|
||||
return runWithRlsContext({ tenantId: identity.tenantId, userId: identity.userId }, () => consumeMcpOAuthRefreshToken(input));
|
||||
}
|
||||
|
||||
export async function revokeMcpOAuthTokenWithRlsBootstrap(token: string, secret: string | undefined | null, now = new Date()): Promise<boolean> {
|
||||
const tokenHash = hashMcpOAuthToken(token, secret);
|
||||
const identity = (await resolveMcpOAuthAccessTokenRlsIdentity(tokenHash)) ?? (await resolveMcpOAuthRefreshTokenRlsIdentity(tokenHash));
|
||||
if (!identity) return false;
|
||||
return runWithRlsContext({ tenantId: identity.tenantId, userId: identity.userId }, () => revokeMcpOAuthToken(token, secret, now));
|
||||
}
|
||||
|
||||
export async function revokeMcpOAuthToken(token: string, secret: string | undefined | null, now = new Date()): Promise<boolean> {
|
||||
const tokenHash = hashMcpOAuthToken(token, secret);
|
||||
const db = getDbClient();
|
||||
|
||||
Reference in new issue
Block a user