import { AuthAccessTokenType, AuthAccessWriteScope, AuthMcpClientAccess, AuthOrchestrationOperateScope, AuthAdministrativeClientScopes, AuthStandardClientScopes, type AuthAccessTokenResult, type AuthBrowserSessionResult, type AuthClientMetadata, type AuthClientSession, type AuthConnectDevice, type AuthCreatePairingCredentialInput, type AuthEnvironmentScope, type AuthPairingLink, type AuthPairingCredentialResult, type AuthSessionId, type AuthSessionState, authScopeResponse, type ServerAuthDescriptor, type ServerAuthSessionMethod, type AuthWebSocketTicketResult, type AuthWebSocketTicketUse, DpopFailureReason, type DpopFailureReason as DpopFailureReasonType, mcpClientScopes, } from "@t3tools/contracts"; import { isLocalLoopbackHost } from "@t3tools/shared/hostClassification"; import { encodeOAuthScope } from "@t3tools/shared/oauthScope"; import * as Context from "effect/Context"; import * as Digest from "effect/crypto/Digest"; import * as DateTime from "effect/DateTime"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Redacted from "effect/Redacted"; import * as Schema from "effect/Schema"; import * as Semaphore from "effect/Semaphore"; import * as HttpServerRequest from "effect/http/HttpServerRequest"; import * as ServerEnvironment from "../environment/ServerEnvironment.ts"; import * as ServerConfig from "../config.ts"; import * as EnvironmentAuthPolicy from "./EnvironmentAuthPolicy.ts"; import * as PairingGrantStore from "./PairingGrantStore.ts"; import * as ServerSecretStore from "./ServerSecretStore.ts"; import * as SessionStore from "./SessionStore.ts"; import { REUSABLE_DEV_SESSION_EXPIRES_AT, resolveReusableDevAuth } from "./ReusableDevAuth.ts"; import { verifyRequestDpopProof } from "./dpop.ts"; import { DESKTOP_RENDERER_ORIGINS } from "../httpCors.ts"; import * as SqlitePersistence from "../persistence/Sqlite.ts"; import * as ManagedLimits from "../policy/managedLimits.ts"; import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; import { MCP_APPROVAL_SUBJECT_PREFIX, PEER_LINK_PAIRING_SUBJECT_PREFIX, } from "../persistence/AuthPairingLinks.ts"; import { requestUrl } from "../requestUrl.ts"; const DEFAULT_SESSION_SUBJECT = "cli-issued-session"; export const INTERNAL_ADMINISTRATIVE_BOOTSTRAP_SUBJECT = "administrative-bootstrap"; /** Sessions and pairing links T3 Connect minted for a device signed in to the linked account. */ export const CLOUD_CONNECT_SUBJECT = "cloud-connect"; export interface IssuedPairingLink { readonly id: string; readonly credential: string; readonly scopes: ReadonlyArray; readonly subject: string; readonly label?: string; readonly createdAt: DateTime.Utc; readonly expiresAt: DateTime.Utc; } export interface IssuedBearerSession { readonly sessionId: AuthSessionId; readonly token: string; readonly method: "bearer-access-token"; readonly scopes: ReadonlyArray; readonly subject: string; readonly client: AuthClientMetadata; readonly expiresAt: DateTime.Utc; } // MCP client tokens are restricted to /mcp; HTTP API and WebSocket RPC would bypass their access cap. const MCP_CLIENT_SUBJECT = "mcp-client"; const MCP_CLIENT_SESSION_TTL = Duration.days(30); // A peer link's session moves back out to its full lifetime when a call finds it this far // short, so a used link never expires and a busy one writes at most once per step. const PEER_LINK_RENEWAL_STEP = Duration.days(1); // A device pairing with its DPoP key renews the session itself, so the token can be short and // the device stays signed in while used. One idle for the whole window pairs again. const RENEWABLE_ACCESS_TOKEN_TTL = Duration.hours(1); const RENEWABLE_SESSION_WINDOW = Duration.days(30); const PAIRING_LINK_SUBJECT = "one-time-token"; export interface McpClientSession { readonly sessionId: AuthSessionId; readonly label: string; readonly access: AuthMcpClientAccess; /** Another T3 Code environment's link, whose work here stays within it. */ readonly peerLink: boolean; /** When a peer link's session ends unless it is used again; the linking side keeps this. */ readonly linkExpiresAt?: DateTime.Utc; } export interface AuthenticatedSession { readonly sessionId: AuthSessionId; readonly subject: string; readonly method: ServerAuthSessionMethod; readonly scopes: ReadonlyArray; readonly proofKeyThumbprint?: string; readonly expiresAt?: DateTime.DateTime; } const serverAuthInternalErrorContext = { cause: Schema.Defect(), }; export class ServerAuthBootstrapCredentialValidationError extends Schema.TaggedError()( "ServerAuthBootstrapCredentialValidationError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to validate bootstrap credential."; } } export class ServerAuthSessionCredentialValidationError extends Schema.TaggedError()( "ServerAuthSessionCredentialValidationError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to validate session credential."; } } export class ServerAuthAuthenticatedSessionIssueError extends Schema.TaggedError()( "ServerAuthAuthenticatedSessionIssueError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to issue authenticated session."; } } export class ServerAuthAuthenticatedAccessTokenIssueError extends Schema.TaggedError()( "ServerAuthAuthenticatedAccessTokenIssueError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to issue authenticated access token."; } } export class ServerAuthPairingLinkCreationError extends Schema.TaggedError()( "ServerAuthPairingLinkCreationError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to create pairing link."; } } export class ServerAuthPairingLinksListError extends Schema.TaggedError()( "ServerAuthPairingLinksListError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to list pairing links."; } } export class ServerAuthPairingLinkRevocationError extends Schema.TaggedError()( "ServerAuthPairingLinkRevocationError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to revoke pairing link."; } } export class ServerAuthSessionTokenIssueError extends Schema.TaggedError()( "ServerAuthSessionTokenIssueError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to issue session token."; } } export class ServerAuthSessionsListError extends Schema.TaggedError()( "ServerAuthSessionsListError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to list sessions."; } } export class ServerAuthSessionRevocationError extends Schema.TaggedError()( "ServerAuthSessionRevocationError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to revoke session."; } } export class ServerAuthOtherSessionsRevocationError extends Schema.TaggedError()( "ServerAuthOtherSessionsRevocationError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to revoke other sessions."; } } export class ServerAuthWebSocketTokenIssueError extends Schema.TaggedError()( "ServerAuthWebSocketTokenIssueError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to issue websocket token."; } } export class ServerAuthDpopReplayStateRecordError extends Schema.TaggedError()( "ServerAuthDpopReplayStateRecordError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to record DPoP proof replay state."; } } export class ServerAuthDpopReplayKeyCalculationError extends Schema.TaggedError()( "ServerAuthDpopReplayKeyCalculationError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to calculate DPoP replay key."; } } export class ServerAuthLinkedCloudAccountVerificationError extends Schema.TaggedError()( "ServerAuthLinkedCloudAccountVerificationError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Could not verify the linked cloud account."; } } export class ServerAuthLinkedCloudAccountReadError extends Schema.TaggedError()( "ServerAuthLinkedCloudAccountReadError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Could not read the linked cloud account."; } } export class ServerAuthLinkedCloudAccountMissingError extends Schema.TaggedError()( "ServerAuthLinkedCloudAccountMissingError", {}, ) { override get message(): string { return "Cloud linked user is not installed for this environment."; } } export class ServerAuthCloudLinkJwtSigningError extends Schema.TaggedError()( "ServerAuthCloudLinkJwtSigningError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to sign cloud link JWT."; } } export class ServerAuthCloudMintPublicKeyMissingError extends Schema.TaggedError()( "ServerAuthCloudMintPublicKeyMissingError", {}, ) { override get message(): string { return "Cloud mint public key is not installed for this environment."; } } export class ServerAuthCloudRelayIssuerMissingError extends Schema.TaggedError()( "ServerAuthCloudRelayIssuerMissingError", {}, ) { override get message(): string { return "Cloud relay issuer is not installed for this environment."; } } export class ServerAuthCloudHealthJwtSigningError extends Schema.TaggedError()( "ServerAuthCloudHealthJwtSigningError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to sign cloud health JWT."; } } export class ServerAuthCloudMintJwtSigningError extends Schema.TaggedError()( "ServerAuthCloudMintJwtSigningError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to sign cloud mint JWT."; } } export class ServerAuthConnectDevicesError extends Schema.TaggedError()( "ServerAuthConnectDevicesError", { ...serverAuthInternalErrorContext, }, ) { override get message(): string { return "Failed to update T3 Connect devices."; } } export const ServerAuthInternalError = Schema.Union([ ServerAuthBootstrapCredentialValidationError, ServerAuthSessionCredentialValidationError, ServerAuthAuthenticatedSessionIssueError, ServerAuthAuthenticatedAccessTokenIssueError, ServerAuthPairingLinkCreationError, ServerAuthPairingLinksListError, ServerAuthPairingLinkRevocationError, ServerAuthSessionTokenIssueError, ServerAuthSessionsListError, ServerAuthSessionRevocationError, ServerAuthOtherSessionsRevocationError, ServerAuthWebSocketTokenIssueError, ServerAuthDpopReplayStateRecordError, ServerAuthDpopReplayKeyCalculationError, ServerAuthLinkedCloudAccountVerificationError, ServerAuthLinkedCloudAccountReadError, ServerAuthLinkedCloudAccountMissingError, ServerAuthCloudLinkJwtSigningError, ServerAuthCloudMintPublicKeyMissingError, ServerAuthCloudRelayIssuerMissingError, ServerAuthCloudHealthJwtSigningError, ServerAuthCloudMintJwtSigningError, ServerAuthConnectDevicesError, ]); export type ServerAuthInternalError = typeof ServerAuthInternalError.Type; export const isServerAuthInternalError = Schema.is(ServerAuthInternalError); export class ServerAuthMissingCredentialError extends Schema.TaggedError()( "ServerAuthMissingCredentialError", {}, ) { override get message(): string { return "Server authentication credential is missing."; } } export class ServerAuthInvalidCredentialError extends Schema.TaggedError()( "ServerAuthInvalidCredentialError", { diagnostic: Schema.optional(Schema.String), dpopFailureReason: Schema.optionalKey(DpopFailureReason), cause: Schema.optional(Schema.Defect()), }, ) { override get message(): string { return "Server authentication credential is invalid."; } } export const ServerAuthCredentialError = Schema.Union([ ServerAuthMissingCredentialError, ServerAuthInvalidCredentialError, ]); export type ServerAuthCredentialError = typeof ServerAuthCredentialError.Type; export const isServerAuthCredentialError = Schema.is(ServerAuthCredentialError); export const serverAuthCredentialReason = ( error: ServerAuthCredentialError, ): "missing_credential" | "invalid_credential" => error._tag === "ServerAuthMissingCredentialError" ? "missing_credential" : "invalid_credential"; export const serverAuthDpopFailureReason = ( error: ServerAuthCredentialError, ): DpopFailureReasonType | undefined => error._tag === "ServerAuthInvalidCredentialError" ? error.dpopFailureReason : undefined; export class ServerAuthInvalidScopeError extends Schema.TaggedError()( "ServerAuthInvalidScopeError", {}, ) { override get message(): string { return "The requested authentication scope is invalid."; } } export class ServerAuthScopeNotGrantedError extends Schema.TaggedError()( "ServerAuthScopeNotGrantedError", {}, ) { override get message(): string { return "The requested authentication scope was not granted."; } } export const ServerAuthInvalidRequestError = Schema.Union([ ServerAuthInvalidScopeError, ServerAuthScopeNotGrantedError, ]); export type ServerAuthInvalidRequestError = typeof ServerAuthInvalidRequestError.Type; export const isServerAuthInvalidRequestError = Schema.is(ServerAuthInvalidRequestError); export const serverAuthInvalidRequestReason = ( error: ServerAuthInvalidRequestError, ): "invalid_scope" | "scope_not_granted" => error._tag === "ServerAuthInvalidScopeError" ? "invalid_scope" : "scope_not_granted"; /** The credential is fine, but the host has not approved this T3 Connect device yet. */ export class ServerAuthConnectApprovalPendingError extends Schema.TaggedError()( "ServerAuthConnectApprovalPendingError", {}, ) { override get message(): string { return "This T3 Connect device is waiting for approval."; } } export class ServerAuthMcpApprovalCodeError extends Schema.TaggedError()( "ServerAuthMcpApprovalCodeError", { reason: Schema.Literals(["unknown_or_used", "not_a_pairing_code", "insufficient_scope"]) }, ) { override get message(): string { return this.reason === "insufficient_scope" ? "That pairing code cannot grant this access. It has not been used, so choose an access level it grants, or create a code with the standard scopes." : this.reason === "not_a_pairing_code" ? "That is not a one-time pairing code." : "That pairing code is unknown, expired, or already used."; } } export class ServerAuthForbiddenOperationError extends Schema.TaggedError()( "ServerAuthForbiddenOperationError", {}, ) { override get message(): string { return "The current authentication session cannot revoke itself."; } } export class EnvironmentAuth extends Context.Service< EnvironmentAuth, { readonly getDescriptor: () => Effect.Effect; readonly getSessionState: ( request: HttpServerRequest.HttpServerRequest, ) => Effect.Effect; readonly createBrowserSession: ( credential: string, requestMetadata: AuthClientMetadata, previousSessionToken?: string, ) => Effect.Effect< { readonly response: AuthBrowserSessionResult; readonly sessionToken: string; readonly cookieName?: string; readonly expireNormalCookie?: boolean; }, ServerAuthInvalidCredentialError | ServerAuthInternalError >; readonly exchangeBootstrapCredentialForAccessToken: ( credential: string, requestedScopes: ReadonlyArray | undefined, requestMetadata: AuthClientMetadata, input?: { readonly proofKeyThumbprint?: string; }, ) => Effect.Effect< AuthAccessTokenResult, | ServerAuthInvalidCredentialError | ServerAuthInvalidRequestError | ServerAuthConnectApprovalPendingError | ServerAuthInternalError >; readonly renewAccessToken: ( accessToken: string, proofKeyThumbprint: string, consumeProof: Effect.Effect, ) => Effect.Effect< AuthAccessTokenResult, ServerAuthInvalidCredentialError | ServerAuthInternalError >; readonly createPairingLink: (input?: { readonly ttl?: Duration.Duration; readonly label?: string; readonly scopes?: ReadonlyArray; readonly subject?: string; readonly proofKeyThumbprint?: string; readonly purpose?: "startup"; readonly mcpApproval?: AuthMcpClientAccess; readonly requiresConnectApproval?: boolean; }) => Effect.Effect< IssuedPairingLink, ServerAuthInternalError | ManagedLimits.ManagedPolicyRefusedError >; /** * The one-time credential T3 Connect hands a device. With approval required, an untrusted * device key gets one that waits for approval, and keeps it across retries. */ readonly issueConnectCredential: (input: { readonly proofKeyThumbprint: string; readonly requireApproval: boolean; }) => Effect.Effect< { readonly credential: string; readonly expiresAt: DateTime.Utc }, ServerAuthInternalError | ManagedLimits.ManagedPolicyRefusedError >; readonly approveConnectDevice: ( pairingLinkId: string, ) => Effect.Effect; /** Pairing link ids of waiting devices that show this code; more than one is ambiguous. */ readonly connectRequestsShowingCode: ( verificationCode: string, ) => Effect.Effect, ServerAuthInternalError>; /** Trusted devices that show this code; more than one is ambiguous. */ readonly connectDevicesShowingCode: ( verificationCode: string, ) => Effect.Effect, ServerAuthInternalError>; readonly listConnectDevices: () => Effect.Effect< ReadonlyArray, ServerAuthInternalError >; /** Stops trusting a device key and ends the sessions it holds. */ readonly revokeConnectDevice: ( proofKeyThumbprint: string, ) => Effect.Effect; readonly issuePairingCredential: ( input?: AuthCreatePairingCredentialInput, ) => Effect.Effect< AuthPairingCredentialResult, ServerAuthInternalError | ManagedLimits.ManagedPolicyRefusedError >; readonly issueStartupPairingCredential: () => Effect.Effect< AuthPairingCredentialResult, ServerAuthInternalError >; readonly listPairingLinks: (input?: { readonly excludeSubjects?: ReadonlyArray; }) => Effect.Effect, ServerAuthInternalError>; readonly revokePairingLink: (id: string) => Effect.Effect; readonly issueSession: (input?: { readonly ttl?: Duration.Duration; readonly subject?: string; readonly scopes?: ReadonlyArray; readonly label?: string; }) => Effect.Effect; readonly listSessions: () => Effect.Effect< ReadonlyArray, ServerAuthInternalError >; readonly revokeSession: ( sessionId: AuthSessionId, ) => Effect.Effect; readonly listClientSessions: ( currentSessionId: AuthSessionId, ) => Effect.Effect, ServerAuthInternalError>; /** Revokes a session; one bound to a device key takes the device's trust and sessions with it. */ readonly revokeClient: ( sessionId: AuthSessionId, ) => Effect.Effect; readonly revokeClientSession: ( currentSessionId: AuthSessionId, targetSessionId: AuthSessionId, ) => Effect.Effect; readonly revokeOtherClientSessions: ( currentSessionId: AuthSessionId, ) => Effect.Effect; readonly authenticateHttpRequest: ( request: HttpServerRequest.HttpServerRequest, ) => Effect.Effect; readonly authenticateWebSocketUpgrade: ( request: HttpServerRequest.HttpServerRequest, use: AuthWebSocketTicketUse, ) => Effect.Effect; readonly issueWebSocketTicket: ( session: Pick, input?: { readonly use?: AuthWebSocketTicketUse }, ) => Effect.Effect; readonly issueStartupPairingUrl: ( baseUrl: string, ) => Effect.Effect; readonly authenticateMcpClient: ( request: HttpServerRequest.HttpServerRequest, ) => Effect.Effect; readonly issueMcpClientSession: (input: { readonly label: string; readonly access: AuthMcpClientAccess; readonly peerLink: boolean; readonly client: AuthClientMetadata; }) => Effect.Effect< { readonly sessionId: AuthSessionId; readonly token: string; readonly expiresAt: DateTime.DateTime; }, ServerAuthInternalError >; /** * Spends a code covering all granted scopes and returns the level it approves. A level-bound * code must name `access`. Rejects desktop grants and leaves refused codes unspent. */ readonly consumeMcpApprovalCode: ( code: string, access: AuthMcpClientAccess, peerLink?: boolean, ) => Effect.Effect< AuthMcpClientAccess, ServerAuthMcpApprovalCodeError | ServerAuthInternalError >; } >()("t3/auth/EnvironmentAuth") {} type BootstrapExchangeResult = { readonly response: AuthBrowserSessionResult; readonly sessionToken: string; readonly cookieName?: string; readonly expireNormalCookie?: boolean; }; const AUTHORIZATION_PREFIX = "Bearer "; const DPOP_AUTHORIZATION_PREFIX = "DPoP "; const WebSocketUpgradeQuery = Schema.Struct({ wsTicket: Schema.optionalKey(Schema.RedactedFromValue(Schema.String)), dpop: Schema.optionalKey(Schema.RedactedFromValue(Schema.String)), }); const decodeWebSocketUpgradeQuery = Schema.decodeEffect(WebSocketUpgradeQuery); const bySessionPriority = (left: AuthClientSession, right: AuthClientSession) => { const leftCanManage = left.scopes.includes(AuthAccessWriteScope); const rightCanManage = right.scopes.includes(AuthAccessWriteScope); if (leftCanManage !== rightCanManage) { return leftCanManage ? -1 : 1; } if (left.connected !== right.connected) { return left.connected ? -1 : 1; } return right.issuedAt.epochMilliseconds - left.issuedAt.epochMilliseconds; }; function toBootstrapExchangeError( cause: PairingGrantStore.BootstrapCredentialError, ): ServerAuthInvalidCredentialError | ServerAuthInternalError { if (PairingGrantStore.isBootstrapCredentialInternalError(cause)) { return new ServerAuthBootstrapCredentialValidationError({ cause }); } return new ServerAuthInvalidCredentialError({ cause, }); } const rejectMcpClientAudience = (session: S) => session.subject === MCP_CLIENT_SUBJECT ? Effect.fail( new ServerAuthInvalidCredentialError({ diagnostic: "MCP client sessions are only accepted by the MCP endpoint.", }), ).pipe(Effect.tap(() => Effect.logWarning("Rejected an MCP client session outside /mcp."))) : Effect.succeed(session); const toSessionVerificationError = (cause: SessionStore.SessionCredentialError) => SessionStore.isSessionCredentialInvalidError(cause) ? new ServerAuthInvalidCredentialError({ cause }) : new ServerAuthSessionCredentialValidationError({ cause }); const mapSessionVerificationErrors = ( effect: Effect.Effect, ): Effect.Effect => effect.pipe(Effect.mapError(toSessionVerificationError)); export function parseBearerToken(request: HttpServerRequest.HttpServerRequest): string | null { const header = request.headers["authorization"]; if (typeof header !== "string" || !/^Bearer /i.test(header)) { return null; } const token = header.slice(AUTHORIZATION_PREFIX.length).trim(); return token.length > 0 ? token : null; } function parseDpopToken(request: HttpServerRequest.HttpServerRequest): string | null { const header = request.headers["authorization"]; if (typeof header !== "string" || !/^DPoP /i.test(header)) { return null; } const token = header.slice(DPOP_AUTHORIZATION_PREFIX.length).trim(); return token.length > 0 ? token : null; } export function selectRequestCredential( request: HttpServerRequest.HttpServerRequest, cookieName: string, legacyCookieName: string | undefined, ) { const cookieToken = request.cookies[cookieName]; if (cookieToken) { return { token: cookieToken, source: "cookie" } as const; } const bearerToken = parseBearerToken(request); if (bearerToken !== null) { return { token: bearerToken, source: "bearer" } as const; } const dpopToken = parseDpopToken(request); if (dpopToken !== null) { return { token: dpopToken, source: "dpop" } as const; } const legacyToken = legacyCookieName ? request.cookies[legacyCookieName] : undefined; if (legacyToken !== undefined) { return { token: legacyToken, source: "legacy-cookie" } as const; } return undefined; } type RequestCredentialSource = NonNullable>["source"]; export const isBrowserCookieCredential = (source: RequestCredentialSource | "dev-cookie") => source === "cookie" || source === "legacy-cookie" || source === "dev-cookie"; function firstHeaderValue(value: string | undefined): string | undefined { const first = value?.split(",")[0]?.trim().toLowerCase(); return first && first.length > 0 ? first : undefined; } // Missing Origin allows same-origin reads and non-browser clients; otherwise require // same-origin Sec-Fetch-Site, this server's host (including X-Forwarded-Host), or a trusted app origin. function isBrowserOriginAllowed( request: HttpServerRequest.HttpServerRequest, trustedOrigins: ReadonlySet, ): boolean { const origin = request.headers.origin; if (origin === undefined) return true; if (request.headers["sec-fetch-site"] === "same-origin") return true; if (trustedOrigins.has(origin)) return true; let originHost: string; try { originHost = new URL(origin).host.toLowerCase(); } catch { return false; } if (originHost === "") return false; return ( originHost === firstHeaderValue(request.headers.host) || originHost === firstHeaderValue(request.headers["x-forwarded-host"]) ); } /** @public */ export const make = Effect.gen(function* () { const policy = yield* EnvironmentAuthPolicy.EnvironmentAuthPolicy; const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore; const sessions = yield* SessionStore.SessionStore; const secretStore = yield* ServerSecretStore.ServerSecretStore; const digest = yield* Digest.Digest; const descriptor = yield* policy.getDescriptor(); const config = yield* ServerConfig.ServerConfig; const managedPolicy = yield* ManagedPolicy.ManagedPolicy; const devAuth = resolveReusableDevAuth(config); const authenticateToken = ( token: string, ): Effect.Effect< AuthenticatedSession, ServerAuthInvalidCredentialError | ServerAuthInternalError > => sessions.verify(token).pipe( Effect.tapError((cause) => SessionStore.isSessionCredentialInvalidError(cause) ? Effect.logWarning("Rejected authenticated session credential.").pipe( Effect.annotateLogs({ reason: cause.message, }), ) : Effect.void, ), mapSessionVerificationErrors, Effect.flatMap(rejectMcpClientAudience), Effect.map((session) => ({ sessionId: session.sessionId, subject: session.subject, method: session.method, scopes: session.scopes, ...(session.proofKeyThumbprint ? { proofKeyThumbprint: session.proofKeyThumbprint } : {}), ...(session.expiresAt ? { expiresAt: session.expiresAt } : {}), })), ); const resolveRequestCredential = (request: HttpServerRequest.HttpServerRequest) => { const selectedCredential = selectRequestCredential( request, sessions.cookieName, sessions.legacyCookieName, ); const hasAuthorization = request.headers.authorization !== undefined; const devCookieToken = devAuth ? request.cookies[devAuth.cookieName] : undefined; return ( selectedCredential ?? (!hasAuthorization && devCookieToken !== undefined ? { token: devCookieToken, source: "dev-cookie" as const } : undefined) ); }; // Cookies span ports; reject cookie-authenticated requests and sockets from other same-site origins. const trustedBrowserOrigins = new Set([ ...(config.devUrl ? [config.devUrl.origin] : []), ...DESKTOP_RENDERER_ORIGINS, ...config.devAllowedOrigins, ]); const authenticateRequest = ( request: HttpServerRequest.HttpServerRequest, ): Effect.Effect => { const dpopToken = parseDpopToken(request); const credential = resolveRequestCredential(request); if (!credential?.token) { return Effect.fail(new ServerAuthMissingCredentialError({})); } if ( isBrowserCookieCredential(credential.source) && !isBrowserOriginAllowed(request, trustedBrowserOrigins) ) { return Effect.fail( new ServerAuthInvalidCredentialError({ diagnostic: "Browser sessions are only accepted from the app's own origin.", }), ); } return authenticateToken(credential.token).pipe( Effect.flatMap((session) => { if (session.proofKeyThumbprint) { if (!dpopToken || dpopToken !== credential.token) { return Effect.fail( new ServerAuthInvalidCredentialError({ diagnostic: "DPoP-bound access token requires DPoP authorization.", dpopFailureReason: "invalid_proof", }), ); } return verifyRequestDpopProof({ request, expectedThumbprint: session.proofKeyThumbprint, expectedAccessToken: dpopToken, }).pipe( Effect.provideService(ServerSecretStore.ServerSecretStore, secretStore), Effect.provideService(Digest.Digest, digest), Effect.as(session), ); } if (dpopToken) { return Effect.fail( new ServerAuthInvalidCredentialError({ diagnostic: "DPoP authorization requires a proof-bound access token.", dpopFailureReason: "invalid_proof", }), ); } return Effect.succeed(session); }), ); }; const getSessionState: EnvironmentAuth["Service"]["getSessionState"] = (request) => authenticateRequest(request).pipe( Effect.map( (session) => ({ authenticated: true, auth: descriptor, ...authScopeResponse(session.scopes), sessionMethod: session.method, ...(session.expiresAt ? { expiresAt: DateTime.toUtc(session.expiresAt) } : {}), }) satisfies AuthSessionState, ), Effect.catchIf(isServerAuthCredentialError, () => Effect.succeed({ authenticated: false, auth: descriptor, } satisfies AuthSessionState), ), Effect.withSpan("EnvironmentAuth.getSessionState"), ); const createBrowserSession: EnvironmentAuth["Service"]["createBrowserSession"] = Effect.fn( "EnvironmentAuth.createBrowserSession", )(function* (credential, requestMetadata, previousSessionToken) { if (devAuth?.matches(credential)) { return yield* sessions.verify(credential).pipe( mapSessionVerificationErrors, Effect.flatMap((session) => DateTime.now.pipe( Effect.map( (now) => ({ response: { authenticated: true, ...authScopeResponse(session.scopes), sessionMethod: session.method, expiresAt: DateTime.toUtc(DateTime.add(now, { days: 30 })), } satisfies AuthBrowserSessionResult, sessionToken: credential, cookieName: devAuth.cookieName, expireNormalCookie: true, }) satisfies BootstrapExchangeResult, ), ), ), Effect.withSpan("EnvironmentAuth.createBrowserSession"), ); } const previousSession = previousSessionToken === undefined ? undefined : yield* sessions.verify(previousSessionToken).pipe( Effect.catchIf(SessionStore.isSessionCredentialInvalidError, () => Effect.void), Effect.mapError((cause) => new ServerAuthSessionCredentialValidationError({ cause })), ); const grant = yield* bootstrapCredentials .consume(credential) .pipe(Effect.mapError(toBootstrapExchangeError)); const session = yield* sessions .issue({ method: "browser-session-cookie", subject: grant.subject, scopes: grant.scopes, ...(previousSession?.method === "browser-session-cookie" ? { replaceSessionId: previousSession.sessionId } : {}), client: { ...requestMetadata, ...(grant.label ? { label: grant.label } : {}), }, }) .pipe(Effect.mapError((cause) => new ServerAuthAuthenticatedSessionIssueError({ cause }))); return { response: { authenticated: true, ...authScopeResponse(session.scopes), sessionMethod: session.method, expiresAt: DateTime.toUtc(session.expiresAt), } satisfies AuthBrowserSessionResult, sessionToken: session.token, } satisfies BootstrapExchangeResult; }); type ResolvedBootstrapGrant = Pick< PairingGrantStore.BootstrapGrant, "scopes" | "subject" | "label" > & { readonly method: PairingGrantStore.BootstrapGrant["method"] | "reusable-dev-token"; }; const resolveBootstrapGrant = ( credential: string, input?: { readonly proofKeyThumbprint?: string; readonly requestedScopes?: ReadonlyArray; readonly requiredScopes?: ReadonlyArray; readonly allowedSubject?: string; /** Shown to approvers when the credential waits on T3 Connect device approval. */ readonly requestMetadata?: AuthClientMetadata; }, ): Effect.Effect< ResolvedBootstrapGrant, | ServerAuthInvalidCredentialError | ServerAuthInternalError | ServerAuthScopeNotGrantedError | ServerAuthConnectApprovalPendingError > => { if (!devAuth?.matches(credential)) { return bootstrapCredentials.consume(credential, input).pipe( Effect.catchTags({ BootstrapCredentialConnectApprovalPendingError: (pending) => recordConnectRequest(pending.pairingLinkId, input?.requestMetadata).pipe( Effect.andThen(Effect.fail(new ServerAuthConnectApprovalPendingError({}))), ), }), Effect.mapError((cause) => cause._tag === "BootstrapCredentialScopeNotGrantedError" ? new ServerAuthScopeNotGrantedError({}) : cause._tag === "ServerAuthConnectApprovalPendingError" || cause._tag === "ServerAuthConnectDevicesError" ? cause : toBootstrapExchangeError(cause), ), ); } return sessions.verify(credential).pipe( mapSessionVerificationErrors, Effect.filterOrFail( (session) => input?.requiredScopes === undefined || input.requiredScopes.every((scope) => session.scopes.includes(scope)), () => new ServerAuthScopeNotGrantedError({}), ), Effect.map( (session) => ({ method: "reusable-dev-token", scopes: session.scopes, subject: "reusable-dev-token-child", }) satisfies ResolvedBootstrapGrant, ), ); }; // T3 Connect credential issuance, DPoP exchanges and device revocation take turns. Two mints // at once would open two requests with one code, and a revoke landing between an exchange // spending its credential and trusting the key would be undone by that exchange. const connectTrustLock = yield* Semaphore.make(1); const toAccessTokenResult = (session: SessionStore.IssuedSession) => DateTime.now.pipe( Effect.map( (now) => ({ access_token: session.token, issued_token_type: AuthAccessTokenType, token_type: session.proofKeyThumbprint ? "DPoP" : "Bearer", expires_in: Math.max( 0, Math.floor((session.expiresAt.epochMilliseconds - now.epochMilliseconds) / 1000), ), scope: encodeOAuthScope(session.scopes), }) satisfies AuthAccessTokenResult, ), ); const renewAccessToken: EnvironmentAuth["Service"]["renewAccessToken"] = Effect.fn( "EnvironmentAuth.renewAccessToken", )(function* (accessToken, proofKeyThumbprint, consumeProof) { const session = yield* sessions .renew(accessToken, { proofKeyThumbprint, ttl: RENEWABLE_ACCESS_TOKEN_TTL, renewalWindow: RENEWABLE_SESSION_WINDOW, consumeProof, }) .pipe( Effect.mapError((cause) => isServerAuthCredentialError(cause) || isServerAuthInternalError(cause) ? cause : toSessionVerificationError(cause), ), ); return yield* toAccessTokenResult(session); }); const exchangeBootstrapCredentialForAccessToken: EnvironmentAuth["Service"]["exchangeBootstrapCredentialForAccessToken"] = (credential, requestedScopes, requestMetadata, input) => { return resolveBootstrapGrant(credential, { ...input, requestMetadata, ...(requestedScopes !== undefined ? { requestedScopes } : {}), }).pipe( // Every T3 Connect key that gets in is remembered, so turning approval on later // keeps the devices already in use and lists them for review. Effect.tap((grant) => grant.subject === CLOUD_CONNECT_SUBJECT && input?.proofKeyThumbprint ? bootstrapCredentials .trustConnectDevice(input.proofKeyThumbprint, requestMetadata) .pipe(Effect.mapError((cause) => new ServerAuthConnectDevicesError({ cause }))) : Effect.void, ), Effect.flatMap((grant) => Effect.gen(function* () { const grantedScopes = requestedScopes === undefined ? grant.scopes : [...new Set(requestedScopes)].filter((scope) => grant.scopes.includes(scope)); if (grantedScopes.length === 0) { return yield* new ServerAuthScopeNotGrantedError({}); } // Only plain pairing links renew. T3 Connect, CLI, desktop and peer sessions keep // their own lifetimes, so unlinking Connect or rotating a credential still cuts off. const renewable = input?.proofKeyThumbprint !== undefined && grant.method === "one-time-token" && grant.subject === PAIRING_LINK_SUBJECT; return yield* sessions .issue({ method: input?.proofKeyThumbprint ? "dpop-access-token" : "bearer-access-token", subject: grant.subject, scopes: grantedScopes, ...(input?.proofKeyThumbprint ? { proofKeyThumbprint: input.proofKeyThumbprint, ttl: renewable ? RENEWABLE_ACCESS_TOKEN_TTL : Duration.hours(1), } : {}), ...(renewable ? { renewalWindow: RENEWABLE_SESSION_WINDOW } : {}), replaceActiveForSubjectAndMethod: grant.method === "desktop-bootstrap", client: { ...requestMetadata, ...(grant.label ? { label: grant.label } : {}), }, }) .pipe( Effect.mapError( (cause) => new ServerAuthAuthenticatedAccessTokenIssueError({ cause }), ), ); }), ), Effect.flatMap(toAccessTokenResult), (exchange) => input?.proofKeyThumbprint ? connectTrustLock.withPermits(1)(exchange) : exchange, Effect.withSpan("EnvironmentAuth.exchangeBootstrapCredentialForAccessToken"), ); }; const toPairingCredential = (issued: IssuedPairingLink): AuthPairingCredentialResult => ({ id: issued.id, credential: issued.credential, ...(issued.label ? { label: issued.label } : {}), expiresAt: issued.expiresAt, }); const issuePairingLink = Effect.fn("EnvironmentAuth.issuePairingLink")( function* (input: Parameters[0]) { const createdAt = yield* DateTime.now; const issued = yield* bootstrapCredentials.issueOneTimeToken({ scopes: input?.scopes ?? AuthStandardClientScopes, subject: input?.subject ?? "one-time-token", ...(input?.ttl ? { ttl: input.ttl } : {}), ...(input?.label ? { label: input.label } : {}), ...(input?.proofKeyThumbprint ? { proofKeyThumbprint: input.proofKeyThumbprint } : {}), ...(input?.purpose ? { purpose: input.purpose } : {}), ...(input?.mcpApproval ? { mcpApproval: input.mcpApproval } : {}), ...(input?.requiresConnectApproval ? { requiresConnectApproval: true } : {}), }); return { id: issued.id, credential: issued.credential, scopes: input?.scopes ?? AuthStandardClientScopes, subject: input?.subject ?? "one-time-token", ...(issued.label ? { label: issued.label } : {}), createdAt: DateTime.toUtc(createdAt), expiresAt: DateTime.toUtc(issued.expiresAt), } satisfies IssuedPairingLink; }, Effect.mapError((cause) => new ServerAuthPairingLinkCreationError({ cause })), ); const createPairingLink: EnvironmentAuth["Service"]["createPairingLink"] = Effect.fn( "EnvironmentAuth.createPairingLink", )(function* (input) { // The startup link is for a browser on this machine; every other link lets a device pair. if (input?.purpose !== "startup" && !ManagedLimits.allowsRemoteClients(managedPolicy)) { return yield* ManagedLimits.refuseRemoteClients( managedPolicy, "so this server cannot create pairing links", ); } return yield* issuePairingLink(input); }); const listPairingLinks: EnvironmentAuth["Service"]["listPairingLinks"] = (input) => bootstrapCredentials.listActive().pipe( Effect.map((pairingLinks) => { const excludedSubjects = input?.excludeSubjects ?? [ INTERNAL_ADMINISTRATIVE_BOOTSTRAP_SUBJECT, ]; return pairingLinks .filter( (pairingLink) => !excludedSubjects.includes(pairingLink.subject) && !pairingLink.subject.startsWith(MCP_APPROVAL_SUBJECT_PREFIX), ) .map((link) => ({ ...link, ...authScopeResponse(link.scopes) })) .toSorted( (left, right) => right.createdAt.epochMilliseconds - left.createdAt.epochMilliseconds, ); }), Effect.mapError((cause) => new ServerAuthPairingLinksListError({ cause })), Effect.withSpan("EnvironmentAuth.listPairingLinks"), ); const revokePairingLink: EnvironmentAuth["Service"]["revokePairingLink"] = (id) => bootstrapCredentials.revoke(id).pipe( Effect.mapError((cause) => new ServerAuthPairingLinkRevocationError({ cause })), Effect.withSpan("EnvironmentAuth.revokePairingLink"), ); const recordConnectRequest = (pairingLinkId: string, client: AuthClientMetadata | undefined) => client === undefined ? Effect.void : bootstrapCredentials .recordConnectRequestClient(pairingLinkId, client) .pipe(Effect.mapError((cause) => new ServerAuthConnectDevicesError({ cause }))); const issueConnectCredential: EnvironmentAuth["Service"]["issueConnectCredential"] = Effect.fn( "EnvironmentAuth.issueConnectCredential", )(function* ({ proofKeyThumbprint, requireApproval }) { return yield* issueConnectCredentialLocked({ proofKeyThumbprint, requireApproval }).pipe( connectTrustLock.withPermits(1), ); }); // Untraced: the issueConnectCredential span above already covers it. const issueConnectCredentialLocked = Effect.fnUntraced(function* ({ proofKeyThumbprint, requireApproval, }: Parameters[0]) { const mustWait = requireApproval && !(yield* bootstrapCredentials .isConnectDeviceTrusted(proofKeyThumbprint) .pipe(Effect.mapError((cause) => new ServerAuthConnectDevicesError({ cause })))); if (mustWait) { const pending = yield* bootstrapCredentials .findPendingConnect(proofKeyThumbprint) .pipe(Effect.mapError((cause) => new ServerAuthConnectDevicesError({ cause }))); if (Option.isSome(pending)) { return { credential: pending.value.credential, expiresAt: pending.value.expiresAt }; } } const issued = yield* createPairingLink({ scopes: AuthStandardClientScopes, subject: CLOUD_CONNECT_SUBJECT, // Long enough for someone to notice the request and approve it. ttl: mustWait ? Duration.minutes(10) : Duration.minutes(2), label: "T3 Connect", proofKeyThumbprint, ...(mustWait ? { requiresConnectApproval: true } : {}), }); return { credential: issued.credential, expiresAt: issued.expiresAt }; }); const approveConnectDevice: EnvironmentAuth["Service"]["approveConnectDevice"] = ( pairingLinkId, ) => bootstrapCredentials.approveConnectRequest(pairingLinkId).pipe( Effect.mapError((cause) => new ServerAuthConnectDevicesError({ cause })), Effect.withSpan("EnvironmentAuth.approveConnectDevice"), ); const connectRequestsShowingCode: EnvironmentAuth["Service"]["connectRequestsShowingCode"] = Effect.fn("EnvironmentAuth.connectRequestsShowingCode")(function* (verificationCode) { const wanted = normalizeVerificationCode(verificationCode); const links = yield* bootstrapCredentials .listActive() .pipe(Effect.mapError((cause) => new ServerAuthPairingLinksListError({ cause }))); return links.flatMap((link) => link.connectDeviceRequest !== undefined && normalizeVerificationCode(link.connectDeviceRequest.verificationCode) === wanted ? [link.id] : [], ); }); const listConnectDevices: EnvironmentAuth["Service"]["listConnectDevices"] = () => bootstrapCredentials.listConnectDevices().pipe( Effect.mapError((cause) => new ServerAuthConnectDevicesError({ cause })), Effect.withSpan("EnvironmentAuth.listConnectDevices"), ); const revokeConnectDevice: EnvironmentAuth["Service"]["revokeConnectDevice"] = Effect.fn( "EnvironmentAuth.revokeConnectDevice", )(function* (proofKeyThumbprint) { return yield* Effect.gen(function* () { const removed = yield* bootstrapCredentials .revokeConnectDevice(proofKeyThumbprint) .pipe(Effect.mapError((cause) => new ServerAuthConnectDevicesError({ cause }))); const revokedSessions = yield* sessions .revokeByProofKey(proofKeyThumbprint) .pipe(Effect.mapError((cause) => new ServerAuthSessionRevocationError({ cause }))); return removed || revokedSessions > 0; }).pipe(connectTrustLock.withPermits(1)); }); const connectDevicesShowingCode: EnvironmentAuth["Service"]["connectDevicesShowingCode"] = Effect.fn("EnvironmentAuth.connectDevicesShowingCode")(function* (verificationCode) { const wanted = normalizeVerificationCode(verificationCode); return (yield* listConnectDevices()).filter( (device) => normalizeVerificationCode(device.verificationCode) === wanted, ); }); const issueSession: EnvironmentAuth["Service"]["issueSession"] = (input) => sessions .issue({ subject: input?.subject ?? DEFAULT_SESSION_SUBJECT, method: "bearer-access-token", scopes: input?.scopes ?? AuthStandardClientScopes, client: { ...(input?.label ? { label: input.label } : {}), deviceType: "bot", }, ...(input?.ttl ? { ttl: input.ttl } : {}), }) .pipe( Effect.map( (issued) => ({ sessionId: issued.sessionId, token: issued.token, method: "bearer-access-token", scopes: issued.scopes, subject: input?.subject ?? DEFAULT_SESSION_SUBJECT, client: issued.client, expiresAt: DateTime.toUtc(issued.expiresAt), }) satisfies IssuedBearerSession, ), Effect.mapError((cause) => new ServerAuthSessionTokenIssueError({ cause })), Effect.withSpan("EnvironmentAuth.issueSession"), ); const listSessions: EnvironmentAuth["Service"]["listSessions"] = () => sessions.listActive().pipe( Effect.map((activeSessions) => activeSessions.toSorted(bySessionPriority)), Effect.mapError((cause) => new ServerAuthSessionsListError({ cause })), Effect.withSpan("EnvironmentAuth.listSessions"), ); const revokeSession: EnvironmentAuth["Service"]["revokeSession"] = (sessionId) => sessions.revoke(sessionId).pipe( Effect.mapError((cause) => new ServerAuthSessionRevocationError({ cause })), Effect.withSpan("EnvironmentAuth.revokeSession"), ); const revokeOtherSessionsExcept = (sessionId: AuthSessionId) => sessions.revokeAllExcept(sessionId).pipe( Effect.mapError((cause) => new ServerAuthOtherSessionsRevocationError({ cause })), Effect.withSpan("EnvironmentAuth.revokeOtherSessionsExcept"), ); const issuePairingCredential: EnvironmentAuth["Service"]["issuePairingCredential"] = (input) => createPairingLink({ scopes: input?.access ? mcpClientScopes(input.access) : (input?.scopes ?? AuthStandardClientScopes), subject: input?.purpose === "peer-link" && input.access ? `${PEER_LINK_PAIRING_SUBJECT_PREFIX}${input.access}` : "one-time-token", ...(input?.label ? { label: input.label } : {}), ...(input?.access && input.purpose !== "peer-link" ? { mcpApproval: input.access } : {}), }).pipe( Effect.map((issued) => ({ ...toPairingCredential(issued), ...(input?.access ? { access: input.access } : {}), ...(input?.access && input.purpose === "peer-link" ? { purpose: input.purpose } : {}), })), Effect.withSpan("EnvironmentAuth.issuePairingCredential"), ); const issueStartupPairingCredential: EnvironmentAuth["Service"]["issueStartupPairingCredential"] = () => { // Unguarded: startup already refused auth policies the managed policy leaves out. const fallback = issuePairingLink({ scopes: AuthAdministrativeClientScopes, subject: INTERNAL_ADMINISTRATIVE_BOOTSTRAP_SUBJECT, purpose: "startup", }).pipe(Effect.map(toPairingCredential)); if (!devAuth) { return fallback.pipe(Effect.withSpan("EnvironmentAuth.issueStartupPairingCredential")); } return sessions.verify(devAuth.credential).pipe( Effect.map( (session) => ({ id: session.sessionId, credential: devAuth.credential, label: "Reusable dev token", expiresAt: DateTime.toUtc(session.expiresAt ?? REUSABLE_DEV_SESSION_EXPIRES_AT), }) satisfies AuthPairingCredentialResult, ), Effect.catch((cause) => SessionStore.isSessionCredentialInvalidError(cause) ? fallback : Effect.fail(new ServerAuthPairingLinkCreationError({ cause })), ), Effect.withSpan("EnvironmentAuth.issueStartupPairingCredential"), ); }; const listClientSessions: EnvironmentAuth["Service"]["listClientSessions"] = (currentSessionId) => listSessions().pipe( Effect.map((clientSessions) => clientSessions.map((clientSession): AuthClientSession => ({ ...clientSession, ...authScopeResponse(clientSession.scopes), current: clientSession.sessionId === currentSessionId, })), ), Effect.withSpan("EnvironmentAuth.listClientSessions"), ); const proofKeyOf = (sessionId: AuthSessionId) => sessions .proofKeyThumbprintOf(sessionId) .pipe(Effect.mapError((cause) => new ServerAuthSessionRevocationError({ cause }))); const revokeClient: EnvironmentAuth["Service"]["revokeClient"] = Effect.fn( "EnvironmentAuth.revokeClient", )(function* (sessionId) { // A T3 Connect device would otherwise mint itself a new session right away. return yield* Option.match(yield* proofKeyOf(sessionId), { onNone: () => revokeSession(sessionId), onSome: revokeConnectDevice, }); }); const revokeClientSession: EnvironmentAuth["Service"]["revokeClientSession"] = Effect.fn( "EnvironmentAuth.revokeClientSession", )(function* (currentSessionId, targetSessionId) { if (currentSessionId === targetSessionId) { return yield* new ServerAuthForbiddenOperationError({}); } const targetProofKey = Option.getOrUndefined(yield* proofKeyOf(targetSessionId)); // Revoking the caller's own device would cut off the caller too. if ( targetProofKey !== undefined && Option.getOrUndefined(yield* proofKeyOf(currentSessionId)) === targetProofKey ) { return yield* revokeSession(targetSessionId); } return yield* revokeClient(targetSessionId); }); const revokeOtherClientSessions: EnvironmentAuth["Service"]["revokeOtherClientSessions"] = Effect.fn("EnvironmentAuth.revokeOtherClientSessions")(function* (currentSessionId) { const currentProofKey = yield* sessions .proofKeyThumbprintOf(currentSessionId) .pipe(Effect.mapError((cause) => new ServerAuthOtherSessionsRevocationError({ cause }))); return yield* Effect.gen(function* () { yield* bootstrapCredentials .revokeConnectDevicesExcept(Option.getOrNull(currentProofKey)) .pipe(Effect.mapError((cause) => new ServerAuthConnectDevicesError({ cause }))); return yield* revokeOtherSessionsExcept(currentSessionId); }).pipe(connectTrustLock.withPermits(1)); }); const issueStartupPairingUrl: EnvironmentAuth["Service"]["issueStartupPairingUrl"] = (baseUrl) => issueStartupPairingCredential().pipe( Effect.map((issued) => { const url = new URL(baseUrl); url.pathname = "/pair"; url.searchParams.delete("token"); url.hash = new URLSearchParams([["token", issued.credential]]).toString(); return url.toString(); }), Effect.withSpan("EnvironmentAuth.issueStartupPairingUrl"), ); // Only the RPC socket's ticket is bound to the client's proof key. The media // routes reuse one ticket across , EventSource and several sockets, and // each of those would need its own single-use proof. const issueWebSocketTicket: EnvironmentAuth["Service"]["issueWebSocketTicket"] = ( session, input, ) => sessions .issueWebSocketToken(session.sessionId, { ...(input?.use ? { audience: input.use } : {}), ...(input?.use === "rpc" && session.proofKeyThumbprint ? { proofKeyThumbprint: session.proofKeyThumbprint } : {}), }) .pipe( Effect.mapError((cause) => new ServerAuthWebSocketTokenIssueError({ cause })), Effect.map( (issued) => ({ ticket: Redacted.make(issued.token), expiresAt: DateTime.toUtc(issued.expiresAt), }) satisfies AuthWebSocketTicketResult, ), Effect.withSpan("EnvironmentAuth.issueWebSocketTicket"), ); const authenticateHttpRequest: EnvironmentAuth["Service"]["authenticateHttpRequest"] = ( request, ) => authenticateRequest(request).pipe(Effect.withSpan("EnvironmentAuth.authenticateHttpRequest")); const authenticateWebSocketUpgrade: EnvironmentAuth["Service"]["authenticateWebSocketUpgrade"] = Effect.fn("EnvironmentAuth.authenticateWebSocketUpgrade")(function* (request, use) { const url = requestUrl(request); if (Option.isSome(url)) { const query = yield* decodeWebSocketUpgradeQuery( Object.fromEntries(url.value.searchParams), ).pipe(Effect.orDie); const websocketTicket = query.wsTicket && Redacted.value(query.wsTicket); if (websocketTicket && websocketTicket.trim().length > 0) { const session = yield* sessions .verifyWebSocketToken(websocketTicket) .pipe(mapSessionVerificationErrors, Effect.flatMap(rejectMcpClientAudience)); if (session.audience !== undefined && session.audience !== use) { return yield* new ServerAuthInvalidCredentialError({ diagnostic: `WebSocket ticket was issued for the ${session.audience} route.`, }); } if (session.proofKeyThumbprint) { // WebSocket clients cannot set headers, so the proof travels in the query. // The proof hashes the ticket; consuming its jti prevents replay after first use. // Only encrypted transport prevents an active interceptor from racing that use. const proof = request.headers.dpop ?? (query.dpop && Redacted.value(query.dpop)); if (!proof) { return yield* new ServerAuthInvalidCredentialError({ diagnostic: "DPoP-bound WebSocket ticket requires a DPoP proof.", dpopFailureReason: "invalid_proof", }); } yield* verifyRequestDpopProof({ request, proof, expectedThumbprint: session.proofKeyThumbprint, expectedAccessToken: websocketTicket, }).pipe( Effect.provideService(ServerSecretStore.ServerSecretStore, secretStore), Effect.provideService(Digest.Digest, digest), ); } return { sessionId: session.sessionId, subject: session.subject, method: session.method, scopes: session.scopes, ...(session.proofKeyThumbprint ? { proofKeyThumbprint: session.proofKeyThumbprint } : {}), ...(session.expiresAt ? { expiresAt: session.expiresAt } : {}), }; } } return yield* authenticateRequest(request); }); /** The expiry of a verified peer link session, moved out when this use finds it a step short. */ const renewPeerLinkSession = Effect.fnUntraced(function* ( sessionId: AuthSessionId, expiresAt: DateTime.DateTime, ) { const now = yield* DateTime.now; const renewed = DateTime.add(now, { milliseconds: Duration.toMillis(MCP_CLIENT_SESSION_TTL), }); if (DateTime.distance(expiresAt, renewed).pipe(Duration.isLessThan(PEER_LINK_RENEWAL_STEP))) { return DateTime.toUtc(expiresAt); } const current = yield* sessions .extend(sessionId, renewed, PEER_LINK_RENEWAL_STEP) .pipe(mapSessionVerificationErrors); // A revoke can land between the verification and the update. if (Option.isNone(current)) { return yield* new ServerAuthInvalidCredentialError({ diagnostic: "The peer link session ended before it could be renewed.", }); } return current.value; }); const authenticateMcpClient: EnvironmentAuth["Service"]["authenticateMcpClient"] = (request) => { const token = parseBearerToken(request); if (token === null) return Effect.fail(new ServerAuthMissingCredentialError({})); return sessions.verify(token).pipe( mapSessionVerificationErrors, Effect.flatMap( Effect.fnUntraced(function* (session) { if (session.subject !== MCP_CLIENT_SUBJECT || session.method !== "bearer-access-token") { return yield* new ServerAuthInvalidCredentialError({ diagnostic: "Only MCP client sessions are accepted here.", }); } const client = { sessionId: session.sessionId, label: session.client.label ?? "MCP client", access: session.scopes.includes(AuthOrchestrationOperateScope) ? (session.runtimeModeCeiling ?? "approval-required") : "read-only", peerLink: session.peerLink === true, } satisfies McpClientSession; if (session.peerLink !== true || session.expiresAt === undefined) return client; return { ...client, linkExpiresAt: yield* renewPeerLinkSession(session.sessionId, session.expiresAt), } satisfies McpClientSession; }), ), Effect.withSpan("EnvironmentAuth.authenticateMcpClient"), ); }; const issueMcpClientSession: EnvironmentAuth["Service"]["issueMcpClientSession"] = (input) => sessions .issue({ subject: MCP_CLIENT_SUBJECT, method: "bearer-access-token", scopes: mcpClientScopes(input.access), ttl: MCP_CLIENT_SESSION_TTL, ...(input.access === "read-only" ? {} : { runtimeModeCeiling: input.access }), peerLink: input.peerLink, client: { ...input.client, label: input.label, deviceType: "bot" }, }) .pipe( Effect.map((issued) => ({ sessionId: issued.sessionId, token: issued.token, expiresAt: issued.expiresAt, })), Effect.mapError((cause) => new ServerAuthSessionTokenIssueError({ cause })), Effect.withSpan("EnvironmentAuth.issueMcpClientSession"), ); const consumeMcpApprovalCode: EnvironmentAuth["Service"]["consumeMcpApprovalCode"] = ( code, access, peerLink = false, ) => resolveBootstrapGrant(code.trim(), { requiredScopes: mcpClientScopes(access), allowedSubject: `${peerLink ? PEER_LINK_PAIRING_SUBJECT_PREFIX : MCP_APPROVAL_SUBJECT_PREFIX}${access}`, }).pipe( Effect.catchTags({ ServerAuthInvalidCredentialError: () => Effect.fail(new ServerAuthMcpApprovalCodeError({ reason: "unknown_or_used" })), ServerAuthConnectApprovalPendingError: () => Effect.fail(new ServerAuthMcpApprovalCodeError({ reason: "unknown_or_used" })), ServerAuthScopeNotGrantedError: () => Effect.fail(new ServerAuthMcpApprovalCodeError({ reason: "insufficient_scope" })), }), Effect.flatMap((grant) => grant.method !== "one-time-token" && grant.method !== "reusable-dev-token" ? Effect.fail(new ServerAuthMcpApprovalCodeError({ reason: "not_a_pairing_code" })) : Effect.succeed(mcpApprovalLevel(grant.subject) ?? access), ), Effect.withSpan("EnvironmentAuth.consumeMcpApprovalCode"), ); yield* revokePairedClientsThePolicyDisallows(managedPolicy, { sessions, pairingLinks: bootstrapCredentials, }); return EnvironmentAuth.of({ getDescriptor: Effect.fn("EnvironmentAuth.getDescriptor")(function () { return Effect.succeed(descriptor); }), getSessionState, createBrowserSession, exchangeBootstrapCredentialForAccessToken, renewAccessToken, createPairingLink, issueConnectCredential, approveConnectDevice, connectRequestsShowingCode, listConnectDevices, revokeConnectDevice, connectDevicesShowingCode, issuePairingCredential, issueStartupPairingCredential, listPairingLinks, revokePairingLink, issueSession, listSessions, revokeSession, listClientSessions, revokeClient, revokeClientSession, revokeOtherClientSessions, authenticateHttpRequest, authenticateWebSocketUpgrade, issueWebSocketTicket, issueStartupPairingUrl, authenticateMcpClient, issueMcpClientSession, consumeMcpApprovalCode, }); }); const isMcpClientAccess = Schema.is(AuthMcpClientAccess); const normalizeVerificationCode = (code: string) => code.toUpperCase().replace(/[^0-9A-Z]/g, ""); /** The level an approval code's subject names, or undefined for any other code. */ const mcpApprovalLevel = (subject: string): AuthMcpClientAccess | undefined => { const level = subject.startsWith(MCP_APPROVAL_SUBJECT_PREFIX) ? subject.slice(MCP_APPROVAL_SUBJECT_PREFIX.length) : undefined; return isMcpClientAccess(level) ? level : undefined; }; /** Subjects of sessions and links a pairing link created, keyed by how the client paired. */ const LINK_PAIRED_SUBJECTS = { link: "one-time-token", connect: CLOUD_CONNECT_SUBJECT } as const; /** * Revokes sessions and unused links from pairing the policy no longer allows, so a policy rolled * out to a machine that already had phones paired takes effect on the next start. */ const revokePairedClientsThePolicyDisallows = Effect.fn( "EnvironmentAuth.revokePairedClientsThePolicyDisallows", )(function* ( policy: ManagedPolicy.ManagedPolicy["Service"], stores: { readonly sessions: SessionStore.SessionStore["Service"]; readonly pairingLinks: PairingGrantStore.PairingGrantStore["Service"]; }, ) { const remoteAllowed = ManagedLimits.allowsRemoteClients(policy); const disallowed = new Set(); if (!remoteAllowed) disallowed.add(LINK_PAIRED_SUBJECTS.link); if (!ManagedLimits.allowsConnect(policy)) disallowed.add(LINK_PAIRED_SUBJECTS.connect); if (disallowed.size === 0) return; const sessions = (yield* stores.sessions.listActive()).filter( (session) => disallowed.has(session.subject) || // The startup link also pairs browsers on this machine; only a device elsewhere goes. (!remoteAllowed && session.subject === INTERNAL_ADMINISTRATIVE_BOOTSTRAP_SUBJECT && session.client.ipAddress !== undefined && !isLocalLoopbackHost(session.client.ipAddress)), ); const links = (yield* stores.pairingLinks.listActive()).filter( (link) => disallowed.has(link.subject) || // Approval codes let an agent elsewhere sign in, as plain codes do. (!remoteAllowed && link.subject.startsWith(MCP_APPROVAL_SUBJECT_PREFIX)), ); yield* Effect.forEach(sessions, (session) => stores.sessions.revoke(session.sessionId), { discard: true, }); yield* Effect.forEach(links, (link) => stores.pairingLinks.revoke(link.id), { discard: true }); if (sessions.length > 0 || links.length > 0) { yield* Effect.logInfo("Revoked clients the managed policy does not allow.", { sessions: sessions.length, pairingLinks: links.length, }); } }); export const layer = Layer.effect(EnvironmentAuth, make).pipe( Layer.provideMerge(PairingGrantStore.layer), Layer.provideMerge(SessionStore.layer), Layer.provideMerge(EnvironmentAuthPolicy.layer), ); const layerStorage = Layer.mergeAll(ServerSecretStore.layer, SqlitePersistence.layerCliConfig); export const layerRuntime = layer.pipe( Layer.provideMerge(ManagedPolicy.layer), Layer.provideMerge(layerStorage), Layer.provideMerge(ServerEnvironment.layerIdentity), );