@kosha-ai/server 0.2.0 → 0.4.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 (125) hide show
  1. package/dist/auth/auth.factory.d.ts.map +1 -1
  2. package/dist/auth/auth.factory.js +0 -5
  3. package/dist/configurations/configurations.controller.d.ts +6 -5
  4. package/dist/configurations/configurations.controller.d.ts.map +1 -1
  5. package/dist/configurations/configurations.controller.js +22 -18
  6. package/dist/configurations/configurations.module.d.ts.map +1 -1
  7. package/dist/configurations/configurations.module.js +1 -2
  8. package/dist/configurations/configurations.service.d.ts +29 -35
  9. package/dist/configurations/configurations.service.d.ts.map +1 -1
  10. package/dist/configurations/configurations.service.js +126 -149
  11. package/dist/configurations/dto/create-configuration.dto.d.ts +2 -2
  12. package/dist/configurations/dto/create-configuration.dto.d.ts.map +1 -1
  13. package/dist/configurations/dto/create-configuration.dto.js +6 -6
  14. package/dist/configurations/dto/update-configuration.dto.d.ts +1 -2
  15. package/dist/configurations/dto/update-configuration.dto.d.ts.map +1 -1
  16. package/dist/configurations/dto/update-configuration.dto.js +7 -13
  17. package/dist/connect/connect.controller.d.ts +12 -2
  18. package/dist/connect/connect.controller.d.ts.map +1 -1
  19. package/dist/connect/connect.controller.js +108 -18
  20. package/dist/connect/connect.module.d.ts.map +1 -1
  21. package/dist/connect/connect.module.js +1 -2
  22. package/dist/connect/connect.repository.d.ts +34 -25
  23. package/dist/connect/connect.repository.d.ts.map +1 -1
  24. package/dist/connect/connect.repository.js +45 -52
  25. package/dist/connect/connect.service.d.ts +54 -8
  26. package/dist/connect/connect.service.d.ts.map +1 -1
  27. package/dist/connect/connect.service.js +312 -68
  28. package/dist/connect/dto/connect-credential.dto.d.ts +8 -0
  29. package/dist/connect/dto/connect-credential.dto.d.ts.map +1 -0
  30. package/dist/connect/dto/connect-credential.dto.js +54 -0
  31. package/dist/connect/dto/create-connect-session.dto.d.ts +1 -1
  32. package/dist/connect/dto/create-connect-session.dto.d.ts.map +1 -1
  33. package/dist/connect/dto/create-connect-session.dto.js +7 -8
  34. package/dist/connect/dto/create-test-session.dto.d.ts +8 -0
  35. package/dist/connect/dto/create-test-session.dto.d.ts.map +1 -1
  36. package/dist/connect/dto/create-test-session.dto.js +45 -3
  37. package/dist/connections/connections.controller.d.ts +4 -3
  38. package/dist/connections/connections.controller.d.ts.map +1 -1
  39. package/dist/connections/connections.controller.js +13 -11
  40. package/dist/connections/connections.repository.d.ts +24 -17
  41. package/dist/connections/connections.repository.d.ts.map +1 -1
  42. package/dist/connections/connections.repository.js +21 -43
  43. package/dist/connections/connections.service.d.ts +48 -13
  44. package/dist/connections/connections.service.d.ts.map +1 -1
  45. package/dist/connections/connections.service.js +111 -32
  46. package/dist/connections/dto/list-connections.dto.d.ts +3 -1
  47. package/dist/connections/dto/list-connections.dto.d.ts.map +1 -1
  48. package/dist/connections/dto/list-connections.dto.js +26 -4
  49. package/dist/connections/environment-connections.controller.d.ts +4 -3
  50. package/dist/connections/environment-connections.controller.d.ts.map +1 -1
  51. package/dist/connections/environment-connections.controller.js +14 -12
  52. package/dist/credentials/credentials.module.d.ts.map +1 -1
  53. package/dist/credentials/credentials.module.js +0 -2
  54. package/dist/credentials/credentials.repository.d.ts +5 -2
  55. package/dist/credentials/credentials.repository.d.ts.map +1 -1
  56. package/dist/credentials/credentials.repository.js +10 -3
  57. package/dist/credentials/credentials.service.d.ts +14 -4
  58. package/dist/credentials/credentials.service.d.ts.map +1 -1
  59. package/dist/credentials/credentials.service.js +106 -30
  60. package/dist/crypto/credential-envelope.d.ts +27 -1
  61. package/dist/crypto/credential-envelope.d.ts.map +1 -1
  62. package/dist/crypto/credential-envelope.js +43 -0
  63. package/dist/egress/egress.listener.d.ts +1 -3
  64. package/dist/egress/egress.listener.d.ts.map +1 -1
  65. package/dist/egress/egress.listener.js +3 -7
  66. package/dist/egress/egress.repository.d.ts +13 -6
  67. package/dist/egress/egress.repository.d.ts.map +1 -1
  68. package/dist/egress/egress.repository.js +31 -44
  69. package/dist/generated/prisma/browser.d.ts +0 -5
  70. package/dist/generated/prisma/browser.d.ts.map +1 -1
  71. package/dist/generated/prisma/client.d.ts +0 -5
  72. package/dist/generated/prisma/client.d.ts.map +1 -1
  73. package/dist/generated/prisma/commonInputTypes.d.ts +0 -55
  74. package/dist/generated/prisma/commonInputTypes.d.ts.map +1 -1
  75. package/dist/generated/prisma/internal/class.d.ts +0 -11
  76. package/dist/generated/prisma/internal/class.d.ts.map +1 -1
  77. package/dist/generated/prisma/internal/class.js +4 -4
  78. package/dist/generated/prisma/internal/prismaNamespace.d.ts +1 -114
  79. package/dist/generated/prisma/internal/prismaNamespace.d.ts.map +1 -1
  80. package/dist/generated/prisma/internal/prismaNamespace.js +2 -29
  81. package/dist/generated/prisma/internal/prismaNamespaceBrowser.d.ts +0 -30
  82. package/dist/generated/prisma/internal/prismaNamespaceBrowser.d.ts.map +1 -1
  83. package/dist/generated/prisma/internal/prismaNamespaceBrowser.js +2 -29
  84. package/dist/generated/prisma/models/Account.d.ts +26 -344
  85. package/dist/generated/prisma/models/Account.d.ts.map +1 -1
  86. package/dist/generated/prisma/models/Environment.d.ts +0 -258
  87. package/dist/generated/prisma/models/Environment.d.ts.map +1 -1
  88. package/dist/generated/prisma/models/User.d.ts +2 -60
  89. package/dist/generated/prisma/models/User.d.ts.map +1 -1
  90. package/dist/generated/prisma/models.d.ts +0 -1
  91. package/dist/generated/prisma/models.d.ts.map +1 -1
  92. package/dist/index.d.ts +1 -2
  93. package/dist/index.d.ts.map +1 -1
  94. package/dist/integrations/client-context.d.ts +20 -0
  95. package/dist/integrations/client-context.d.ts.map +1 -0
  96. package/dist/integrations/client-context.js +23 -0
  97. package/dist/integrations/integrations.controller.d.ts +6 -0
  98. package/dist/integrations/integrations.controller.d.ts.map +1 -1
  99. package/dist/integrations/integrations.controller.js +3 -0
  100. package/dist/integrations/integrations.module.d.ts.map +1 -1
  101. package/dist/integrations/integrations.module.js +3 -1
  102. package/dist/integrations/provider-store.d.ts +110 -0
  103. package/dist/integrations/provider-store.d.ts.map +1 -0
  104. package/dist/integrations/provider-store.js +302 -0
  105. package/dist/proxy/proxy.controller.d.ts +17 -5
  106. package/dist/proxy/proxy.controller.d.ts.map +1 -1
  107. package/dist/proxy/proxy.controller.js +46 -17
  108. package/dist/proxy/proxy.module.d.ts.map +1 -1
  109. package/dist/proxy/proxy.module.js +1 -2
  110. package/dist/proxy/proxy.repository.d.ts +11 -14
  111. package/dist/proxy/proxy.repository.d.ts.map +1 -1
  112. package/dist/proxy/proxy.repository.js +17 -27
  113. package/dist/proxy/proxy.service.d.ts +6 -8
  114. package/dist/proxy/proxy.service.d.ts.map +1 -1
  115. package/dist/proxy/proxy.service.js +72 -31
  116. package/package.json +4 -4
  117. package/prisma/migrations/{20260929101936_init → 20261008090000_init}/migration.sql +5 -58
  118. package/prisma/schema.prisma +2 -43
  119. package/dist/configurations/configurations.repository.d.ts +0 -43
  120. package/dist/configurations/configurations.repository.d.ts.map +0 -1
  121. package/dist/configurations/configurations.repository.js +0 -76
  122. package/dist/generated/prisma/models/Configuration.d.ts +0 -1385
  123. package/dist/generated/prisma/models/Configuration.d.ts.map +0 -1
  124. package/dist/generated/prisma/models/Configuration.js +0 -1
  125. package/prisma/migrations/20260930102529_connection_config/migration.sql +0 -2
