@kosha-ai/server 0.1.0 → 0.3.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 (196) 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 +43 -2
  7. package/dist/bootstrap.d.ts.map +1 -1
  8. package/dist/bootstrap.js +24 -6
  9. package/dist/configurations/configurations.controller.d.ts +12 -136
  10. package/dist/configurations/configurations.controller.d.ts.map +1 -1
  11. package/dist/configurations/configurations.controller.js +65 -28
  12. package/dist/configurations/configurations.module.d.ts.map +1 -1
  13. package/dist/configurations/configurations.module.js +2 -0
  14. package/dist/configurations/configurations.service.d.ts +32 -200
  15. package/dist/configurations/configurations.service.d.ts.map +1 -1
  16. package/dist/configurations/configurations.service.js +98 -170
  17. package/dist/configurations/dto/create-configuration.dto.d.ts +7 -0
  18. package/dist/configurations/dto/create-configuration.dto.d.ts.map +1 -0
  19. package/dist/configurations/dto/create-configuration.dto.js +44 -0
  20. package/dist/configurations/dto/update-configuration.dto.d.ts +5 -0
  21. package/dist/configurations/dto/update-configuration.dto.d.ts.map +1 -0
  22. package/dist/configurations/dto/update-configuration.dto.js +23 -0
  23. package/dist/connect/connect.controller.d.ts +6 -8
  24. package/dist/connect/connect.controller.d.ts.map +1 -1
  25. package/dist/connect/connect.controller.js +46 -15
  26. package/dist/connect/connect.module.d.ts.map +1 -1
  27. package/dist/connect/connect.module.js +4 -3
  28. package/dist/connect/connect.repository.d.ts +78 -0
  29. package/dist/connect/connect.repository.d.ts.map +1 -0
  30. package/dist/connect/connect.repository.js +134 -0
  31. package/dist/connect/connect.service.d.ts +29 -21
  32. package/dist/connect/connect.service.d.ts.map +1 -1
  33. package/dist/connect/connect.service.js +157 -168
  34. package/dist/connect/dto/create-connect-session.dto.d.ts +8 -0
  35. package/dist/connect/dto/create-connect-session.dto.d.ts.map +1 -0
  36. package/dist/connect/dto/create-connect-session.dto.js +54 -0
  37. package/dist/connect/dto/create-test-session.dto.d.ts +11 -0
  38. package/dist/connect/dto/create-test-session.dto.d.ts.map +1 -0
  39. package/dist/connect/dto/create-test-session.dto.js +41 -0
  40. package/dist/connections/connections.controller.d.ts +10 -62
  41. package/dist/connections/connections.controller.d.ts.map +1 -1
  42. package/dist/connections/connections.controller.js +41 -17
  43. package/dist/connections/connections.module.d.ts.map +1 -1
  44. package/dist/connections/connections.module.js +4 -1
  45. package/dist/connections/connections.repository.d.ts +31 -0
  46. package/dist/connections/connections.repository.d.ts.map +1 -0
  47. package/dist/connections/connections.repository.js +44 -0
  48. package/dist/connections/connections.service.d.ts +58 -68
  49. package/dist/connections/connections.service.d.ts.map +1 -1
  50. package/dist/connections/connections.service.js +137 -55
  51. package/dist/connections/dto/list-connections.dto.d.ts +7 -0
  52. package/dist/connections/dto/list-connections.dto.d.ts.map +1 -0
  53. package/dist/connections/dto/list-connections.dto.js +47 -0
  54. package/dist/connections/environment-connections.controller.d.ts +27 -0
  55. package/dist/connections/environment-connections.controller.d.ts.map +1 -0
  56. package/dist/connections/environment-connections.controller.js +83 -0
  57. package/dist/connections/environment-connections.module.d.ts +11 -0
  58. package/dist/connections/environment-connections.module.d.ts.map +1 -0
  59. package/dist/connections/environment-connections.module.js +21 -0
  60. package/dist/credentials/credentials.module.d.ts.map +1 -1
  61. package/dist/credentials/credentials.module.js +2 -4
  62. package/dist/credentials/credentials.repository.d.ts +11 -0
  63. package/dist/credentials/credentials.repository.d.ts.map +1 -0
  64. package/dist/credentials/credentials.repository.js +28 -0
  65. package/dist/credentials/credentials.service.d.ts +25 -31
  66. package/dist/credentials/credentials.service.d.ts.map +1 -1
  67. package/dist/credentials/credentials.service.js +100 -62
  68. package/dist/crypto/credential-envelope.d.ts +56 -0
  69. package/dist/crypto/credential-envelope.d.ts.map +1 -0
  70. package/dist/crypto/credential-envelope.js +70 -0
  71. package/dist/crypto/crypto.service.d.ts +8 -0
  72. package/dist/crypto/crypto.service.d.ts.map +1 -1
  73. package/dist/crypto/crypto.service.js +12 -0
  74. package/dist/egress/dto/mint-grant.dto.d.ts +7 -0
  75. package/dist/egress/dto/mint-grant.dto.d.ts.map +1 -0
  76. package/dist/egress/dto/mint-grant.dto.js +44 -0
  77. package/dist/egress/egress.controller.d.ts +2 -8
  78. package/dist/egress/egress.controller.d.ts.map +1 -1
  79. package/dist/egress/egress.controller.js +27 -6
  80. package/dist/egress/egress.listener.d.ts.map +1 -1
  81. package/dist/egress/egress.listener.js +6 -6
  82. package/dist/egress/egress.module.d.ts.map +1 -1
  83. package/dist/egress/egress.module.js +10 -2
  84. package/dist/egress/egress.repository.d.ts +65 -0
  85. package/dist/egress/egress.repository.d.ts.map +1 -0
  86. package/dist/egress/egress.repository.js +103 -0
  87. package/dist/egress/grants.service.d.ts +23 -78
  88. package/dist/egress/grants.service.d.ts.map +1 -1
  89. package/dist/egress/grants.service.js +44 -104
  90. package/dist/environments/dto/create-environment.dto.d.ts +6 -0
  91. package/dist/environments/dto/create-environment.dto.d.ts.map +1 -0
  92. package/dist/environments/dto/create-environment.dto.js +34 -0
  93. package/dist/environments/dto/update-environment.dto.d.ts +5 -0
  94. package/dist/environments/dto/update-environment.dto.d.ts.map +1 -0
  95. package/dist/environments/dto/update-environment.dto.js +24 -0
  96. package/dist/environments/environments.controller.d.ts +53 -0
  97. package/dist/environments/environments.controller.d.ts.map +1 -0
  98. package/dist/environments/environments.controller.js +101 -0
  99. package/dist/environments/environments.module.d.ts +3 -0
  100. package/dist/environments/environments.module.d.ts.map +1 -0
  101. package/dist/environments/environments.module.js +15 -0
  102. package/dist/environments/environments.repository.d.ts +37 -0
  103. package/dist/environments/environments.repository.d.ts.map +1 -0
  104. package/dist/environments/environments.repository.js +82 -0
  105. package/dist/environments/environments.service.d.ts +43 -0
  106. package/dist/environments/environments.service.d.ts.map +1 -0
  107. package/dist/environments/environments.service.js +124 -0
  108. package/dist/generated/prisma/browser.d.ts +11 -39
  109. package/dist/generated/prisma/browser.d.ts.map +1 -1
  110. package/dist/generated/prisma/client.d.ts +11 -39
  111. package/dist/generated/prisma/client.d.ts.map +1 -1
  112. package/dist/generated/prisma/commonInputTypes.d.ts +50 -146
  113. package/dist/generated/prisma/commonInputTypes.d.ts.map +1 -1
  114. package/dist/generated/prisma/enums.d.ts +1 -21
  115. package/dist/generated/prisma/enums.d.ts.map +1 -1
  116. package/dist/generated/prisma/enums.js +1 -18
  117. package/dist/generated/prisma/internal/class.d.ts +16 -60
  118. package/dist/generated/prisma/internal/class.d.ts.map +1 -1
  119. package/dist/generated/prisma/internal/class.js +4 -4
  120. package/dist/generated/prisma/internal/prismaNamespace.d.ts +166 -583
  121. package/dist/generated/prisma/internal/prismaNamespace.d.ts.map +1 -1
  122. package/dist/generated/prisma/internal/prismaNamespace.js +25 -104
  123. package/dist/generated/prisma/internal/prismaNamespaceBrowser.d.ts +26 -111
  124. package/dist/generated/prisma/internal/prismaNamespaceBrowser.d.ts.map +1 -1
  125. package/dist/generated/prisma/internal/prismaNamespaceBrowser.js +25 -104
  126. package/dist/generated/prisma/models/EgressGrant.d.ts +131 -134
  127. package/dist/generated/prisma/models/EgressGrant.d.ts.map +1 -1
  128. package/dist/generated/prisma/models/Environment.d.ts +1386 -0
  129. package/dist/generated/prisma/models/Environment.d.ts.map +1 -0
  130. package/dist/generated/prisma/models/Invitation.d.ts +42 -1
  131. package/dist/generated/prisma/models/Invitation.d.ts.map +1 -1
  132. package/dist/generated/prisma/models/Organization.d.ts +122 -408
  133. package/dist/generated/prisma/models/Organization.d.ts.map +1 -1
  134. package/dist/generated/prisma/models/Session.d.ts +36 -1
  135. package/dist/generated/prisma/models/Session.d.ts.map +1 -1
  136. package/dist/generated/prisma/models/Team.d.ts +1498 -0
  137. package/dist/generated/prisma/models/Team.d.ts.map +1 -0
  138. package/dist/generated/prisma/models/TeamMember.d.ts +1281 -0
  139. package/dist/generated/prisma/models/TeamMember.d.ts.map +1 -0
  140. package/dist/generated/prisma/models/User.d.ts +278 -0
  141. package/dist/generated/prisma/models/User.d.ts.map +1 -1
  142. package/dist/generated/prisma/models.d.ts +3 -7
  143. package/dist/generated/prisma/models.d.ts.map +1 -1
  144. package/dist/index.d.ts +14 -5
  145. package/dist/index.d.ts.map +1 -1
  146. package/dist/index.js +11 -2
  147. package/dist/integrations/integrations.controller.d.ts +4 -1
  148. package/dist/integrations/integrations.controller.d.ts.map +1 -1
  149. package/dist/integrations/integrations.controller.js +1 -0
  150. package/dist/integrations/integrations.module.js +3 -3
  151. package/dist/integrations/provider-store.d.ts +104 -0
  152. package/dist/integrations/provider-store.d.ts.map +1 -0
  153. package/dist/integrations/provider-store.js +234 -0
  154. package/dist/kosha.module.d.ts.map +1 -1
  155. package/dist/kosha.module.js +9 -3
  156. package/dist/proxy/proxy.controller.d.ts +12 -3
  157. package/dist/proxy/proxy.controller.d.ts.map +1 -1
  158. package/dist/proxy/proxy.controller.js +31 -10
  159. package/dist/proxy/proxy.module.d.ts.map +1 -1
  160. package/dist/proxy/proxy.module.js +4 -2
  161. package/dist/proxy/proxy.repository.d.ts +21 -0
  162. package/dist/proxy/proxy.repository.d.ts.map +1 -0
  163. package/dist/proxy/proxy.repository.js +37 -0
  164. package/dist/proxy/proxy.service.d.ts +15 -5
  165. package/dist/proxy/proxy.service.d.ts.map +1 -1
  166. package/dist/proxy/proxy.service.js +63 -26
  167. package/package.json +7 -4
  168. package/prisma/migrations/{20260908000000_init → 20261008090000_init}/migration.sql +99 -16
  169. package/prisma/schema.prisma +56 -202
  170. package/dist/generated/prisma/models/Configuration.d.ts +0 -2018
  171. package/dist/generated/prisma/models/Configuration.d.ts.map +0 -1
  172. package/dist/generated/prisma/models/ConnectSession.d.ts +0 -1606
  173. package/dist/generated/prisma/models/ConnectSession.d.ts.map +0 -1
  174. package/dist/generated/prisma/models/Connection.d.ts +0 -1949
  175. package/dist/generated/prisma/models/Connection.d.ts.map +0 -1
  176. package/dist/generated/prisma/models/ConnectionCredential.d.ts +0 -1293
  177. package/dist/generated/prisma/models/ConnectionCredential.d.ts.map +0 -1
  178. package/dist/generated/prisma/models/ConnectionCredential.js +0 -1
  179. package/dist/generated/prisma/models/EndUser.d.ts +0 -1666
  180. package/dist/generated/prisma/models/EndUser.d.ts.map +0 -1
  181. package/dist/generated/prisma/models/EndUser.js +0 -1
  182. package/dist/generated/prisma/models/OAuthState.d.ts +0 -1404
  183. package/dist/generated/prisma/models/OAuthState.d.ts.map +0 -1
  184. package/dist/generated/prisma/models/OAuthState.js +0 -1
  185. package/dist/generated/prisma/models/Provider.d.ts +0 -1462
  186. package/dist/generated/prisma/models/Provider.d.ts.map +0 -1
  187. package/dist/generated/prisma/models/Provider.js +0 -1
  188. package/dist/integrations/provider-sync.service.d.ts +0 -24
  189. package/dist/integrations/provider-sync.service.d.ts.map +0 -1
  190. package/dist/integrations/provider-sync.service.js +0 -74
  191. package/prisma/migrations/20260908161813_add_connect_domain/migration.sql +0 -173
  192. package/prisma/migrations/20260916000000_add_configuration_slug/migration.sql +0 -15
  193. package/prisma/migrations/20260921000000_add_egress_grant/migration.sql +0 -31
  194. /package/dist/generated/prisma/models/{Configuration.js → Environment.js} +0 -0
  195. /package/dist/generated/prisma/models/{ConnectSession.js → Team.js} +0 -0
  196. /package/dist/generated/prisma/models/{Connection.js → TeamMember.js} +0 -0
