Skip to content

Commit c8b4b5f

Browse files
committed
wip(plugin-auth): platform-admin door to mint and revoke SCIM connection credentials
Claude-Session: https://claude.ai/code/session_016WV4cycHNJyS5MXHqaGsFE Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 256a4b7 commit c8b4b5f

6 files changed

Lines changed: 505 additions & 0 deletions

File tree

‎packages/plugins/plugin-auth/src/auth-manager.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ import { createAuthInstance, type AuthInstanceHost } from './auth-manager-auth-i
213213
import { buildPluginList, type PluginListHost } from './auth-manager-plugin-list.js';
214214
import { resolveTurnstileKeys } from './turnstile-sign-up.js';
215215
import { assertTrustedAppSchemes, registeredAppSchemesOf } from './one-time-token-handoff.js';
216+
import type { MintScimConnectionCredentialInput, MintedScimConnectionCredential } from './scim-connection-service.js';
216217

217218
/**
218219
* Detect WebContainer (StackBlitz) environment.
@@ -6143,6 +6144,32 @@ export class AuthManager {
61436144
return this.config.trustProxy === true;
61446145
}
61456146

6147+
/**
6148+
* [#30958] Is the SCIM 2.0 service provider mounted on this deployment? The
6149+
* same decision `buildPluginList` mounts `@better-auth/scim` on
6150+
* ({@link resolveScimEnabled}), so the credential door's "SCIM is off"
6151+
* refusal can never disagree with the `/scim/v2` routes actually served.
6152+
*/
6153+
public isScimWired(): boolean {
6154+
return resolveScimEnabled(this.config.plugins);
6155+
}
6156+
6157+
/**
6158+
* [#30958] Mint a SCIM connection bearer with this deployment's auth secret,
6159+
* the key the mounted SCIM plugin's verifier digests with
6160+
* (`buildPluginList`), so the bearer is one that plugin accepts. The secret
6161+
* never leaves the manager. The only caller is the platform-admin
6162+
* `/admin/scim/mint-credential` mount (`scim-connection-admin.ts`), which
6163+
* hands the engine to write through.
6164+
*/
6165+
public async mintScimConnectionCredential(
6166+
engine: IDataEngine,
6167+
input: MintScimConnectionCredentialInput,
6168+
): Promise<MintedScimConnectionCredential> {
6169+
const { mintScimConnectionCredential } = await import('./scim-connection-service.js');
6170+
return mintScimConnectionCredential(engine as never, this.resolveAuthSecret(), input);
6171+
}
6172+
61466173
/**
61476174
* Returns the data engine wired into this auth manager. Used by route
61486175
* handlers (e.g. bootstrap-status) that need to query identity tables

‎packages/plugins/plugin-auth/src/auth-plugin.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3874,6 +3874,51 @@ export class AuthPlugin implements Plugin {
38743874
}
38753875
});
38763876

3877+
// ────────────────────────────────────────────────────────────────────
3878+
// [#30958] ADR-0134 — admin: mint and revoke the bearer credentials SCIM
3879+
// connections authenticate with. The SCIM plugin verifies against
3880+
// `sys_scim_connection_credential` rows, which the data API never creates
3881+
// or updates (engine-owned, list only), so these two mounts are the
3882+
// credential lifecycle's only door. Same family as `set-user-manager`: the
3883+
// shared platform-admin gate runs first, ahead of any body read, and the
3884+
// store writes are credited to the admin. The minted bearer is answered
3885+
// once, `no-store`, and kept nowhere; see `scim-connection-admin.ts`.
3886+
{
3887+
const scimCredentialDeps = (actor: PlatformAdminActor): import('./scim-connection-admin.js').ScimCredentialAdminDeps => ({
3888+
getDataEngine: () => this.authManager!.getDataEngine(),
3889+
isScimEnabled: () => this.authManager!.isScimWired(),
3890+
mint: (engine, input) => this.authManager!.mintScimConnectionCredential(engine, input),
3891+
attributed: (fn) => runAttributedToUser(actor, fn),
3892+
logger: { warn: (m) => ctx.logger.warn(m), error: (m) => ctx.logger.error(m) },
3893+
});
3894+
rawApp.post(`${basePath}/admin/scim/mint-credential`, async (c: any) => {
3895+
try {
3896+
const actor = await gateAdmin(c);
3897+
if (actor instanceof Response) return actor;
3898+
const { runMintScimCredential } = await import('./scim-connection-admin.js');
3899+
const { status, body, headers } = await runMintScimCredential(scimCredentialDeps(actor), actor, c.req.raw);
3900+
return c.json(body, status as any, headers);
3901+
} catch (error) {
3902+
const err = error instanceof Error ? error : new Error(String(error));
3903+
ctx.logger.error('[AuthPlugin] admin/scim/mint-credential failed', err);
3904+
return c.json({ success: false, error: { code: 'INTERNAL_ERROR', message: err.message } }, 500);
3905+
}
3906+
});
3907+
rawApp.post(`${basePath}/admin/scim/revoke-credential`, async (c: any) => {
3908+
try {
3909+
const actor = await gateAdmin(c);
3910+
if (actor instanceof Response) return actor;
3911+
const { runRevokeScimCredential } = await import('./scim-connection-admin.js');
3912+
const { status, body } = await runRevokeScimCredential(scimCredentialDeps(actor), actor, c.req.raw);
3913+
return c.json(body, status as any);
3914+
} catch (error) {
3915+
const err = error instanceof Error ? error : new Error(String(error));
3916+
ctx.logger.error('[AuthPlugin] admin/scim/revoke-credential failed', err);
3917+
return c.json({ success: false, error: { code: 'INTERNAL_ERROR', message: err.message } }, 500);
3918+
}
3919+
});
3920+
}
3921+
38773922
// ────────────────────────────────────────────────────────────────────
38783923
// #2766 V1 — admin direct user management. `sys_user` CRUD is suppressed
38793924
// (managedBy better-auth), and until now the only add-a-teammate path was

‎packages/plugins/plugin-auth/src/auth-route-ledger.conformance.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,13 @@ describe('auth route ledger hygiene', () => {
246246
// below holds it to the same proof — better-auth serves no roster.
247247
'POST /api/v1/auth/admin/list-platform-admins',
248248
'POST /api/v1/auth/admin/oauth2/toggle-disabled',
249+
// [#30958] On this pin's own terms: `auth-plugin.ts` mounts both itself
250+
// (`rawApp.post` on the `${basePath}/admin/scim/mint-credential` and
251+
// `${basePath}/admin/scim/revoke-credential` templates, ahead of the
252+
// catch-all), and the `live.has(route)` loop below holds them to the
253+
// same proof — stable `@better-auth/scim` serves no credential endpoint.
254+
'POST /api/v1/auth/admin/scim/mint-credential',
255+
'POST /api/v1/auth/admin/scim/revoke-credential',
249256
// [#16678] The thirteenth, added on this pin's own terms exactly as the
250257
// set-initial-password row below was: `auth-plugin.ts` mounts it itself
251258
// (a `rawApp.post` on the `${basePath}/admin/set-user-manager`

‎packages/plugins/plugin-auth/src/auth-route-ledger.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ export const AUTH_ROUTE_LEDGER: readonly AuthRouteLedgerEntry[] = [
238238
{ route: 'POST /api/v1/auth/admin/import-users', family: 'objectstack-mount', source: 'objectstack', disposition: 'server-only', note: 'no SDK method builds this URL — objectui app-shell\'s identity-import wizard (views/identityImport.ts) posts it directly from the Users list; platform-admin gated (ADR-0068). A dedicated identity-import door rather than the generic sys_user import, because a direct ObjectQL write would bypass better-auth\'s password hashing and mint no sys_account credential' },
239239
{ route: 'POST /api/v1/auth/admin/list-platform-admins', family: 'objectstack-mount', source: 'objectstack', disposition: 'server-only', note: 'no SDK method builds this URL — the sys_user list_platform_admins action posts it directly from the Users list; answers who holds platform-admin standing (the last-admin guard\'s enumerator, each id confirmed by hasPlatformAdminStanding), served only to a caller who holds that standing; reads no body. ADR-0095 D3' },
240240
{ route: 'POST /api/v1/auth/admin/oauth2/toggle-disabled', family: 'objectstack-mount', source: 'objectstack', disposition: 'server-only', note: 'no SDK method builds this URL — the sys_oauth_application disable/enable actions post it directly; ObjectStack mount closing a vendor gap (better-auth\'s /admin/oauth2/update-client strips `disabled` from its body schema), platform-admin gated (ADR-0068)' },
241+
// #30958 — the tracker id stays HERE and never in the `note` string, per the
242+
// #16678 note below.
243+
{ route: 'POST /api/v1/auth/admin/scim/mint-credential', family: 'objectstack-mount', source: 'objectstack', disposition: 'server-only', requires: 'scim', note: 'no SDK method builds this URL — an operator script or the identity-provider setup posts it; mints the bearer a SCIM connection authenticates with (scim-connection-service.ts) and answers the plaintext once, Cache-Control: no-store — only its keyed one-way digest is stored, and nothing logs it; refused 503 SERVICE_UNAVAILABLE while SCIM is off; one sys_audit_log row per mint naming the admin and the connection, never the credential; platform-admin gated (ADR-0068), ADR-0134' },
244+
{ route: 'POST /api/v1/auth/admin/scim/revoke-credential', family: 'objectstack-mount', source: 'objectstack', disposition: 'server-only', note: 'no SDK method builds this URL — an operator script posts it with the credentialId the mint answered; switches that credential row inactive, so the SCIM verifier refuses its bearer from the next request on (rotation is a second mint for the same connection, then this); 404 for an unknown credential, 409 when already revoked; one sys_audit_log row per revoke naming the admin and the connection; served whether or not SCIM is on; platform-admin gated (ADR-0068), ADR-0134' },
241245
{ route: 'POST /api/v1/auth/admin/sso/register', family: 'objectstack-mount', source: 'objectstack', disposition: 'server-only', requires: 'sso', note: 'no SDK method builds this URL — the sys_sso_provider register action posts flat form fields; ObjectStack bridge re-dispatching into @better-auth/sso /sso/register, platform-admin gated ahead of the delegation (ADR-0068 D4), so registering an identity provider never rests on the vendor\'s own check alone. Distinct path from the vendor\'s own /sso/register, which the catch-all serves' },
242246
{ route: 'POST /api/v1/auth/admin/sso/register-saml', family: 'objectstack-mount', source: 'objectstack', disposition: 'server-only', requires: 'sso', note: 'no SDK method builds this URL — the sys_sso_provider register_saml_provider action posts flat fields the bridge reshapes into better-auth\'s nested samlConfig; platform-admin gated ahead of the delegation (ADR-0068 D4), ADR-0069 P3' },
243247
{ route: 'POST /api/v1/auth/admin/sso/request-domain-verification', family: 'objectstack-mount', source: 'objectstack', disposition: 'server-only', requires: 'sso', note: 'no SDK method builds this URL — the sys_sso_provider action posts it and renders the returned DNS TXT record; ObjectStack bridge over @better-auth/sso, additionally gated on the opt-in ssoDomainVerification switch (OS_SSO_DOMAIN_VERIFICATION) — off means the inner endpoint 404s, the mount itself is unconditional; platform-admin gated (ADR-0068 D4), ADR-0135 D6' },

0 commit comments

Comments
 (0)