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.
588 lines • 29.2 kB
JavaScript
/**
* `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 { gateSecret } from "../secret-gate.js";
import { LocalError } from "../errors.js";
import { requireProjectCredentials } from "../project-credentials.js";
import { assertEmailAddress, assertHttpUrl, assertNonEmptyString, assertStringInSet, } from "../validation.js";
const MAGIC_LINK_INTENTS = ["signin", "invite", "claim", "recovery"];
const TEST_SESSION_LABEL = /^[a-z0-9][a-z0-9-]{0,31}$/;
const AUTH_SETTINGS_FIELDS = [
"allow_password_set",
"preferred_sign_in_method",
"public_signup",
"require_passkey_for_project_admin",
"allowed_email_domains",
"google_client_ids",
"apple_client_ids",
"test_mode",
"browser_session_idle_seconds",
"browser_session_max_seconds",
];
const SIGN_IN_METHODS = ["password", "magic_link", "oauth_google", "passkey"];
const PUBLIC_SIGNUP_POLICIES = ["open", "known_email", "invite_only"];
function validateAuthSettings(settings) {
const raw = settings;
for (const key of Object.keys(raw)) {
if (!AUTH_SETTINGS_FIELDS.includes(key)) {
throw new LocalError(`Unknown auth settings field: ${key}`, "updating auth settings");
}
}
if (raw.allow_password_set !== undefined &&
typeof raw.allow_password_set !== "boolean") {
throw new LocalError("allow_password_set must be a boolean.", "updating auth settings");
}
if (raw.require_passkey_for_project_admin !== undefined &&
typeof raw.require_passkey_for_project_admin !== "boolean") {
throw new LocalError("require_passkey_for_project_admin must be a boolean.", "updating auth settings");
}
if (raw.test_mode !== undefined && raw.test_mode !== null && typeof raw.test_mode !== "boolean") {
throw new LocalError("test_mode must be true, false, or null.", "updating auth settings");
}
// The range and the idle-within-max rule are the server's to judge; the SDK checks the shape.
for (const key of ["browser_session_idle_seconds", "browser_session_max_seconds"]) {
const v = raw[key];
if (v !== undefined && v !== null && !(typeof v === "number" && Number.isInteger(v))) {
throw new LocalError(`${key} must be a whole number of seconds, or null for the default.`, "updating auth settings");
}
}
if (raw.preferred_sign_in_method !== undefined && raw.preferred_sign_in_method !== null) {
assertStringInSet(raw.preferred_sign_in_method, SIGN_IN_METHODS, "preferred_sign_in_method", "updating auth settings");
}
if (raw.public_signup !== undefined) {
assertStringInSet(raw.public_signup, PUBLIC_SIGNUP_POLICIES, "public_signup", "updating auth settings");
}
if (raw.allowed_email_domains !== undefined) {
if (!Array.isArray(raw.allowed_email_domains) ||
!raw.allowed_email_domains.every((d) => typeof d === "string")) {
throw new LocalError('allowed_email_domains must be an array of strings (e.g., ["example.com"]); pass [] to clear.', "updating auth settings");
}
}
if (raw.google_client_ids !== undefined) {
if (!Array.isArray(raw.google_client_ids) ||
!raw.google_client_ids.every((d) => typeof d === "string")) {
throw new LocalError('google_client_ids must be an array of Google OAuth client IDs (e.g., ["<project_number>-<client_suffix>.apps.googleusercontent.com"]); pass [] to clear.', "updating auth settings");
}
}
if (raw.apple_client_ids !== undefined) {
if (!Array.isArray(raw.apple_client_ids) ||
!raw.apple_client_ids.every((d) => typeof d === "string")) {
throw new LocalError('apple_client_ids must be an array of Apple bundle IDs or Services IDs (e.g., ["com.example.app"]); pass [] to clear.', "updating auth settings");
}
}
}
function appleKeyPath(projectId) {
return `/projects/v1/${encodeURIComponent(projectId)}/auth/providers/apple`;
}
export class Auth {
client;
magicLink;
verify;
setPassword;
promoteUser;
demoteUser;
constructor(client) {
this.client = client;
this.magicLink = this.requestMagicLink.bind(this);
this.verify = this.verifyMagicLink.bind(this);
this.setPassword = this.setUserPassword.bind(this);
this.promoteUser = this.promote.bind(this);
this.demoteUser = this.demote.bind(this);
}
/** Request a passwordless email credential. Link remains the wire default. */
async requestMagicLink(projectId, opts) {
if (!opts || typeof opts !== "object" || Array.isArray(opts)) {
throw new LocalError("r.auth.requestMagicLink(projectId, opts) requires an opts object as the 2nd argument (e.g., { email, redirectUrl })", "requesting magic link");
}
assertEmailAddress(opts.email, "email", "requesting magic link");
const delivery = opts.delivery ?? "link";
assertStringInSet(delivery, ["link", "code", "both"], "delivery", "requesting magic link");
if (delivery !== "code" || opts.redirectUrl !== undefined) {
assertHttpUrl(opts.redirectUrl, "redirectUrl", "requesting magic link");
}
if (opts.intent !== undefined) {
assertStringInSet(opts.intent, MAGIC_LINK_INTENTS, "intent", "requesting magic link");
}
if (opts.locale !== undefined && typeof opts.locale !== "string") {
throw new LocalError("locale must be a string such as \"he\"", "requesting magic link");
}
const project = await requireProjectCredentials(this.client, projectId, "requesting magic link");
const body = {
email: opts.email,
};
if (opts.redirectUrl !== undefined)
body.redirect_url = opts.redirectUrl;
if (opts.delivery !== undefined)
body.delivery = opts.delivery;
if (opts.intent)
body.intent = opts.intent;
if (opts.clientState !== undefined)
body.client_state = opts.clientState;
if (opts.locale !== undefined)
body.locale = opts.locale;
const result = await this.client.request("/auth/v1/magic-link", {
method: "POST",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${opts.intent === "invite" ? project.service_key : project.anon_key}`,
},
body,
context: "requesting magic link",
});
return {
message: result.message,
...(result.warnings ? { warnings: result.warnings } : {}),
...(result.challenge_id ? { challengeId: result.challenge_id } : {}),
};
}
/** Exchange a magic-link token for access + refresh tokens. */
async verifyMagicLink(projectId, token) {
gateSecret(this.client, "auth.verifyMagicLink", projectId, token);
assertNonEmptyString(token, "token", "verifying magic link");
const project = await requireProjectCredentials(this.client, projectId, "verifying magic link");
return this.client.request("/auth/v1/token?grant_type=magic_link", {
method: "POST",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${project.anon_key}`,
},
body: { token },
context: "verifying magic link",
});
}
/** Exchange an opaque challenge handle plus a six-digit code. Never auto-retried. */
async verifyEmailCode(projectId, opts) {
gateSecret(this.client, "auth.verifyEmailCode", projectId, opts);
if (!opts || typeof opts !== "object" || Array.isArray(opts)) {
throw new LocalError("r.auth.verifyEmailCode(projectId, opts) requires { challengeId, code }", "verifying email code");
}
if (typeof opts.challengeId !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(opts.challengeId)) {
throw new LocalError("challengeId must be a UUID.", "verifying email code");
}
if (typeof opts.code !== "string" || !/^\d{6}$/.test(opts.code)) {
throw new LocalError("code must be exactly six digits.", "verifying email code");
}
const project = await requireProjectCredentials(this.client, projectId, "verifying email code");
return this.client.request("/auth/v1/token?grant_type=email_code", {
method: "POST",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${project.anon_key}`,
},
body: { challenge_id: opts.challengeId, code: opts.code },
context: "verifying email code",
});
}
/**
* 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.
*/
async refreshSession(projectId, refreshToken) {
gateSecret(this.client, "auth.refreshSession", projectId, refreshToken);
assertNonEmptyString(refreshToken, "refreshToken", "refreshing user session");
const project = await requireProjectCredentials(this.client, projectId, "refreshing user session");
return this.client.request("/auth/v1/token?grant_type=refresh_token", {
method: "POST",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${project.anon_key}`,
},
body: { refresh_token: refreshToken },
context: "refreshing user session",
});
}
/**
* 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.
*/
async actorToken(projectId, accessToken) {
gateSecret(this.client, "auth.actorToken", projectId, accessToken);
assertNonEmptyString(accessToken, "accessToken", "getting user actor token");
const project = await requireProjectCredentials(this.client, projectId, "getting user actor token");
return this.client.request("/auth/v1/actor-token", {
method: "POST",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${accessToken}`,
},
body: {},
context: "getting user actor token",
});
}
/** End a user's token session: the refresh token stops working. Safe to repeat. */
async endSession(projectId, refreshToken) {
gateSecret(this.client, "auth.endSession", projectId, refreshToken);
assertNonEmptyString(refreshToken, "refreshToken", "ending user session");
const project = await requireProjectCredentials(this.client, projectId, "ending user session");
await this.client.request("/auth/v1/logout", {
method: "POST",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${project.anon_key}`,
},
body: { refresh_token: refreshToken },
context: "ending user session",
});
}
/**
* Set / change / reset the authenticated user's password. The caller's
* `accessToken` (from `verifyMagicLink` or a prior login) is used as the
* Bearer credential.
*/
async setUserPassword(projectId, opts) {
gateSecret(this.client, "auth.setUserPassword", projectId, opts);
if (!opts || typeof opts !== "object" || Array.isArray(opts)) {
throw new LocalError("r.auth.setUserPassword(projectId, opts) requires an opts object as the 2nd argument (e.g., { accessToken, newPassword })", "setting user password");
}
assertNonEmptyString(opts.accessToken, "accessToken", "setting user password");
assertNonEmptyString(opts.newPassword, "newPassword", "setting user password");
if (opts.currentPassword !== undefined) {
assertNonEmptyString(opts.currentPassword, "currentPassword", "setting user password");
}
const project = await requireProjectCredentials(this.client, projectId, "setting user password");
const body = { new_password: opts.newPassword };
if (opts.currentPassword !== undefined)
body.current_password = opts.currentPassword;
await this.client.request("/auth/v1/user/password", {
method: "POST",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${opts.accessToken}`,
},
body,
context: "setting user password",
});
}
/** Update project-level auth settings. Requires service key. */
async settings(projectId, settings) {
if (!settings || typeof settings !== "object" || Array.isArray(settings)) {
throw new LocalError("r.auth.settings(projectId, settings) requires a settings object as the 2nd argument (e.g., { allow_password_set: true })", "updating auth settings");
}
validateAuthSettings(settings);
const project = await requireProjectCredentials(this.client, projectId, "updating auth settings");
return this.client.request("/auth/v1/settings", {
method: "PATCH",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${project.service_key}`,
},
body: settings,
context: "updating auth settings",
});
}
/** Create or update an auth user. Requires service key. */
async createUser(projectId, opts) {
if (!opts || typeof opts !== "object" || Array.isArray(opts)) {
throw new LocalError("r.auth.createUser(projectId, opts) requires an opts object as the 2nd argument (e.g., { email, isAdmin })", "creating auth user");
}
assertEmailAddress(opts.email, "email", "creating auth user");
if (opts.redirectUrl !== undefined) {
assertHttpUrl(opts.redirectUrl, "redirectUrl", "creating auth user");
}
if (opts.isAdmin !== undefined && typeof opts.isAdmin !== "boolean") {
throw new LocalError("isAdmin must be a boolean when provided.", "creating auth user");
}
if (opts.sendInvite !== undefined && typeof opts.sendInvite !== "boolean") {
throw new LocalError("sendInvite must be a boolean when provided.", "creating auth user");
}
const project = await requireProjectCredentials(this.client, projectId, "creating auth user");
const body = { email: opts.email };
if (typeof opts.isAdmin === "boolean")
body.is_admin = opts.isAdmin;
if (typeof opts.sendInvite === "boolean")
body.send_invite = opts.sendInvite;
if (opts.redirectUrl !== undefined)
body.redirect_url = opts.redirectUrl;
if (opts.clientState !== undefined)
body.client_state = opts.clientState;
return this.client.request("/auth/v1/admin/users", {
method: "POST",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${project.service_key}`,
},
body,
context: "creating auth user",
});
}
/**
* 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`.
*/
async getUser(projectId, opts) {
if (!opts || typeof opts !== "object" || Array.isArray(opts)) {
throw new LocalError("r.auth.getUser(projectId, opts) requires an opts object as the 2nd argument (e.g., { email })", "looking up auth user");
}
assertEmailAddress(opts.email, "email", "looking up auth user");
const project = await requireProjectCredentials(this.client, projectId, "looking up auth user");
return this.client.request(`/auth/v1/admin/users/lookup?email=${encodeURIComponent(opts.email)}`, {
method: "GET",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${project.service_key}`,
},
context: "looking up auth user",
});
}
/**
* 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.
*/
async createTestSession(projectId, opts) {
gateSecret(this.client, "auth.createTestSession", projectId, opts);
if (!opts || typeof opts !== "object" || Array.isArray(opts)) {
throw new LocalError("r.auth.createTestSession(projectId, opts) requires an opts object as the 2nd argument (e.g., { label: \"alice\" })", "minting test session");
}
if (typeof opts.label !== "string" || !TEST_SESSION_LABEL.test(opts.label)) {
throw new LocalError("label must match ^[a-z0-9][a-z0-9-]{0,31}$ (lowercase letters, digits and dashes; a test-user label, not an email or user id).", "minting test session");
}
if (opts.isAdmin !== undefined && typeof opts.isAdmin !== "boolean") {
throw new LocalError("isAdmin must be a boolean when provided.", "minting test session");
}
const project = await requireProjectCredentials(this.client, projectId, "minting test session");
const body = { label: opts.label };
if (typeof opts.isAdmin === "boolean")
body.is_admin = opts.isAdmin;
return this.client.request("/auth/v1/test-sessions", {
method: "POST",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${project.service_key}`,
},
body,
context: "minting test session",
withAuth: false,
});
}
/**
* 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.
*/
async listTestEmails(projectId, opts = {}) {
gateSecret(this.client, "auth.listTestEmails", projectId, opts);
const project = await requireProjectCredentials(this.client, projectId, "listing captured mail");
const query = new URLSearchParams();
if (opts.to)
query.set("to", opts.to);
if (opts.limit !== undefined)
query.set("limit", String(opts.limit));
if (opts.cursor)
query.set("cursor", opts.cursor);
const qs = query.toString();
return this.client.request(`/auth/v1/test-emails${qs ? `?${qs}` : ""}`, {
method: "GET",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${project.service_key}`,
},
context: "listing captured mail",
withAuth: false,
});
}
/** Create/update an auth user and send a trusted invite. Requires service key. */
async inviteUser(projectId, opts) {
return this.createUser(projectId, { ...opts, sendInvite: true });
}
/** Create WebAuthn registration options for the authenticated user. */
async createPasskeyRegistrationOptions(projectId, opts) {
gateSecret(this.client, "auth.createPasskeyRegistrationOptions", projectId, opts);
if (!opts || typeof opts !== "object") {
throw new LocalError("r.auth.createPasskeyRegistrationOptions(projectId, opts) requires { accessToken, appOrigin }", "creating passkey registration options");
}
const project = await requireProjectCredentials(this.client, projectId, "creating passkey registration options");
return this.client.request("/auth/v1/passkeys/register/options", {
method: "POST",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${opts.accessToken}`,
},
body: { app_origin: opts.appOrigin },
context: "creating passkey registration options",
});
}
/** Verify and store a WebAuthn passkey registration. */
async verifyPasskeyRegistration(projectId, opts) {
gateSecret(this.client, "auth.verifyPasskeyRegistration", projectId, opts);
if (!opts || typeof opts !== "object") {
throw new LocalError("r.auth.verifyPasskeyRegistration(projectId, opts) requires { accessToken, challengeId, response }", "verifying passkey registration");
}
const project = await requireProjectCredentials(this.client, projectId, "verifying passkey registration");
const body = {
challenge_id: opts.challengeId,
response: opts.response,
};
if (opts.label)
body.label = opts.label;
return this.client.request("/auth/v1/passkeys/register/verify", {
method: "POST",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${opts.accessToken}`,
},
body,
context: "verifying passkey registration",
});
}
/** Create WebAuthn login options. */
async createPasskeyLoginOptions(projectId, opts) {
if (!opts || typeof opts !== "object") {
throw new LocalError("r.auth.createPasskeyLoginOptions(projectId, opts) requires { appOrigin }", "creating passkey login options");
}
const project = await requireProjectCredentials(this.client, projectId, "creating passkey login options");
const body = { app_origin: opts.appOrigin };
if (opts.email)
body.email = opts.email;
return this.client.request("/auth/v1/passkeys/login/options", {
method: "POST",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${project.anon_key}`,
},
body,
context: "creating passkey login options",
});
}
/** Verify a WebAuthn login assertion and return a normal auth session. */
async verifyPasskeyLogin(projectId, opts) {
gateSecret(this.client, "auth.verifyPasskeyLogin", projectId, opts);
if (!opts || typeof opts !== "object") {
throw new LocalError("r.auth.verifyPasskeyLogin(projectId, opts) requires { challengeId, response }", "verifying passkey login");
}
const project = await requireProjectCredentials(this.client, projectId, "verifying passkey login");
return this.client.request("/auth/v1/passkeys/login/verify", {
method: "POST",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${project.anon_key}`,
},
body: {
challenge_id: opts.challengeId,
response: opts.response,
},
context: "verifying passkey login",
});
}
/** List the authenticated user's active passkeys. */
async listPasskeys(projectId, opts) {
gateSecret(this.client, "auth.listPasskeys", projectId, opts);
if (!opts || typeof opts !== "object") {
throw new LocalError("r.auth.listPasskeys(projectId, opts) requires { accessToken }", "listing passkeys");
}
const project = await requireProjectCredentials(this.client, projectId, "listing passkeys");
return this.client.request("/auth/v1/passkeys", {
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${opts.accessToken}`,
},
context: "listing passkeys",
withAuth: false,
});
}
/** Delete one authenticated-user passkey by id. */
async deletePasskey(projectId, opts) {
gateSecret(this.client, "auth.deletePasskey", projectId, opts);
if (!opts || typeof opts !== "object") {
throw new LocalError("r.auth.deletePasskey(projectId, opts) requires { accessToken, passkeyId }", "deleting passkey");
}
const project = await requireProjectCredentials(this.client, projectId, "deleting passkey");
await this.client.request(`/auth/v1/passkeys/${encodeURIComponent(opts.passkeyId)}`, {
method: "DELETE",
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${opts.accessToken}`,
},
context: "deleting passkey",
});
}
/**
* 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`.
*/
async setAppleKey(projectId, key) {
gateSecret(this.client, "auth.setAppleKey", projectId);
if (!projectId)
throw new LocalError("auth.setAppleKey requires a projectId", "setting the Sign in with Apple key");
if (!key || typeof key.teamId !== "string" || typeof key.keyId !== "string" || typeof key.p8 !== "string") {
throw new LocalError("auth.setAppleKey requires { teamId, keyId, p8 } (p8 is the .p8 file's text)", "setting the Sign in with Apple key");
}
return this.client.request(appleKeyPath(projectId), {
method: "POST",
body: { team_id: key.teamId, key_id: key.keyId, private_key: key.p8 },
projectKeyFallback: projectId,
context: "setting the Sign in with Apple key",
});
}
/** Native Apple sign-in on a project: the key's public fields, the apps, the notification URL, what is missing. */
async appleKeyStatus(projectId) {
if (!projectId)
throw new LocalError("auth.appleKeyStatus requires a projectId", "reading Sign in with Apple");
return this.client.request(appleKeyPath(projectId), {
projectKeyFallback: projectId,
context: "reading Sign in with Apple",
});
}
/** Remove the project's Sign in with Apple key. `{ removed: false }` when none was set. */
async removeAppleKey(projectId) {
if (!projectId)
throw new LocalError("auth.removeAppleKey requires a projectId", "removing the Sign in with Apple key");
return this.client.request(appleKeyPath(projectId), {
method: "DELETE",
projectKeyFallback: projectId,
context: "removing the Sign in with Apple key",
});
}
/** List configured auth providers for a project. Uses the project's anon key. */
async providers(projectId) {
const project = await requireProjectCredentials(this.client, projectId, "listing auth providers");
const result = await this.client.request("/auth/v1/providers", {
headers: {
apikey: project.anon_key,
Authorization: `Bearer ${project.anon_key}`,
},
context: "listing auth providers",
withAuth: false,
});
return {
...result,
magic_link: {
enabled: result.magic_link.enabled,
deliveryModes: result.magic_link.delivery_modes ?? ["link"],
},
};
}
/** Promote a user (by email) to `project_admin`. Requires service key. */
async promote(projectId, email) {
const project = await requireProjectCredentials(this.client, projectId, "promoting user");
await this.client.request(`/projects/v1/admin/${projectId}/promote-user`, {
method: "POST",
headers: { Authorization: `Bearer ${project.service_key}` },
body: { email },
context: "promoting user",
});
}
/** Demote a user (by email) from `project_admin` back to the default role. */
async demote(projectId, email) {
const project = await requireProjectCredentials(this.client, projectId, "demoting user");
await this.client.request(`/projects/v1/admin/${projectId}/demote-user`, {
method: "POST",
headers: { Authorization: `Bearer ${project.service_key}` },
body: { email },
context: "demoting user",
});
}
}
//# sourceMappingURL=auth.js.map