@xemahq/authorization-nest 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/dist/authorization.module.d.ts.map +1 -1
  2. package/dist/authorization.module.js +16 -1
  3. package/dist/authorization.module.js.map +1 -1
  4. package/dist/durable-delegation/decision-codes.d.ts +6 -0
  5. package/dist/durable-delegation/decision-codes.d.ts.map +1 -0
  6. package/dist/durable-delegation/decision-codes.js +14 -0
  7. package/dist/durable-delegation/decision-codes.js.map +1 -0
  8. package/dist/durable-delegation/holder-client.d.ts +65 -0
  9. package/dist/durable-delegation/holder-client.d.ts.map +1 -0
  10. package/dist/durable-delegation/holder-client.js +113 -0
  11. package/dist/durable-delegation/holder-client.js.map +1 -0
  12. package/dist/durable-delegation/holder-errors.d.ts +22 -0
  13. package/dist/durable-delegation/holder-errors.d.ts.map +1 -0
  14. package/dist/durable-delegation/holder-errors.js +44 -0
  15. package/dist/durable-delegation/holder-errors.js.map +1 -0
  16. package/dist/durable-delegation/holder-lifecycle.d.ts +43 -0
  17. package/dist/durable-delegation/holder-lifecycle.d.ts.map +1 -0
  18. package/dist/durable-delegation/holder-lifecycle.js +173 -0
  19. package/dist/durable-delegation/holder-lifecycle.js.map +1 -0
  20. package/dist/grant-version.reader.d.ts +19 -0
  21. package/dist/grant-version.reader.d.ts.map +1 -0
  22. package/dist/grant-version.reader.js +71 -0
  23. package/dist/grant-version.reader.js.map +1 -0
  24. package/dist/index.d.ts +6 -0
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +6 -0
  27. package/dist/index.js.map +1 -1
  28. package/dist/route-action-evaluator.d.ts +38 -0
  29. package/dist/route-action-evaluator.d.ts.map +1 -0
  30. package/dist/route-action-evaluator.js +146 -0
  31. package/dist/route-action-evaluator.js.map +1 -0
  32. package/package.json +6 -6
@@ -1 +1 @@
1
- {"version":3,"file":"authorization.module.d.ts","sourceRoot":"","sources":["../src/authorization.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE5D,OAAO,EAGL,KAAK,wBAAwB,EAC9B,MAAM,yBAAyB,CAAC;AAqBjC,qBACa,uBAAuB;WACpB,OAAO,CACnB,OAAO,CAAC,EAAE,OAAO,CAAC,wBAAwB,CAAC,GAC1C,aAAa;CAkBjB"}
1
+ {"version":3,"file":"authorization.module.d.ts","sourceRoot":"","sources":["../src/authorization.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE5D,OAAO,EAGL,KAAK,wBAAwB,EAC9B,MAAM,yBAAyB,CAAC;AA0BjC,qBACa,uBAAuB;WACpB,OAAO,CACnB,OAAO,CAAC,EAAE,OAAO,CAAC,wBAAwB,CAAC,GAC1C,aAAa;CA+BjB"}
@@ -12,7 +12,9 @@ const common_1 = require("@nestjs/common");
12
12
  const authorization_options_1 = require("./authorization.options");
13
13
  const authorize_decorator_1 = require("./authorize.decorator");
14
14
  const authzen_transport_1 = require("./authzen.transport");
15
+ const grant_version_reader_1 = require("./grant-version.reader");
15
16
  const policy_decisions_1 = require("./policy-decisions");
17
+ const route_action_evaluator_1 = require("./route-action-evaluator");
16
18
  const xema_authz_service_1 = require("./xema-authz.service");
17
19
  let XemaAuthorizationModule = XemaAuthorizationModule_1 = class XemaAuthorizationModule {
18
20
  static forRoot(options) {
@@ -28,9 +30,22 @@ let XemaAuthorizationModule = XemaAuthorizationModule_1 = class XemaAuthorizatio
28
30
  { provide: authzen_transport_1.AUTHZEN_PDP_TRANSPORT, useExisting: authzen_transport_1.HttpAuthzenPdpTransport },
29
31
  xema_authz_service_1.XemaAuthz,
30
32
  policy_decisions_1.XemaPolicyDecisions,
33
+ grant_version_reader_1.HttpAuthorizationGrantVersionReader,
34
+ {
35
+ provide: grant_version_reader_1.AUTHORIZATION_GRANT_VERSION_READER,
36
+ useExisting: grant_version_reader_1.HttpAuthorizationGrantVersionReader,
37
+ },
38
+ route_action_evaluator_1.XemaRouteActionEvaluator,
39
+ authorize_decorator_1.XemaAuthorizeGuard,
40
+ ],
41
+ exports: [
42
+ xema_authz_service_1.XemaAuthz,
43
+ policy_decisions_1.XemaPolicyDecisions,
44
+ route_action_evaluator_1.XemaRouteActionEvaluator,
31
45
  authorize_decorator_1.XemaAuthorizeGuard,
46
+ authzen_transport_1.AUTHZEN_PDP_TRANSPORT,
47
+ grant_version_reader_1.AUTHORIZATION_GRANT_VERSION_READER,
32
48
  ],
33
- exports: [xema_authz_service_1.XemaAuthz, policy_decisions_1.XemaPolicyDecisions, authorize_decorator_1.XemaAuthorizeGuard, authzen_transport_1.AUTHZEN_PDP_TRANSPORT],
34
49
  };
35
50
  }
36
51
  };
