Skip to main content

Auth context metadata API

The root SDK's createAuthContext(params), validateAuthContext(context) and extractInjectionFields(context) validate and propagate explicit operation metadata. They do not verify a JWT, provision a principal/grant or authenticate a runtime actor.

import { createAuthContext } from '@cortexmemory/sdk';

export const metadata = createAuthContext({ userId: 'user-123', tenantId: 'tenant-123' });

Use those helpers for scoped metadata in explicit domain/admin APIs. userId, tenantId, session/provider fields and custom claims must still agree with separately verified transport authority. Caller-written claims.admin or a tenant selector cannot grant access.

Runtime applications use CortexRuntimeClient.fetchToken, official Convex verification and current trusted grant records. session() returns server-derived identity/scope. See authentication, runtime client and admin transport.

Parameter and result contracts​

createAuthContext validates identity/tenant/session metadata and returns a normalized AuthContext. Validation rejects malformed metadata; it does not verify a token, its expiry or signature. extractInjectionFields selects those fields for explicit domain requests rather than creating backend authority.

AuthContextParams​

export interface AuthContextParams {
userId: string;
tenantId?: string;
organizationId?: string;
sessionId?: string;
authProvider?: string;
authMethod?: AuthMethod;
authenticatedAt?: number;
claims?: Record<string, unknown>;
metadata?: Record<string, unknown>;
}

AuthContext​

export interface AuthContext {
userId: string;
tenantId?: string;
organizationId?: string;
sessionId?: string;
authProvider?: string;
authMethod?: AuthMethod;
authenticatedAt?: number;
claims?: Record<string, unknown>;
metadata?: Record<string, unknown>;
}

AuthMethod​

export type AuthMethod = "oauth" | "api_key" | "jwt" | "session" | "custom";

Helper outcomes and validation​

createAuthContext(params) returns only defined fields and shallow copies claims/metadata. userId is required. User/tenant/organization/session IDs must be nonempty strings of at most 256 characters. Optional authenticatedAt is a nonnegative millisecond timestamp; authMethod must be one of the declared values, and claims/metadata must be object maps. validateAuthContext(value) returns true with a TypeScript type predicate when valid and throws AuthValidationError when invalid; it does not return false for malformed input. Errors include code and optional field, such as MISSING_USER_ID, INVALID_USER_ID, EMPTY_USER_ID, USER_ID_TOO_LONG and the corresponding optional-ID codes.

extractInjectionFields(context) returns { userId, tenantId?, sessionId? }; provider/claims/roles are not injected as authority. Token verification, expiration and grants remain backend checks.