@@ -1,216 +1,48 @@
1
- import type { ResolvedKoshaConfig } from '../config/kosha-config.js';
2
1
  import { CryptoService } from '../crypto/crypto.service.js';
3
- import type { Prisma } from '../generated/prisma/client.js';
2
+ import { EnvironmentsService } from '../environments/environments.service.js';
4
3
  import { IntegrationRegistry } from '../integrations/integration.registry.js';
5
- import { PrismaService } from '../prisma/prisma.service.js';
6
- export interface CreateConfigurationInput {
7
- /** Provider key, e.g. "github". */
4
+ import { ProviderStore, type ProviderConfiguration, type PublicProviderConfiguration } from '../integrations/provider-store.js';
5
+ import type { CreateConfigurationDto } from './dto/create-configuration.dto.js';
6
+ import type { UpdateConfigurationDto } from './dto/update-configuration.dto.js';
7
+ /** A configuration with the provider it belongs to named, as a client sees it. */
8
+ export type DescribedConfiguration = PublicProviderConfiguration & {
8
9
  provider: string;
9
- name: string;
10
- slug?: string;
11
- clientId: string;
12
- clientSecret: string;
13
- scopes?: string[];
14
- environment?: 'SANDBOX' | 'LIVE';
15
- settings?: Record<string, unknown>;
16
- }
17
- export interface UpdateConfigurationInput {
18
- name?: string;
19
- scopes?: string[];
20
- clientId?: string;
21
- clientSecret?: string;
22
- isEnabled?: boolean;
23
- settings?: Record<string, unknown>;
10
+ authType: string;
11
+ };
12
+ /** A configuration with its secrets opened. Never leaves the server. */
13
+ export interface ConfigurationWithCredentials {
14
+ configuration: ProviderConfiguration;
15
+ credentials: Record<string, unknown>;
24
16
  }
25
17
  export declare class ConfigurationsService {
26
- private readonly prisma;
18
+ private readonly store;
19
+ private readonly environments;
27
20
  private readonly crypto;
28
21
  private readonly registry;
29
- private readonly config;
30
- constructor(prisma: PrismaService, crypto: CryptoService, registry: IntegrationRegistry, config: ResolvedKoshaConfig);
22
+ constructor(store: ProviderStore, environments: EnvironmentsService, crypto: CryptoService, registry: IntegrationRegistry);
31
23
  /**
32
- * The redirect URI the provider must be told about. Derived from
33
- * `server.publicUrl` so it is stable and inspectable in the admin, rather
34
- * than being reconstructed per request.
24
+ * Every configuration in an environment, across every installed provider.
25
+ *
26
+ * Kosha owns no table that lists them, so this asks each provider in turn.
27
+ * An environment holds a handful, and each read is an index lookup.
35
28
  */
36
- callbackUrlFor(providerKey: string): string;
37
- list(organizationId: string): Prisma.PrismaPromise<({
38
- provider: {
39
- name: string;
40
- key: string;
41
- id: string;
42
- createdAt: Date;
43
- updatedAt: Date;
44
- authType: import("../generated/prisma/enums.js").AuthType;
45
- authorizationUrl: string | null;
46
- tokenUrl: string | null;
47
- revokeUrl: string | null;
48
- apiBaseUrl: string | null;
49
- scopesAvailable: string[];
50
- docsUrl: string | null;
51
- logoUrl: string | null;
52
- isActive: boolean;
53
- };
54
- } & {
55
- name: string;
56
- id: string;
57
- createdAt: Date;
58
- updatedAt: Date;
59
- metadata: import("@prisma/client/runtime/client").JsonValue | null;
60
- scopes: string[];
61
- slug: string;
62
- organizationId: string;
63
- providerId: string;
64
- environment: import("../generated/prisma/enums.js").ConnectionEnvironment;
65
- authType: import("../generated/prisma/enums.js").AuthType;
66
- clientId: string | null;
67
- redirectUri: string | null;
68
- isEnabled: boolean;
69
- })[]>;
70
- get(organizationId: string, id: string): Promise<{
71
- provider: {
72
- name: string;
73
- key: string;
74
- id: string;
75
- createdAt: Date;
76
- updatedAt: Date;
77
- authType: import("../generated/prisma/enums.js").AuthType;
78
- authorizationUrl: string | null;
79
- tokenUrl: string | null;
80
- revokeUrl: string | null;
81
- apiBaseUrl: string | null;
82
- scopesAvailable: string[];
83
- docsUrl: string | null;
84
- logoUrl: string | null;
85
- isActive: boolean;
86
- };
87
- } & {
88
- name: string;
89
- id: string;
90
- createdAt: Date;
91
- updatedAt: Date;
92
- metadata: import("@prisma/client/runtime/client").JsonValue | null;
93
- scopes: string[];
94
- slug: string;
95
- organizationId: string;
96
- providerId: string;
97
- environment: import("../generated/prisma/enums.js").ConnectionEnvironment;
98
- authType: import("../generated/prisma/enums.js").AuthType;
99
- clientId: string | null;
100
- redirectUri: string | null;
101
- isEnabled: boolean;
102
- }>;
103
- create(organizationId: string, input: CreateConfigurationInput): Promise<{
104
- provider: {
105
- name: string;
106
- key: string;
107
- id: string;
108
- createdAt: Date;
109
- updatedAt: Date;
110
- authType: import("../generated/prisma/enums.js").AuthType;
111
- authorizationUrl: string | null;
112
- tokenUrl: string | null;
113
- revokeUrl: string | null;
114
- apiBaseUrl: string | null;
115
- scopesAvailable: string[];
116
- docsUrl: string | null;
117
- logoUrl: string | null;
118
- isActive: boolean;
119
- };
120
- } & {
121
- name: string;
122
- id: string;
123
- createdAt: Date;
124
- updatedAt: Date;
125
- metadata: import("@prisma/client/runtime/client").JsonValue | null;
126
- scopes: string[];
127
- slug: string;
128
- organizationId: string;
129
- providerId: string;
130
- environment: import("../generated/prisma/enums.js").ConnectionEnvironment;
131
- authType: import("../generated/prisma/enums.js").AuthType;
132
- clientId: string | null;
133
- redirectUri: string | null;
134
- isEnabled: boolean;
135
- }>;
136
- update(organizationId: string, id: string, input: UpdateConfigurationInput): Promise<{
137
- provider: {
138
- name: string;
139
- key: string;
140
- id: string;
141
- createdAt: Date;
142
- updatedAt: Date;
143
- authType: import("../generated/prisma/enums.js").AuthType;
144
- authorizationUrl: string | null;
145
- tokenUrl: string | null;
146
- revokeUrl: string | null;
147
- apiBaseUrl: string | null;
148
- scopesAvailable: string[];
149
- docsUrl: string | null;
150
- logoUrl: string | null;
151
- isActive: boolean;
152
- };
153
- } & {
154
- name: string;
155
- id: string;
156
- createdAt: Date;
157
- updatedAt: Date;
158
- metadata: import("@prisma/client/runtime/client").JsonValue | null;
159
- scopes: string[];
160
- slug: string;
161
- organizationId: string;
162
- providerId: string;
163
- environment: import("../generated/prisma/enums.js").ConnectionEnvironment;
164
- authType: import("../generated/prisma/enums.js").AuthType;
165
- clientId: string | null;
166
- redirectUri: string | null;
167
- isEnabled: boolean;
168
- }>;
169
- remove(organizationId: string, id: string): Promise<void>;
29
+ list(organizationId: string, userId: string, environmentId: string): Promise<DescribedConfiguration[]>;
30
+ get(organizationId: string, userId: string, environmentId: string, provider: string, id: string): Promise<DescribedConfiguration>;
31
+ create(organizationId: string, userId: string, environmentId: string, input: CreateConfigurationDto): Promise<DescribedConfiguration>;
32
+ update(organizationId: string, userId: string, environmentId: string, provider: string, id: string, input: UpdateConfigurationDto): Promise<DescribedConfiguration>;
33
+ /** Refused while connections still point at this row. */
34
+ remove(organizationId: string, userId: string, environmentId: string, provider: string, id: string): Promise<void>;
170
35
  /**
171
- * Includes the decrypted client secret, for the connect flow only. Never
36
+ * Opens the secret half, for the connect and proxy paths only. Never
172
37
  * reachable from a controller.
173
38
  */
174
- getWithDecryptedSecret(configurationId: string): Promise<{
175
- clientSecret: string;
176
- provider: {
177
- name: string;
178
- key: string;
179
- id: string;
180
- createdAt: Date;
181
- updatedAt: Date;
182
- authType: import("../generated/prisma/enums.js").AuthType;
183
- authorizationUrl: string | null;
184
- tokenUrl: string | null;
185
- revokeUrl: string | null;
186
- apiBaseUrl: string | null;
187
- scopesAvailable: string[];
188
- docsUrl: string | null;
189
- logoUrl: string | null;
190
- isActive: boolean;
191
- };
192
- name: string;
193
- id: string;
194
- createdAt: Date;
195
- updatedAt: Date;
196
- metadata: import("@prisma/client/runtime/client").JsonValue | null;
197
- scopes: string[];
198
- slug: string;
199
- organizationId: string;
200
- providerId: string;
201
- environment: import("../generated/prisma/enums.js").ConnectionEnvironment;
202
- authType: import("../generated/prisma/enums.js").AuthType;
203
- clientId: string | null;
204
- encryptedClientSecret: string | null;
205
- redirectUri: string | null;
206
- isEnabled: boolean;
207
- }>;
208
- private validateScopes;
39
+ getWithCredentials(provider: string, configurationId: string): Promise<ConfigurationWithCredentials>;
209
40
  /**
210
- * Checks per-organization settings against the integration's declared
211
- * `configFields`. Integrations describe their settings as data so the admin
212
- * can render a form; the same description is what validates here.
41
+ * Seals the whole credential set as one blob, so the shape is the auth
42
+ * type's business rather than the schema's: an OAuth2 provider stores a
43
+ * client id and secret, and a provider whose end users bring their own key
44
+ * stores nothing at all.
213
45
  */
214
- private validateSettings;
46
+ private sealCredentials;
215
47
  }
216
48
  //# sourceMappingURL=configurations.service.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"configurations.service.d.ts","sourceRoot":"","sources":["../../src/configurations/configurations.service.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAErE,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,+BAA+B,CAAC;AAC5D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;;;;;;;;;;;;;;;;;;;;;;;IAYtC,MAAM,CAAC,cAAc,EAAE,MAAM,EAAE,KAAK,EAAE,wBAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAiD9D,MAAM,CACV,cAAc,EAAE,MAAM,EACtB,EAAE,EAAE,MAAM,EACV,KAAK,EAAE,wBAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IA2B3B,MAAM,CAAC,cAAc,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM;IAK/C;;;OAGG;IACG,sBAAsB,CAAC,eAAe,EAAE,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAoBpD,OAAO,CAAC,cAAc;IAetB;;;;OAIG;IACH,OAAO,CAAC,gBAAgB;CAiCzB"}
1
+ {"version":3,"file":"configurations.service.d.ts","sourceRoot":"","sources":["../../src/configurations/configurations.service.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,yCAAyC,CAAC;AAC9E,OAAO,EAAE,mBAAmB,EAAE,MAAM,yCAAyC,CAAC;AAC9E,OAAO,EACL,aAAa,EACb,KAAK,qBAAqB,EAC1B,KAAK,2BAA2B,EACjC,MAAM,mCAAmC,CAAC;AAC3C,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mCAAmC,CAAC;AAChF,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mCAAmC,CAAC;AAEhF,kFAAkF;AAClF,MAAM,MAAM,sBAAsB,GAAG,2BAA2B,GAAG;IACjE,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,wEAAwE;AACxE,MAAM,WAAW,4BAA4B;IAC3C,aAAa,EAAE,qBAAqB,CAAC;IACrC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACtC;AAED,qBACa,qBAA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"}
@@ -1,216 +1,144 @@
1
- import { __decorate, __metadata, __param } from "tslib";
2
- import { BadRequestException, Inject, Injectable, NotFoundException, } from '@nestjs/common';
3
- import { KOSHA_CONFIG } from '../config/tokens.js';
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { BadRequestException, ConflictException, Injectable, NotFoundException, } from '@nestjs/common';
4
3
  import { CryptoService } from '../crypto/crypto.service.js';
4
+ import { EnvironmentsService } from '../environments/environments.service.js';
5
5
  import { IntegrationRegistry } from '../integrations/integration.registry.js';
6
- import { PrismaService } from '../prisma/prisma.service.js';
7
- /** Never selected over the REST API. */
8
- const PUBLIC_OMIT = { encryptedClientSecret: true };
6
+ import { ProviderStore, } from '../integrations/provider-store.js';
9
7
  let ConfigurationsService = class ConfigurationsService {
10
- prisma;
8
+ store;
9
+ environments;
11
10
  crypto;
12
11
  registry;
13
- config;
14
- constructor(prisma, crypto, registry, config) {
15
- this.prisma = prisma;
12
+ constructor(store, environments, crypto, registry) {
13
+ this.store = store;
14
+ this.environments = environments;
16
15
  this.crypto = crypto;
17
16
  this.registry = registry;
18
- this.config = config;
19
17
  }
20
18
  /**
21
- * The redirect URI the provider must be told about. Derived from
22
- * `server.publicUrl` so it is stable and inspectable in the admin, rather
23
- * than being reconstructed per request.
19
+ * Every configuration in an environment, across every installed provider.
20
+ *
21
+ * Kosha owns no table that lists them, so this asks each provider in turn.
22
+ * An environment holds a handful, and each read is an index lookup.
24
23
  */
25
- callbackUrlFor(providerKey) {
26
- const { publicUrl, globalPrefix } = this.config.server;
27
- return `${publicUrl}/${globalPrefix}/connect/callback/${providerKey}`;
24
+ async list(organizationId, userId, environmentId) {
25
+ await this.environments.getForMember(organizationId, userId, environmentId);
26
+ const perProvider = await Promise.all(this.registry.list().map(async (integration) => {
27
+ const rows = await this.store.listConfigurations(integration, environmentId);
28
+ return rows.map((row) => describe(row, integration));
29
+ }));
30
+ return perProvider
31
+ .flat()
32
+ .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
28
33
  }
29
- list(organizationId) {
30
- return this.prisma.configuration.findMany({
31
- where: { organizationId },
32
- include: { provider: true },
33
- omit: PUBLIC_OMIT,
34
- orderBy: { createdAt: 'desc' },
35
- });
36
- }
37
- async get(organizationId, id) {
38
- const configuration = await this.prisma.configuration.findFirst({
39
- where: { id, organizationId },
40
- include: { provider: true },
41
- omit: PUBLIC_OMIT,
42
- });
43
- if (!configuration) {
34
+ async get(organizationId, userId, environmentId, provider, id) {
35
+ await this.environments.getForMember(organizationId, userId, environmentId);
36
+ const integration = this.registry.get(provider);
37
+ const configuration = await this.store.findConfigurationById(integration, environmentId, id);
38
+ if (!configuration)
44
39
  throw new NotFoundException('Configuration not found');
45
- }
46
- return configuration;
40
+ return describe(configuration, integration);
47
41
  }
48
- async create(organizationId, input) {
49
- if (!input?.name?.trim()) {
50
- throw new BadRequestException('name is required');
51
- }
52
- if (!input.clientId?.trim() || !input.clientSecret?.trim()) {
53
- throw new BadRequestException('clientId and clientSecret are required');
54
- }
55
- // The registry is the authority on what Kosha can talk to; a provider row
56
- // may linger after its package was uninstalled.
42
+ async create(organizationId, userId, environmentId, input) {
43
+ await this.environments.getForMember(organizationId, userId, environmentId);
44
+ // The registry is the authority on what Kosha can talk to. There is no
45
+ // provider table to consult.
57
46
  const integration = this.registry.get(input.provider);
58
- const provider = await this.prisma.provider.findUnique({
59
- where: { key: integration.key },
60
- });
61
- if (!provider) {
62
- throw new BadRequestException(`Provider "${input.provider}" has not been synced yet. Restart the server.`);
63
- }
64
- const scopes = this.validateScopes(input.provider, input.scopes ?? []);
65
- const settings = this.validateSettings(input.provider, input.settings);
66
- const slug = slugify(input.slug ?? input.name);
47
+ const name = input.name.trim();
48
+ const slug = slugify(input.slug ?? name);
67
49
  if (!slug) {
68
50
  throw new BadRequestException('slug must contain at least one letter or digit');
69
51
  }
70
- return this.prisma.configuration.create({
71
- data: {
72
- organizationId,
73
- providerId: provider.id,
74
- name: input.name.trim(),
75
- slug,
76
- authType: provider.authType,
77
- environment: input.environment ?? 'LIVE',
78
- clientId: input.clientId,
79
- encryptedClientSecret: this.crypto.encrypt(input.clientSecret),
80
- redirectUri: this.callbackUrlFor(provider.key),
81
- scopes,
82
- metadata: toJsonInput(settings),
83
- },
84
- include: { provider: true },
85
- omit: PUBLIC_OMIT,
52
+ // Read first: the unique index is per provider, so this is the only place
53
+ // a clash can be reported against the slug the caller actually sent.
54
+ const clash = await this.store.findConfigurationBySlug(integration, environmentId, slug);
55
+ if (clash) {
56
+ throw new ConflictException(`A ${integration.key} configuration with slug "${slug}" already exists in this environment`);
57
+ }
58
+ const created = await this.store.createConfiguration(integration, {
59
+ environmentId,
60
+ name,
61
+ slug,
62
+ credentials: this.sealCredentials(integration, input.credentials),
86
63
  });
64
+ return describe(created, integration);
87
65
  }
88
- async update(organizationId, id, input) {
89
- const existing = await this.get(organizationId, id);
90
- return this.prisma.configuration.update({
91
- where: { id },
92
- data: {
93
- name: input.name?.trim(),
94
- scopes: input.scopes
95
- ? this.validateScopes(existing.provider.key, input.scopes)
96
- : undefined,
97
- clientId: input.clientId,
98
- encryptedClientSecret: input.clientSecret
99
- ? this.crypto.encrypt(input.clientSecret)
100
- : undefined,
101
- isEnabled: input.isEnabled,
102
- metadata: input.settings
103
- ? toJsonInput(this.validateSettings(existing.provider.key, input.settings))
104
- : undefined,
105
- },
106
- include: { provider: true },
107
- omit: PUBLIC_OMIT,
66
+ async update(organizationId, userId, environmentId, provider, id, input) {
67
+ await this.get(organizationId, userId, environmentId, provider, id);
68
+ const integration = this.registry.get(provider);
69
+ const updated = await this.store.updateConfiguration(integration, id, {
70
+ name: input.name?.trim(),
71
+ credentials: input.credentials
72
+ ? this.sealCredentials(integration, input.credentials)
73
+ : undefined,
108
74
  });
75
+ return describe(updated, integration);
109
76
  }
110
- async remove(organizationId, id) {
111
- await this.get(organizationId, id);
112
- await this.prisma.configuration.delete({ where: { id } });
77
+ /** Refused while connections still point at this row. */
78
+ async remove(organizationId, userId, environmentId, provider, id) {
79
+ await this.get(organizationId, userId, environmentId, provider, id);
80
+ await this.store.deleteConfiguration(this.registry.get(provider), id);
113
81
  }
114
82
  /**
115
- * Includes the decrypted client secret, for the connect flow only. Never
83
+ * Opens the secret half, for the connect and proxy paths only. Never
116
84
  * reachable from a controller.
117
85
  */
118
- async getWithDecryptedSecret(configurationId) {
119
- const configuration = await this.prisma.configuration.findUnique({
120
- where: { id: configurationId },
121
- include: { provider: true },
122
- });
123
- if (!configuration) {
86
+ async getWithCredentials(provider, configurationId) {
87
+ const integration = this.registry.get(provider);
88
+ const configuration = await this.store.findConfigurationWithCredentials(integration, configurationId);
89
+ if (!configuration)
124
90
  throw new NotFoundException('Configuration not found');
125
- }
126
- if (!configuration.encryptedClientSecret) {
127
- throw new BadRequestException(`Configuration "${configuration.name}" has no client secret stored`);
128
- }
129
91
  return {
130
- ...configuration,
131
- clientSecret: this.crypto.decrypt(configuration.encryptedClientSecret),
92
+ configuration,
93
+ credentials: configuration.credentials
94
+ ? JSON.parse(this.crypto.decrypt(configuration.credentials))
95
+ : {},
132
96
  };
133
97
  }
134
- validateScopes(providerKey, scopes) {
135
- const available = new Set(this.registry.get(providerKey).auth.scopesAvailable);
136
- const unknown = scopes.filter((scope) => !available.has(scope));
137
- if (unknown.length > 0) {
138
- throw new BadRequestException(`Unknown scope(s) for ${providerKey}: ${unknown.join(', ')}. Available: ${[...available].join(', ')}`);
139
- }
140
- return [...new Set(scopes)];
141
- }
142
98
  /**
143
- * Checks per-organization settings against the integration's declared
144
- * `configFields`. Integrations describe their settings as data so the admin
145
- * can render a form; the same description is what validates here.
99
+ * Seals the whole credential set as one blob, so the shape is the auth
100
+ * type's business rather than the schema's: an OAuth2 provider stores a
101
+ * client id and secret, and a provider whose end users bring their own key
102
+ * stores nothing at all.
146
103
  */
147
- validateSettings(providerKey, settings) {
148
- const fields = this.registry.get(providerKey).configFields ?? [];
149
- if (!settings && fields.length === 0)
150
- return undefined;
151
- const provided = settings ?? {};
152
- const problems = [];
153
- const known = new Map(fields.map((field) => [field.key, field]));
154
- for (const key of Object.keys(provided)) {
155
- if (!known.has(key))
156
- problems.push(`unknown setting "${key}"`);
157
- }
158
- for (const field of fields) {
159
- const value = provided[field.key];
160
- if (value === undefined || value === null || value === '') {
161
- if (field.required)
162
- problems.push(`"${field.key}" is required`);
163
- continue;
104
+ sealCredentials(integration, credentials) {
105
+ const provided = credentials ?? {};
106
+ if (integration.auth.type === 'OAUTH2') {
107
+ const missing = ['clientId', 'clientSecret'].filter((key) => !provided[key]);
108
+ if (missing.length > 0) {
109
+ throw new BadRequestException(`${integration.key} is an OAuth2 integration and needs ${missing.join(' and ')}`);
164
110
  }
165
- problems.push(...describeTypeMismatch(field, value));
166
- }
167
- if (problems.length > 0) {
168
- throw new BadRequestException(`Invalid settings for ${providerKey}: ${problems.join('; ')}`);
169
111
  }
170
- return provided;
112
+ return Object.keys(provided).length > 0
113
+ ? this.crypto.encrypt(JSON.stringify(provided))
114
+ : null;
171
115
  }
172
116
  };
173
117
  ConfigurationsService = __decorate([
174
118
  Injectable(),
175
- __param(3, Inject(KOSHA_CONFIG)),
176
- __metadata("design:paramtypes", [PrismaService,
119
+ __metadata("design:paramtypes", [ProviderStore,
120
+ EnvironmentsService,
177
121
  CryptoService,
178
- IntegrationRegistry, Object])
122
+ IntegrationRegistry])
179
123
  ], ConfigurationsService);
180
124
  export { ConfigurationsService };
181
- /** Lowercase, digits and single dashes. Empty if the input has no word chars. */
125
+ /**
126
+ * The provider and its auth type come from the registry rather than from the
127
+ * row: both are facts about the installed package, and a copy in the database
128
+ * would be one more thing to keep in step.
129
+ */
130
+ function describe(configuration, integration) {
131
+ return {
132
+ ...configuration,
133
+ provider: integration.key,
134
+ authType: integration.auth.type,
135
+ };
136
+ }
137
+ /** The handle a caller quotes in a proxy URL, so it has to be URL-safe. */
182
138
  function slugify(value) {
183
139
  return value
184
140
  .toLowerCase()
141
+ .trim()
185
142
  .replace(/[^a-z0-9]+/g, '-')
186
143
  .replace(/^-+|-+$/g, '');
187
144
  }
188
- function describeTypeMismatch(field, value) {
189
- switch (field.type) {
190
- case 'number':
191
- return typeof value === 'number' && Number.isFinite(value)
192
- ? []
193
- : [`"${field.key}" must be a number`];
194
- case 'boolean':
195
- return typeof value === 'boolean'
196
- ? []
197
- : [`"${field.key}" must be a boolean`];
198
- case 'select': {
199
- const allowed = (field.options ?? []).map((option) => option.value);
200
- return allowed.includes(String(value))
201
- ? []
202
- : [`"${field.key}" must be one of: ${allowed.join(', ')}`];
203
- }
204
- default:
205
- return typeof value === 'string'
206
- ? []
207
- : [`"${field.key}" must be a string`];
208
- }
209
- }
210
- /**
211
- * Prisma's Json input type does not accept a plain index signature, so the
212
- * validated settings object is narrowed at the persistence boundary only.
213
- */
214
- function toJsonInput(value) {
215
- return value;
216
- }
@@ -0,0 +1,7 @@
1
+ export declare class CreateConfigurationDto {
2
+ provider: string;
3
+ name: string;
4
+ slug?: string;
5
+ credentials?: Record<string, unknown>;
6
+ }
7
+ //# sourceMappingURL=create-configuration.dto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-configuration.dto.d.ts","sourceRoot":"","sources":["../../../src/configurations/dto/create-configuration.dto.ts"],"names":[],"mappings":"AASA,qBAAa,sBAAsB;IAQjC,QAAQ,EAAG,MAAM,CAAC;IAMlB,IAAI,EAAG,MAAM,CAAC;IAUd,IAAI,CAAC,EAAE,MAAM,CAAC;IASd,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACvC"}
@@ -0,0 +1,44 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
3
+ import { IsObject, IsOptional, IsString, MaxLength, MinLength, } from 'class-validator';
4
+ export class CreateConfigurationDto {
5
+ provider;
6
+ name;
7
+ slug;
8
+ credentials;
9
+ }
10
+ __decorate([
11
+ ApiProperty({
12
+ example: 'github',
13
+ description: 'Key of an installed integration package. Validated against the registry.',
14
+ }),
15
+ IsString(),
16
+ MinLength(1),
17
+ __metadata("design:type", String)
18
+ ], CreateConfigurationDto.prototype, "provider", void 0);
19
+ __decorate([
20
+ ApiProperty({ example: 'GitHub' }),
21
+ IsString(),
22
+ MinLength(1),
23
+ MaxLength(64),
24
+ __metadata("design:type", String)
25
+ ], CreateConfigurationDto.prototype, "name", void 0);
26
+ __decorate([
27
+ ApiPropertyOptional({
28
+ example: 'github',
29
+ description: 'Caller-facing handle, quoted on proxy calls. Derived from the name when omitted.',
30
+ }),
31
+ IsOptional(),
32
+ IsString(),
33
+ MaxLength(64),
34
+ __metadata("design:type", String)
35
+ ], CreateConfigurationDto.prototype, "slug", void 0);
36
+ __decorate([
37
+ ApiPropertyOptional({
38
+ description: 'Secret half, encrypted at rest. Shape is the auth type’s: an OAuth2 integration needs clientId and clientSecret.',
39
+ example: { clientId: 'Iv1.abc', clientSecret: '…' },
40
+ }),
41
+ IsOptional(),
42
+ IsObject(),
43
+ __metadata("design:type", Object)
44
+ ], CreateConfigurationDto.prototype, "credentials", void 0);
@@ -0,0 +1,5 @@
1
+ export declare class UpdateConfigurationDto {
2
+ name?: string;
3
+ credentials?: Record<string, unknown>;
4
+ }
5
+ //# sourceMappingURL=update-configuration.dto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"update-configuration.dto.d.ts","sourceRoot":"","sources":["../../../src/configurations/dto/update-configuration.dto.ts"],"names":[],"mappings":"AASA,qBAAa,sBAAsB;IAMjC,IAAI,CAAC,EAAE,MAAM,CAAC;IAOd,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACvC"}