@kosha-ai/server 0.0.6 → 1.0.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 (210) hide show
  1. package/dist/auth/api-key.guard.d.ts +7 -2
  2. package/dist/auth/api-key.guard.d.ts.map +1 -1
  3. package/dist/auth/api-key.guard.js +28 -8
  4. package/dist/auth/auth.factory.d.ts +15 -1
  5. package/dist/auth/auth.factory.d.ts.map +1 -1
  6. package/dist/auth/auth.factory.js +48 -2
  7. package/dist/bootstrap.d.ts.map +1 -1
  8. package/dist/bootstrap.js +24 -6
  9. package/dist/config/kosha-config.d.ts +21 -0
  10. package/dist/config/kosha-config.d.ts.map +1 -1
  11. package/dist/config/kosha-config.js +31 -2
  12. package/dist/configurations/configurations.controller.d.ts +11 -136
  13. package/dist/configurations/configurations.controller.d.ts.map +1 -1
  14. package/dist/configurations/configurations.controller.js +58 -25
  15. package/dist/configurations/configurations.module.d.ts.map +1 -1
  16. package/dist/configurations/configurations.module.js +4 -1
  17. package/dist/configurations/configurations.repository.d.ts +43 -0
  18. package/dist/configurations/configurations.repository.d.ts.map +1 -0
  19. package/dist/configurations/configurations.repository.js +76 -0
  20. package/dist/configurations/configurations.service.d.ts +38 -194
  21. package/dist/configurations/configurations.service.d.ts.map +1 -1
  22. package/dist/configurations/configurations.service.js +118 -110
  23. package/dist/configurations/dto/create-configuration.dto.d.ts +8 -0
  24. package/dist/configurations/dto/create-configuration.dto.d.ts.map +1 -0
  25. package/dist/configurations/dto/create-configuration.dto.js +54 -0
  26. package/dist/configurations/dto/update-configuration.dto.d.ts +7 -0
  27. package/dist/configurations/dto/update-configuration.dto.d.ts.map +1 -0
  28. package/dist/configurations/dto/update-configuration.dto.js +39 -0
  29. package/dist/connect/connect.controller.d.ts +6 -8
  30. package/dist/connect/connect.controller.d.ts.map +1 -1
  31. package/dist/connect/connect.controller.js +45 -15
  32. package/dist/connect/connect.module.d.ts.map +1 -1
  33. package/dist/connect/connect.module.js +4 -2
  34. package/dist/connect/connect.repository.d.ts +78 -0
  35. package/dist/connect/connect.repository.d.ts.map +1 -0
  36. package/dist/connect/connect.repository.js +146 -0
  37. package/dist/connect/connect.service.d.ts +28 -19
  38. package/dist/connect/connect.service.d.ts.map +1 -1
  39. package/dist/connect/connect.service.js +149 -163
  40. package/dist/connect/dto/create-connect-session.dto.d.ts +8 -0
  41. package/dist/connect/dto/create-connect-session.dto.d.ts.map +1 -0
  42. package/dist/connect/dto/create-connect-session.dto.js +55 -0
  43. package/dist/connect/dto/create-test-session.dto.d.ts +10 -0
  44. package/dist/connect/dto/create-test-session.dto.d.ts.map +1 -0
  45. package/dist/connect/dto/create-test-session.dto.js +34 -0
  46. package/dist/connections/connections.controller.d.ts +8 -61
  47. package/dist/connections/connections.controller.d.ts.map +1 -1
  48. package/dist/connections/connections.controller.js +31 -9
  49. package/dist/connections/connections.module.d.ts.map +1 -1
  50. package/dist/connections/connections.module.js +4 -1
  51. package/dist/connections/connections.repository.d.ts +24 -0
  52. package/dist/connections/connections.repository.d.ts.map +1 -0
  53. package/dist/connections/connections.repository.js +66 -0
  54. package/dist/connections/connections.service.d.ts +24 -69
  55. package/dist/connections/connections.service.d.ts.map +1 -1
  56. package/dist/connections/connections.service.js +59 -56
  57. package/dist/connections/dto/list-connections.dto.d.ts +5 -0
  58. package/dist/connections/dto/list-connections.dto.d.ts.map +1 -0
  59. package/dist/connections/dto/list-connections.dto.js +25 -0
  60. package/dist/connections/environment-connections.controller.d.ts +26 -0
  61. package/dist/connections/environment-connections.controller.d.ts.map +1 -0
  62. package/dist/connections/environment-connections.controller.js +81 -0
  63. package/dist/connections/environment-connections.module.d.ts +11 -0
  64. package/dist/connections/environment-connections.module.d.ts.map +1 -0
  65. package/dist/connections/environment-connections.module.js +21 -0
  66. package/dist/credentials/credentials.module.d.ts +3 -0
  67. package/dist/credentials/credentials.module.d.ts.map +1 -0
  68. package/dist/credentials/credentials.module.js +15 -0
  69. package/dist/credentials/credentials.repository.d.ts +8 -0
  70. package/dist/credentials/credentials.repository.d.ts.map +1 -0
  71. package/dist/credentials/credentials.repository.js +21 -0
  72. package/dist/credentials/credentials.service.d.ts +45 -0
  73. package/dist/credentials/credentials.service.d.ts.map +1 -0
  74. package/dist/credentials/credentials.service.js +135 -0
  75. package/dist/crypto/credential-envelope.d.ts +56 -0
  76. package/dist/crypto/credential-envelope.d.ts.map +1 -0
  77. package/dist/crypto/credential-envelope.js +70 -0
  78. package/dist/crypto/crypto.service.d.ts +8 -0
  79. package/dist/crypto/crypto.service.d.ts.map +1 -1
  80. package/dist/crypto/crypto.service.js +12 -0
  81. package/dist/egress/ca.service.d.ts +36 -0
  82. package/dist/egress/ca.service.d.ts.map +1 -0
  83. package/dist/egress/ca.service.js +117 -0
  84. package/dist/egress/dto/mint-grant.dto.d.ts +7 -0
  85. package/dist/egress/dto/mint-grant.dto.d.ts.map +1 -0
  86. package/dist/egress/dto/mint-grant.dto.js +44 -0
  87. package/dist/egress/egress.controller.d.ts +18 -0
  88. package/dist/egress/egress.controller.d.ts.map +1 -0
  89. package/dist/egress/egress.controller.js +80 -0
  90. package/dist/egress/egress.listener.d.ts +39 -0
  91. package/dist/egress/egress.listener.d.ts.map +1 -0
  92. package/dist/egress/egress.listener.js +328 -0
  93. package/dist/egress/egress.module.d.ts +7 -0
  94. package/dist/egress/egress.module.d.ts.map +1 -0
  95. package/dist/egress/egress.module.js +31 -0
  96. package/dist/egress/egress.repository.d.ts +58 -0
  97. package/dist/egress/egress.repository.d.ts.map +1 -0
  98. package/dist/egress/egress.repository.js +116 -0
  99. package/dist/egress/grants.service.d.ts +77 -0
  100. package/dist/egress/grants.service.d.ts.map +1 -0
  101. package/dist/egress/grants.service.js +159 -0
  102. package/dist/egress/host-rules.service.d.ts +35 -0
  103. package/dist/egress/host-rules.service.d.ts.map +1 -0
  104. package/dist/egress/host-rules.service.js +48 -0
  105. package/dist/environments/dto/create-environment.dto.d.ts +6 -0
  106. package/dist/environments/dto/create-environment.dto.d.ts.map +1 -0
  107. package/dist/environments/dto/create-environment.dto.js +34 -0
  108. package/dist/environments/dto/update-environment.dto.d.ts +5 -0
  109. package/dist/environments/dto/update-environment.dto.d.ts.map +1 -0
  110. package/dist/environments/dto/update-environment.dto.js +24 -0
  111. package/dist/environments/environments.controller.d.ts +53 -0
  112. package/dist/environments/environments.controller.d.ts.map +1 -0
  113. package/dist/environments/environments.controller.js +101 -0
  114. package/dist/environments/environments.module.d.ts +3 -0
  115. package/dist/environments/environments.module.d.ts.map +1 -0
  116. package/dist/environments/environments.module.js +15 -0
  117. package/dist/environments/environments.repository.d.ts +37 -0
  118. package/dist/environments/environments.repository.d.ts.map +1 -0
  119. package/dist/environments/environments.repository.js +82 -0
  120. package/dist/environments/environments.service.d.ts +43 -0
  121. package/dist/environments/environments.service.d.ts.map +1 -0
  122. package/dist/environments/environments.service.js +124 -0
  123. package/dist/generated/prisma/browser.d.ts +17 -35
  124. package/dist/generated/prisma/browser.d.ts.map +1 -1
  125. package/dist/generated/prisma/client.d.ts +17 -35
  126. package/dist/generated/prisma/client.d.ts.map +1 -1
  127. package/dist/generated/prisma/commonInputTypes.d.ts +104 -145
  128. package/dist/generated/prisma/commonInputTypes.d.ts.map +1 -1
  129. package/dist/generated/prisma/enums.d.ts +1 -21
  130. package/dist/generated/prisma/enums.d.ts.map +1 -1
  131. package/dist/generated/prisma/enums.js +1 -18
  132. package/dist/generated/prisma/internal/class.d.ts +24 -46
  133. package/dist/generated/prisma/internal/class.d.ts.map +1 -1
  134. package/dist/generated/prisma/internal/class.js +4 -4
  135. package/dist/generated/prisma/internal/prismaNamespace.d.ts +258 -472
  136. package/dist/generated/prisma/internal/prismaNamespace.d.ts.map +1 -1
  137. package/dist/generated/prisma/internal/prismaNamespace.js +45 -83
  138. package/dist/generated/prisma/internal/prismaNamespaceBrowser.d.ts +45 -85
  139. package/dist/generated/prisma/internal/prismaNamespaceBrowser.d.ts.map +1 -1
  140. package/dist/generated/prisma/internal/prismaNamespaceBrowser.js +45 -83
  141. package/dist/generated/prisma/models/Account.d.ts +344 -26
  142. package/dist/generated/prisma/models/Account.d.ts.map +1 -1
  143. package/dist/generated/prisma/models/Configuration.d.ts +218 -851
  144. package/dist/generated/prisma/models/Configuration.d.ts.map +1 -1
  145. package/dist/generated/prisma/models/EgressGrant.d.ts +1535 -0
  146. package/dist/generated/prisma/models/EgressGrant.d.ts.map +1 -0
  147. package/dist/generated/prisma/models/Environment.d.ts +1644 -0
  148. package/dist/generated/prisma/models/Environment.d.ts.map +1 -0
  149. package/dist/generated/prisma/models/Invitation.d.ts +42 -1
  150. package/dist/generated/prisma/models/Invitation.d.ts.map +1 -1
  151. package/dist/generated/prisma/models/Organization.d.ts +183 -322
  152. package/dist/generated/prisma/models/Organization.d.ts.map +1 -1
  153. package/dist/generated/prisma/models/Session.d.ts +36 -1
  154. package/dist/generated/prisma/models/Session.d.ts.map +1 -1
  155. package/dist/generated/prisma/models/Team.d.ts +1498 -0
  156. package/dist/generated/prisma/models/Team.d.ts.map +1 -0
  157. package/dist/generated/prisma/models/TeamMember.d.ts +1281 -0
  158. package/dist/generated/prisma/models/TeamMember.d.ts.map +1 -0
  159. package/dist/generated/prisma/models/User.d.ts +338 -2
  160. package/dist/generated/prisma/models/User.d.ts.map +1 -1
  161. package/dist/generated/prisma/models.d.ts +4 -6
  162. package/dist/generated/prisma/models.d.ts.map +1 -1
  163. package/dist/index.d.ts +20 -5
  164. package/dist/index.d.ts.map +1 -1
  165. package/dist/index.js +17 -3
  166. package/dist/integrations/integrations.controller.d.ts +2 -1
  167. package/dist/integrations/integrations.controller.d.ts.map +1 -1
  168. package/dist/integrations/integrations.module.d.ts.map +1 -1
  169. package/dist/integrations/integrations.module.js +0 -2
  170. package/dist/kosha.module.d.ts.map +1 -1
  171. package/dist/kosha.module.js +13 -3
  172. package/dist/proxy/proxy.controller.d.ts +8 -3
  173. package/dist/proxy/proxy.controller.d.ts.map +1 -1
  174. package/dist/proxy/proxy.controller.js +26 -10
  175. package/dist/proxy/proxy.module.d.ts.map +1 -1
  176. package/dist/proxy/proxy.module.js +5 -3
  177. package/dist/proxy/proxy.repository.d.ts +24 -0
  178. package/dist/proxy/proxy.repository.d.ts.map +1 -0
  179. package/dist/proxy/proxy.repository.js +47 -0
  180. package/dist/proxy/proxy.service.d.ts +17 -21
  181. package/dist/proxy/proxy.service.d.ts.map +1 -1
  182. package/dist/proxy/proxy.service.js +61 -110
  183. package/package.json +9 -4
  184. package/prisma/migrations/20260929101936_init/migration.sql +345 -0
  185. package/prisma/migrations/20260930102529_connection_config/migration.sql +2 -0
  186. package/prisma/schema.prisma +103 -178
  187. package/dist/generated/prisma/models/ConnectSession.d.ts +0 -1606
  188. package/dist/generated/prisma/models/ConnectSession.d.ts.map +0 -1
  189. package/dist/generated/prisma/models/Connection.d.ts +0 -1949
  190. package/dist/generated/prisma/models/Connection.d.ts.map +0 -1
  191. package/dist/generated/prisma/models/ConnectionCredential.d.ts +0 -1293
  192. package/dist/generated/prisma/models/ConnectionCredential.d.ts.map +0 -1
  193. package/dist/generated/prisma/models/EndUser.d.ts +0 -1539
  194. package/dist/generated/prisma/models/EndUser.d.ts.map +0 -1
  195. package/dist/generated/prisma/models/OAuthState.d.ts +0 -1404
  196. package/dist/generated/prisma/models/OAuthState.d.ts.map +0 -1
  197. package/dist/generated/prisma/models/OAuthState.js +0 -1
  198. package/dist/generated/prisma/models/Provider.d.ts +0 -1462
  199. package/dist/generated/prisma/models/Provider.d.ts.map +0 -1
  200. package/dist/generated/prisma/models/Provider.js +0 -1
  201. package/dist/integrations/provider-sync.service.d.ts +0 -24
  202. package/dist/integrations/provider-sync.service.d.ts.map +0 -1
  203. package/dist/integrations/provider-sync.service.js +0 -74
  204. package/prisma/migrations/20260908000000_init/migration.sql +0 -209
  205. package/prisma/migrations/20260908161813_add_connect_domain/migration.sql +0 -173
  206. package/prisma/migrations/20260916000000_add_configuration_slug/migration.sql +0 -15
  207. /package/dist/generated/prisma/models/{ConnectSession.js → EgressGrant.js} +0 -0
  208. /package/dist/generated/prisma/models/{Connection.js → Environment.js} +0 -0
  209. /package/dist/generated/prisma/models/{ConnectionCredential.js → Team.js} +0 -0
  210. /package/dist/generated/prisma/models/{EndUser.js → TeamMember.js} +0 -0
