Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 | 75x 75x | /**
* PostgreSQL Session Storage Backend (Prisma)
*
* Production-grade storage for multi-instance deployments.
* Uses Prisma ORM for type-safe database access.
*
* Features:
* - Type-safe queries via Prisma
* - Automatic migrations on startup
* - Connection pooling (Prisma managed)
* - Automatic cleanup of expired entries
*/
import {
OAuthSession,
DeviceFlowState as DeviceFlowStateType,
AuthCodeFlowState as AuthCodeFlowStateType,
AuthorizationCode as AuthorizationCodeType,
} from "../types";
import { SessionStorageBackend, SessionStorageStats } from "./types";
import { logger } from "../../logger";
/**
* Explicit interfaces for Prisma model results.
* These mirror the Prisma schema but with explicit types that ESLint can resolve.
*/
interface PrismaOAuthSessionRow {
id: string;
mcpAccessToken: string;
mcpRefreshToken: string;
mcpTokenExpiry: bigint;
gitlabAccessToken: string;
gitlabRefreshToken: string;
gitlabTokenExpiry: bigint;
gitlabUserId: number;
gitlabUsername: string;
clientId: string;
scopes: string[];
createdAt: bigint;
updatedAt: bigint;
}
interface PrismaDeviceFlowStateRow {
state: string;
deviceCode: string;
userCode: string;
verificationUri: string;
verificationUriComplete: string | null;
expiresAt: bigint;
interval: number;
clientId: string;
codeChallenge: string;
codeChallengeMethod: string;
redirectUri: string | null;
}
interface PrismaAuthCodeFlowStateRow {
internalState: string;
clientId: string;
codeChallenge: string;
codeChallengeMethod: string;
clientState: string;
clientRedirectUri: string;
callbackUri: string;
expiresAt: bigint;
}
interface PrismaAuthorizationCodeRow {
code: string;
sessionId: string;
clientId: string;
codeChallenge: string;
codeChallengeMethod: string;
redirectUri: string | null;
expiresAt: bigint;
}
interface PrismaMcpSessionMappingRow {
mcpSessionId: string;
oauthSessionId: string;
oauthSession?: PrismaOAuthSessionRow;
}
interface PrismaBatchPayload {
count: number;
}
/**
* Generic Prisma client interface.
* We use a loose interface here to avoid type compatibility issues with Prisma's complex types.
*/
interface GenericPrismaClient {
$connect(): Promise<void>;
$disconnect(): Promise<void>;
$transaction(operations: unknown[]): Promise<unknown[]>;
oAuthSession: {
create(args: unknown): Promise<unknown>;
findUnique(args: unknown): Promise<unknown>;
findFirst(args: unknown): Promise<unknown>;
findMany(): Promise<unknown>;
update(args: unknown): Promise<unknown>;
delete(args: unknown): Promise<unknown>;
deleteMany(args: unknown): Promise<unknown>;
count(): Promise<number>;
};
deviceFlowState: {
upsert(args: unknown): Promise<unknown>;
findUnique(args: unknown): Promise<unknown>;
findFirst(args: unknown): Promise<unknown>;
delete(args: unknown): Promise<unknown>;
deleteMany(args: unknown): Promise<unknown>;
count(): Promise<number>;
};
authCodeFlowState: {
create(args: unknown): Promise<unknown>;
findUnique(args: unknown): Promise<unknown>;
delete(args: unknown): Promise<unknown>;
deleteMany(args: unknown): Promise<unknown>;
count(): Promise<number>;
};
authorizationCode: {
create(args: unknown): Promise<unknown>;
findUnique(args: unknown): Promise<unknown>;
delete(args: unknown): Promise<unknown>;
deleteMany(args: unknown): Promise<unknown>;
count(): Promise<number>;
};
mcpSessionMapping: {
upsert(args: unknown): Promise<unknown>;
findUnique(args: unknown): Promise<unknown>;
delete(args: unknown): Promise<unknown>;
count(): Promise<number>;
};
}
export interface PostgreSQLStorageOptions {
/** PostgreSQL connection string (optional, uses OAUTH_STORAGE_POSTGRESQL_URL if not provided) */
connectionString?: string;
}
export class PostgreSQLStorageBackend implements SessionStorageBackend {
readonly type = "postgresql" as const;
private prisma: GenericPrismaClient | null = null;
private cleanupIntervalId: ReturnType<typeof setInterval> | null = null;
async initialize(): Promise<void> {
try {
// Dynamic import Prisma client to avoid initialization if not used
const prismaModule = (await import("../../../generated/prisma/client")) as {
PrismaClient: new (opts: Record<string, unknown>) => GenericPrismaClient;
};
// Create Prisma client
this.prisma = new prismaModule.PrismaClient({});
// Connect and test
await this.prisma.$connect();
// Start cleanup interval
this.startCleanupInterval();
logger.info("PostgreSQL storage backend initialized via Prisma");
} catch (error) {
logger.error({ err: error as Error }, "Failed to initialize PostgreSQL storage backend");
throw error;
}
}
private getPrisma(): GenericPrismaClient {
if (!this.prisma) {
throw new Error("PostgreSQL/Prisma client not initialized");
}
return this.prisma;
}
// Session operations
async createSession(session: OAuthSession): Promise<void> {
const prisma = this.getPrisma();
await prisma.oAuthSession.create({
data: {
id: session.id,
mcpAccessToken: session.mcpAccessToken,
mcpRefreshToken: session.mcpRefreshToken,
mcpTokenExpiry: BigInt(session.mcpTokenExpiry),
gitlabAccessToken: session.gitlabAccessToken,
gitlabRefreshToken: session.gitlabRefreshToken,
gitlabTokenExpiry: BigInt(session.gitlabTokenExpiry),
gitlabUserId: session.gitlabUserId,
gitlabUsername: session.gitlabUsername,
clientId: session.clientId,
scopes: session.scopes,
createdAt: BigInt(session.createdAt),
updatedAt: BigInt(session.updatedAt),
},
});
logger.debug({ sessionId: session.id }, "Session created in PostgreSQL");
}
async getSession(sessionId: string): Promise<OAuthSession | undefined> {
const prisma = this.getPrisma();
const row = (await prisma.oAuthSession.findUnique({
where: { id: sessionId },
})) as PrismaOAuthSessionRow | null;
return row ? this.rowToSession(row) : undefined;
}
async getSessionByToken(token: string): Promise<OAuthSession | undefined> {
const prisma = this.getPrisma();
const row = (await prisma.oAuthSession.findFirst({
where: { mcpAccessToken: token },
})) as PrismaOAuthSessionRow | null;
return row ? this.rowToSession(row) : undefined;
}
async getSessionByRefreshToken(refreshToken: string): Promise<OAuthSession | undefined> {
const prisma = this.getPrisma();
const row = (await prisma.oAuthSession.findFirst({
where: { mcpRefreshToken: refreshToken },
})) as PrismaOAuthSessionRow | null;
return row ? this.rowToSession(row) : undefined;
}
async updateSession(sessionId: string, updates: Partial<OAuthSession>): Promise<boolean> {
const prisma = this.getPrisma();
const data: Record<string, unknown> = {
updatedAt: BigInt(Date.now()),
};
if (updates.mcpAccessToken !== undefined) {
data.mcpAccessToken = updates.mcpAccessToken;
}
if (updates.mcpRefreshToken !== undefined) {
data.mcpRefreshToken = updates.mcpRefreshToken;
}
if (updates.mcpTokenExpiry !== undefined) {
data.mcpTokenExpiry = BigInt(updates.mcpTokenExpiry);
}
if (updates.gitlabAccessToken !== undefined) {
data.gitlabAccessToken = updates.gitlabAccessToken;
}
if (updates.gitlabRefreshToken !== undefined) {
data.gitlabRefreshToken = updates.gitlabRefreshToken;
}
if (updates.gitlabTokenExpiry !== undefined) {
data.gitlabTokenExpiry = BigInt(updates.gitlabTokenExpiry);
}
try {
await prisma.oAuthSession.update({
where: { id: sessionId },
data,
});
return true;
} catch {
return false;
}
}
async deleteSession(sessionId: string): Promise<boolean> {
const prisma = this.getPrisma();
try {
await prisma.oAuthSession.delete({
where: { id: sessionId },
});
return true;
} catch {
return false;
}
}
async getAllSessions(): Promise<OAuthSession[]> {
const prisma = this.getPrisma();
const rows = (await prisma.oAuthSession.findMany()) as PrismaOAuthSessionRow[];
return rows.map(row => this.rowToSession(row));
}
private rowToSession(row: PrismaOAuthSessionRow): OAuthSession {
return {
id: row.id,
mcpAccessToken: row.mcpAccessToken,
mcpRefreshToken: row.mcpRefreshToken,
mcpTokenExpiry: Number(row.mcpTokenExpiry),
gitlabAccessToken: row.gitlabAccessToken,
gitlabRefreshToken: row.gitlabRefreshToken,
gitlabTokenExpiry: Number(row.gitlabTokenExpiry),
gitlabUserId: row.gitlabUserId,
gitlabUsername: row.gitlabUsername,
clientId: row.clientId,
scopes: row.scopes,
createdAt: Number(row.createdAt),
updatedAt: Number(row.updatedAt),
};
}
// Device flow operations
async storeDeviceFlow(state: string, flow: DeviceFlowStateType): Promise<void> {
const prisma = this.getPrisma();
await prisma.deviceFlowState.upsert({
where: { state },
update: {
deviceCode: flow.deviceCode,
userCode: flow.userCode,
expiresAt: BigInt(flow.expiresAt),
},
create: {
state,
deviceCode: flow.deviceCode,
userCode: flow.userCode,
verificationUri: flow.verificationUri,
verificationUriComplete: flow.verificationUriComplete ?? null,
expiresAt: BigInt(flow.expiresAt),
interval: flow.interval,
clientId: flow.clientId,
codeChallenge: flow.codeChallenge,
codeChallengeMethod: flow.codeChallengeMethod,
redirectUri: flow.redirectUri ?? null,
},
});
}
async getDeviceFlow(state: string): Promise<DeviceFlowStateType | undefined> {
const prisma = this.getPrisma();
const row = (await prisma.deviceFlowState.findUnique({
where: { state },
})) as PrismaDeviceFlowStateRow | null;
return row ? this.rowToDeviceFlow(row) : undefined;
}
async getDeviceFlowByDeviceCode(deviceCode: string): Promise<DeviceFlowStateType | undefined> {
const prisma = this.getPrisma();
const row = (await prisma.deviceFlowState.findFirst({
where: { deviceCode },
})) as PrismaDeviceFlowStateRow | null;
return row ? this.rowToDeviceFlow(row) : undefined;
}
async deleteDeviceFlow(state: string): Promise<boolean> {
const prisma = this.getPrisma();
try {
await prisma.deviceFlowState.delete({
where: { state },
});
return true;
} catch {
return false;
}
}
private rowToDeviceFlow(row: PrismaDeviceFlowStateRow): DeviceFlowStateType {
return {
deviceCode: row.deviceCode,
userCode: row.userCode,
verificationUri: row.verificationUri,
verificationUriComplete: row.verificationUriComplete ?? undefined,
expiresAt: Number(row.expiresAt),
interval: row.interval,
clientId: row.clientId,
codeChallenge: row.codeChallenge,
codeChallengeMethod: row.codeChallengeMethod,
state: row.state,
redirectUri: row.redirectUri ?? undefined,
};
}
// Auth code flow operations
async storeAuthCodeFlow(internalState: string, flow: AuthCodeFlowStateType): Promise<void> {
const prisma = this.getPrisma();
await prisma.authCodeFlowState.create({
data: {
internalState,
clientId: flow.clientId,
codeChallenge: flow.codeChallenge,
codeChallengeMethod: flow.codeChallengeMethod,
clientState: flow.clientState,
clientRedirectUri: flow.clientRedirectUri,
callbackUri: flow.callbackUri,
expiresAt: BigInt(flow.expiresAt),
},
});
}
async getAuthCodeFlow(internalState: string): Promise<AuthCodeFlowStateType | undefined> {
const prisma = this.getPrisma();
const row = (await prisma.authCodeFlowState.findUnique({
where: { internalState },
})) as PrismaAuthCodeFlowStateRow | null;
if (!row) return undefined;
return this.rowToAuthCodeFlow(row);
}
async deleteAuthCodeFlow(internalState: string): Promise<boolean> {
const prisma = this.getPrisma();
try {
await prisma.authCodeFlowState.delete({
where: { internalState },
});
return true;
} catch {
return false;
}
}
private rowToAuthCodeFlow(row: PrismaAuthCodeFlowStateRow): AuthCodeFlowStateType {
return {
clientId: row.clientId,
codeChallenge: row.codeChallenge,
codeChallengeMethod: row.codeChallengeMethod,
clientState: row.clientState,
internalState: row.internalState,
clientRedirectUri: row.clientRedirectUri,
callbackUri: row.callbackUri,
expiresAt: Number(row.expiresAt),
};
}
// Authorization code operations
async storeAuthCode(code: AuthorizationCodeType): Promise<void> {
const prisma = this.getPrisma();
await prisma.authorizationCode.create({
data: {
code: code.code,
sessionId: code.sessionId,
clientId: code.clientId,
codeChallenge: code.codeChallenge,
codeChallengeMethod: code.codeChallengeMethod,
redirectUri: code.redirectUri ?? null,
expiresAt: BigInt(code.expiresAt),
},
});
}
async getAuthCode(code: string): Promise<AuthorizationCodeType | undefined> {
const prisma = this.getPrisma();
const row = (await prisma.authorizationCode.findUnique({
where: { code },
})) as PrismaAuthorizationCodeRow | null;
if (!row) return undefined;
return this.rowToAuthCode(row);
}
async deleteAuthCode(code: string): Promise<boolean> {
const prisma = this.getPrisma();
try {
await prisma.authorizationCode.delete({
where: { code },
});
return true;
} catch {
return false;
}
}
private rowToAuthCode(row: PrismaAuthorizationCodeRow): AuthorizationCodeType {
return {
code: row.code,
sessionId: row.sessionId,
clientId: row.clientId,
codeChallenge: row.codeChallenge,
codeChallengeMethod: row.codeChallengeMethod,
redirectUri: row.redirectUri ?? undefined,
expiresAt: Number(row.expiresAt),
};
}
// MCP session mapping
async associateMcpSession(mcpSessionId: string, oauthSessionId: string): Promise<void> {
const prisma = this.getPrisma();
await prisma.mcpSessionMapping.upsert({
where: { mcpSessionId },
update: { oauthSessionId },
create: { mcpSessionId, oauthSessionId },
});
}
async getSessionByMcpSessionId(mcpSessionId: string): Promise<OAuthSession | undefined> {
const prisma = this.getPrisma();
const mapping = (await prisma.mcpSessionMapping.findUnique({
where: { mcpSessionId },
include: { oauthSession: true },
})) as PrismaMcpSessionMappingRow | null;
return mapping?.oauthSession ? this.rowToSession(mapping.oauthSession) : undefined;
}
async removeMcpSessionAssociation(mcpSessionId: string): Promise<boolean> {
const prisma = this.getPrisma();
try {
await prisma.mcpSessionMapping.delete({
where: { mcpSessionId },
});
return true;
} catch {
return false;
}
}
// Cleanup
async cleanup(): Promise<void> {
const prisma = this.getPrisma();
const now = BigInt(Date.now());
const maxSessionAge = BigInt(7 * 24 * 60 * 60 * 1000); // 7 days
try {
// Use transaction for atomic cleanup
const results = (await prisma.$transaction([
prisma.oAuthSession.deleteMany({
where: {
createdAt: { lt: now - maxSessionAge },
},
}),
prisma.deviceFlowState.deleteMany({
where: { expiresAt: { lt: now } },
}),
prisma.authCodeFlowState.deleteMany({
where: { expiresAt: { lt: now } },
}),
prisma.authorizationCode.deleteMany({
where: { expiresAt: { lt: now } },
}),
])) as PrismaBatchPayload[];
const expiredSessions = results[0].count;
const expiredDeviceFlows = results[1].count;
const expiredAuthCodeFlows = results[2].count;
const expiredAuthCodes = results[3].count;
if (
expiredSessions > 0 ||
expiredDeviceFlows > 0 ||
expiredAuthCodeFlows > 0 ||
expiredAuthCodes > 0
) {
logger.debug(
{ expiredSessions, expiredDeviceFlows, expiredAuthCodeFlows, expiredAuthCodes },
"PostgreSQL cleanup completed"
);
}
} catch (error) {
logger.error({ err: error as Error }, "PostgreSQL cleanup failed");
}
}
async close(): Promise<void> {
this.stopCleanupInterval();
if (this.prisma) {
await this.prisma.$disconnect();
this.prisma = null;
}
logger.info("PostgreSQL storage backend closed");
}
async getStats(): Promise<SessionStorageStats> {
const prisma = this.getPrisma();
const [sessions, deviceFlows, authCodeFlows, authCodes, mappings] = await Promise.all([
prisma.oAuthSession.count(),
prisma.deviceFlowState.count(),
prisma.authCodeFlowState.count(),
prisma.authorizationCode.count(),
prisma.mcpSessionMapping.count(),
]);
return {
sessions,
deviceFlows,
authCodeFlows,
authCodes,
mcpSessionMappings: mappings,
};
}
private startCleanupInterval(): void {
this.cleanupIntervalId = setInterval(
() => {
this.cleanup().catch(err => logger.error({ err }, "PostgreSQL cleanup error"));
},
5 * 60 * 1000
);
if (this.cleanupIntervalId.unref) {
this.cleanupIntervalId.unref();
}
}
private stopCleanupInterval(): void {
if (this.cleanupIntervalId) {
clearInterval(this.cleanupIntervalId);
this.cleanupIntervalId = null;
}
}
}
|