@friggframework/core 2.0.0--canary.685.022c3c3.0 → 2.0.0--canary.683.8a84ab1.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 (38) hide show
  1. package/CLAUDE.md +0 -23
  2. package/README.md +1 -1
  3. package/credential/use-cases/credential-operations-for-user.js +33 -110
  4. package/credential/use-cases/reauthorize-credential-for-user.js +13 -10
  5. package/handlers/app-definition-loader.js +0 -9
  6. package/index.js +3 -4
  7. package/integrations/index.js +5 -0
  8. package/integrations/integration-router.js +26 -28
  9. package/integrations/repositories/integration-repository-documentdb.js +2 -1
  10. package/integrations/utils/map-integration-dto.js +44 -1
  11. package/management-api/management-api-config.js +4 -70
  12. package/management-api/openapi/components.js +0 -1
  13. package/management-api/openapi/v2-operations.js +3 -6
  14. package/management-api/v2/dependencies.js +2 -2
  15. package/management-api/v2/dto.js +0 -3
  16. package/management-api/v2/handlers/credentials.js +2 -6
  17. package/modules/repositories/module-repository-documentdb.js +8 -6
  18. package/modules/repositories/module-repository-mongo.js +1 -2
  19. package/modules/repositories/module-repository-postgres.js +1 -2
  20. package/modules/repositories/module-repository.js +1 -2
  21. package/modules/scope-config.js +16 -1
  22. package/modules/scope-policy.js +3 -17
  23. package/modules/scope-visibility.js +37 -18
  24. package/modules/use-cases/authorize-entity-for-user.js +2 -4
  25. package/modules/use-cases/create-entity-from-credential.js +113 -0
  26. package/modules/use-cases/execute-entity-proxy-request.js +18 -50
  27. package/modules/use-cases/get-entity-module-for-user.js +2 -9
  28. package/modules/use-cases/get-entity-options-by-id.js +4 -1
  29. package/modules/use-cases/get-module.js +4 -1
  30. package/modules/use-cases/multi-step-authorization.js +1 -4
  31. package/modules/use-cases/refresh-entity-options.js +4 -1
  32. package/modules/use-cases/test-module-auth.js +4 -1
  33. package/package.json +5 -5
  34. package/types/modules/index.d.ts +9 -41
  35. package/types/user/index.d.ts +14 -4
  36. package/user/use-cases/get-user-from-adopter-jwt.js +17 -7
  37. package/user/user.js +17 -4
  38. package/modules/repositories/entity-not-found.js +0 -18
@@ -56,8 +56,11 @@ class GetModule {
56
56
  );
57
57
  }
58
58
 
59
+ // Built for the entity's OWNER, not the caller: a caller may use an
60
+ // organization's entity, and a token refresh during this call must
61
+ // update the owner's credential, not create one under the caller.
59
62
  const module = new Module({
60
- userId,
63
+ userId: entity.userId ?? userId,
61
64
  entity,
62
65
  definition: moduleDefinition,
63
66
  });