@@ -1,16 +1,18 @@
1
1
  import { __decorate } from "tslib";
2
2
  import { Module } from '@nestjs/common';
3
3
  import { ConfigurationsModule } from '../configurations/configurations.module.js';
4
- import { ConnectModule } from '../connect/connect.module.js';
4
+ import { CredentialsModule } from '../credentials/credentials.module.js';
5
+ import { EnvironmentsModule } from '../environments/environments.module.js';
5
6
  import { ProxyController } from './proxy.controller.js';
7
+ import { ProxyRepository } from './proxy.repository.js';
6
8
  import { ProxyService } from './proxy.service.js';
7
9
  let ProxyModule = class ProxyModule {
8
10
  };
9
11
  ProxyModule = __decorate([
10
12
  Module({
11
- imports: [ConnectModule, ConfigurationsModule],
13
+ imports: [CredentialsModule, EnvironmentsModule, ConfigurationsModule],
12
14
  controllers: [ProxyController],
13
- providers: [ProxyService],
15
+ providers: [ProxyService, ProxyRepository],
14
16
  })
15
17
  ], ProxyModule);
16
18
  export { ProxyModule };
@@ -0,0 +1,24 @@
1
+ import type { Account } from '../generated/prisma/client.js';
2
+ import { PrismaService } from '../prisma/prisma.service.js';
3
+ /** Whose credential a caller is asking for. */
4
+ export interface CredentialOwner {
5
+ /** The app's tenant, when the credential belongs to one. */
6
+ organizationExternalId?: string | null;
7
+ /** The app's id for the person, when it belongs to them. */
8
+ externalUserId?: string | null;
9
+ }
10
+ export declare class ProxyRepository {
11
+ private readonly prisma;
12
+ constructor(prisma: PrismaService);
13
+ /**
14
+ * A tenant-owned credential is one read against the unique index on
15
+ * (environmentId, organizationExternalId, providerId) — no join, since both
16
+ * columns are on the row.
17
+ *
18
+ * A personal one joins `user`, because the app's id for a person lives
19
+ * there. That join is on `user.email`, which is unique, and the derived form
20
+ * carries the environment — so it is an index lookup rather than a scan.
21
+ */
22
+ findCredential(environmentId: string, environmentSlug: string, providerId: string, owner: CredentialOwner): Promise<Account | null>;
23
+ }
24
+ //# sourceMappingURL=proxy.repository.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy.repository.d.ts","sourceRoot":"","sources":["../../src/proxy/proxy.repository.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D,+CAA+C;AAC/C,MAAM,WAAW,eAAe;IAC9B,4DAA4D;IAC5D,sBAAsB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACvC,4DAA4D;IAC5D,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAChC;AAED,qBACa,eAAe;IACd,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,aAAa;IAElD;;;;;;;;OAQG;IACG,cAAc,CAClB,aAAa,EAAE,MAAM,EACrB,eAAe,EAAE,MAAM,EACvB,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,eAAe,GACrB,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;CAwB3B"}
@@ -0,0 +1,47 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { Injectable } from '@nestjs/common';
3
+ import { derivedEmail } from '../connect/connect.repository.js';
4
+ import { PrismaService } from '../prisma/prisma.service.js';
5
+ let ProxyRepository = class ProxyRepository {
6
+ prisma;
7
+ constructor(prisma) {
8
+ this.prisma = prisma;
9
+ }
10
+ /**
11
+ * A tenant-owned credential is one read against the unique index on
12
+ * (environmentId, organizationExternalId, providerId) — no join, since both
13
+ * columns are on the row.
14
+ *
15
+ * A personal one joins `user`, because the app's id for a person lives
16
+ * there. That join is on `user.email`, which is unique, and the derived form
17
+ * carries the environment — so it is an index lookup rather than a scan.
18
+ */
19
+ async findCredential(environmentId, environmentSlug, providerId, owner) {
20
+ if (owner.organizationExternalId) {
21
+ return this.prisma.account.findFirst({
22
+ where: {
23
+ environmentId,
24
+ providerId,
25
+ organizationExternalId: owner.organizationExternalId,
26
+ },
27
+ });
28
+ }
29
+ if (!owner.externalUserId)
30
+ return null;
31
+ return this.prisma.account.findFirst({
32
+ where: {
33
+ environmentId,
34
+ providerId,
35
+ organizationExternalId: null,
36
+ user: {
37
+ email: derivedEmail(environmentSlug, owner.externalUserId),
38
+ },
39
+ },
40
+ });
41
+ }
42
+ };
43
+ ProxyRepository = __decorate([
44
+ Injectable(),
45
+ __metadata("design:paramtypes", [PrismaService])
46
+ ], ProxyRepository);
47
+ export { ProxyRepository };
@@ -1,8 +1,8 @@
1
- import { ConnectService } from '../connect/connect.service.js';
2
1
  import { ConfigurationsService } from '../configurations/configurations.service.js';