@@ -1,9 +1,10 @@
1
1
  import { __decorate, __metadata } from "tslib";
2
- import { BadGatewayException, Injectable, NotFoundException, } from '@nestjs/common';
3
- import { ConfigurationsService } from '../configurations/configurations.service.js';
2
+ import { assertPublicUrl } from '@kosha-ai/integration-sdk';
3
+ import { BadGatewayException, BadRequestException, Injectable, NotFoundException, } from '@nestjs/common';
4
4
  import { CredentialsService } from '../credentials/credentials.service.js';
5
5
  import { EnvironmentsService } from '../environments/environments.service.js';
6
6
  import { IntegrationRegistry } from '../integrations/integration.registry.js';
7
+ import { ProviderStore } from '../integrations/provider-store.js';
7
8
  import { ProxyRepository } from './proxy.repository.js';
8
9
  /**
9
10
  * Never forwarded upstream: hop-by-hop framing headers, and anything carrying
@@ -19,38 +20,48 @@ const STRIPPED_REQUEST_HEADERS = new Set([
19
20
  'authorization',
20
21
  'cookie',
21
22
  'x-api-key',
23
+ 'x-kosha-token-kind',
24
+ 'x-kosha-owner',
22
25
  ]);
23
26
  let ProxyService = class ProxyService {
24
- credentials;
27
+ proxy;
25
28
  environments;
26
- configurations;
29
+ store;
27
30
  registry;
28
31
  resolver;
29
- constructor(credentials, environments, configurations, registry, resolver) {
30
- this.credentials = credentials;
32
+ constructor(proxy, environments, store, registry, resolver) {
33
+ this.proxy = proxy;
31
34
  this.environments = environments;
32
- this.configurations = configurations;
35
+ this.store = store;
33
36
  this.registry = registry;
34
37
  this.resolver = resolver;
35
38
  }
36
39
  async forward(organizationId, environmentId, target, request) {
37
40
  const environment = await this.environments.get(organizationId, environmentId);
38
- const configuration = await this.configurations.getBySlug(environment.id, target.configurationSlug);
39
- if (!configuration.isEnabled) {
40
- throw new NotFoundException('Configuration is disabled');
41
+ // Every miss below answers the same 404: a caller learns nothing about
42
+ // what exists in another environment, or under another owner.
43
+ const integration = this.registry.find(target.provider);
44
+ if (!integration)
45
+ throw new NotFoundException('Connection not found');
46
+ // Depends only on the provider, so a bad header is refused before any read.
47
+ const tokenKind = requestedTokenKind(integration, request.headers);
48
+ // Independent reads, so they go together: the configuration this slug
49
+ // names, and the person this owner names.
50
+ const [configuration, userId] = await Promise.all([
51
+ this.store.findConfigurationBySlugWithCredentials(integration, environment.id, target.configurationSlug),
52
+ this.proxy.findUserId(environment.slug, target.owner),
53
+ ]);
54
+ if (!configuration || !userId) {
55
+ throw new NotFoundException('Connection not found');
41
56
  }
42
- // The owner string is whatever the app calls its tenant or its user, and
43
- // Kosha cannot tell which. A tenant-owned credential wins when both match,
44
- // since that is the shared one.
45
- const account = (await this.credentials.findCredential(environment.id, environment.slug, configuration.id, { organizationExternalId: target.owner })) ??
46
- (await this.credentials.findCredential(environment.id, environment.slug, configuration.id, { externalUserId: target.owner }));
47
- // Deliberately the same answer as a wrong environment or a wrong tenant:
48
- // a caller learns nothing about what exists elsewhere.
57
+ const connection = await this.store.findConnection(integration, environment.id, configuration.id, userId);
58
+ if (!connection)
59
+ throw new NotFoundException('Connection not found');
60
+ const account = await this.proxy.findAccount(connection.accountId);
49
61
  if (!account)
50
62
  throw new NotFoundException('Connection not found');
51
- const integration = this.registry.get(configuration.integrationKey);
52
- const credential = await this.resolver.resolve(account, integration);
53
- const baseUrl = integration.api.resolveBaseUrl?.({
63
+ const credential = await this.resolver.resolve(account, integration, configuration, tokenKind);
64
+ const resolved = integration.api.resolveBaseUrl?.({
54
65
  credential: {
55
66
  accessToken: credential.accessToken,
56
67
  tokenType: credential.tokenType,
@@ -59,11 +70,21 @@ let ProxyService = class ProxyService {
59
70
  // A provider with no fixed host builds its base URL from this — every
60
71
  // Jira path carries a cloudId — so passing nothing made those
61
72
  // providers unreachable by construction.
62
- metadata: asMetadata(account.connectionConfig),
63
- }) ?? integration.api.baseUrl;
73
+ metadata: asMetadata(connection.metadata),
74
+ // And what the operator set up: a self-hosted provider's host is a
75
+ // column on its own configuration, not a fact the package knows.
76
+ fields: configuration.fields,
77
+ });
78
+ // A host the package hardcoded is ours; one resolved from a configuration
79
+ // or a connection was chosen by an operator, so it is checked.
80
+ const baseUrl = resolved ?? integration.api.baseUrl;
64
81
  if (!baseUrl) {
65
82
  throw new BadGatewayException('Provider has no resolvable base URL for this connection');
66
83
  }
84
+ // Re-resolved per call, not only when saved: a host that answered
85
+ // publicly yesterday can point inside the network today.
86
+ if (resolved)
87
+ await assertPublicUrl(baseUrl);
67
88
  const targetUrl = this.buildTargetUrl(baseUrl, request);
68
89
  /*
69
90
  * Header names are compared lowercased, and the integration's headers are
@@ -118,20 +139,40 @@ ProxyService = __decorate([
118
139
  Injectable(),
119
140
  __metadata("design:paramtypes", [ProxyRepository,
120
141
  EnvironmentsService,
121
- ConfigurationsService,
142
+ ProviderStore,
122
143
  IntegrationRegistry,
123
144
  CredentialsService])
124
145
  ], ProxyService);
125
146
  export { ProxyService };
126
147
  /**
127
- * A connection's stored config is free-form JSON, so it may be an array, a
128
- * number or null. Only an object is a config bag; anything else is treated as
129
- * absent rather than handed to an integration that expects to index it.
148
+ * Stored metadata is free-form JSON, so it may be an array, a number or null.
149
+ * Only an object is a metadata bag; anything else is treated as absent rather
150
+ * than handed to an integration that expects to index it.
130
151
  */