@@ -1 +1 @@
1
- {"version":3,"file":"authorization.module.js","sourceRoot":"","sources":["../src/authorization.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAA4D;AAE5D,mEAIiC;AACjC,+DAA2D;AAC3D,2DAG6B;AAC7B,yDAAyD;AACzD,6DAAiD;AAe1C,IAAM,uBAAuB,+BAA7B,MAAM,uBAAuB;IAC3B,MAAM,CAAC,OAAO,CACnB,OAA2C;QAE3C,OAAO;YACL,MAAM,EAAE,yBAAuB;YAC/B,MAAM,EAAE,IAAI;YACZ,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,kDAA0B;oBACnC,QAAQ,EAAE,IAAA,uDAA+B,EAAC,OAAO,CAAC;iBACnD;gBACD,2CAAuB;gBACvB,EAAE,OAAO,EAAE,yCAAqB,EAAE,WAAW,EAAE,2CAAuB,EAAE;gBACxE,8BAAS;gBACT,sCAAmB;gBACnB,wCAAkB;aACnB;YACD,OAAO,EAAE,CAAC,8BAAS,EAAE,sCAAmB,EAAE,wCAAkB,EAAE,yCAAqB,CAAC;SACrF,CAAC;IACJ,CAAC;CACF,CAAA;AArBY,0DAAuB;kCAAvB,uBAAuB;IADnC,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,uBAAuB,CAqBnC"}
1
+ {"version":3,"file":"authorization.module.js","sourceRoot":"","sources":["../src/authorization.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAA4D;AAE5D,mEAIiC;AACjC,+DAA2D;AAC3D,2DAG6B;AAC7B,iEAGgC;AAChC,yDAAyD;AACzD,qEAAoE;AACpE,6DAAiD;AAe1C,IAAM,uBAAuB,+BAA7B,MAAM,uBAAuB;IAC3B,MAAM,CAAC,OAAO,CACnB,OAA2C;QAE3C,OAAO;YACL,MAAM,EAAE,yBAAuB;YAC/B,MAAM,EAAE,IAAI;YACZ,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,kDAA0B;oBACnC,QAAQ,EAAE,IAAA,uDAA+B,EAAC,OAAO,CAAC;iBACnD;gBACD,2CAAuB;gBACvB,EAAE,OAAO,EAAE,yCAAqB,EAAE,WAAW,EAAE,2CAAuB,EAAE;gBACxE,8BAAS;gBACT,sCAAmB;gBACnB,0DAAmC;gBACnC;oBACE,OAAO,EAAE,yDAAkC;oBAC3C,WAAW,EAAE,0DAAmC;iBACjD;gBACD,iDAAwB;gBACxB,wCAAkB;aACnB;YACD,OAAO,EAAE;gBACP,8BAAS;gBACT,sCAAmB;gBACnB,iDAAwB;gBACxB,wCAAkB;gBAClB,yCAAqB;gBACrB,yDAAkC;aACnC;SACF,CAAC;IACJ,CAAC;CACF,CAAA;AAlCY,0DAAuB;kCAAvB,uBAAuB;IADnC,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,uBAAuB,CAkCnC"}
@@ -0,0 +1,6 @@
1
+ export declare enum DurableDelegationDecisionCode {
2
+ AuthorityUnavailable = "DURABLE_DELEGATION_AUTHORITY_UNAVAILABLE",
3
+ Ended = "DURABLE_DELEGATION_ENDED"
4
+ }
5
+ export declare function isDurableDelegationDecisionCode(reason: string | undefined): reason is DurableDelegationDecisionCode;
6
+ //# sourceMappingURL=decision-codes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decision-codes.d.ts","sourceRoot":"","sources":["../../src/durable-delegation/decision-codes.ts"],"names":[],"mappings":"AAgBA,oBAAY,6BAA6B;IACvC,oBAAoB,6CAA6C;IACjE,KAAK,6BAA6B;CACnC;AAGD,wBAAgB,+BAA+B,CAC7C,MAAM,EAAE,MAAM,GAAG,SAAS,GACzB,MAAM,IAAI,6BAA6B,CAKzC"}
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DurableDelegationDecisionCode = void 0;
4
+ exports.isDurableDelegationDecisionCode = isDurableDelegationDecisionCode;
5
+ var DurableDelegationDecisionCode;
6
+ (function (DurableDelegationDecisionCode) {
7
+ DurableDelegationDecisionCode["AuthorityUnavailable"] = "DURABLE_DELEGATION_AUTHORITY_UNAVAILABLE";
8
+ DurableDelegationDecisionCode["Ended"] = "DURABLE_DELEGATION_ENDED";
9
+ })(DurableDelegationDecisionCode || (exports.DurableDelegationDecisionCode = DurableDelegationDecisionCode = {}));
10
+ function isDurableDelegationDecisionCode(reason) {
11
+ return (reason === DurableDelegationDecisionCode.AuthorityUnavailable ||
12
+ reason === DurableDelegationDecisionCode.Ended);
13
+ }
14
+ //# sourceMappingURL=decision-codes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decision-codes.js","sourceRoot":"","sources":["../../src/durable-delegation/decision-codes.ts"],"names":[],"mappings":";;;AAsBA,0EAOC;AAbD,IAAY,6BAGX;AAHD,WAAY,6BAA6B;IACvC,kGAAiE,CAAA;IACjE,mEAAkC,CAAA;AACpC,CAAC,EAHW,6BAA6B,6CAA7B,6BAA6B,QAGxC;AAGD,SAAgB,+BAA+B,CAC7C,MAA0B;IAE1B,OAAO,CACL,MAAM,KAAK,6BAA6B,CAAC,oBAAoB;QAC7D,MAAM,KAAK,6BAA6B,CAAC,KAAK,CAC/C,CAAC;AACJ,CAAC"}
@@ -0,0 +1,65 @@
1
+ import { type ServiceRegistry } from '@xemahq/contracts/service-registry';
2
+ import { IdentityBootstrapService } from '@xemahq/identity-client';
3
+ import { type ForwardedCallerContext } from '@xemahq/platform-common';
4
+ export declare enum DurableDelegationOwnerKind {
5
+ User = "USER",
6
+ Team = "TEAM",
7
+ Org = "ORG"
8
+ }
9
+ export declare enum DurableDelegationRecordState {
10
+ Provisioning = "PROVISIONING",
11
+ Active = "ACTIVE",
12
+ Revoked = "REVOKED",
13
+ Expired = "EXPIRED"
14
+ }
15
+ export interface DurableDelegationCreate {
16
+ readonly owner: {
17
+ readonly kind: DurableDelegationOwnerKind;
18
+ readonly id: string;
19
+ };
20
+ readonly scope: readonly string[];
21
+ readonly capabilities: readonly string[];
22
+ readonly integrationId: string;
23
+ readonly environmentId: string;
24
+ readonly requestKey: string;
25
+ readonly holderMember?: string;
26
+ readonly expiresAt?: Date;
27
+ }
28
+ export interface DurableDelegationView {
29
+ readonly id: string;
30
+ readonly state: DurableDelegationRecordState;
31
+ readonly endReason?: string;
32
+ readonly owner: {
33
+ readonly kind: DurableDelegationOwnerKind;
34
+ readonly id: string;
35
+ };
36
+ readonly scope: readonly string[];
37
+ readonly capabilities: readonly string[];
38
+ readonly integrationId: string;
39
+ readonly environmentId: string;
40
+ readonly holderWorkload: string;
41
+ readonly carrierId?: string;
42
+ readonly expiresAt?: string;
43
+ readonly lastDenial?: {
44
+ readonly at: string;
45
+ readonly check: string;
46
+ readonly right: string;
47
+ readonly scope: string;
48
+ };
49
+ }
50
+ export declare class DurableDelegationRequestError extends Error {
51
+ readonly status: number;
52
+ readonly code: string | undefined;
53
+ constructor(message: string, status: number, code: string | undefined);
54
+ }
55
+ export declare class DurableDelegationHolderClient {
56
+ private readonly registry;
57
+ private readonly identity;
58
+ constructor(registry: ServiceRegistry, identity: IdentityBootstrapService);
59
+ create(author: ForwardedCallerContext, request: DurableDelegationCreate): Promise<DurableDelegationView>;
60
+ read(orgId: string, delegationId: string): Promise<DurableDelegationView>;
61
+ revoke(orgId: string, delegationId: string): Promise<DurableDelegationView>;
62
+ mintContext(carrierId: string): Promise<string>;
63
+ private send;
64
+ }
65
+ //# sourceMappingURL=holder-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"holder-client.d.ts","sourceRoot":"","sources":["../../src/durable-delegation/holder-client.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,eAAe,EACrB,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AACnE,OAAO,EAGL,KAAK,sBAAsB,EAC5B,MAAM,yBAAyB,CAAC;AAOjC,oBAAY,0BAA0B;IACpC,IAAI,SAAS;IACb,IAAI,SAAS;IACb,GAAG,QAAQ;CACZ;AAGD,oBAAY,4BAA4B;IACtC,YAAY,iBAAiB;IAC7B,MAAM,WAAW;IACjB,OAAO,YAAY;IACnB,OAAO,YAAY;CACpB;AAGD,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,KAAK,EAAE;QAAE,QAAQ,CAAC,IAAI,EAAE,0BAA0B,CAAC;QAAC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAA;KAAE,CAAC;IACnF,QAAQ,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;IAClC,QAAQ,CAAC,YAAY,EAAE,SAAS,MAAM,EAAE,CAAC;IACzC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAE/B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAE/B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAE/B,QAAQ,CAAC,SAAS,CAAC,EAAE,IAAI,CAAC;CAC3B;AAGD,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,KAAK,EAAE,4BAA4B,CAAC;IAC7C,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC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AAC,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,qBAAqB,CAAC;IAc3E,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;YAQ9C,IAAI;CA8BnB"}
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.DurableDelegationHolderClient = exports.DurableDelegationRequestError = exports.DurableDelegationRecordState = exports.DurableDelegationOwnerKind = void 0;
16
+ const common_1 = require("@nestjs/common");
17
+ const service_registry_1 = require("@xemahq/contracts/service-registry");
18
+ const identity_client_1 = require("@xemahq/identity-client");
19
+ const platform_common_1 = require("@xemahq/platform-common");
20
+ const service_registry_nest_1 = require("@xemahq/service-registry-nest");
21
+ var DurableDelegationOwnerKind;
22
+ (function (DurableDelegationOwnerKind) {
23
+ DurableDelegationOwnerKind["User"] = "USER";
24
+ DurableDelegationOwnerKind["Team"] = "TEAM";
25
+ DurableDelegationOwnerKind["Org"] = "ORG";
26
+ })(DurableDelegationOwnerKind || (exports.DurableDelegationOwnerKind = DurableDelegationOwnerKind = {}));
27
+ var DurableDelegationRecordState;
28
+ (function (DurableDelegationRecordState) {
29
+ DurableDelegationRecordState["Provisioning"] = "PROVISIONING";
30
+ DurableDelegationRecordState["Active"] = "ACTIVE";
31
+ DurableDelegationRecordState["Revoked"] = "REVOKED";
32
+ DurableDelegationRecordState["Expired"] = "EXPIRED";
33
+ })(DurableDelegationRecordState || (exports.DurableDelegationRecordState = DurableDelegationRecordState = {}));
34
+ class DurableDelegationRequestError extends Error {
35
+ status;
36
+ code;
37
+ constructor(message, status, code) {
38
+ super(message);
39
+ this.status = status;
40
+ this.code = code;
41
+ this.name = 'DurableDelegationRequestError';
42
+ }
43
+ }
44
+ exports.DurableDelegationRequestError = DurableDelegationRequestError;
45
+ const DELEGATED_CONTEXT_AUDIENCE = service_registry_1.ServiceName.XEMA_CAPABILITY_ROUTER;
46
+ let DurableDelegationHolderClient = class DurableDelegationHolderClient {
47
+ registry;
48
+ identity;
49
+ constructor(registry, identity) {
50
+ this.registry = registry;
51
+ this.identity = identity;
52
+ }
53
+ async create(author, request) {
54
+ return this.send('POST', '/durable-delegations', author.orgId, {
55
+ ...(await (0, platform_common_1.forwardedCallerHeaders)(author, this.identity)),
56
+ }, {
57
+ owner: request.owner,
58
+ scope: [...request.scope],
59
+ capabilities: [...request.capabilities],
60
+ integrationId: request.integrationId,
61
+ environmentId: request.environmentId,
62
+ requestKey: request.requestKey,
63
+ ...(request.holderMember === undefined ? {} : { holderMember: request.holderMember }),
64
+ ...(request.expiresAt === undefined ? {} : { expiresAt: request.expiresAt.toISOString() }),
65
+ });
66
+ }
67
+ async read(orgId, delegationId) {
68
+ return this.send('GET', `/internal/durable-delegations/${encodeURIComponent(delegationId)}`, orgId, { Authorization: `Bearer ${await this.identity.getAccessToken()}` });
69
+ }
70
+ async revoke(orgId, delegationId) {
71
+ return this.send('POST', `/internal/durable-delegations/${encodeURIComponent(delegationId)}/revoke`, orgId, { Authorization: `Bearer ${await this.identity.getAccessToken()}` });
72
+ }
73
+ async mintContext(carrierId) {
74
+ const minted = await this.identity.mintDelegatedContext({
75
+ delegationId: carrierId,
76
+ audience: DELEGATED_CONTEXT_AUDIENCE,
77
+ });
78
+ return minted.accessToken;
79
+ }
80
+ async send(method, path, orgId, headers, body) {
81
+ const baseUrl = await (0, service_registry_nest_1.resolveHttpUrl)(this.registry, service_registry_1.ServiceName.AUTHORIZATION_API);
82
+ const response = await fetch(`${baseUrl.replace(/\/+$/, '')}${path}`, {
83
+ method,
84
+ headers: {
85
+ ...headers,
86
+ 'X-Xema-Org-Id': orgId,
87
+ ...(body === undefined ? {} : { 'Content-Type': 'application/json' }),
88
+ },
89
+ ...(body === undefined ? {} : { body: JSON.stringify(body) }),
90
+ });
91
+ if (!response.ok) {
92
+ const text = await response.text();
93
+ throw new DurableDelegationRequestError(`authorization-api ${method} ${path} failed with HTTP ${response.status}: ${text.slice(0, 2000)}`, response.status, codeOf(text));
94
+ }
95
+ return (0, platform_common_1.unwrapPlatformResponseEnvelope)(await response.json(), `authorization-api ${method} ${path}`);
96
+ }
97
+ };
98
+ exports.DurableDelegationHolderClient = DurableDelegationHolderClient;
99
+ exports.DurableDelegationHolderClient = DurableDelegationHolderClient = __decorate([
100
+ (0, common_1.Injectable)(),
101
+ __param(0, (0, common_1.Inject)(service_registry_nest_1.SERVICE_REGISTRY_TOKEN)),
102
+ __metadata("design:paramtypes", [Object, identity_client_1.IdentityBootstrapService])
103
+ ], DurableDelegationHolderClient);
104
+ function codeOf(text) {
105
+ try {
106
+ const parsed = JSON.parse(text);
107
+ return typeof parsed.code === 'string' ? parsed.code : undefined;
108
+ }
109
+ catch {
110
+ return undefined;
111
+ }
112
+ }
113
+ //# sourceMappingURL=holder-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"holder-client.js","sourceRoot":"","sources":["../../src/durable-delegation/holder-client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAoD;AACpD,yEAG4C;AAC5C,6DAAmE;AACnE,6DAIiC;AACjC,yEAGuC;AAGvC,IAAY,0B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GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAuB,CAAC;QACtD,OAAO,OAAO,MAAM,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;IACnE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC"}
@@ -0,0 +1,22 @@
1
+ export declare enum DelegationRequiredReason {
2
+ NeverAuthorised = "never-authorised",
3
+ Unprovisioned = "unprovisioned",
4
+ Ended = "ended",
5
+ Stale = "stale"
6
+ }
7
+ export declare class DelegationRequiredError extends Error {
8
+ readonly reason: DelegationRequiredReason;
9
+ readonly subject: string;
10
+ constructor(reason: DelegationRequiredReason, subject: string);
11
+ }
12
+ export declare class DelegationUseDeniedError extends Error {
13
+ readonly delegationId: string;
14
+ readonly detail: string;
15
+ constructor(delegationId: string, detail: string);
16
+ }
17
+ export declare class DelegationEndedError extends Error {
18
+ readonly delegationId: string;
19
+ readonly endReason: string | undefined;
20
+ constructor(delegationId: string, endReason: string | undefined);
21
+ }
22
+ //# sourceMappingURL=holder-errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"holder-errors.d.ts","sourceRoot":"","sources":["../../src/durable-delegation/holder-errors.ts"],"names":[],"mappings":"AAkBA,oBAAY,wBAAwB;IAElC,eAAe,qBAAqB;IAEpC,aAAa,kBAAkB;IAE/B,KAAK,UAAU;IAEf,KAAK,UAAU;CAChB;AAED,qBAAa,uBAAwB,SAAQ,KAAK;aAE9B,MAAM,EAAE,wBAAwB;aAChC,OAAO,EAAE,MAAM;gBADf,MAAM,EAAE,wBAAwB,EAChC,OAAO,EAAE,MAAM;CAKlC;AAED,qBAAa,wBAAyB,SAAQ,KAAK;aAE/B,YAAY,EAAE,MAAM;aACpB,MAAM,EAAE,MAAM;gBADd,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,MAAM;CAKjC;AAED,qBAAa,oBAAqB,SAAQ,KAAK;aAE3B,YAAY,EAAE,MAAM;aACpB,SAAS,EAAE,MAAM,GAAG,SAAS;gBAD7B,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE,MAAM,GAAG,SAAS;CAKhD"}
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DelegationEndedError = exports.DelegationUseDeniedError = exports.DelegationRequiredError = exports.DelegationRequiredReason = void 0;
4
+ var DelegationRequiredReason;
5
+ (function (DelegationRequiredReason) {
6
+ DelegationRequiredReason["NeverAuthorised"] = "never-authorised";
7
+ DelegationRequiredReason["Unprovisioned"] = "unprovisioned";
8
+ DelegationRequiredReason["Ended"] = "ended";
9
+ DelegationRequiredReason["Stale"] = "stale";
10
+ })(DelegationRequiredReason || (exports.DelegationRequiredReason = DelegationRequiredReason = {}));
11
+ class DelegationRequiredError extends Error {
12
+ reason;
13
+ subject;
14
+ constructor(reason, subject) {
15
+ super(`${subject} needs a person to authorise its durable work (${reason}).`);
16
+ this.reason = reason;
17
+ this.subject = subject;
18
+ this.name = 'DelegationRequiredError';
19
+ }
20
+ }
21
+ exports.DelegationRequiredError = DelegationRequiredError;
22
+ class DelegationUseDeniedError extends Error {
23
+ delegationId;
24
+ detail;
25
+ constructor(delegationId, detail) {
26
+ super(`authority currently unavailable for durable delegation ${delegationId}: ${detail}`);
27
+ this.delegationId = delegationId;
28
+ this.detail = detail;
29
+ this.name = 'DelegationUseDeniedError';
30
+ }
31
+ }
32
+ exports.DelegationUseDeniedError = DelegationUseDeniedError;
33
+ class DelegationEndedError extends Error {
34
+ delegationId;
35
+ endReason;
36
+ constructor(delegationId, endReason) {
37
+ super(`durable delegation ${delegationId} has ended${endReason === undefined ? '' : ` (${endReason})`}.`);
38
+ this.delegationId = delegationId;
39
+ this.endReason = endReason;
40
+ this.name = 'DelegationEndedError';
41
+ }
42
+ }
43
+ exports.DelegationEndedError = DelegationEndedError;
44
+ //# sourceMappingURL=holder-errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"holder-errors.js","sourceRoot":"","sources":["../../src/durable-delegation/holder-errors.ts"],"names":[],"mappings":";;;AAkBA,IAAY,wBASX;AATD,WAAY,wBAAwB;IAElC,gEAAoC,CAAA;IAEpC,2DAA+B,CAAA;IAE/B,2CAAe,CAAA;IAEf,2CAAe,CAAA;AACjB,CAAC,EATW,wBAAwB,wCAAxB,wBAAwB,QASnC;AAED,MAAa,uBAAwB,SAAQ,KAAK;IAE9B;IACA;IAFlB,YACkB,MAAgC,EAChC,OAAe;QAE/B,KAAK,CAAC,GAAG,OAAO,kDAAkD,MAAM,IAAI,CAAC,CAAC;QAH9D,WAAM,GAAN,MAAM,CAA0B;QAChC,YAAO,GAAP,OAAO,CAAQ;QAG/B,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;IACxC,CAAC;CACF;AARD,0DAQC;AAED,MAAa,wBAAyB,SAAQ,KAAK;IAE/B;IACA;IAFlB,YACkB,YAAoB,EACpB,MAAc;QAE9B,KAAK,CAAC,0DAA0D,YAAY,KAAK,MAAM,EAAE,CAAC,CAAC;QAH3E,iBAAY,GAAZ,YAAY,CAAQ;QACpB,WAAM,GAAN,MAAM,CAAQ;QAG9B,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IACzC,CAAC;CACF;AARD,4DAQC;AAED,MAAa,oBAAqB,SAAQ,KAAK;IAE3B;IACA;IAFlB,YACkB,YAAoB,EACpB,SAA6B;QAE7C,KAAK,CAAC,sBAAsB,YAAY,aAAa,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,SAAS,GAAG,GAAG,CAAC,CAAC;QAH1F,iBAAY,GAAZ,YAAY,CAAQ;QACpB,cAAS,GAAT,SAAS,CAAoB;QAG7C,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACrC,CAAC;CACF;AARD,oDAQC"}
@@ -0,0 +1,43 @@
1
+ import { type DurableDelegationCreate, DurableDelegationHolderClient, type DurableDelegationView } from './holder-client';
2
+ import { DelegationEndedError, DelegationUseDeniedError } from './holder-errors';
3
+ import type { ForwardedCallerContext } from '@xemahq/platform-common';
4
+ export declare enum HolderDelegationState {
5
+ NotRequired = "not_required",
6
+ Required = "required",
7
+ Unprovisioned = "unprovisioned",
8
+ Held = "held",
9
+ Ended = "ended"
10
+ }
11
+ export interface HolderDelegationRow {
12
+ readonly state: HolderDelegationState;
13
+ readonly delegationId: string | null;
14
+ readonly carrierId: string | null;
15
+ readonly capabilities: readonly string[];
16
+ }
17
+ export interface HolderDelegationStore {
18
+ read(): Promise<HolderDelegationRow | null>;
19
+ writeIf(expected: Pick<HolderDelegationRow, 'state' | 'delegationId'>, next: HolderDelegationRow): Promise<boolean>;
20
+ }
21
+ export interface HolderDelegationNeed {
22
+ readonly capabilities: readonly string[];
23
+ }
24
+ export interface HeldAuthority {
25
+ readonly delegationId: string;
26
+ readonly orgId: string;
27
+ readonly authorization: string;
28
+ }
29
+ export declare class DurableDelegationHolder {
30
+ private readonly client;
31
+ private readonly logger;
32
+ constructor(client: DurableDelegationHolderClient);
33
+ reconcileNeed(store: HolderDelegationStore, orgId: string, need: HolderDelegationNeed | null): Promise<HolderDelegationRow>;
34
+ authorize(store: HolderDelegationStore, author: ForwardedCallerContext, request: DurableDelegationCreate): Promise<HolderDelegationRow>;
35
+ authority(row: HolderDelegationRow, orgId: string, subject: string): Promise<HeldAuthority>;
36
+ onDenied(store: HolderDelegationStore, row: HolderDelegationRow, reason: string | undefined, detail: string): Promise<DelegationUseDeniedError | DelegationEndedError | undefined>;
37
+ refresh(store: HolderDelegationStore, orgId: string): Promise<HolderDelegationRow | null>;
38
+ endFor(row: HolderDelegationRow, orgId: string): Promise<void>;
39
+ private revokeQuietly;
40
+ }
41
+ export declare function rowOf(view: DurableDelegationView): HolderDelegationRow;
42
+ export declare function needTransition(row: HolderDelegationRow, need: HolderDelegationNeed | null): HolderDelegationRow | null;
43
+ //# sourceMappingURL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,uBAAuB;IAGf,OAAO,CAAC,QAAQ,CAAC,MAAM;IAF1C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA4C;gBAE/B,MAAM,EAAE,6BAA6B;IAE5D,aAAa,CACxB,KAAK,EAAE,qBAAqB,EAC5B,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,oBAAoB,GAAG,IAAI,GAChC,OAAO,CAAC,mBAAmB,CAAC;IAoBlB,SAAS,CACpB,KAAK,EAAE,qBAAqB,EAC5B,MAAM,EAAE,sBAAsB,EAC9B,OAAO,EAAE,uBAAuB,GAC/B,OAAO,CAAC,mBAAmB,CAAC;IAuBlB,SAAS,CACpB,GAAG,EAAE,mBAAmB,EACxB,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,aAAa,CAAC;IA2BZ,QAAQ,CACnB,KAAK,EAAE,qBAAqB,EAC5B,GAAG,EAAE,mBAAmB,EACxB,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,wBAAwB,GAAG,oBAAoB,GAAG,SAAS,CAAC;IAW1D,OAAO,CAAC,KAAK,EAAE,qBAAqB,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC;IAazF,MAAM,CAAC,GAAG,EAAE,mBAAmB,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;YAM7D,aAAa;CAc5B;AAGD,wBAAgB,KAAK,CAAC,IAAI,EAAE,qBAAqB,GAAG,mBAAmB,CAetE;AAGD,wBAAgB,cAAc,CAC5B,GAAG,EAAE,mBAAmB,EACxB,IAAI,EAAE,oBAAoB,GAAG,IAAI,GAChC,mBAAmB,GAAG,IAAI,CAgB5B"}
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var DurableDelegationHolder_1;
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.DurableDelegationHolder = exports.HolderDelegationState = void 0;
14
+ exports.rowOf = rowOf;
15
+ exports.needTransition = needTransition;
16
+ const common_1 = require("@nestjs/common");
17
+ const decision_codes_1 = require("./decision-codes");
18
+ const holder_client_1 = require("./holder-client");
19
+ const holder_errors_1 = require("./holder-errors");
20
+ var HolderDelegationState;
21
+ (function (HolderDelegationState) {
22
+ HolderDelegationState["NotRequired"] = "not_required";
23
+ HolderDelegationState["Required"] = "required";
24
+ HolderDelegationState["Unprovisioned"] = "unprovisioned";
25
+ HolderDelegationState["Held"] = "held";
26
+ HolderDelegationState["Ended"] = "ended";
27
+ })(HolderDelegationState || (exports.HolderDelegationState = HolderDelegationState = {}));
28
+ const MAX_CAS_ATTEMPTS = 3;
29
+ let DurableDelegationHolder = DurableDelegationHolder_1 = class DurableDelegationHolder {
30
+ client;
31
+ logger = new common_1.Logger(DurableDelegationHolder_1.name);
32
+ constructor(client) {
33
+ this.client = client;
34
+ }
35
+ async reconcileNeed(store, orgId, need) {
36
+ for (let attempt = 0; attempt < MAX_CAS_ATTEMPTS; attempt += 1) {
37
+ const row = await store.read();
38
+ if (row === null) {
39
+ throw new common_1.ConflictException('The holder row disappeared while its need was being stated.');
40
+ }
41
+ const next = needTransition(row, need);
42
+ if (next === null) {
43
+ return row;
44
+ }
45
+ if (await store.writeIf(row, next)) {
46
+ if (row.delegationId !== null && next.delegationId === null) {
47
+ await this.revokeQuietly(orgId, row.delegationId);
48
+ }
49
+ return next;
50
+ }
51
+ }
52
+ throw new common_1.ConflictException('The holder row kept changing while its need was being stated.');
53
+ }
54
+ async authorize(store, author, request) {
55
+ const view = await this.client.create(author, request);
56
+ const next = rowOf(view);
57
+ for (let attempt = 0; attempt < MAX_CAS_ATTEMPTS; attempt += 1) {
58
+ const row = await store.read();
59
+ if (row === null) {
60
+ await this.revokeQuietly(author.orgId, view.id);
61
+ throw new common_1.ConflictException('The holder row disappeared while it was being authorised.');
62
+ }
63
+ if (row.delegationId === view.id) {
64
+ return row;
65
+ }
66
+ if (await store.writeIf(row, next)) {
67
+ if (row.delegationId !== null) {
68
+ await this.revokeQuietly(author.orgId, row.delegationId);
69
+ }
70
+ return next;
71
+ }
72
+ }
73
+ throw new common_1.ConflictException('The holder row kept changing while it was being authorised.');
74
+ }
75
+ async authority(row, orgId, subject) {
76
+ switch (row.state) {
77
+ case HolderDelegationState.Held:
78
+ break;
79
+ case HolderDelegationState.NotRequired:
80
+ case HolderDelegationState.Required:
81
+ throw new holder_errors_1.DelegationRequiredError(holder_errors_1.DelegationRequiredReason.NeverAuthorised, subject);
82
+ case HolderDelegationState.Unprovisioned:
83
+ throw new holder_errors_1.DelegationRequiredError(holder_errors_1.DelegationRequiredReason.Unprovisioned, subject);
84
+ case HolderDelegationState.Ended:
85
+ throw new holder_errors_1.DelegationRequiredError(holder_errors_1.DelegationRequiredReason.Ended, subject);
86
+ }
87
+ if (row.delegationId === null || row.carrierId === null) {
88
+ throw new holder_errors_1.DelegationRequiredError(holder_errors_1.DelegationRequiredReason.Unprovisioned, subject);
89
+ }
90
+ return {
91
+ delegationId: row.delegationId,
92
+ orgId,
93
+ authorization: `Bearer ${await this.client.mintContext(row.carrierId)}`,
94
+ };
95
+ }
96
+ async onDenied(store, row, reason, detail) {
97
+ if (!(0, decision_codes_1.isDurableDelegationDecisionCode)(reason) || row.delegationId === null) {
98
+ return undefined;
99
+ }
100
+ if (reason === decision_codes_1.DurableDelegationDecisionCode.AuthorityUnavailable) {
101
+ return new holder_errors_1.DelegationUseDeniedError(row.delegationId, detail);
102
+ }
103
+ await store.writeIf(row, { ...row, state: HolderDelegationState.Ended, carrierId: null });
104
+ return new holder_errors_1.DelegationEndedError(row.delegationId, undefined);
105
+ }
106
+ async refresh(store, orgId) {
107
+ const row = await store.read();
108
+ if (row === null || row.delegationId === null) {
109
+ return row;
110
+ }
111
+ const view = await this.client.read(orgId, row.delegationId);
112
+ const next = rowOf(view);
113
+ if (next.state === row.state && next.carrierId === row.carrierId) {
114
+ return row;
115
+ }
116
+ return (await store.writeIf(row, next)) ? next : store.read();
117
+ }
118
+ async endFor(row, orgId) {
119
+ if (row.delegationId !== null) {
120
+ await this.client.revoke(orgId, row.delegationId);
121
+ }
122
+ }
123
+ async revokeQuietly(orgId, delegationId) {
124
+ try {
125
+ await this.client.revoke(orgId, delegationId);
126
+ }
127
+ catch (error) {
128
+ this.logger.warn({
129
+ message: 'durable-delegation-superseded-revoke-failed',
130
+ delegationId,
131
+ error: error instanceof Error ? error.message : String(error),
132
+ });
133
+ }
134
+ }
135
+ };
136
+ exports.DurableDelegationHolder = DurableDelegationHolder;
137
+ exports.DurableDelegationHolder = DurableDelegationHolder = DurableDelegationHolder_1 = __decorate([
138
+ (0, common_1.Injectable)(),
139
+ __metadata("design:paramtypes", [holder_client_1.DurableDelegationHolderClient])
140
+ ], DurableDelegationHolder);
141
+ function rowOf(view) {
142
+ switch (view.state) {
143
+ case holder_client_1.DurableDelegationRecordState.Active:
144
+ return {
145
+ state: view.carrierId === undefined ? HolderDelegationState.Unprovisioned : HolderDelegationState.Held,
146
+ delegationId: view.id,
147
+ carrierId: view.carrierId ?? null,
148
+ capabilities: [...view.capabilities],
149
+ };
150
+ case holder_client_1.DurableDelegationRecordState.Provisioning:
151
+ return { state: HolderDelegationState.Unprovisioned, delegationId: view.id, carrierId: null, capabilities: [...view.capabilities] };
152
+ case holder_client_1.DurableDelegationRecordState.Revoked:
153
+ case holder_client_1.DurableDelegationRecordState.Expired:
154
+ return { state: HolderDelegationState.Ended, delegationId: view.id, carrierId: null, capabilities: [...view.capabilities] };
155
+ }
156
+ }
157
+ function needTransition(row, need) {
158
+ if (need === null) {
159
+ return row.state === HolderDelegationState.NotRequired && row.delegationId === null
160
+ ? null
161
+ : { state: HolderDelegationState.NotRequired, delegationId: null, carrierId: null, capabilities: [] };
162
+ }
163
+ const lendsExactly = row.capabilities.length === need.capabilities.length &&
164
+ need.capabilities.every((c) => row.capabilities.includes(c));
165
+ if (row.delegationId !== null && lendsExactly) {
166
+ return null;
167
+ }
168
+ if (row.state === HolderDelegationState.Required && row.delegationId === null) {
169
+ return null;
170
+ }
171
+ return { state: HolderDelegationState.Required, delegationId: null, carrierId: null, capabilities: [] };
172
+ }
173
+ //# sourceMappingURL=holder-lifecycle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"holder-lifecycle.js","sourceRoot":"","sources":["../../src/durable-delegation/holder-lifecycle.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAkPA,sBAeC;AAGD,wCAmBC;AAvRD,2CAAuE;AAGvE,qDAG0B;AAC1B,mDAKyB;AACzB,mDAKyB;AAazB,IAAY,qBAWX;AAXD,WAAY,qBAAqB;IAE/B,qDAA4B,CAAA;IAE5B,8CAAqB,CAAA;IAErB,wDAA+B,CAAA;IAE/B,sCAAa,CAAA;IAEb,wCAAe,CAAA;AACjB,CAAC,EAXW,qBAAqB,qCAArB,qBAAqB,QAWhC;AAsCD,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAuBpB,IAAM,uBAAuB,+BAA7B,MAAM,uBAAuB;IAGE;IAFnB,MAAM,GAAG,IAAI,eAAM,CAAC,yBAAuB,CAAC,IAAI,CAAC,CAAC;IAEnE,YAAoC,MAAqC;QAArC,WAAM,GAAN,MAAM,CAA+B;IAAG,CAAC;IAEtE,KAAK,CAAC,aAAa,CACxB,KAA4B,EAC5B,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,EAAE,EAAE,CAAC,GAAG,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/D,IAAI,GAAG,CAAC,YAAY,KAAK,IAAI,IAAI,YAAY,EAAE,CAAC;QAC9C,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,GAAG,CAAC,KAAK,KAAK,qBAAqB,CAAC,QAAQ,IAAI,GAAG,CAAC,YAAY,KAAK,IAAI,EAAE,CAAC;QAC9E,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,qBAAqB,CAAC,QAAQ,EAAE,YAAY,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,YAAY,EAAE,EAAE,EAAE,CAAC;AAC1G,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { type ServiceRegistry } from '@xemahq/contracts/service-registry';
2
+ import { IdentityBootstrapService } from '@xemahq/identity-client';
3
+ import { type XemaAuthorizationOptions } from './authorization.options';
4
+ export interface AuthorizationGrantVersionReader {
5
+ read(orgId: string): Promise<number>;
6
+ }
7
+ export declare const AUTHORIZATION_GRANT_VERSION_READER: unique symbol;
8
+ export declare class HttpAuthorizationGrantVersionReader implements AuthorizationGrantVersionReader {
9
+ private readonly registry;
10
+ private readonly identityBootstrap;
11
+ private readonly options;
12
+ constructor(registry: ServiceRegistry, identityBootstrap: IdentityBootstrapService, options: XemaAuthorizationOptions);
13
+ read(orgId: string): Promise<number>;
14
+ }
15
+ export declare function assertGrantVersion(orgId: string, body: {
16
+ readonly orgId?: unknown;
17
+ readonly grantVersion?: unknown;
18
+ }): number;
19
+ //# sourceMappingURL=grant-version.reader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grant-version.reader.d.ts","sourceRoot":"","sources":["../src/grant-version.reader.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,eAAe,EACrB,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AAOnE,OAAO,EAEL,KAAK,wBAAwB,EAC9B,MAAM,yBAAyB,CAAC;AAUjC,MAAM,WAAW,+BAA+B;IAC9C,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CACtC;AAED,eAAO,MAAM,kCAAkC,eAE9C,CAAC;AAKF,qBACa,mCACX,YAAW,+BAA+B;IAIxC,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,iBAAiB;IAElC,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAHP,QAAQ,EAAE,eAAe,EACzB,iBAAiB,EAAE,wBAAwB,EAE3C,OAAO,EAAE,wBAAwB;IAGvC,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CA4BlD;AAGD,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,MAAM,EACb,IAAI,EAAE;IAAE,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,YAAY,CAAC,EAAE,OAAO,CAAA;CAAE,GAClE,MAAM,CAaR"}
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.HttpAuthorizationGrantVersionReader = exports.AUTHORIZATION_GRANT_VERSION_READER = void 0;
16
+ exports.assertGrantVersion = assertGrantVersion;
17
+ const common_1 = require("@nestjs/common");
18
+ const service_registry_1 = require("@xemahq/contracts/service-registry");
19
+ const identity_client_1 = require("@xemahq/identity-client");
20
+ const platform_common_1 = require("@xemahq/platform-common");
21
+ const service_registry_nest_1 = require("@xemahq/service-registry-nest");
22
+ const authorization_options_1 = require("./authorization.options");
23
+ const authzen_transport_1 = require("./authzen.transport");
24
+ exports.AUTHORIZATION_GRANT_VERSION_READER = Symbol('AUTHORIZATION_GRANT_VERSION_READER');
25
+ const GRANT_VERSION_PATH = '/authorization/grant-version/';
26
+ let HttpAuthorizationGrantVersionReader = class HttpAuthorizationGrantVersionReader {
27
+ registry;
28
+ identityBootstrap;
29
+ options;
30
+ constructor(registry, identityBootstrap, options) {
31
+ this.registry = registry;
32
+ this.identityBootstrap = identityBootstrap;
33
+ this.options = options;
34
+ }
35
+ async read(orgId) {
36
+ const baseUrl = await (0, service_registry_nest_1.resolveHttpUrl)(this.registry, service_registry_1.ServiceName.AUTHORIZATION_API);
37
+ const path = `${GRANT_VERSION_PATH}${encodeURIComponent(orgId)}`;
38
+ const response = await fetch(`${baseUrl.replace(/\/+$/, '')}${path}`, {
39
+ method: 'GET',
40
+ headers: {
41
+ Authorization: `Bearer ${await this.identityBootstrap.getAccessToken()}`,
42
+ 'X-Xema-Org-Id': orgId,
43
+ },
44
+ signal: AbortSignal.timeout(this.options.requestTimeoutMs),
45
+ });
46
+ if (!response.ok) {
47
+ const text = await response.text();
48
+ throw new authzen_transport_1.AuthzenPdpError(`authorization-api GET ${path} failed with HTTP ${response.status}: ${text.slice(0, 2000)}`, response.status, undefined);
49
+ }
50
+ const body = (0, platform_common_1.unwrapPlatformResponseEnvelope)(await response.json(), `authorization-api GET ${path}`);
51
+ return assertGrantVersion(orgId, body);
52
+ }
53
+ };
54
+ exports.HttpAuthorizationGrantVersionReader = HttpAuthorizationGrantVersionReader;
55
+ exports.HttpAuthorizationGrantVersionReader = HttpAuthorizationGrantVersionReader = __decorate([
56
+ (0, common_1.Injectable)(),
57
+ __param(0, (0, common_1.Inject)(service_registry_nest_1.SERVICE_REGISTRY_TOKEN)),
58
+ __param(2, (0, common_1.Inject)(authorization_options_1.XEMA_AUTHORIZATION_OPTIONS)),
59
+ __metadata("design:paramtypes", [Object, identity_client_1.IdentityBootstrapService, Object])
60
+ ], HttpAuthorizationGrantVersionReader);
61
+ function assertGrantVersion(orgId, body) {
62
+ const version = body.grantVersion;
63
+ if (body.orgId !== orgId ||
64
+ typeof version !== 'number' ||
65
+ !Number.isSafeInteger(version) ||
66
+ version < 0) {
67
+ throw new Error(`authorization-api returned a malformed policy revision for org "${orgId}".`);
68
+ }
69
+ return version;
70
+ }
71
+ //# sourceMappingURL=grant-version.reader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grant-version.reader.js","sourceRoot":"","sources":["../src/grant-version.reader.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AA+EA,gDAgBC;AA/FD,2CAAoD;AACpD,yEAG4C;AAC5C,6DAAmE;AACnE,6DAAyE;AACzE,yEAGuC;AAEvC,mEAGiC;AACjC,2DAAsD;AAazC,QAAA,kCAAkC,GAAG,MAAM,CACtD,oCAAoC,CACrC,CAAC;AAGF,MAAM,kBAAkB,GAAG,+BAA+B,CAAC;AAGpD,IAAM,mCAAmC,GAAzC,MAAM,mCAAmC;IAK3B;IACA;IAEA;IALnB,YAEmB,QAAyB,EACzB,iBAA2C,EAE3C,OAAiC;QAHjC,aAAQ,GAAR,QAAQ,CAAiB;QACzB,sBAAiB,GAAjB,iBAAiB,CAA0B;QAE3C,YAAO,GAAP,OAAO,CAA0B;IACjD,CAAC;IAEG,KAAK,CAAC,IAAI,CAAC,KAAa;QAC7B,MAAM,OAAO,GAAG,MAAM,IAAA,sCAAc,EAClC,IA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Aa,EACb,IAAmE;IAEnE,MAAM,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC;IAClC,IACE,IAAI,CAAC,KAAK,KAAK,KAAK;QACpB,OAAO,OAAO,KAAK,QAAQ;QAC3B,CAAC,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC;QAC9B,OAAO,GAAG,CAAC,EACX,CAAC;QACD,MAAM,IAAI,KAAK,CACb,mEAAmE,KAAK,IAAI,CAC7E,CAAC;IACJ,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
package/dist/index.d.ts CHANGED
@@ -6,7 +6,13 @@ export * from './authzen.transport';
6
6
  export * from './subject-from-context';
7
7
  export * from './xema-authz.service';
8
8
  export * from './policy-decisions';
9
+ export * from './grant-version.reader';
10
+ export * from './route-action-evaluator';
9
11
  export * from './internal-caller-or-member-context.guard';
10
12
  export * from './organization-delegation-admission';
11
13
  export * from './relayable-caller';
14
+ export * from './durable-delegation/decision-codes';
15
+ export * from './durable-delegation/holder-client';
16
+ export * from './durable-delegation/holder-errors';
17
+ export * from './durable-delegation/holder-lifecycle';
12
18
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,2CAA2C,CAAC;AAC1D,cAAc,qCAAqC,CAAC;AACpD,cAAc,oBAAoB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AACzC,cAAc,2CAA2C,CAAC;AAC1D,cAAc,qCAAqC,CAAC;AACpD,cAAc,oBAAoB,CAAC;AACnC,cAAc,qCAAqC,CAAC;AACpD,cAAc,oCAAoC,CAAC;AACnD,cAAc,oCAAoC,CAAC;AACnD,cAAc,uCAAuC,CAAC"}
package/dist/index.js CHANGED
@@ -22,7 +22,13 @@ __exportStar(require("./authzen.transport"), exports);
22
22
  __exportStar(require("./subject-from-context"), exports);
23
23
  __exportStar(require("./xema-authz.service"), exports);
24
24
  __exportStar(require("./policy-decisions"), exports);
25
+ __exportStar(require("./grant-version.reader"), exports);
26
+ __exportStar(require("./route-action-evaluator"), exports);
25
27
  __exportStar(require("./internal-caller-or-member-context.guard"), exports);
26
28
  __exportStar(require("./organization-delegation-admission"), exports);
27
29
  __exportStar(require("./relayable-caller"), exports);
30
+ __exportStar(require("./durable-delegation/decision-codes"), exports);
31
+ __exportStar(require("./durable-delegation/holder-client"), exports);
32
+ __exportStar(require("./durable-delegation/holder-errors"), exports);
33
+ __exportStar(require("./durable-delegation/holder-lifecycle"), exports);
28
34
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAmC;AACnC,yDAAuC;AACvC,0DAAwC;AACxC,wDAAsC;AACtC,sDAAoC;AACpC,yDAAuC;AACvC,uDAAqC;AACrC,qDAAmC;AACnC,4EAA0D;AAC1D,sEAAoD;AACpD,qDAAmC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAmC;AACnC,yDAAuC;AACvC,0DAAwC;AACxC,wDAAsC;AACtC,sDAAoC;AACpC,yDAAuC;AACvC,uDAAqC;AACrC,qDAAmC;AACnC,yDAAuC;AACvC,2DAAyC;AACzC,4EAA0D;AAC1D,sEAAoD;AACpD,qDAAmC;AACnC,sEAAoD;AACpD,qEAAmD;AACnD,qEAAmD;AACnD,wEAAsD"}
@@ -0,0 +1,38 @@
1
+ import { type CapabilityRef } from '@xemahq/contracts/capability';
2
+ import { type AuthorizationGrantVersionReader } from './grant-version.reader';
3
+ import { XemaPolicyDecisions } from './policy-decisions';
4
+ import type { RequestContext } from '@xemahq/platform-common';
5
+ export interface RouteActionCandidate<Action extends string> {
6
+ readonly key: string;
7
+ readonly action: Action;
8
+ readonly capabilityRef: CapabilityRef;
9
+ }
10
+ export type RouteActionScope = {
11
+ readonly kind: 'org';
12
+ readonly orgId: string;
13
+ } | {
14
+ readonly kind: 'project';
15
+ readonly orgId: string;
16
+ readonly projectId: string;
17
+ } | {
18
+ readonly kind: 'platform';
19
+ readonly orgId: string;
20
+ };
21
+ export interface RouteActionEvaluation<Action extends string> {
22
+ readonly allowed: ReadonlyMap<string, Action>;
23
+ readonly policyRevision: number;
24
+ }
25
+ export declare enum RouteActionPolicyErrorCode {
26
+ CrossOrg = "ROUTE_ACTION_POLICY_CROSS_ORG",
27
+ CrossProject = "ROUTE_ACTION_POLICY_CROSS_PROJECT",
28
+ Stale = "ROUTE_ACTION_POLICY_STALE",
29
+ RequestRejected = "ROUTE_ACTION_POLICY_REQUEST_REJECTED",
30
+ Unavailable = "ROUTE_ACTION_POLICY_UNAVAILABLE"
31
+ }
32
+ export declare class XemaRouteActionEvaluator {
33
+ private readonly decisions;
34
+ private readonly grantVersions;
35
+ constructor(decisions: XemaPolicyDecisions, grantVersions: AuthorizationGrantVersionReader);
36
+ evaluate<Action extends string>(context: RequestContext, scope: RouteActionScope, candidates: readonly RouteActionCandidate<Action>[]): Promise<RouteActionEvaluation<Action>>;
37
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CAChD;AA6BD,qBACa,wBAAwB;IAEjC,OAAO,CAAC,QAAQ,CAAC,SAAS;IAE1B,OAAO,CAAC,QAAQ,CAAC,aAAa;gBAFb,SAAS,EAAE,mBAAmB,EAE9B,aAAa,EAAE,+BAA+B;IAGpD,QAAQ,CAAC,MAAM,SAAS,MAAM,EACzC,OAAO,EAAE,cAAc,EACvB,KAAK,EAAE,gBAAgB,EACvB,UAAU,EAAE,SAAS,oBAAoB,CAAC,MAAM,CAAC,EAAE,GAClD,OAAO,CAAC,qBAAqB,CAAC,MAAM,CAAC,CAAC;CAsD1C"}
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.XemaRouteActionEvaluator = exports.RouteActionPolicyErrorCode = void 0;
16
+ const node_crypto_1 = require("node:crypto");
17
+ const common_1 = require("@nestjs/common");
18
+ const execution_context_1 = require("@xemahq/contracts/execution-context");
19
+ const execution_environment_1 = require("@xemahq/contracts/execution-environment");
20
+ const policy_1 = require("@xemahq/contracts/policy");
21
+ const space_1 = require("@xemahq/contracts/space");
22
+ const authzen_transport_1 = require("./authzen.transport");
23
+ const grant_version_reader_1 = require("./grant-version.reader");
24
+ const policy_decisions_1 = require("./policy-decisions");
25
+ const relayable_caller_1 = require("./relayable-caller");
26
+ var RouteActionPolicyErrorCode;
27
+ (function (RouteActionPolicyErrorCode) {
28
+ RouteActionPolicyErrorCode["CrossOrg"] = "ROUTE_ACTION_POLICY_CROSS_ORG";
29
+ RouteActionPolicyErrorCode["CrossProject"] = "ROUTE_ACTION_POLICY_CROSS_PROJECT";
30
+ RouteActionPolicyErrorCode["Stale"] = "ROUTE_ACTION_POLICY_STALE";
31
+ RouteActionPolicyErrorCode["RequestRejected"] = "ROUTE_ACTION_POLICY_REQUEST_REJECTED";
32
+ RouteActionPolicyErrorCode["Unavailable"] = "ROUTE_ACTION_POLICY_UNAVAILABLE";
33
+ })(RouteActionPolicyErrorCode || (exports.RouteActionPolicyErrorCode = RouteActionPolicyErrorCode = {}));
34
+ class PolicyRevisionChangedError extends Error {
35
+ constructor(before, after) {
36
+ super(`Authorization policy revision changed from ${before} to ${after}.`);
37
+ this.name = PolicyRevisionChangedError.name;
38
+ }
39
+ }
40
+ let XemaRouteActionEvaluator = class XemaRouteActionEvaluator {
41
+ decisions;
42
+ grantVersions;
43
+ constructor(decisions, grantVersions) {
44
+ this.decisions = decisions;
45
+ this.grantVersions = grantVersions;
46
+ }
47
+ async evaluate(context, scope, candidates) {
48
+ assertScope(context, scope);
49
+ assertUniqueKeys(candidates);
50
+ try {
51
+ const revisionBefore = await this.grantVersions.read(scope.orgId);
52
+ if (candidates.length === 0) {
53
+ return { allowed: new Map(), policyRevision: revisionBefore };
54
+ }
55
+ const forwardedCaller = (0, relayable_caller_1.relayableCallerContext)(context, scope.orgId);
56
+ const decisions = await this.decisions.decideAll(candidates.map((candidate) => toPolicyRequest(context, scope, candidate)), forwardedCaller === undefined ? undefined : { forwardedCaller });
57
+ const revisionAfter = await this.grantVersions.read(scope.orgId);
58
+ if (revisionAfter !== revisionBefore) {
59
+ throw new PolicyRevisionChangedError(revisionBefore, revisionAfter);
60
+ }
61
+ const allowed = new Map();
62
+ decisions.forEach((decision, index) => {
63
+ const candidate = candidates[index];
64
+ if (decision.kind === policy_1.PolicyDecisionKind.Allow && candidate !== undefined) {
65
+ allowed.set(candidate.key, candidate.action);
66
+ }
67
+ });
68
+ return { allowed, policyRevision: revisionAfter };
69
+ }
70
+ catch (error) {
71
+ if (error instanceof PolicyRevisionChangedError) {
72
+ throw new common_1.ServiceUnavailableException({
73
+ code: RouteActionPolicyErrorCode.Stale,
74
+ message: 'Authorization policy changed during action evaluation; refresh and retry.',
75
+ });
76
+ }
77
+ const status = error instanceof policy_decisions_1.PolicyDecisionRequestError || error instanceof authzen_transport_1.AuthzenPdpError
78
+ ? error.status
79
+ : undefined;
80
+ if (status !== undefined && status >= 400 && status < 500 && status !== 408 && status !== 429) {
81
+ throw new common_1.InternalServerErrorException({
82
+ code: RouteActionPolicyErrorCode.RequestRejected,
83
+ message: `Authorization policy refused this service's evaluation request (HTTP ${status}); ` +
84
+ `actions are denied fail-closed. This is a defect in the calling service, not a policy-plane outage: ` +
85
+ error.message,
86
+ });
87
+ }
88
+ throw new common_1.ServiceUnavailableException({
89
+ code: RouteActionPolicyErrorCode.Unavailable,
90
+ message: 'Authorization policy is unavailable; actions are denied fail-closed.',
91
+ });
92
+ }
93
+ }
94
+ };
95
+ exports.XemaRouteActionEvaluator = XemaRouteActionEvaluator;
96
+ exports.XemaRouteActionEvaluator = XemaRouteActionEvaluator = __decorate([
97
+ (0, common_1.Injectable)(),
98
+ __param(1, (0, common_1.Inject)(grant_version_reader_1.AUTHORIZATION_GRANT_VERSION_READER)),
99
+ __metadata("design:paramtypes", [policy_decisions_1.XemaPolicyDecisions, Object])
100
+ ], XemaRouteActionEvaluator);
101
+ function assertScope(context, scope) {
102
+ if (scope.orgId !== context.orgId) {
103
+ throw new common_1.ForbiddenException({
104
+ code: RouteActionPolicyErrorCode.CrossOrg,
105
+ message: 'The action resource does not belong to the active organization.',
106
+ });
107
+ }
108
+ if (scope.kind === 'project' && context.projectId !== scope.projectId) {
109
+ throw new common_1.ForbiddenException({
110
+ code: RouteActionPolicyErrorCode.CrossProject,
111
+ message: 'The action resource does not belong to the active project.',
112
+ });
113
+ }
114
+ }
115
+ function assertUniqueKeys(candidates) {
116
+ const keys = new Set();
117
+ for (const candidate of candidates) {
118
+ if (candidate.key.length === 0 || keys.has(candidate.key)) {
119
+ throw new Error('Route action candidates require unique non-empty keys.');
120
+ }
121
+ keys.add(candidate.key);
122
+ }
123
+ }
124
+ function toPolicyRequest(context, scope, candidate) {
125
+ const environment = scope.kind === 'platform'
126
+ ? execution_environment_1.ExecutionEnvironmentKind.System
127
+ : scope.kind === 'project'
128
+ ? execution_environment_1.ExecutionEnvironmentKind.Project
129
+ : execution_environment_1.ExecutionEnvironmentKind.Org;
130
+ return {
131
+ id: (0, node_crypto_1.randomUUID)(),
132
+ requestId: context.correlationId,
133
+ traceId: context.correlationId,
134
+ subject: { kind: context.subject.kind, id: context.subject.id },
135
+ caller: { kind: execution_context_1.CallerKind.Web },
136
+ space: scope.kind === 'platform'
137
+ ? { tier: space_1.SpaceKind.System, orgId: scope.orgId }
138
+ : scope.kind === 'project'
139
+ ? { tier: space_1.SpaceKind.Project, orgId: scope.orgId, projectId: scope.projectId }
140
+ : { tier: space_1.SpaceKind.Org, orgId: scope.orgId },
141
+ environment: { id: (0, execution_environment_1.formatEnvironmentRef)(environment), kind: environment },
142
+ capability: { ref: candidate.capabilityRef },
143
+ ...(context.delegation === undefined ? {} : { delegation: context.delegation }),
144
+ };
145
+ }
146
+ //# sourceMappingURL=route-action-evaluator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route-action-evaluator.js","sourceRoot":"","sources":["../src/route-action-evaluator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,6CAAyC;AAEzC,2CAMwB;AAExB,2EAAiE;AACjE,mFAGiD;AACjD,qDAAkF;AAClF,mDAAoD;AAEpD,2DAAsD;AACtD,iEAGgC;AAChC,yDAAqF;AACrF,yDAA4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EAAC,WAAW,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE;QACzE,UAAU,EAAE,EAAE,GAAG,EAAE,SAAS,CAAC,aAAa,EAAE;QAC5C,GAAG,CAAC,OAAO,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC;KAChF,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xemahq/authorization-nest",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "AuthZEN 1.0 PEP for Xema services: one Nest module, an AuthZEN client against authorization-api, and the @Authorize route guard.",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/",
@@ -24,11 +24,11 @@
24
24
  "@nestjs/common": "11.1.18",
25
25
  "@nestjs/core": "11.1.18",
26
26
  "@nestjs/testing": "11.1.18",
27
- "@xemahq/contracts": ">=0.50.0",
28
- "@xemahq/eslint-config": "^0.5.0",
29
- "@xemahq/identity-client": "^0.15.5",
30
- "@xemahq/platform-common": "^0.59.0",
31
- "@xemahq/service-registry-nest": "^0.19.0",
27
+ "@xemahq/contracts": ">=0.59.0",
28
+ "@xemahq/eslint-config": "^0.12.0",
29
+ "@xemahq/identity-client": "^0.16.0",
30
+ "@xemahq/platform-common": "^0.64.0",
31
+ "@xemahq/service-registry-nest": "^0.20.0",
32
32
  "@types/jest": "^30.0.0",
33
33
  "@types/node": "25.2.3",
34
34
  "eslint": "^9.39.4",