Skip to content

Commit ff3c8f8

Browse files
committed
fix(plugin-auth): read a SCIM connection's organization from its live credentials only
Claude-Session: https://claude.ai/code/session_01FEkQ5mEnsnzoPLq43wQyYy Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ef76a59 commit ff3c8f8

2 files changed

Lines changed: 35 additions & 15 deletions

File tree

‎packages/plugins/plugin-auth/src/idp-group-mapping-admin.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import type { IDataEngine } from '@objectstack/spec/contracts';
4646
import { withSystemContext } from './objectql-adapter.js';
4747
import type { PlatformAdminActor } from './platform-admin-gate.js';
4848
import { readJsonRequestBody } from './request-json-body.js';
49-
import { SCIM_CREDENTIAL_OBJECT } from './scim-connection-service.js';
49+
import { SCIM_CREDENTIAL_OBJECT, scimCredentialIsLive } from './scim-connection-service.js';
5050
import {
5151
IDP_GROUP_MAPPING_OBJECT,
5252
SCIM_MAPPING_SOURCE_KIND,
@@ -124,21 +124,29 @@ function readText(body: Record<string, unknown>, key: string, max: number, refus
124124
return trimmed === '' ? undefined : trimmed;
125125
}
126126

127-
/** The connection's credential rows: its owning organization and its provisioning domains. */
127+
/**
128+
* The connection's credential rows: its owning organization, read from its LIVE
129+
* credentials only (what the bearer verifier accepts: `scimCredentialIsLive`),
130+
* and its provisioning domains, read from every row. A revoked credential stays
131+
* stored (revoke switches `active` off), so counting it would let a credential
132+
* revoked in one organization make the connection ambiguous forever once a new
133+
* one is minted in another; the domains still include it, so a delete re-projects
134+
* every domain the connection ever provisioned.
135+
*/
128136
async function readConnection(
129137
engine: IDataEngine,
130138
connectionId: string,
131139
): Promise<{ found: false } | { found: true; organizations: string[]; domains: string[] }> {
132140
const rows = await engine.find(
133141
SCIM_CREDENTIAL_OBJECT,
134-
{ where: { connection_id: connectionId }, fields: ['organization_id', 'provisioning_domain_id'] },
142+
{ where: { connection_id: connectionId }, fields: ['organization_id', 'provisioning_domain_id', 'active', 'expires_at'] },
135143
{ context: SYSTEM_CTX } as never,
136144
);
137145
const list = Array.isArray(rows) ? (rows as Record<string, unknown>[]) : [];
138146
if (list.length === 0) return { found: false };
139147
return {
140148
found: true,
141-
organizations: [...new Set(list.map((r) => text(r.organization_id)))],
149+
organizations: [...new Set(list.filter((r) => scimCredentialIsLive(r)).map((r) => text(r.organization_id)))],
142150
domains: [...new Set(list.map((r) => text(r.provisioning_domain_id) || connectionId))],
143151
};
144152
}
@@ -234,6 +242,9 @@ export async function runSetIdpGroupMapping(
234242
if (!connection.found) {
235243
return refuseFields([{ field: 'connectionId', code: 'reference_not_found', message: `No SCIM connection '${connectionId}' exists: mint its credential first.` }]);
236244
}
245+
if (connection.organizations.length === 0) {
246+
return refuseFields([{ field: 'connectionId', code: 'rule_violation', message: `SCIM connection '${connectionId}' has no live credential (every one is revoked or expired), so no organization owns it. Mint a credential for it first.` }]);
247+
}
237248
if (connection.organizations.includes('')) {
238249
return refuseFields([{ field: 'connectionId', code: 'rule_violation', message: `SCIM connection '${connectionId}' has a credential owned by no organization, so a grant it makes would belong to no organization. Mint the connection's credential in an organization first.` }]);
239250
}

‎packages/plugins/plugin-auth/src/scim-connection-service.ts‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,24 @@ export interface ScimBearerVerification {
273273
expiresAt?: Date;
274274
}
275275

276+
/**
277+
* Is credential `row` live — affirmatively `active` and not expired? The one
278+
* reading of a credential row's liveness: the bearer verifier below refuses a
279+
* row this answers `false` for, and the IdP group mapping door reads a
280+
* connection's owning organization from live rows only, so a revoked credential
281+
* never decides anything.
282+
*/
283+
export function scimCredentialIsLive(row: Record<string, unknown>, now: number = Date.now()): boolean {
284+
// sqlite hands booleans back as 0/1 — refuse anything not affirmatively active.
285+
const active = row.active;
286+
if (active === false || active === 0 || active == null) return false;
287+
const rawExpiry = row.expires_at;
288+
if (rawExpiry == null) return true;
289+
const parsed = new Date(rawExpiry as string);
290+
if (Number.isNaN(parsed.getTime())) return false; // unreadable expiry fails closed
291+
return parsed.getTime() > now;
292+
}
293+
276294
/**
277295
* Application-owned bearer verification for the stable scim plugin: digest the
278296
* presented token, probe the credential store by digest (unique index), refuse
@@ -295,18 +313,9 @@ export async function verifyScimBearerToken(
295313
// A storage fault reads as "cannot verify", never as "verified".
296314
return null;
297315
}
298-
if (!row) return null;
299-
// sqlite hands booleans back as 0/1 — refuse anything not affirmatively active.
300-
const active = (row as { active?: unknown }).active;
301-
if (active === false || active === 0 || active == null) return null;
316+
if (!row || !scimCredentialIsLive(row)) return null;
302317
const rawExpiry = (row as { expires_at?: unknown }).expires_at;
303-
let expiresAt: Date | undefined;
304-
if (rawExpiry != null) {
305-
const parsed = new Date(rawExpiry as string);
306-
if (Number.isNaN(parsed.getTime())) return null; // unreadable expiry fails closed
307-
if (parsed.getTime() <= Date.now()) return null;
308-
expiresAt = parsed;
309-
}
318+
const expiresAt = rawExpiry != null ? new Date(rawExpiry as string) : undefined;
310319
const connectionId = String((row as { connection_id?: unknown }).connection_id ?? '');
311320
if (!connectionId) return null;
312321
const domain = (row as { provisioning_domain_id?: unknown }).provisioning_domain_id;

0 commit comments

Comments
 (0)