|
16 | 16 | import { describe, it, expect, vi } from 'vitest'; |
17 | 17 |
|
18 | 18 | import { PLATFORM_CAPABILITIES } from '@objectstack/spec/security'; |
| 19 | +import { assembleExecutionContext } from '@objectstack/core'; |
19 | 20 |
|
20 | 21 | import { HttpDispatcher } from '../http-dispatcher.js'; |
21 | 22 | import type { HttpProtocolContext } from '../http-dispatcher.js'; |
22 | | -import { CONNECTOR_CONNECTION_CAPABILITY, CONNECTOR_OAUTH_CALLBACK_QUERY_PARAMS } from './automation.js'; |
| 23 | +import { |
| 24 | + CONNECTOR_CONNECTION_CAPABILITY, |
| 25 | + CONNECTOR_OAUTH_CALLBACK_QUERY_PARAMS, |
| 26 | + connectorConnectionDelegatedCaller, |
| 27 | + connectorConnectionDelegatedCallerRefusal, |
| 28 | +} from './automation.js'; |
23 | 29 |
|
24 | 30 | /** An authorization code as a callback carries it — a dummy. */ |
25 | 31 | const AUTH_CODE = 'dummy-authorization-code'; |
@@ -255,3 +261,119 @@ describe('ADR-0143 D5 — connector connection door: what it accepts', () => { |
255 | 261 | } |
256 | 262 | }); |
257 | 263 | }); |
| 264 | + |
| 265 | +// ADR-0143 D5 · ruling A (record 6548764795): connecting, reconnecting and |
| 266 | +// revoking an organization's connection is a human administrator's act. The |
| 267 | +// begin (`POST`) and revoke (`DELETE`) legs refuse a caller that acts for a |
| 268 | +// person instead of being that person signed in, by name, whatever it holds; |
| 269 | +// the read leg and the callback are unchanged. |
| 270 | +describe('ADR-0143 D5 — connector connection door: begin and revoke refuse a delegated caller by name', () => { |
| 271 | + /** The organization administrator's resolved authorization, as core's resolver hands it to the assembler. */ |
| 272 | + const holderAuthz = { |
| 273 | + userId: 'u_orgadmin', |
| 274 | + tenantId: 'org_a', |
| 275 | + positions: ['organization_admin'], |
| 276 | + permissions: [], |
| 277 | + directPermissions: [], |
| 278 | + systemPermissions: ORG_ADMIN_GRANT, |
| 279 | + org_user_ids: ['u_orgadmin'], |
| 280 | + accessible_org_ids: ['org_a'], |
| 281 | + }; |
| 282 | + const ENTRY = { localization: undefined, requestLocale: undefined, accessToken: undefined, authGate: undefined } as const; |
| 283 | + |
| 284 | + /** The holder, signed in: a session admitted them. */ |
| 285 | + const HOLDER_SIGNED_IN = (): HttpProtocolContext => ({ |
| 286 | + request: {}, |
| 287 | + admission: { credential: 'session', authGate: undefined }, |
| 288 | + executionContext: assembleExecutionContext({ authz: holderAuthz, oauth: undefined, ...ENTRY }), |
| 289 | + } as HttpProtocolContext); |
| 290 | + |
| 291 | + /** |
| 292 | + * An AI agent the holder connected over OAuth with `actions:execute`, as the |
| 293 | + * shared assembler builds its envelope: `principalKind: 'agent'`, |
| 294 | + * `onBehalfOf` the holder, and the holder's own capability list. |
| 295 | + */ |
| 296 | + const AGENT_FOR_HOLDER = (): HttpProtocolContext => ({ |
| 297 | + request: {}, |
| 298 | + admission: { credential: 'session', authGate: undefined }, |
| 299 | + executionContext: assembleExecutionContext({ |
| 300 | + authz: holderAuthz, |
| 301 | + oauth: { userId: 'u_orgadmin', scopes: ['openid', 'actions:execute'], clientId: 'client_agent', scopePermissions: [], delegatesActions: true }, |
| 302 | + ...ENTRY, |
| 303 | + }), |
| 304 | + } as HttpProtocolContext); |
| 305 | + |
| 306 | + /** An OAuth access token that names no client: the principal stays the holder, the credential is still the token. */ |
| 307 | + const OAUTH_TOKEN_FOR_HOLDER = (): HttpProtocolContext => ({ |
| 308 | + request: {}, |
| 309 | + admission: { credential: 'session', authGate: undefined }, |
| 310 | + executionContext: assembleExecutionContext({ |
| 311 | + authz: holderAuthz, |
| 312 | + oauth: { userId: 'u_orgadmin', scopes: ['openid', 'actions:execute'], scopePermissions: [], delegatesActions: true }, |
| 313 | + ...ENTRY, |
| 314 | + }), |
| 315 | + } as HttpProtocolContext); |
| 316 | + |
| 317 | + /** An API key the holder minted: it resolves as the holder, admitted by the key. */ |
| 318 | + const API_KEY_FOR_HOLDER = (): HttpProtocolContext => ({ |
| 319 | + request: {}, |
| 320 | + admission: { credential: 'api_key', authGate: undefined }, |
| 321 | + executionContext: assembleExecutionContext({ authz: holderAuthz, oauth: undefined, ...ENTRY }), |
| 322 | + } as HttpProtocolContext); |
| 323 | + |
| 324 | + const DELEGATED = [ |
| 325 | + ['agent', AGENT_FOR_HOLDER, 'an agent acting on a person\'s behalf'], |
| 326 | + ['oauth', OAUTH_TOKEN_FOR_HOLDER, 'an OAuth access token'], |
| 327 | + ['api_key', API_KEY_FOR_HOLDER, 'an API key'], |
| 328 | + ] as const; |
| 329 | + |
| 330 | + it('the fixtures are what they say: each delegated caller holds the capability its human holds', () => { |
| 331 | + expect(HOLDER_SIGNED_IN().executionContext).toMatchObject({ principalKind: 'human', systemPermissions: ORG_ADMIN_GRANT }); |
| 332 | + expect(AGENT_FOR_HOLDER().executionContext).toMatchObject({ principalKind: 'agent', onBehalfOf: { userId: 'u_orgadmin' }, systemPermissions: ORG_ADMIN_GRANT }); |
| 333 | + expect(OAUTH_TOKEN_FOR_HOLDER().executionContext).toMatchObject({ principalKind: 'human', oauthScopes: ['openid', 'actions:execute'], systemPermissions: ORG_ADMIN_GRANT }); |
| 334 | + expect(API_KEY_FOR_HOLDER().executionContext).toMatchObject({ principalKind: 'human', systemPermissions: ORG_ADMIN_GRANT }); |
| 335 | + for (const [kind, ctx] of DELEGATED) expect(connectorConnectionDelegatedCaller(ctx())).toBe(kind); |
| 336 | + expect(connectorConnectionDelegatedCaller(HOLDER_SIGNED_IN())).toBeUndefined(); |
| 337 | + }); |
| 338 | + |
| 339 | + it.each(DELEGATED)('%s: refused on begin and on revoke, 403 naming its kind and the rule, and the service is never reached', async (kind, ctx, named) => { |
| 340 | + for (const [verb, member] of [['POST', (h: Harness) => h.begin], ['DELETE', (h: Harness) => h.revoke]] as const) { |
| 341 | + const h = boot(); |
| 342 | + const { response } = await connection(h, verb, ctx()); |
| 343 | + expect(statusOf(response)).toBe(403); |
| 344 | + expect(codeOf(response)).toBe('PERMISSION_DENIED'); |
| 345 | + expect(messageOf(response)).toBe(connectorConnectionDelegatedCallerRefusal(kind)); |
| 346 | + expect(messageOf(response)).toContain(named); |
| 347 | + expect(messageOf(response)).toContain('human administrator'); |
| 348 | + expect(member(h)).not.toHaveBeenCalled(); |
| 349 | + for (const m of [h.get, h.begin, h.complete, h.revoke]) expect(m).not.toHaveBeenCalled(); |
| 350 | + } |
| 351 | + }); |
| 352 | + |
| 353 | + it('the same human, signed in, is admitted on begin and on revoke', async () => { |
| 354 | + const h = boot(); |
| 355 | + const begun = await connection(h, 'POST', HOLDER_SIGNED_IN()); |
| 356 | + expect(statusOf(begun.response)).toBe(200); |
| 357 | + expect(h.begin).toHaveBeenCalledWith({ connector: 'crm', context: expect.objectContaining({ userId: 'u_orgadmin', tenantId: 'org_a' }) }); |
| 358 | + const revoked = await connection(h, 'DELETE', HOLDER_SIGNED_IN()); |
| 359 | + expect(statusOf(revoked.response)).toBe(200); |
| 360 | + expect(h.revoke).toHaveBeenCalledTimes(1); |
| 361 | + }); |
| 362 | + |
| 363 | + it('a system call is admitted on begin and on revoke, even carrying an API-key admission', async () => { |
| 364 | + const h = boot(); |
| 365 | + const sys = { request: {}, admission: { credential: 'api_key', authGate: undefined }, executionContext: { userId: 'system', isSystem: true } } as HttpProtocolContext; |
| 366 | + expect(statusOf((await connection(h, 'POST', sys)).response)).toBe(200); |
| 367 | + expect(statusOf((await connection(h, 'DELETE', sys)).response)).toBe(200); |
| 368 | + expect(h.begin).toHaveBeenCalledTimes(1); |
| 369 | + expect(h.revoke).toHaveBeenCalledTimes(1); |
| 370 | + }); |
| 371 | + |
| 372 | + it.each(DELEGATED)('%s: the read leg and the callback answer as before (admitted for a holder)', async (_kind, ctx) => { |
| 373 | + const h = boot(); |
| 374 | + expect(statusOf((await connection(h, 'GET', ctx())).response)).toBe(200); |
| 375 | + expect(statusOf((await callback(h, ctx(), { code: AUTH_CODE, state: 's' })).response)).toBe(200); |
| 376 | + expect(h.get).toHaveBeenCalledTimes(1); |
| 377 | + expect(h.complete).toHaveBeenCalledTimes(1); |
| 378 | + }); |
| 379 | +}); |
0 commit comments