UNPKG

run402-mcp

Version:

MCP server for Run402 — full-stack backend infrastructure for AI agents: Postgres, auth, storage, serverless functions and atomic deploys. Paid with x402/MPP. Includes $0.03 image generation.

447 lines • 18.5 kB
/** * `auth` namespace — project-scoped user authentication: magic links, password * set/change, auth settings, and admin role promotion/demotion. * * Magic link + password ops use the project's anon key (they represent * end-user flows). Settings + promote/demote use the service key. */ import type { Client } from "../kernel.js"; import { type NextAction } from "../errors.js"; export type EmailAuthDelivery = "link" | "code" | "both"; interface MagicLinkOptionsBase { email: string; intent?: "signin" | "invite" | "claim" | "recovery"; clientState?: unknown; /** * Language of the email, as a BCP 47 tag (the page's language). The gateway * renders its own translation for `en`, `he` (right to left) and `ru`, and * English for anything else. */ locale?: string; } export type MagicLinkOptions = (MagicLinkOptionsBase & { delivery?: "link"; redirectUrl: string; }) | (MagicLinkOptionsBase & { delivery: "both"; redirectUrl: string; }) | (MagicLinkOptionsBase & { delivery: "code"; redirectUrl?: string; }); export interface MagicLinkRequestWarning { code: string; message: string; [key: string]: unknown; } export interface MagicLinkRequestResult { message: string; warnings?: MagicLinkRequestWarning[]; challengeId?: string; } export interface EmailCodeVerifyOptions { challengeId: string; code: string; } export interface MagicLinkUser { id: string; email: string; } export interface MagicLinkVerifyResult { access_token: string; refresh_token: string; token_type: string; expires_in: number; user: MagicLinkUser; magic_link?: { intent: "signin" | "invite" | "claim" | "recovery"; client_state: string | null; state_source: "anonymous" | "service_key"; state_trusted: boolean; delivery?: EmailAuthDelivery; verified_with?: "link" | "email_code"; }; } export interface AuthProvidersResult { magic_link: { enabled: boolean; /** Capability-gated email modes. Absent means link-only compatibility. */ deliveryModes: EmailAuthDelivery[]; }; [key: string]: unknown; } export interface SetPasswordOptions { accessToken: string; newPassword: string; /** Required for password change; omit for reset (via magic link) or initial set. */ currentPassword?: string; } export interface AuthSettings { allow_password_set?: boolean; preferred_sign_in_method?: "password" | "magic_link" | "oauth_google" | "passkey" | null; public_signup?: "open" | "known_email" | "invite_only"; require_passkey_for_project_admin?: boolean; /** * Restrict hosted Google sign-in to these email domains, enforced at token * issuance. `[]` or omitted = unrestricted. Entries are normalized * (lowercased, leading `@` stripped, trimmed, deduped) and domain-validated * server-side; pass an explicit `[]` to clear an existing restriction. */ allowed_email_domains?: string[]; /** * The Google OAuth client IDs this project's phone apps sign in with: the * web client ID the app passes to Google as its server client ID, and each * Android or iOS client ID its builds are registered under, from the app * owner's own Google Cloud project. At most 10; `[]` turns native Google * sign-in off. Phone apps then sign in at `POST /auth/v1/native-sessions`. */ google_client_ids?: string[]; /** * The Apple client IDs this project's native Sign in with Apple accepts: * each iPhone app's bundle ID (and, for web sign-in, a Services ID), as * registered in the owner's Apple Developer account. At most 10; `[]` turns * native Apple sign-in off. Native Apple sign-in also needs the app's Sign in * with Apple key: `r.auth.setAppleKey` (CLI: `run402 auth apple-key set`). */ apple_client_ids?: string[]; /** * Whether `createTestSession` may mint for this project: `true` / `false` * set it explicitly, `null` returns to the tier default (on for prototype, * off for hobby and team). */ test_mode?: boolean | null; /** * How long a browser sign-in may go unused before it ends, in whole * seconds (3600 to 7776000); `null` restores the default, 43200 (12 hours). * May not exceed `browser_session_max_seconds`. A shorter value applies at * once to people already signed in. */ browser_session_idle_seconds?: number | null; /** * A browser sign-in's longest life from signing in, in whole seconds (3600 * to 7776000); `null` restores the default, 2592000 (30 days). A longer value * applies to sign-ins from then on. */ browser_session_max_seconds?: number | null; } export interface AuthSettingsResult { allow_password_set: boolean; preferred_sign_in_method: "password" | "magic_link" | "oauth_google" | "passkey" | null; public_signup: "open" | "known_email" | "invite_only"; require_passkey_for_project_admin: boolean; /** Normalized email-domain allowlist for hosted Google sign-in; `[]` = unrestricted. */ allowed_email_domains: string[]; /** Google OAuth client IDs for native sign-in; `[]` = native Google sign-in off. */ google_client_ids: string[]; /** Apple client IDs (bundle IDs, Services IDs) for native Sign in with Apple; `[]` = off. */ apple_client_ids?: string[]; /** How long a browser sign-in may go unused, in seconds, in effect. */ browser_session_idle_seconds?: number; /** A browser sign-in's longest life from signing in, in seconds, in effect. */ browser_session_max_seconds?: number; /** Test-session minting: the stored setting, the value in force, and where it comes from. */ test_mode?: { value: boolean | null; effective: boolean; source: "explicit" | "tier_default"; default_for_tier: boolean; }; } export interface CreateAuthUserOptions { email: string; isAdmin?: boolean; sendInvite?: boolean; redirectUrl?: string; clientState?: unknown; } export interface AuthUserAdminResult { id: string; email: string; is_admin: boolean; email_verified_at: string | null; created: boolean; invite_sent: boolean; } export interface GetAuthUserOptions { /** The user's email; matched case-insensitively. */ email: string; } /** A project auth user from `GET /auth/v1/admin/users/lookup`. */ export interface AuthUserRecord { id: string; email: string; is_admin: boolean; /** True for a watermarked test user (`<label>@<project_id>.test.invalid`). */ is_test: boolean; email_verified_at: string | null; created_at: string; } export interface CreateTestSessionOptions { /** Test-user label (`^[a-z0-9][a-z0-9-]{0,31}$`); the user's email is `<label>@<project_id>.test.invalid`. */ label: string; isAdmin?: boolean; } export interface ListTestEmailsOptions { /** Only mail meant for this address. */ to?: string; /** 1 to 100, default 50. */ limit?: number; cursor?: string; } /** One captured message, as the project meant to send it. */ export interface TestEmailCapture { id: string; to_email: string; from_address: string | null; subject: string | null; body_text: string | null; body_html: string | null; template: string | null; kind: string; /** ISO-8601. */ created_at: string; } export interface ListTestEmailsResult { emails: TestEmailCapture[]; has_more: boolean; next_cursor: string | null; } /** The test-session bundle from `POST /auth/v1/test-sessions`, returned unchanged. */ export interface TestSessionResult { user: { id: string; label: string; email: string; is_test: true; is_admin: boolean; created: boolean; created_at: string; email_verified_at: string | null; }; session: { cookie_name: string; cookie_value: string; session_id: string; amr: string[]; auth_time: string; expires_at: string; hard_expires_at: string; }; access_token: string; token_type: "bearer"; expires_in: number; /** The origins that serve the project's routes: each managed subdomain as * `https://<name>.run402.app` then `https://<name>.run402.com`, then custom * domains whose web serving is effective. Empty when the project has no host yet. */ app_origins: string[]; /** * The app's MCP endpoint and a token to call it as the test user, present * when the project has a host that serves its routes: `url` is * `/_run402/mcp` on `app_origins[0]`, and `access_token` is an MCP access * token valid only at that resource and for no longer than the test * session. POST `tools/list` or `tools/call` to `url` with * `Authorization: Bearer <access_token>`: no browser, no consent screen. */ mcp?: { url: string; access_token: string; expires_at: string; }; /** `ssr` curls `app_origins[0]` with the cookie, and `mcp` is a ready-to-run * `tools/list` call against `mcp.url`; both are null when there is no origin. */ examples: { ssr: string | null; rest: string; mcp?: string | null; }; test_mode: Record<string, unknown>; /** Present only when `app_origins` is empty: one `deploy` action saying how * a release claims a subdomain. */ next_actions?: NextAction[]; } /** The data-plane token the gateway mints for a user: role always `authenticated`, 60 seconds. */ export interface ActorTokenResult { actor_token: string; token_type: string; /** Seconds until the token expires. */ expires_in: number; user_id: string; } export interface AuthSessionResult extends MagicLinkVerifyResult { elevation_required?: boolean; required_method?: "passkey"; effective_role?: "authenticated"; intended_role?: "project_admin"; } /** The project's Sign in with Apple key (`team_id`, `key_id`, the `.p8` file's text). */ export interface AppleSignInKeyInput { teamId: string; keyId: string; /** The text of the `.p8` file Apple downloads once for the key: a PKCS#8 PEM of a P-256 private key. */ p8: string; } export type AppleSignInMissing = "key" | "client_ids"; /** The key's public fields; the key itself is never returned. */ export interface AppleSignInKey { team_id: string; key_id: string; set_at: string; last_error: string | null; last_error_at: string | null; /** The URL to give Apple for server-to-server notifications. */ notification_url: string; /** What native Apple sign-in still lacks; `[]` when it is on. */ missing: AppleSignInMissing[]; next_actions?: unknown[]; } /** Native Apple sign-in on a project: the key's public fields (each null when no key is set), the apps, the notification URL, what is missing. */ export interface AppleSignInStatus { configured: boolean; team_id: string | null; key_id: string | null; set_at: string | null; last_error: string | null; last_error_at: string | null; client_ids: string[]; notification_url: string; missing: AppleSignInMissing[]; next_actions?: unknown[]; } export interface PasskeyOptionsResult { challenge_id: string; options: unknown; } export interface PasskeyRecord { id: string; rp_id: string; created_origin: string; last_used_origin?: string | null; transports: string[]; label: string | null; credential_device_type: string | null; credential_backed_up: boolean | null; created_at: string; last_used_at: string | null; } export interface PasskeyRegistrationOptions { accessToken: string; appOrigin: string; } export interface PasskeyRegistrationVerifyOptions { accessToken: string; challengeId: string; response: unknown; label?: string; } export interface PasskeyLoginOptions { appOrigin: string; email?: string; } export interface PasskeyLoginVerifyOptions { challengeId: string; response: unknown; } export interface PasskeyListOptions { accessToken: string; } export interface PasskeyDeleteOptions { accessToken: string; passkeyId: string; } export declare class Auth { private readonly client; readonly magicLink: (projectId: string, opts: MagicLinkOptions) => Promise<MagicLinkRequestResult>; readonly verify: (projectId: string, token: string) => Promise<MagicLinkVerifyResult>; readonly setPassword: (projectId: string, opts: SetPasswordOptions) => Promise<void>; readonly promoteUser: (projectId: string, email: string) => Promise<void>; readonly demoteUser: (projectId: string, email: string) => Promise<void>; constructor(client: Client); /** Request a passwordless email credential. Link remains the wire default. */ requestMagicLink(projectId: string, opts: MagicLinkOptions): Promise<MagicLinkRequestResult>; /** Exchange a magic-link token for access + refresh tokens. */ verifyMagicLink(projectId: string, token: string): Promise<MagicLinkVerifyResult>; /** Exchange an opaque challenge handle plus a six-digit code. Never auto-retried. */ verifyEmailCode(projectId: string, opts: EmailCodeVerifyOptions): Promise<MagicLinkVerifyResult>; /** * Exchange a user's refresh token for a new access token and the next * refresh token. Refresh tokens are single-use: store the returned one. * Never auto-retried, because a repeat spends a token that is already gone. */ refreshSession(projectId: string, refreshToken: string): Promise<AuthSessionResult>; /** * Exchange a user's access token for the data-plane token the gateway hands * a function on the app's own host: role always `authenticated`, also for a * project admin, valid 60 seconds. Send it as the Bearer on `/rest/v1` to * read the database as the deployed app does for that user. */ actorToken(projectId: string, accessToken: string): Promise<ActorTokenResult>; /** End a user's token session: the refresh token stops working. Safe to repeat. */ endSession(projectId: string, refreshToken: string): Promise<void>; /** * Set / change / reset the authenticated user's password. The caller's * `accessToken` (from `verifyMagicLink` or a prior login) is used as the * Bearer credential. */ setUserPassword(projectId: string, opts: SetPasswordOptions): Promise<void>; /** Update project-level auth settings. Requires service key. */ settings(projectId: string, settings: AuthSettings): Promise<AuthSettingsResult>; /** Create or update an auth user. Requires service key. */ createUser(projectId: string, opts: CreateAuthUserOptions): Promise<AuthUserAdminResult>; /** * Look up one project auth user by email (a member or a test user), for * seeding a role table keyed by user id. Read-only. Requires service key. * No match throws the gateway's 404 `AUTH_USER_NOT_FOUND`. */ getUser(projectId: string, opts: GetAuthUserOptions): Promise<AuthUserRecord>; /** * Mint a session for a watermarked test user (`<label>@<project_id>.test.invalid`, * never a real user). Requires service key and the project's test mode. * The bundle carries a session cookie value and a bearer token. */ createTestSession(projectId: string, opts: CreateTestSessionOptions): Promise<TestSessionResult>; /** * The project's captured mail, newest first: messages to its test-session * addresses (`<label>@<project_id>.test.invalid`), and, on a branch, * everything the branch's sandbox rerouted, under the address it was meant * for. A sign-in code sent on a branch is read here. Requires service key. */ listTestEmails(projectId: string, opts?: ListTestEmailsOptions): Promise<ListTestEmailsResult>; /** Create/update an auth user and send a trusted invite. Requires service key. */ inviteUser(projectId: string, opts: Omit<CreateAuthUserOptions, "sendInvite">): Promise<AuthUserAdminResult>; /** Create WebAuthn registration options for the authenticated user. */ createPasskeyRegistrationOptions(projectId: string, opts: PasskeyRegistrationOptions): Promise<PasskeyOptionsResult>; /** Verify and store a WebAuthn passkey registration. */ verifyPasskeyRegistration(projectId: string, opts: PasskeyRegistrationVerifyOptions): Promise<PasskeyRecord>; /** Create WebAuthn login options. */ createPasskeyLoginOptions(projectId: string, opts: PasskeyLoginOptions): Promise<PasskeyOptionsResult>; /** Verify a WebAuthn login assertion and return a normal auth session. */ verifyPasskeyLogin(projectId: string, opts: PasskeyLoginVerifyOptions): Promise<AuthSessionResult>; /** List the authenticated user's active passkeys. */ listPasskeys(projectId: string, opts: PasskeyListOptions): Promise<{ passkeys: PasskeyRecord[]; }>; /** Delete one authenticated-user passkey by id. */ deletePasskey(projectId: string, opts: PasskeyDeleteOptions): Promise<void>; /** * Give Run402 the project's Sign in with Apple key, replacing any * (`POST /projects/v1/:project_id/auth/providers/apple`). Run402 holds it * encrypted, signs a short client secret from it for each call to Apple, * and never returns it. It consumes a secret, so the MCP sandbox refuses it * with `SECRET_REQUIRES_CLI` naming `run402 auth apple-key set`. */ setAppleKey(projectId: string, key: AppleSignInKeyInput): Promise<AppleSignInKey>; /** Native Apple sign-in on a project: the key's public fields, the apps, the notification URL, what is missing. */ appleKeyStatus(projectId: string): Promise<AppleSignInStatus>; /** Remove the project's Sign in with Apple key. `{ removed: false }` when none was set. */ removeAppleKey(projectId: string): Promise<{ removed: boolean; }>; /** List configured auth providers for a project. Uses the project's anon key. */ providers(projectId: string): Promise<AuthProvidersResult>; /** Promote a user (by email) to `project_admin`. Requires service key. */ promote(projectId: string, email: string): Promise<void>; /** Demote a user (by email) from `project_admin` back to the default role. */ demote(projectId: string, email: string): Promise<void>; } export {}; //# sourceMappingURL=auth.d.ts.map