mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 13:23:04 +00:00
724c88c201
A mechanical prettier --write over server/src, server/tests, server/scripts and shared/src with the workspaces' .prettierrc; nothing but layout and import order changes, and every file keeps the same set of imports. shared is formatted throughout and its format baseline is empty. The server baseline keeps the 35 files that Prettier would grow past their lint:size limit (they need a split first, which is not a formatting change), and the generated entity files stay as their generator writes them.
104 lines
5.0 KiB
TypeScript
104 lines
5.0 KiB
TypeScript
import { OauthClients } from '../../db/entities/OauthClients.entity';
|
|
import { OauthConsents } from '../../db/entities/OauthConsents.entity';
|
|
import { OauthTokens } from '../../db/entities/OauthTokens.entity';
|
|
import { Users } from '../../db/entities/Users.entity';
|
|
import { AddonsModule } from '../addons/addons.module';
|
|
import { AddonsService } from '../addons/addons.service';
|
|
import { createMcpAddonGate } from '../addons/mcp-addon-gate';
|
|
import { AuditModule } from '../audit/audit.module';
|
|
import { RateLimitModule } from '../common/rate-limit.module';
|
|
import { SchedulingModule } from '../scheduling/scheduling.module';
|
|
import { OauthApiController } from './oauth-api.controller';
|
|
import { OauthPublicController } from './oauth-public.controller';
|
|
import { TrekClientsStore, TrekOAuthProvider } from './oauth-sdk.provider';
|
|
import { OauthTokenRetentionJob } from './oauth-token-retention.job';
|
|
import { PendingCodeStore, pendingCodesSlot, processPendingCodes } from './oauth.pending-codes';
|
|
import { OauthService } from './oauth.service';
|
|
import { MikroOrmModule } from '@mikro-orm/nestjs';
|
|
import { authorizationHandler } from '@modelcontextprotocol/sdk/server/auth/handlers/authorize';
|
|
import { clientRegistrationHandler } from '@modelcontextprotocol/sdk/server/auth/handlers/register';
|
|
import { Module, type OnModuleDestroy } from '@nestjs/common';
|
|
import type { MiddlewareConsumer, NestModule } from '@nestjs/common';
|
|
|
|
/**
|
|
* OAuth 2.1 server (MCP). Public token/userinfo/revoke endpoints + the SPA's
|
|
* authenticated consent/client/session management, plus the SDK's own
|
|
* /oauth/authorize and /oauth/register Express routers, mounted here through
|
|
* MiddlewareConsumer over the injected TrekOAuthProvider/TrekClientsStore.
|
|
* forRoutes('<path>') is an Express prefix mount — the same app.use('/path')
|
|
* shape (and the same req.url stripping) the pre-init platform mount used, and
|
|
* exactly what the SDK routers are built for. The handlers are constructed once
|
|
* per app instance in configure(), preserving the per-app lifetime of the DCR
|
|
* handler's built-in rate limiter.
|
|
*
|
|
* The handlers bring their own body parsers; the global 100kb parsers run
|
|
* first and theirs no-op on the already-consumed stream. The one observable
|
|
* difference vs the pre-init mount: bracketed keys in a POST /oauth/authorize
|
|
* body now parse with qs (extended:true) rather than querystring — the SDK
|
|
* zod-rejects such bodies either way.
|
|
*
|
|
* Pending authorization codes live behind the PendingCodeStore port
|
|
* (oauth.pending-codes.ts), provided here as the process-wide in-memory
|
|
* instance: the consent controller (container singleton) writes them, the
|
|
* SDK exchange path reads them back through the same injected singleton. The
|
|
* constructor installs whichever store the container resolved in
|
|
* pendingCodesSlot, so the sweep and a hand-built OauthService follow a
|
|
* swapped provider too.
|
|
*
|
|
* Exports OauthService for AdminController (admin OAuth-session panel) and the
|
|
* MCP transport's token verification.
|
|
*
|
|
* `MikroOrmModule.forFeature([OauthClients, OauthTokens, OauthConsents,
|
|
* Users])` (Plan 3b Task 4 deliverable): `Users` is registered for cross-
|
|
* module availability (inventory §6 — `OauthTokensRepository` needs to be
|
|
* importable from `AuthModule` too, Task 5's `revokeAllForUser`, the same
|
|
* shape `McpTokensRepository`/`InviteTokensRepository` already use across
|
|
* `nest/tokens`/`nest/auth`/`nest/oidc`) even though `OauthService` itself
|
|
* never injects `UsersRepository` directly — OA16/OA31 read `users` columns
|
|
* THROUGH `OauthTokensRepository`'s own join methods, not a separate
|
|
* `Users` call (Task 1's ruling: "do not add users methods" for this join).
|
|
*/
|
|
@Module({
|
|
imports: [
|
|
RateLimitModule,
|
|
AuditModule,
|
|
AddonsModule,
|
|
SchedulingModule,
|
|
MikroOrmModule.forFeature([OauthClients, OauthTokens, OauthConsents, Users]),
|
|
],
|
|
controllers: [OauthPublicController, OauthApiController],
|
|
providers: [
|
|
OauthService,
|
|
TrekClientsStore,
|
|
TrekOAuthProvider,
|
|
OauthTokenRetentionJob,
|
|
{ provide: PendingCodeStore, useValue: processPendingCodes },
|
|
],
|
|
exports: [OauthService],
|
|
})
|
|
export class OauthModule implements NestModule, OnModuleDestroy {
|
|
constructor(
|
|
private readonly addons: AddonsService,
|
|
private readonly provider: TrekOAuthProvider,
|
|
private readonly clients: TrekClientsStore,
|
|
private readonly pendingCodes: PendingCodeStore,
|
|
) {
|
|
pendingCodesSlot.install(pendingCodes);
|
|
}
|
|
|
|
onModuleDestroy(): void {
|
|
pendingCodesSlot.release(this.pendingCodes);
|
|
}
|
|
|
|
configure(consumer: MiddlewareConsumer): void {
|
|
const mcpAddonGate = createMcpAddonGate(this.addons);
|
|
|
|
// SDK authorize handler: validates OAuth params, calls provider.authorize()
|
|
// which redirects to the SPA consent page at /oauth/consent
|
|
consumer.apply(mcpAddonGate, authorizationHandler({ provider: this.provider })).forRoutes('oauth/authorize');
|
|
|
|
// SDK DCR handler: accepts registrations without scope (fixes issue #959 bug 2)
|
|
consumer.apply(mcpAddonGate, clientRegistrationHandler({ clientsStore: this.clients })).forRoutes('oauth/register');
|
|
}
|
|
}
|