131
- function asMetadata(connectionConfig) {
132
- return connectionConfig !== null &&
133
- typeof connectionConfig === 'object' &&
134
- !Array.isArray(connectionConfig)
135
- ? connectionConfig
152
+ function asMetadata(metadata) {
153
+ return metadata !== null &&
154
+ typeof metadata === 'object' &&
155
+ !Array.isArray(metadata)
156
+ ? metadata
136
157
  : undefined;
137
158
  }
159
+ /**
160
+ * Which stored token to send. The primary unless `x-kosha-token-kind` names
161
+ * another, and a kind the provider does not issue is refused rather than
162
+ * quietly answered with the primary. The header is stripped before the
163
+ * request leaves Kosha.
164
+ */
165
+ function requestedTokenKind(integration, headers) {
166
+ const raw = headers['x-kosha-token-kind'];
167
+ const kind = (Array.isArray(raw) ? raw[0] : raw)?.trim();
168
+ if (!kind)
169
+ return undefined;
170
+ const kinds = integration.auth?.tokenKinds ?? [];
171
+ if (!kinds.includes(kind)) {
172
+ // Says what would have worked, since the caller has no other way to ask.
173
+ throw new BadRequestException(kinds.length > 0
174
+ ? `${integration.key} issues no "${kind}" token; use one of: ${kinds.join(', ')}`
175
+ : `${integration.key} issues a single token, so x-kosha-token-kind does not apply`);
176
+ }
177
+ return kind;
178
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kosha-ai/server",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Kosha server: OAuth handshake, encrypted credential storage, token refresh, and an authenticated request proxy for third-party integrations",
5
5
  "keywords": [
6
6
  "kosha",
@@ -109,7 +109,7 @@
109
109
  "rxjs": "^7.8.0",
110
110
  "tslib": "^2.3.0",
111
111
  "zod": "4.5.4",
112
- "@kosha-ai/integration-sdk": "0.2.0"
112
+ "@kosha-ai/integration-sdk": "0.4.0"
113
113
  },
114
114
  "devDependencies": {
115
115
  "@nestjs/testing": "^11.0.0",
@@ -117,7 +117,7 @@
117
117
  "@types/node-forge": "^1.3.14",
118
118
  "@types/pg": "^8.23.1",
119
119
  "prisma": "^7.10.0",
120
- "@kosha-ai/integration-github": "0.2.0",
121
- "@kosha-ai/integration-slack": "0.2.0"
120
+ "@kosha-ai/integration-github": "0.4.0",
121
+ "@kosha-ai/integration-slack": "0.4.0"
122
122
  }
123
123
  }
@@ -1,3 +1,6 @@
1
+ -- CreateSchema
2
+ CREATE SCHEMA IF NOT EXISTS "public";
3
+
1
4
  -- CreateTable
2
5
  CREATE TABLE "user" (
3
6
  "id" TEXT NOT NULL,
@@ -7,7 +10,6 @@ CREATE TABLE "user" (
7
10
  "image" TEXT,
8
11
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
9
12
  "updatedAt" TIMESTAMP(3) NOT NULL,
10
- "externalId" TEXT,
11
13
 
12
14
  CONSTRAINT "user_pkey" PRIMARY KEY ("id")
13
15
  );
@@ -33,7 +35,7 @@ CREATE TABLE "account" (
33
35
  "id" TEXT NOT NULL,
34
36
  "accountId" TEXT NOT NULL,
35
37
  "providerId" TEXT NOT NULL,
36
- "userId" TEXT,
38
+ "userId" TEXT NOT NULL,
37
39
  "accessToken" TEXT,
38
40
  "refreshToken" TEXT,
39
41
  "idToken" TEXT,
@@ -43,8 +45,6 @@ CREATE TABLE "account" (
43
45
  "password" TEXT,
44
46
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
45
47
  "updatedAt" TIMESTAMP(3) NOT NULL,
46
- "environmentId" TEXT,
47
- "organizationExternalId" TEXT,
48
48
 
49
49
  CONSTRAINT "account_pkey" PRIMARY KEY ("id")
50
50
  );
@@ -163,23 +163,6 @@ CREATE TABLE "environment" (
163
163
  CONSTRAINT "environment_pkey" PRIMARY KEY ("id")
164
164
  );
165
165
 
166
- -- CreateTable
167
- CREATE TABLE "configuration" (
168
- "id" TEXT NOT NULL,
169
- "environmentId" TEXT NOT NULL,
170
- "integrationKey" TEXT NOT NULL,
171
- "authType" TEXT NOT NULL,
172
- "name" TEXT NOT NULL,
173
- "slug" TEXT NOT NULL,
174
- "credentials" TEXT,
175
- "settings" JSONB,
176
- "isEnabled" BOOLEAN NOT NULL DEFAULT true,
177
- "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
178
- "updatedAt" TIMESTAMP(3) NOT NULL,
179
-
180
- CONSTRAINT "configuration_pkey" PRIMARY KEY ("id")
181
- );
182
-
183
166
  -- CreateTable
184
167
  CREATE TABLE "egress_grant" (
185
168
  "id" TEXT NOT NULL,
@@ -200,9 +183,6 @@ CREATE TABLE "egress_grant" (
200
183
  -- CreateIndex
201
184
  CREATE UNIQUE INDEX "user_email_key" ON "user"("email");
202
185
 
203
- -- CreateIndex
204
- CREATE INDEX "user_externalId_idx" ON "user"("externalId");
205
-
206
186
  -- CreateIndex
207
187
  CREATE UNIQUE INDEX "session_token_key" ON "session"("token");
208
188
 
@@ -215,12 +195,6 @@ CREATE INDEX "account_userId_idx" ON "account"("userId");
215
195
  -- CreateIndex
216
196
  CREATE INDEX "account_providerId_idx" ON "account"("providerId");
217
197
 
218
- -- CreateIndex
219
- CREATE INDEX "account_environmentId_organizationExternalId_idx" ON "account"("environmentId", "organizationExternalId");
220
-
221
- -- CreateIndex
222
- CREATE UNIQUE INDEX "account_environmentId_organizationExternalId_providerId_key" ON "account"("environmentId", "organizationExternalId", "providerId");
223
-
224
198
  -- CreateIndex
225
199
  CREATE INDEX "verification_identifier_idx" ON "verification"("identifier");
226
200
 
@@ -269,12 +243,6 @@ CREATE INDEX "environment_teamId_idx" ON "environment"("teamId");
269
243
  -- CreateIndex
270
244
  CREATE UNIQUE INDEX "environment_organizationId_slug_key" ON "environment"("organizationId", "slug");
271
245
 
272
- -- CreateIndex
273
- CREATE INDEX "configuration_environmentId_idx" ON "configuration"("environmentId");
274
-
275
- -- CreateIndex
276
- CREATE UNIQUE INDEX "configuration_environmentId_slug_key" ON "configuration"("environmentId", "slug");
277
-
278
246
  -- CreateIndex
279
247
  CREATE INDEX "egress_grant_tokenHash_idx" ON "egress_grant"("tokenHash");
280
248
 
@@ -288,10 +256,7 @@ CREATE INDEX "egress_grant_userId_idx" ON "egress_grant"("userId");
288
256
  ALTER TABLE "session" ADD CONSTRAINT "session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE;
289
257
 
290
258
  -- AddForeignKey
291
- ALTER TABLE "account" ADD CONSTRAINT "account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE SET NULL ON UPDATE CASCADE;
292
-
293
- -- AddForeignKey
294
- ALTER TABLE "account" ADD CONSTRAINT "account_environmentId_fkey" FOREIGN KEY ("environmentId") REFERENCES "environment"("id") ON DELETE CASCADE ON UPDATE CASCADE;
259
+ ALTER TABLE "account" ADD CONSTRAINT "account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE;
295
260
 
296
261
  -- AddForeignKey
297
262
  ALTER TABLE "member" ADD CONSTRAINT "member_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
@@ -320,26 +285,8 @@ ALTER TABLE "environment" ADD CONSTRAINT "environment_organizationId_fkey" FOREI
320
285
  -- AddForeignKey
321
286
  ALTER TABLE "environment" ADD CONSTRAINT "environment_teamId_fkey" FOREIGN KEY ("teamId") REFERENCES "team"("id") ON DELETE SET NULL ON UPDATE CASCADE;
322
287
 
323
- -- AddForeignKey
324
- ALTER TABLE "configuration" ADD CONSTRAINT "configuration_environmentId_fkey" FOREIGN KEY ("environmentId") REFERENCES "environment"("id") ON DELETE CASCADE ON UPDATE CASCADE;
325
-
326
288
  -- AddForeignKey
327
289
  ALTER TABLE "egress_grant" ADD CONSTRAINT "egress_grant_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
328
290
 
329
291
  -- AddForeignKey
330
292
  ALTER TABLE "egress_grant" ADD CONSTRAINT "egress_grant_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE;
331
-
332
- -- A personal connection has no tenant, and Postgres treats NULLs as distinct,
333
- -- so the unique index above does not constrain one. Prisma cannot declare a
334
- -- partial index, so it is written here by hand.
335
- CREATE UNIQUE INDEX "account_personal_connection_key"
336
- ON "account" ("environmentId", "userId", "providerId")
337
- WHERE "organizationExternalId" IS NULL;
338
-
339
- -- A credential belongs to a person or to a tenant. userId is nullable so that
340
- -- deleting the person who pasted a tenant's key does not delete the key, which
341
- -- is the tenant's rather than theirs; this check stops that leaving a row
342
- -- belonging to nobody. Deleting someone who holds personal connections fails
343
- -- here until those rows are dealt with explicitly.
344
- ALTER TABLE "account" ADD CONSTRAINT "account_has_an_owner"
345
- CHECK ("userId" IS NOT NULL OR "organizationExternalId" IS NOT NULL);
@@ -21,8 +21,6 @@ model User {
21
21
  createdAt DateTime @default(now())
22
22
  updatedAt DateTime @updatedAt
23
23
 
24
- externalId String?
25
-
26
24
  sessions Session[]
27
25
  accounts Account[]
28
26
  members Member[]
@@ -30,7 +28,6 @@ model User {
30
28
  teamMembers TeamMember[]
31
29
  egressGrants EgressGrant[]
32
30
 
33
- @@index([externalId])
34
31
  @@map("user")
35
32
  }
36
33
 
@@ -55,8 +52,8 @@ model Account {
55
52
  id String @id
56
53
  accountId String
57
54
  providerId String
58
- userId String?
59
- user User? @relation(fields: [userId], references: [id], onDelete: SetNull)
55
+ userId String
56
+ user User @relation(fields: [userId], references: [id], onDelete: Cascade)
60
57
  accessToken String?
61
58
  refreshToken String?
62
59
  idToken String?
@@ -67,20 +64,8 @@ model Account {
67
64
  createdAt DateTime @default(now())
68
65
  updatedAt DateTime @updatedAt
69
66
 
70
- environmentId String?
71
- environment Environment? @relation(fields: [environmentId], references: [id], onDelete: Cascade)
72
- organizationExternalId String?
73
-
74
- /// Per-connection provider parameters, for providers that need any: the
75
- /// Jira site, the Zendesk subdomain. Non-secret, and held on the connection
76
- /// rather than on the configuration because one OAuth app serves every
77
- /// tenant. Null on a Kosha login, like every other column here.
78
- connectionConfig Json?
79
-
80
- @@unique([environmentId, organizationExternalId, providerId])
81
67
  @@index([userId])
82
68
  @@index([providerId])
83
- @@index([environmentId, organizationExternalId])
84
69
  @@map("account")
85
70
  }
86
71
 
@@ -219,38 +204,12 @@ model Environment {
219
204
  createdAt DateTime @default(now())
220
205
  updatedAt DateTime @updatedAt
221
206
 
222
- configurations Configuration[]
223
- accounts Account[]
224
-
225
207
  @@unique([organizationId, slug])
226
208
  @@index([organizationId])
227
209
  @@index([teamId])
228
210
  @@map("environment")
229
211
  }
230
212
 
231
- model Configuration {
232
- id String @id @default(cuid())
233
- environmentId String
234
- environment Environment @relation(fields: [environmentId], references: [id], onDelete: Cascade)
235
-
236
- integrationKey String
237
- authType String
238
-
239
- name String
240
- slug String
241
-
242
- credentials String?
243
- settings Json?
244
-
245
- isEnabled Boolean @default(true)
246
- createdAt DateTime @default(now())
247
- updatedAt DateTime @updatedAt
248
-
249
- @@unique([environmentId, slug])
250
- @@index([environmentId])
251
- @@map("configuration")
252
- }
253
-
254
213
  model EgressGrant {
255
214
  id String @id @default(cuid())
256
215
  organizationId String
@@ -1,43 +0,0 @@
1
- import type { Configuration, Prisma } from '../generated/prisma/client.js';
2
- import { PrismaService } from '../prisma/prisma.service.js';
3
- export interface ConfigurationRow {
4
- environmentId: string;
5
- integrationKey: string;
6
- authType: string;
7
- name: string;
8
- slug: string;
9
- credentials: string | null;
10
- settings: Prisma.InputJsonValue | undefined;
11
- }
12
- /** A configuration as a client may see it: everything but the secrets. */
13
- export type PublicConfiguration = Omit<Configuration, 'credentials'>;
14
- export interface ConfigurationChanges {
15
- name?: string;
16
- /** Null clears the stored secrets; undefined leaves them alone. */
17
- credentials?: string | null;
18
- settings?: Prisma.InputJsonValue;
19
- isEnabled?: boolean;
20
- }
21
- export declare class ConfigurationsRepository {
22
- private readonly prisma;
23
- constructor(prisma: PrismaService);
24
- findManyByEnvironment(environmentId: string): Promise<PublicConfiguration[]>;
25
- findById(environmentId: string, id: string): Promise<PublicConfiguration | null>;
26
- /** How a caller's slug becomes the row the proxy and connect flows use. */
27
- findBySlug(environmentId: string, slug: string): Promise<PublicConfiguration | null>;
28
- /**
29
- * The one read that keeps the secrets, for the connect and proxy paths.
30
- * Never reachable from a controller.
31
- */
32
- findAnyById(id: string): Promise<Configuration | null>;
33
- create(row: ConfigurationRow): Promise<PublicConfiguration>;
34
- update(id: string, changes: ConfigurationChanges): Promise<PublicConfiguration>;
35
- delete(id: string): Promise<void>;
36
- /**
37
- * Credentials pointing at this configuration. `account.providerId` holds the
38
- * id as a plain string, so this count is what stands in for the foreign key
39
- * the design gave up.
40
- */
41
- countCredentials(configurationId: string): Promise<number>;
42
- }
43
- //# sourceMappingURL=configurations.repository.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"configurations.repository.d.ts","sourceRoot":"","sources":["../../src/configurations/configurations.repository.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,+BAA+B,CAAC;AAC3E,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D,MAAM,WAAW,gBAAgB;IAC/B,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,QAAQ,EAAE,MAAM,CAAC,cAAc,GAAG,SAAS,CAAC;CAC7C;AAED,0EAA0E;AAC1E,MAAM,MAAM,mBAAmB,GAAG,IAAI,CAAC,aAAa,EAAE,aAAa,CAAC,CAAC;AAErE,MAAM,WAAW,oBAAoB;IACnC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,mEAAmE;IACnE,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,QAAQ,CAAC,EAAE,MAAM,CAAC,cAAc,CAAC;IACjC,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAWD,qBACa,wBAAwB;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,aAAa;IAElD,qBAAqB,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,mBAAmB,EAAE,CAAC;IAQ5E,QAAQ,CACN,aAAa,EAAE,MAAM,EACrB,EAAE,EAAE,MAAM,GACT,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC;IAOtC,2EAA2E;IAC3E,UAAU,CACR,aAAa,EAAE,MAAM,EACrB,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC;IAOtC;;;OAGG;IACH,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAItD,MAAM,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,mBAAmB,CAAC;IAO3D,MAAM,CACJ,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,mBAAmB,CAAC;IAQzB,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIvC;;;;OAIG;IACH,gBAAgB,CAAC,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAK3D"}
@@ -1,76 +0,0 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { Injectable } from '@nestjs/common';
3
- import { PrismaService } from '../prisma/prisma.service.js';
4
- /**
5
- * `credentials` is ciphertext by the time it reaches here, and stays ciphertext
6
- * on the way out. Nothing in this file can read a secret.
7
- *
8
- * Every read that can reach a client leaves the column out entirely. Ciphertext
9
- * is not plaintext, but there is no reason for a browser to hold it, and the
10
- * one path that needs the secrets asks for them by name.
11
- */
12
- const WITHOUT_SECRETS = { credentials: true };
13
- let ConfigurationsRepository = class ConfigurationsRepository {
14
- prisma;
15
- constructor(prisma) {
16
- this.prisma = prisma;
17
- }
18
- findManyByEnvironment(environmentId) {
19
- return this.prisma.configuration.findMany({
20
- where: { environmentId },
21
- omit: WITHOUT_SECRETS,
22
- orderBy: { createdAt: 'desc' },
23
- });
24
- }
25
- findById(environmentId, id) {
26
- return this.prisma.configuration.findFirst({
27
- where: { id, environmentId },
28
- omit: WITHOUT_SECRETS,
29
- });
30
- }
31
- /** How a caller's slug becomes the row the proxy and connect flows use. */
32
- findBySlug(environmentId, slug) {
33
- return this.prisma.configuration.findFirst({
34
- where: { environmentId, slug },
35
- omit: WITHOUT_SECRETS,
36
- });
37
- }
38
- /**
39
- * The one read that keeps the secrets, for the connect and proxy paths.
40
- * Never reachable from a controller.
41
- */
42
- findAnyById(id) {
43
- return this.prisma.configuration.findUnique({ where: { id } });
44
- }
45
- create(row) {
46
- return this.prisma.configuration.create({
47
- data: row,
48
- omit: WITHOUT_SECRETS,
49
- });
50
- }
51
- update(id, changes) {
52
- return this.prisma.configuration.update({
53
- where: { id },
54
- data: changes,
55
- omit: WITHOUT_SECRETS,
56
- });
57
- }
58
- async delete(id) {
59
- await this.prisma.configuration.delete({ where: { id } });
60
- }
61
- /**
62
- * Credentials pointing at this configuration. `account.providerId` holds the
63
- * id as a plain string, so this count is what stands in for the foreign key
64
- * the design gave up.
65
- */
66
- countCredentials(configurationId) {
67
- return this.prisma.account.count({
68
- where: { providerId: configurationId },
69
- });
70
- }
71
- };
72
- ConfigurationsRepository = __decorate([
73
- Injectable(),
74
- __metadata("design:paramtypes", [PrismaService])
75
- ], ConfigurationsRepository);
76
- export { ConfigurationsRepository };