@@ -166,10 +166,8 @@ class SubmitAuthorizationStep {
166
166
  * @param {Object} params
167
167
  * @param {string} [params.scope] - the scope the caller asks for, from the
168
168
  * deciding level's allow list (ADR-056 §3.1). Never `global`.
169
- * @param {string} [params.fixedScope] - re-authorization keeps the scope the
170
- * credential already has, whatever the config says now.
171
169
  */
172
- async execute({ user, entityType, data, step = 1, sessionId, credentialId = null, scope, fixedScope }) {
170
+ async execute({ user, entityType, data, step = 1, sessionId, credentialId = null, scope }) {
173
171
  if (!data || typeof data !== 'object' || Array.isArray(data)) {
174
172
  throw Boom.badRequest('data must be an object', {
175
173
  code: 'VALIDATION_ERROR',
@@ -185,7 +183,6 @@ class SubmitAuthorizationStep {
185
183
  user,
186
184
  entityType,
187
185
  scope,
188
- fixedScope,
189
186
  });
190
187
 
191
188
  if (!isMultiStep(definition)) {
@@ -61,8 +61,11 @@ class RefreshEntityOptions {
61
61
  );
62
62
  }
63
63
 
64
+ // Built for the entity's OWNER, not the caller: a caller may use an
65
+ // organization's entity, and a token refresh during this call must
66
+ // update the owner's credential, not create one under the caller.
64
67
  const module = new Module({
65
- userId,
68
+ userId: entity.userId ?? userId,
66
69
  entity,
67
70
  definition: moduleDefinition,
68
71
  });
@@ -60,8 +60,11 @@ class TestModuleAuth {
60
60
  );
61
61
  }
62
62
 
63
+ // Built for the entity's OWNER, not the caller: a caller may use an
64
+ // organization's entity, and a token refresh during this call must
65
+ // update the owner's credential, not create one under the caller.
63
66
  const module = new Module({
64
- userId,
67
+ userId: entity.userId ?? userId,
65
68
  entity,
66
69
  definition: moduleDefinition,
67
70
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@friggframework/core",
3
3
  "prettier": "@friggframework/prettier-config",
4
- "version": "2.0.0--canary.685.022c3c3.0",
4
+ "version": "2.0.0--canary.683.8a84ab1.0",
5
5
  "dependencies": {
6
6
  "@aws-sdk/client-apigatewaymanagementapi": "^3.588.0",
7
7
  "@aws-sdk/client-kms": "^3.588.0",
@@ -48,9 +48,9 @@
48
48
  }
49
49
  },
50
50
  "devDependencies": {
51
- "@friggframework/eslint-config": "2.0.0--canary.685.022c3c3.0",
52
- "@friggframework/prettier-config": "2.0.0--canary.685.022c3c3.0",
53
- "@friggframework/test": "2.0.0--canary.685.022c3c3.0",
51
+ "@friggframework/eslint-config": "2.0.0--canary.683.8a84ab1.0",
52
+ "@friggframework/prettier-config": "2.0.0--canary.683.8a84ab1.0",
53
+ "@friggframework/test": "2.0.0--canary.683.8a84ab1.0",
54
54
  "@prisma/client": "^6.19.3",
55
55
  "@types/lodash": "4.17.15",
56
56
  "@typescript-eslint/eslint-plugin": "^8.0.0",
@@ -90,5 +90,5 @@
90
90
  "publishConfig": {
91
91
  "access": "public"
92
92
  },
93
- "gitHead": "022c3c3ae2d4e9d22046d94911fc00854416cb2a"
93
+ "gitHead": "8a84ab14e5e56c7e7939b1b6a0b656ed9301dcfd"
94
94
  }
@@ -68,49 +68,12 @@ declare module "@friggframework/core" {
68
68
  owner?: "derive" | "individual" | "organization";
69
69
  }
70
70
 
71
- /** One operation a module or the app lets the entity proxy forward (ADR-052). */
72
- export interface ProxyAllowRule {
73
- method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "*";
74
- /** Relative to the module's base URL. `:param` is one segment; a trailing `/**` is a subtree. */
75
- path: string;
76
- }
77
-
78
- /** `appDefinition.managementApi.proxy`: the entity proxy, POST /api/v2/entities/:id/proxy (ADR-052). */
79
- export interface ManagementApiProxyConfig {
80
- /** Mount the route. Default false. */
81
- enable?: boolean;
82
- /** Upstream timeout in ms; can only be lowered from 25000. */
83
- timeoutMs?: number;
84
- /** Largest request body in bytes; default 1 MiB, at most 10 MiB. */
85
- maxRequestBytes?: number;
86
- /** Largest response body in bytes; can only be lowered from 5 MiB. */
87
- maxResponseBytes?: number;
88
- /**
89
- * Entity scopes the proxy accepts (ADR-056 §3.5). Default `['individual']`.
90
- * Add `'organization'` to let a member with use access proxy through the
91
- * organization's shared credential. `global` is never accepted. An unknown,
92
- * empty or repeated value throws ManagementApiConfigError at load.
93
- */
94
- scopes?: Array<Exclude<EntityScope, "global">>;
95
- /** Per-module allow-list override; `'module'` uses the module's own list. */
96
- modules?: Record<string, { allow: "module" | ProxyAllowRule[] }>;
97
- }
98
-
99
- /** `appDefinition.managementApi` (ADR-052, ADR-053). */
100
- export interface ManagementApiConfig {
101
- /** Serve the deprecated v1 routes. Default true. */
102
- v1?: boolean;
103
- proxy?: ManagementApiProxyConfig;
104
- }
105
-
106
71
  export interface AppDefinition {
107
72
  integrations: any[];
108
73
  /** App-level entity type registry (ADR-059). */
109
74
  entities?: AppDefinitionEntities;
110
75
  /** Who owns what a person connects (ADR-056). */
111
76
  ownership?: AppOwnership;
112
- /** Management API versions and the entity proxy (ADR-052, ADR-053, ADR-056 §3.5). */
113
- managementApi?: ManagementApiConfig;
114
77
  [key: string]: unknown;
115
78
  }
116
79
 
@@ -175,10 +138,15 @@ declare module "@friggframework/core" {
175
138
  appDefinition?: Pick<AppDefinition, "entities" | "integrations">
176
139
  ): EntityTypeRegistry;
177
140
 
178
- /** Invalid `managementApi` setting (today: `proxy.scopes`), thrown at app-definition load. */
179
- export class ManagementApiConfigError extends Error {
180
- code: "MANAGEMENT_API_CONFIG_INVALID";
181
- }
141
+ /**
142
+ * @deprecated ADR-059. Use `getEntityTypeRegistry(appDefinition)` and read
143
+ * each entry's `definition`. Returns the registry's definitions for these
144
+ * integrations (plus registry-only types when `entities` is passed).
145
+ */
146
+ export function getModulesDefinitionFromIntegrationClasses(
147
+ integrationClasses: any[],
148
+ entities?: AppDefinitionEntities
149
+ ): any[];
182
150
 
183
151
  /** Invalid `entities` registry. `code` is always `ENTITY_REGISTRY_INVALID`. */
184
152
  export class EntityRegistryError extends Error {
@@ -63,12 +63,19 @@ declare module "@friggframework/core/user" {
63
63
  */
64
64
  strictUserValidation?: boolean;
65
65
  /**
66
- * Roles (the adopter token's role claim) that may manage organization-level
67
- * things (ADR-056 §3). Default `['ADMIN']`; a token with no role claim is
68
- * `MEMBER`, never admin; a shared-secret (`x-frigg` header) caller needs no
69
- * role. Compared case-insensitively. Validated at load.
66
+ * Roles that may manage organization-level things (ADR-056 §3). Default
67
+ * `['ADMIN']`. The role is the adopter token's role claim, else the stored
68
+ * membership `orgRole`, else `MEMBER` (never admin). A shared-secret
69
+ * (`x-frigg` header) caller needs no role, nor does a Frigg bearer token
70
+ * unless `orgAdminGateBearerTokens`. Compared case-insensitively.
71
+ * Validated at load.
70
72
  */
71
73
  orgAdminRoles?: string[];
74
+ /**
75
+ * Judge Frigg bearer-token callers by role like adopter-token callers.
76
+ * Default false: they keep their behaviour from before organization roles.
77
+ */
78
+ orgAdminGateBearerTokens?: boolean;
72
79
  authModes?: UserAuthModes;
73
80
  jwtConfig?: AdopterJwtConfig;
74
81
  }
@@ -120,5 +127,8 @@ declare module "@friggframework/core/user" {
120
127
  /** The organization role from the adopter token for this request, or null. */
121
128
  getOrgRole(): string | null;
122
129
  setOrgRole(orgRole: string | null): void;
130
+ /** The stored `orgRole` of the membership of the active organization, or null. Used when the token carries no role. */
131
+ getMembershipRole(): string | null;
132
+ setMembershipRole(membershipRole: string | null): void;
123
133
  }
124
134
  }
@@ -74,8 +74,14 @@ class GetUserFromAdopterJwt {
74
74
  const role = claims[roleClaim];
75
75
 
76
76
  const orgRole = typeof role === 'string' && role !== '' ? role : null;
77
- const user = await this._findOrCreateUser(appUserId, appOrgId, orgRole);
77
+ const { user, membership } = await this._findOrCreateUser(
78
+ appUserId,
79
+ appOrgId,
80
+ orgRole
81
+ );
78
82
  user.setOrgRole(orgRole);
83
+ // What the membership row says: the role when the token carries none.
84
+ user.setMembershipRole(membership?.orgRole ?? null);
79
85
 
80
86
  log.debug('Adopter token accepted', {
81
87
  eventName: `${log.name}.accepted`,
@@ -98,12 +104,16 @@ class GetUserFromAdopterJwt {
98
104
 
99
105
  // The token's organization is the active one whether or not this is
100
106
  // the person's first time in it.
101
- const { individual } = await this.ensureOrganizationMembership.execute({
102
- individual: created.individual,
103
- organization: created.organization,
104
- orgRole,
105
- });
106
- return buildUser(cfg, individual, created.organization);
107
+ const { individual, membership } =
108
+ await this.ensureOrganizationMembership.execute({
109
+ individual: created.individual,
110
+ organization: created.organization,
111
+ orgRole,
112
+ });
113
+ return {
114
+ user: buildUser(cfg, individual, created.organization),
115
+ membership,
116
+ };
107
117
  }
108
118
  }
109
119
 
package/user/user.js CHANGED
@@ -4,8 +4,10 @@ const bcrypt = require('bcryptjs');
4
4
  * How the request that produced a `User` proved who it is. Set by
5
5
  * AuthenticateUser, never inferred from a missing value. Organization roles
6
6
  * (ADR-056 §3, §3.6) depend on it: a shared-secret caller is the app's own
7
- * backend and is trusted to manage; a person with a token has the role the
8
- * token carries, or `MEMBER`.
7
+ * backend and is trusted to manage; a Frigg bearer token keeps its pre-roles
8
+ * behaviour unless the app opts in (`user.orgAdminGateBearerTokens`); an
9
+ * adopter token has the role it carries, else the stored membership role,
10
+ * else `MEMBER`.
9
11
  */
10
12
  const AUTH_SOURCES = Object.freeze({
11
13
  SHARED_SECRET: 'shared-secret',
@@ -31,9 +33,12 @@ class User {
31
33
  this.individualUser = individualUser;
32
34
  this.organizationUser = organizationUser;
33
35
  this.usePassword = usePassword;
34
- // Organization role from an adopter token claim (ADR-056 §2). Per request:
35
- // the role stored on a membership is only what the first token carried.
36
+ // Organization role from an adopter token claim (ADR-056 §2), per
37
+ // request. When present it wins over the stored membership role.
36
38
  this.orgRole = null;
39
+ // The role stored on the person's membership of the active organization
40
+ // (OrganizationMembership.orgRole); used when the token carries none.
41
+ this.membershipRole = null;
37
42
  // How this request authenticated (one of AUTH_SOURCES); null until AuthenticateUser says.
38
43
  this.authSource = null;
39
44
 
@@ -115,6 +120,14 @@ class User {
115
120
  this.orgRole = orgRole ?? null;
116
121
  }
117
122
 
123
+ getMembershipRole() {
124
+ return this.membershipRole;
125
+ }
126
+
127
+ setMembershipRole(membershipRole) {
128
+ this.membershipRole = membershipRole ?? null;
129
+ }
130
+
118
131
  getIndividualUser() {
119
132
  return this.individualUser;
120
133
  }
@@ -1,18 +0,0 @@
1
- /**
2
- * The error `findEntityById` throws for an id with no row, and the one
3
- * predicate that recognises it (as integration-not-found.js does for
4
- * integrations). The adapters build the error here and use cases match it
5
- * here, so a missing entity can be told from any other failure without
6
- * widening a regex.
7
- *
8
- * Why it matters: a use case that answers 404 for another tenant's entity must
9
- * answer the same 404 for an id that does not exist. Otherwise the status code
10
- * says which ids exist (ADR-052 §3, ADR-056 §3.6).
11
- */
12
- const MESSAGE = /^Entity .+ not found$/;
13
-
14
- const entityNotFoundError = (id) => new Error(`Entity ${id} not found`);
15
-
16
- const isEntityNotFound = (error) => MESSAGE.test(error?.message || '');
17
-
18
- module.exports = { entityNotFoundError, isEntityNotFound };