> This page is for MVS Auth.

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.mvscloud.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.mvscloud.com/_mcp/server.

# Operator-app OIDC integration

> Register an operator app as an OIDC relying party of the MVS platform IdP at c2-auth.mvscloud.com — endpoints, Better Auth genericOAuth config, claims, registration, and token verification.

The MVS **platform** OIDC provider is live in production at
`https://c2-auth.mvscloud.com`. It is the operator-facing IdP: use it when the
relying party is an MVS operator/admin tool (e.g. `mvs-pay`, `mvs-ops-ai`,
`mvs-c2`) whose users are **operators** — the `PlatformUser` directory, signed
in via Okta SSO + passkeys. Any standards-compliant OIDC client library works
(e.g. `openid-client`, Better Auth's `genericOAuth`).

> **Note**
>
> Two providers, two surfaces. Tenant apps consume **central** auth at
> `c-auth.mvscloud.com` (see the [OIDC integration guide](/mvs-auth/guides/integrating/oidc-relying-party-integration));
> operator apps consume **platform** auth at `c2-auth.mvscloud.com`. The two are
> fully isolated: c2-auth FKs to `PlatformUser` and writes the
> `platform_oauth_*` tables; c-auth FKs to `User` and writes `oauth_client`. A
> client registered on one surface is invisible to the other. For the
> shared-cookie redirect model (no OIDC), see the
> [Platform auth integration guide](/mvs-auth/guides/integrating/platform-auth-integration).

## 1. Endpoints

Resolve everything from the discovery document — let your client library derive
the rest, and do **not** hardcode endpoints other than discovery.

| Purpose                           | URL                                                                       |
| --------------------------------- | ------------------------------------------------------------------------- |
| Issuer                            | `https://c2-auth.mvscloud.com` (exact, no trailing slash)                 |
| Discovery                         | `https://c2-auth.mvscloud.com/.well-known/openid-configuration`           |
| Authorization                     | `https://c2-auth.mvscloud.com/api/platform-auth/oauth2/authorize`         |
| Token                             | `https://c2-auth.mvscloud.com/api/platform-auth/oauth2/token`             |
| UserInfo                          | `https://c2-auth.mvscloud.com/api/platform-auth/oauth2/userinfo`          |
| JWKS                              | `https://c2-auth.mvscloud.com/api/platform-auth/jwks`                     |
| Admin registration (self-service) | `POST https://c2-auth.mvscloud.com/api/platform-auth/admin/oauth-clients` |

> **Warning**
>
> The platform JWKS lives under `/api/platform-auth/jwks` — **not**
> `/api/auth/jwks` (that path is central). Always resolve `jwks_uri` from
> discovery rather than hardcoding it.

**Invariants** (enforced server-side, non-negotiable):

* **PKCE S256 mandatory** — `plain` is rejected.
* **ID token signing is EdDSA (Ed25519) only** — pin `algorithms: ["EdDSA"]`
  in verifiers; never accept RS256/HS256.
* Issuer is exactly `https://c2-auth.mvscloud.com` (no trailing slash).
* **Dynamic client registration (RFC 7591) is disabled** — every RP needs a
  `platform_oauth_client` row (see §4).

## 2. Configure your client (Better Auth `genericOAuth`)

For an operator app running its **own** Better Auth instance and consuming
c2-auth as an upstream OIDC provider:

```typescript
import { betterAuth } from "better-auth";
import { genericOAuth } from "better-auth/plugins";

export const auth = betterAuth({
  baseURL: process.env.APP_BASE_URL, // e.g. https://pay.mvscloud.com
  // ...adapter, secret, other plugins...
  plugins: [
    genericOAuth({
      config: [
        {
          providerId: "c2-auth",
          discoveryUrl:
            "https://c2-auth.mvscloud.com/.well-known/openid-configuration",
          clientId: process.env.MVS_OIDC_CLIENT_ID!,
          clientSecret: process.env.MVS_OIDC_CLIENT_SECRET!,
          scopes: ["openid", "profile", "email", "offline_access"],
          pkce: true, // S256 — set explicitly
          authentication: "basic", // client_secret_basic
          requireIssuerValidation: true, // RFC 9207

          // Operator role/groups travel as namespaced claims (see §3).
          mapProfileToUser: (profile) => ({
            role: (profile["https://mvscloud.com/role"] as string | null) ?? null,
            groups: (() => {
              const raw = profile["https://mvscloud.com/groups"] as string | null;
              try {
                return raw ? (JSON.parse(raw) as string[]) : [];
              } catch {
                return [];
              }
            })(),
          }),
        },
      ],
    }),
  ],
});
```

Trigger sign-in from the client (Better Auth ≥ 1.7):

```typescript
authClient.signIn.social({ provider: "c2-auth", callbackURL: "/dashboard" });
```

### Callback / redirect URI

The redirect URI you register at c2-auth depends on your Better Auth version —
this is the value the operator adds to your client's exact-match allowlist:

| Better Auth version             | Redirect URI to register                          |
| ------------------------------- | ------------------------------------------------- |
| **≥ 1.7.0** (core social route) | `{APP_BASE_URL}/api/auth/callback/c2-auth`        |
| ≤ 1.6.x (legacy)                | `{APP_BASE_URL}/api/auth/oauth2/callback/c2-auth` |
| SSO plugin (any)                | `{APP_BASE_URL}/api/auth/sso/callback/c2-auth`    |

The repo pins better-auth at `1.7.0-beta.5`, so the **≥ 1.7** path applies:
register `{APP_BASE_URL}/api/auth/callback/c2-auth`.

> **Note**
>
> Set your client env to the credentials returned at registration:
> `MVS_OIDC_CLIENT_ID` and `MVS_OIDC_CLIENT_SECRET` (the `mvs_cs_…` secret is
> shown once and stored hashed). If the app also consumes central auth — rare
> for operator apps — use a distinct prefix (e.g. `C2_AUTH_OIDC_CLIENT_*`) to
> disambiguate.

## 3. Claims

The c2-auth ID token **and** `/userinfo` response carry these operator claims,
sourced from `PlatformUser` (not tenant-org membership). `sub`, `email`,
`name`, etc. are standard.

| Claim                         | Value                                                          |
| ----------------------------- | -------------------------------------------------------------- |
| `https://mvscloud.com/role`   | Operator role string (`super_admin` \| `admin` \| …) or `null` |
| `https://mvscloud.com/groups` | JSON-stringified array, e.g. `'["admin"]'`, or `null`          |

> **Warning**
>
> Authorize on `role` / `groups`, never on `email`. These claims live on the ID
> token and `/userinfo` — they are **not** on the access token. Consume them
> from the ID token or userinfo.

## 4. Register your client

Dynamic registration is off; every RP needs a `platform_oauth_client` row. Both
paths below write that row through the one shared registration core
(`surface: "platform"`) — identical validation, the `mvs_cs_` secret scheme,
and idempotent-by-name upsert.

### Path A — operator CLI

Dry-run by default; add `--apply` to write. Requires `AUTH_DATABASE_URL`
pointing at the auth database, run from the `mvs-auth` repo.

```bash
# DRY-RUN (plan only, no write)
AUTH_DATABASE_URL=... pnpm --filter @mvs/auth-server oidc:register:platform -- \
  --name="MVS Pay (Operator)" \
  --redirect="https://pay.mvscloud.com/api/auth/callback/c2-auth" \
  --type=confidential

# APPLY (writes the row; prints the integration packet + one-time secret)
AUTH_DATABASE_URL=... pnpm --filter @mvs/auth-server oidc:register:platform -- \
  --name="MVS Pay (Operator)" \
  --redirect="https://pay.mvscloud.com/api/auth/callback/c2-auth" \
  --post-logout="https://pay.mvscloud.com/logged-out" \
  --type=confidential \
  --apply
```

Flags mirror the central CLI: `--scopes`, `--grant-types`, `--client-id`,
`--no-skip-consent`, `--rotate-secret`, `--force`. Re-running with the same
`--name` is idempotent (it refuses to mint a duplicate unless `--force`).

### Path B — self-service HTTP API

The admin registration route is gated by
`PLATFORM_OAUTH_SELF_REGISTRATION_ENABLED=true` (already on in prod). A platform
**admin** (a `PlatformUser` with an admin role) either calls it with an admin
session, or mints a platform **Initial Access Token (IAT)** to delegate scoped,
bounded registration.

```bash
curl -sX POST https://c2-auth.mvscloud.com/api/platform-auth/admin/oauth-clients \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer mvs_iat_…" \
  -d '{
    "name": "MVS Pay (Operator)",
    "redirectUris": ["https://pay.mvscloud.com/api/auth/callback/c2-auth"],
    "scopes": ["openid", "profile", "email", "offline_access"],
    "clientType": "confidential"
  }'
```

Response (the `clientSecret` is shown **once**):

```json
{
  "action": "create",
  "clientId": "…",
  "clientSecret": "mvs_cs_…",
  "registrationId": "clr…",
  "infisical": { "written": true, "ref": "infisical:/mvs-pay:MVS_OIDC_CLIENT_SECRET" }
}
```

Platform IATs are minted at
`POST https://c2-auth.mvscloud.com/api/platform-auth/admin/registration-tokens`
(admin session only). The same request shape, IAT bounds, and Infisical
auto-provision (`infisicalTarget`) as central apply here — see the
[self-service client registration guide](/mvs-auth/guides/integrating/self-service-client-registration).

> **Warning**
>
> The surfaces are isolated. A platform IAT can **only** mint platform clients —
> presenting it to the central route (`/api/auth/admin/oauth-clients`) returns
> `401`. Always register operator apps against `c2-auth`, not `c-auth`.

## 5. Verify tokens (resource servers)

ID tokens are EdDSA JWTs — verify against the platform JWKS, pinning the issuer,
your audience, and `EdDSA` only.

```typescript
import { createRemoteJWKSet, jwtVerify } from "jose";

const JWKS = createRemoteJWKSet(
  new URL("https://c2-auth.mvscloud.com/api/platform-auth/jwks"),
);

export async function verifyOperatorToken(token: string, expectedAudience: string) {
  const { payload } = await jwtVerify(token, JWKS, {
    issuer: "https://c2-auth.mvscloud.com",
    audience: expectedAudience, // your API origin (see §6)
    algorithms: ["EdDSA"], // EdDSA only
  });
  const role = payload["https://mvscloud.com/role"] as string | null;
  if (!role || !["admin", "super_admin"].includes(role)) {
    throw new Error("forbidden: insufficient operator role");
  }
  return payload;
}
```

## 6. JWT access tokens (`PLATFORM_OIDC_VALID_AUDIENCES`)

By default the provider issues **opaque** access tokens (introspect them, or
just use the ID token). To receive an **EdDSA JWT** access token bound to your
API, send the RFC 8707 `resource` parameter on `/authorize`, and the operator
must add your API origin to the platform audience allowlist.

* **Env var:** `PLATFORM_OIDC_VALID_AUDIENCES` (Infisical path `/platform-auth`,
  prod) — distinct from central's `OIDC_VALID_AUDIENCES`.
* **Format:** comma-separated absolute origins, exact match, no wildcards —
  e.g. `https://pay.mvscloud.com/api,https://ops-ai.mvscloud.com/api`.
* **Never** set it to the issuer origin — self-aliasing rejects every RP. By
  design the parser never defaults to the issuer; unset means no aud-bound JWTs
  are accepted.
* **Cold-start flag:** changing it requires a **redeploy** of
  `mvs-platform-auth`.

> **Note**
>
> ID-token and opaque-token flows work **without** this var. Only set it when an
> operator RP needs aud-bound JWT access tokens, and only the operator can set
> it (it is not self-service).

## 7. Security model

| Control           | Enforcement                                                                                                                                                  |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Surface isolation | Platform IATs/clients live in `platform_oauth_*` / `oauth_registration_token (surface='platform')`; cross-surface use is rejected with `401`. Verified live. |
| PKCE              | Forced **S256** on every registered client; `plain` is rejected at authorize/token.                                                                          |
| Redirect URIs     | Absolute HTTPS, exact-match allowlist, no wildcards. IAT registrations are further bounded to the token's `redirectUriPrefixes`.                             |
| Secret storage    | `client_secret` and IAT are stored only as SHA-256 base64url hashes; plaintext shown once; never recoverable.                                                |
| ID token signing  | EdDSA (Ed25519) only; the platform uses its own `platform_jwks` keys, never central's.                                                                       |
| Provider gate     | The provider is live in prod behind `PLATFORM_OIDC_PROVIDER_ENABLED=true` + a pinned `PLATFORM_OIDC_PROVIDER_ISSUER_URL` (cold-start reads).                 |

> **Note**
>
> The platform OIDC provider is **shipped and live in production**. Full
> request/response schemas are in the **Platform Auth API** reference.