3
- import { CryptoService } from '../crypto/crypto.service.js';
2
+ import { CredentialsService } from '../credentials/credentials.service.js';
3
+ import { EnvironmentsService } from '../environments/environments.service.js';
4
4
  import { IntegrationRegistry } from '../integrations/integration.registry.js';
5
- import { PrismaService } from '../prisma/prisma.service.js';
5
+ import { ProxyRepository } from './proxy.repository.js';
6
6
  export interface ProxyRequest {
7
7
  method: string;
8
8
  path: string;
@@ -15,26 +15,22 @@ export interface ProxyResult {
15
15
  headers: Headers;
16
16
  body: string;
17
17
  }
18
+ /**
19
+ * Who the caller says the credential belongs to. One of these, taken from the
20
+ * URL: a tenant of the consuming app, or one of its users.
21
+ */
22
+ export interface ProxyTarget {
23
+ configurationSlug: string;
24
+ owner: string;
25
+ }
18
26
  export declare class ProxyService {
19
- private readonly prisma;
20
- private readonly crypto;
21
- private readonly registry;
22
- private readonly connect;
27
+ private readonly credentials;
28
+ private readonly environments;
23
29
  private readonly configurations;
24
- private readonly logger;
25
- constructor(prisma: PrismaService, crypto: CryptoService, registry: IntegrationRegistry, connect: ConnectService, configurations: ConfigurationsService);
26
- forward(organizationId: string, connectionId: string, request: ProxyRequest): Promise<ProxyResult>;
27
- /**
28
- * Returns a usable access token, refreshing first when the stored one has
29
- * expired and the provider supports refresh.
30
- *
31
- * Without this an expired token is a dead end for the end user: they have to
32
- * walk the whole consent flow again for a token the provider was willing to
33
- * renew.
34
- */
35
- private resolveCredential;
36
- private runTokenRefreshedHook;
37
- private markExpired;
30
+ private readonly registry;
31
+ private readonly resolver;
32
+ constructor(credentials: ProxyRepository, environments: EnvironmentsService, configurations: ConfigurationsService, registry: IntegrationRegistry, resolver: CredentialsService);
33
+ forward(organizationId: string, environmentId: string, target: ProxyTarget, request: ProxyRequest): Promise<ProxyResult>;
38
34
  private buildTargetUrl;
39
35
  }
40
36
  //# sourceMappingURL=proxy.service.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"proxy.service.d.ts","sourceRoot":"","sources":["../../src/proxy/proxy.service.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAC/D,OAAO,EAAE,qBAAqB,EAAE,MAAM,6CAA6C,CAAC;AACpF,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,yCAAyC,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAkB5D,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC;IACvD,IAAI,EAAE,OAAO,CAAC;CACf;AAED,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,qBACa,YAAY;IAIrB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,cAAc;IAPjC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiC;gBAGrC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,aAAa,EACrB,QAAQ,EAAE,mBAAmB,EAC7B,OAAO,EAAE,cAAc,EACvB,cAAc,EAAE,qBAAqB;IAGlD,OAAO,CACX,cAAc,EAAE,MAAM,EACtB,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE,YAAY,GACpB,OAAO,CAAC,WAAW,CAAC;IAwEvB;;;;;;;OAOG;YACW,iBAAiB;YA0EjB,qBAAqB;YAqBrB,WAAW;IAOzB,OAAO,CAAC,cAAc;CAevB"}
1
+ {"version":3,"file":"proxy.service.d.ts","sourceRoot":"","sources":["../../src/proxy/proxy.service.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,qBAAqB,EAAE,MAAM,6CAA6C,CAAC;AACpF,OAAO,EAAE,kBAAkB,EAAE,MAAM,uCAAuC,CAAC;AAC3E,OAAO,EAAE,mBAAmB,EAAE,MAAM,yCAAyC,CAAC;AAC9E,OAAO,EAAE,mBAAmB,EAAE,MAAM,yCAAyC,CAAC;AAC9E,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAkBxD,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC;IACvD,IAAI,EAAE,OAAO,CAAC;CACf;AAED,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,iBAAiB,EAAE,MAAM,CAAC;IAC1B,KAAK,EAAE,MAAM,CAAC;CACf;AAED,qBACa,YAAY;IAErB,OAAO,CAAC,QAAQ,CAAC,WAAW;IAC5B,OAAO,CAAC,QAAQ,CAAC,YAAY;IAC7B,OAAO,CAAC,QAAQ,CAAC,cAAc;IAC/B,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAJR,WAAW,EAAE,eAAe,EAC5B,YAAY,EAAE,mBAAmB,EACjC,cAAc,EAAE,qBAAqB,EACrC,QAAQ,EAAE,mBAAmB,EAC7B,QAAQ,EAAE,kBAAkB;IAGzC,OAAO,CACX,cAAc,EAAE,MAAM,EACtB,aAAa,EAAE,MAAM,EACrB,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,YAAY,GACpB,OAAO,CAAC,WAAW,CAAC;IAqGvB,OAAO,CAAC,cAAc;CAevB"}
@@ -1,11 +1,10 @@
1
- var ProxyService_1;
2
1
  import { __decorate, __metadata } from "tslib";
3
- import { BadGatewayException, ForbiddenException, Injectable, Logger, NotFoundException, UnauthorizedException, } from '@nestjs/common';
4
- import { ConnectService } from '../connect/connect.service.js';
2
+ import { BadGatewayException, Injectable, NotFoundException, } from '@nestjs/common';
5
3
  import { ConfigurationsService } from '../configurations/configurations.service.js';
6
- import { CryptoService } from '../crypto/crypto.service.js';
4
+ import { CredentialsService } from '../credentials/credentials.service.js';
5
+ import { EnvironmentsService } from '../environments/environments.service.js';
7
6
  import { IntegrationRegistry } from '../integrations/integration.registry.js';
8
- import { PrismaService } from '../prisma/prisma.service.js';
7
+ import { ProxyRepository } from './proxy.repository.js';
9
8
  /**
10
9
  * Never forwarded upstream: hop-by-hop framing headers, and anything carrying
11
10
  * Kosha's own credentials. `cookie` matters most — forwarding it would leak
@@ -21,37 +20,51 @@ const STRIPPED_REQUEST_HEADERS = new Set([
21
20
  'cookie',
22
21
  'x-api-key',
23
22
  ]);
24
- let ProxyService = ProxyService_1 = class ProxyService {
25
- prisma;
26
- crypto;
27
- registry;
28
- connect;
23
+ let ProxyService = class ProxyService {
24
+ credentials;
25
+ environments;
29
26
  configurations;
30
- logger = new Logger(ProxyService_1.name);
31
- constructor(prisma, crypto, registry, connect, configurations) {
32
- this.prisma = prisma;
33
- this.crypto = crypto;
34
- this.registry = registry;
35
- this.connect = connect;
27
+ registry;
28
+ resolver;
29
+ constructor(credentials, environments, configurations, registry, resolver) {
30
+ this.credentials = credentials;
31
+ this.environments = environments;
36
32
  this.configurations = configurations;
33
+ this.registry = registry;
34
+ this.resolver = resolver;
37
35
  }
38
- async forward(organizationId, connectionId, request) {
39
- const connection = await this.prisma.connection.findFirst({
40
- where: { id: connectionId, organizationId },
41
- include: {
42
- configuration: { include: { provider: true } },
43
- credential: true,
44
- },
45
- });
46
- if (!connection) {
47
- throw new NotFoundException('Connection not found');
36
+ async forward(organizationId, environmentId, target, request) {
37
+ 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');
48
41
  }
49
- if (connection.status !== 'ACTIVE' || !connection.credential) {
50
- throw new ForbiddenException(`Connection is ${connection.status.toLowerCase()}`);
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.
49
+ if (!account)
50
+ 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?.({
54
+ credential: {
55
+ accessToken: credential.accessToken,
56
+ tokenType: credential.tokenType,
57
+ },
58
+ // What the provider said about this account when it was connected.
59
+ // A provider with no fixed host builds its base URL from this — every
60
+ // Jira path carries a cloudId — so passing nothing made those
61
+ // providers unreachable by construction.
62
+ metadata: asMetadata(account.connectionConfig),
63
+ }) ?? integration.api.baseUrl;
64
+ if (!baseUrl) {
65
+ throw new BadGatewayException('Provider has no resolvable base URL for this connection');
51
66
  }
52
- const integration = this.registry.get(connection.configuration.provider.key);
53
- const credential = await this.resolveCredential(connection);
54
- const targetUrl = this.buildTargetUrl(integration.api.baseUrl, request);
67
+ const targetUrl = this.buildTargetUrl(baseUrl, request);
55
68
  /*
56
69
  * Header names are compared lowercased, and the integration's headers are
57
70
  * applied last so they win. Without the normalisation a caller's `accept`
@@ -82,86 +95,12 @@ let ProxyService = ProxyService_1 = class ProxyService {
82
95
  ? JSON.stringify(request.body)
83
96
  : undefined,
84
97
  });
85
- await this.prisma.connection.update({
86
- where: { id: connection.id },
87
- data: { lastUsedAt: new Date() },
88
- });
89
98
  return {
90
99
  status: response.status,
91
100
  headers: response.headers,
92
101
  body: await response.text(),
93
102
  };
94
103
  }
95
- /**
96
- * Returns a usable access token, refreshing first when the stored one has
97
- * expired and the provider supports refresh.
98
- *
99
- * Without this an expired token is a dead end for the end user: they have to
100
- * walk the whole consent flow again for a token the provider was willing to
101
- * renew.
102
- */
103
- async resolveCredential(connection) {
104
- const credential = connection.credential;
105
- if (!credential) {
106
- throw new ForbiddenException('Connection has no stored credential');
107
- }
108
- const expired = credential.expiresAt !== null && credential.expiresAt <= new Date();
109
- if (!expired) {
110
- return {
111
- accessToken: this.crypto.decrypt(credential.accessToken),
112
- tokenType: credential.tokenType,
113
- };
114
- }
115
- const integration = this.registry.get(connection.configuration.provider.key);
116
- const canRefresh = typeof integration.auth.refresh === 'function' && credential.refreshToken;
117
- if (!canRefresh) {
118
- await this.markExpired(connection.id, 'Access token expired');
119
- throw new UnauthorizedException('Connection token expired; the end user must reconnect');
120
- }
121
- const configuration = await this.configurations.getWithDecryptedSecret(connection.configurationId);
122
- try {
123
- const tokens = await integration.auth.refresh?.({
124
- client: {
125
- clientId: configuration.clientId ?? '',
126
- clientSecret: configuration.clientSecret,
127
- redirectUri: configuration.redirectUri ??
128
- this.configurations.callbackUrlFor(configuration.provider.key),
129
- scopes: configuration.scopes,
130
- },
131
- refreshToken: this.crypto.decrypt(credential.refreshToken),
132
- });
133
- if (!tokens)
134
- throw new Error('Refresh returned no tokens');
135
- await this.connect.storeCredential(connection.id, tokens);
136
- await this.runTokenRefreshedHook(connection.id, integration, tokens);
137
- return {
138
- accessToken: tokens.accessToken,
139
- tokenType: tokens.tokenType ?? credential.tokenType,
140
- };
141
- }
142
- catch (error) {
143
- const message = error instanceof Error ? error.message : 'Refresh failed';
144
- await this.markExpired(connection.id, `Token refresh failed: ${message}`);
145
- throw new UnauthorizedException('Connection token expired and could not be refreshed; the end user must reconnect');
146
- }
147
- }
148
- async runTokenRefreshedHook(connectionId, integration, tokens) {
149
- const hook = integration.hooks?.onTokenRefreshed;
150
- if (!hook)
151
- return;
152
- try {
153
- await hook({ connectionId, tokens });
154
- }
155
- catch (error) {
156
- this.logger.error(`onTokenRefreshed hook failed for "${integration.key}": ${error instanceof Error ? error.message : error}`);
157
- }
158
- }
159
- async markExpired(connectionId, reason) {
160
- await this.prisma.connection.update({
161
- where: { id: connectionId },
162
- data: { status: 'EXPIRED', errorMessage: reason },
163
- });
164
- }
165
104
  buildTargetUrl(baseUrl, request) {
166
105
  const base = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`;
167
106
  const url = new URL(request.path.replace(/^\/+/, ''), base);
@@ -175,12 +114,24 @@ let ProxyService = ProxyService_1 = class ProxyService {
175
114
  return url;
176
115
  }
177
116
  };
178
- ProxyService = ProxyService_1 = __decorate([
117
+ ProxyService = __decorate([
179
118
  Injectable(),
180
- __metadata("design:paramtypes", [PrismaService,
181
- CryptoService,
119
+ __metadata("design:paramtypes", [ProxyRepository,
120
+ EnvironmentsService,
121
+ ConfigurationsService,
182
122
  IntegrationRegistry,
183
- ConnectService,
184
- ConfigurationsService])
123
+ CredentialsService])
185
124
  ], ProxyService);
186
125
  export { ProxyService };
126
+ /**
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.
130
+ */
131
+ function asMetadata(connectionConfig) {
132
+ return connectionConfig !== null &&
133
+ typeof connectionConfig === 'object' &&
134
+ !Array.isArray(connectionConfig)
135
+ ? connectionConfig
136
+ : undefined;
137
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kosha-ai/server",
3
- "version": "0.0.6",
3
+ "version": "1.0.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",
@@ -95,24 +95,29 @@
95
95
  "@nestjs/common": "^11.0.0",
96
96
  "@nestjs/core": "^11.0.0",
97
97
  "@nestjs/platform-express": "^11.0.0",
98
+ "@nestjs/swagger": "^11.4.7",
98
99
  "@prisma/adapter-pg": "^7.10.0",
99
100
  "@prisma/client": "^7.10.0",
100
101
  "@thallesp/nestjs-better-auth": "^2.8.0",
101
102
  "better-auth": "^1.7.3",
103
+ "class-transformer": "^0.5.1",
104
+ "class-validator": "^0.15.1",
102
105
  "express": "^5.2.1",
106
+ "node-forge": "^1.4.0",
103
107
  "pg": "^8.23.0",
104
108
  "reflect-metadata": "^0.2.0",
105
109
  "rxjs": "^7.8.0",
106
110
  "tslib": "^2.3.0",
107
111
  "zod": "4.5.4",
108
- "@kosha-ai/integration-sdk": "0.0.6"
112
+ "@kosha-ai/integration-sdk": "1.0.0"
109
113
  },
110
114
  "devDependencies": {
111
115
  "@nestjs/testing": "^11.0.0",
112
116
  "@types/express": "^5.0.6",
117
+ "@types/node-forge": "^1.3.14",
113
118
  "@types/pg": "^8.23.1",
114
119
  "prisma": "^7.10.0",
115
- "@kosha-ai/integration-github": "0.0.6",
116
- "@kosha-ai/integration-slack": "0.0.6"
120
+ "@kosha-ai/integration-github": "1.0.0",
121
+ "@kosha-ai/integration-slack": "1.0.0"
117
122
  }
118
123
  }