@kosha-ai/server 0.3.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 (45) hide show
  1. package/dist/configurations/configurations.service.d.ts +6 -0
  2. package/dist/configurations/configurations.service.d.ts.map +1 -1
  3. package/dist/configurations/configurations.service.js +58 -1
  4. package/dist/configurations/dto/create-configuration.dto.d.ts +1 -0
  5. package/dist/configurations/dto/create-configuration.dto.d.ts.map +1 -1
  6. package/dist/configurations/dto/create-configuration.dto.js +10 -0
  7. package/dist/configurations/dto/update-configuration.dto.d.ts +1 -0
  8. package/dist/configurations/dto/update-configuration.dto.d.ts.map +1 -1
  9. package/dist/configurations/dto/update-configuration.dto.js +10 -0
  10. package/dist/connect/connect.controller.d.ts +11 -1
  11. package/dist/connect/connect.controller.d.ts.map +1 -1
  12. package/dist/connect/connect.controller.js +102 -13
  13. package/dist/connect/connect.repository.d.ts +11 -2
  14. package/dist/connect/connect.repository.d.ts.map +1 -1
  15. package/dist/connect/connect.repository.js +8 -3
  16. package/dist/connect/connect.service.d.ts +47 -0
  17. package/dist/connect/connect.service.d.ts.map +1 -1
  18. package/dist/connect/connect.service.js +269 -28
  19. package/dist/connect/dto/connect-credential.dto.d.ts +8 -0
  20. package/dist/connect/dto/connect-credential.dto.d.ts.map +1 -0
  21. package/dist/connect/dto/connect-credential.dto.js +54 -0
  22. package/dist/connect/dto/create-test-session.dto.d.ts +7 -0
  23. package/dist/connect/dto/create-test-session.dto.d.ts.map +1 -1
  24. package/dist/connect/dto/create-test-session.dto.js +36 -1
  25. package/dist/credentials/credentials.service.d.ts +9 -3
  26. package/dist/credentials/credentials.service.d.ts.map +1 -1
  27. package/dist/credentials/credentials.service.js +91 -26
  28. package/dist/crypto/credential-envelope.d.ts +27 -1
  29. package/dist/crypto/credential-envelope.d.ts.map +1 -1
  30. package/dist/crypto/credential-envelope.js +43 -0
  31. package/dist/integrations/client-context.d.ts +20 -0
  32. package/dist/integrations/client-context.d.ts.map +1 -0
  33. package/dist/integrations/client-context.js +23 -0
  34. package/dist/integrations/integrations.controller.d.ts +4 -0
  35. package/dist/integrations/integrations.controller.d.ts.map +1 -1
  36. package/dist/integrations/integrations.controller.js +2 -0
  37. package/dist/integrations/provider-store.d.ts +8 -2
  38. package/dist/integrations/provider-store.d.ts.map +1 -1
  39. package/dist/integrations/provider-store.js +82 -14
  40. package/dist/proxy/proxy.controller.d.ts +9 -1
  41. package/dist/proxy/proxy.controller.d.ts.map +1 -1
  42. package/dist/proxy/proxy.controller.js +34 -10
  43. package/dist/proxy/proxy.service.d.ts.map +1 -1
  44. package/dist/proxy/proxy.service.js +39 -4
  45. package/package.json +4 -4
@@ -1,5 +1,5 @@
1
1
  import { __decorate, __metadata, __param } from "tslib";
2
- import { All, BadRequestException, Controller, Param, Req, Res, UseGuards, } from '@nestjs/common';
2
+ import { All, BadRequestException, Controller, Headers, Param, Req, Res, UseGuards, } from '@nestjs/common';
3
3
  import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';
4
4
  import { AllowAnonymous } from '@thallesp/nestjs-better-auth';
5
5
  import { ApiKeyGuard } from '../auth/api-key.guard.js';
@@ -27,11 +27,16 @@ let ProxyController = class ProxyController {
27
27
  * provider's table has its own unique index, so a slug is only unique
28
28
  * within one provider.
29
29
  */
30
- async forward(request, response, provider, configurationSlug, owner) {
30
+ // `{/*path}` wraps the slash too, so MCP's `.../owner` with nothing after
31
+ // it also matches — `/{*path}` alone would still require that slash.
32
+ async forward(request, response, provider, configurationSlug, pathOwner, headerOwner) {
31
33
  if (!request.environmentId) {
32
34
  throw new BadRequestException('This route needs an API key, which is what names the environment');
33
35
  }
34
- const { path, query } = splitUpstreamTarget(request.originalUrl, provider, configurationSlug, owner);
36
+ const owner = resolveOwner(headerOwner, pathOwner);
37
+ // Always the path segment here, not the resolved owner -- the marker has
38
+ // to match what is literally in the URL.
39
+ const { path, query } = splitUpstreamTarget(request.originalUrl, provider, configurationSlug, pathOwner);
35
40
  const result = await this.proxy.forward(request.organizationId, request.environmentId, { provider, configurationSlug, owner }, {
36
41
  method: request.method,
37
42
  path,
@@ -49,21 +54,27 @@ let ProxyController = class ProxyController {
49
54
  }
50
55
  };
51
56
  __decorate([
52
- All(':provider/:configurationSlug/:owner/*path'),
57
+ All(':provider/:configurationSlug/:owner{/*path}'),
53
58
  AllowAnonymous(),
54
59
  UseGuards(ApiKeyGuard),
55
60
  ApiSecurity('x-api-key'),
56
61
  ApiOperation({
57
62
  summary: 'Forward a request to a connected provider',
58
- description: 'The environment comes from the API key. `owner` is the app’s own id for the person who connected.',
63
+ description: 'The environment comes from the API key. `owner` is the app’s own id ' +
64
+ 'for the person who connected. A caller whose URL has to stay fixed ' +
65
+ 'across calls — an MCP client connection, which has no per-call path ' +
66
+ 'the way an OpenAPI connection does — may send `x-kosha-owner` instead ' +
67
+ 'and put any placeholder in the `owner` segment; the header wins when ' +
68
+ 'both are present.',
59
69
  }),
60
70
  __param(0, Req()),
61
71
  __param(1, Res()),
62
72
  __param(2, Param('provider')),
63
73
  __param(3, Param('configurationSlug')),
64
74
  __param(4, Param('owner')),
75
+ __param(5, Headers('x-kosha-owner')),
65
76
  __metadata("design:type", Function),
66
- __metadata("design:paramtypes", [Object, Object, String, String, String]),
77
+ __metadata("design:paramtypes", [Object, Object, String, String, String, Object]),
67
78
  __metadata("design:returntype", Promise)
68
79
  ], ProxyController.prototype, "forward", null);
69
80
  ProxyController = __decorate([
@@ -72,14 +83,27 @@ ProxyController = __decorate([
72
83
  __metadata("design:paramtypes", [ProxyService])
73
84
  ], ProxyController);
74
85
  export { ProxyController };
86
+ /**
87
+ * The header, when a caller sent one, else the URL's `:owner` segment.
88
+ *
89
+ * For a caller whose URL can't vary per request (an MCP connection has one
90
+ * fixed `url`, unlike an OpenAPI connection's per-call operation paths).
91
+ * Additive: every existing caller sends no header and is unaffected.
92
+ */
93
+ export function resolveOwner(headerOwner, pathOwner) {
94
+ return headerOwner?.trim() ? headerOwner.trim() : pathOwner;
95
+ }
75
96
  /**
76
97
  * Recovers the upstream path from the raw URL rather than the parsed route
77
98
  * params, so the provider receives the caller's encoding untouched.
78
99
  */
79
100
  export function splitUpstreamTarget(originalUrl, provider, configurationSlug, owner) {
80
- const marker = `/${provider}/${configurationSlug}/${owner}/`;
81
- const markerIndex = originalUrl.indexOf(marker);
82
- const rest = markerIndex >= 0 ? originalUrl.slice(markerIndex + marker.length) : '';
83
- const [path, query = ''] = rest.split('?');
101
+ const [beforeQuery, query = ''] = originalUrl.split('?');
102
+ // No trailing slash in the marker, so an owner with nothing after it also matches.
103
+ const marker = `/${provider}/${configurationSlug}/${owner}`;
104
+ const markerIndex = beforeQuery.indexOf(marker);
105
+ const path = markerIndex >= 0
106
+ ? beforeQuery.slice(markerIndex + marker.length).replace(/^\/+/, '')
107
+ : '';
84
108
  return { path, query };
85
109
  }
@@ -1 +1 @@
1
- {"version":3,"file":"proxy.service.d.ts","sourceRoot":"","sources":["../../src/proxy/proxy.service.ts"],"names":[],"mappings":"AAKA,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,aAAa,EAAE,MAAM,mCAAmC,CAAC;AAClE,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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vD,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,yFAAyF;AACzF,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,KAAK,EAAE,MAAM,CAAC;CACf;AAED,qBACa,YAAY;IAErB,OAAO,CAAC,QAAQ,CAAC,KAAK;IACtB,OAAO,CAAC,QAAQ,CAAC,YAAY;IAC7B,OAAO,CAAC,QAAQ,CAAC,KAAK;IACtB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAJR,KAAK,EAAE,eAAe,EACtB,YAAY,EAAE,mBAAmB,EACjC,KAAK,EAAE,aAAa,EACpB,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;IAsHvB,OAAO,CAAC,cAAc;CAevB"}
@@ -1,5 +1,6 @@
1
1
  import { __decorate, __metadata } from "tslib";
2
- import { BadGatewayException, Injectable, NotFoundException, } from '@nestjs/common';
2
+ import { assertPublicUrl } from '@kosha-ai/integration-sdk';
3
+ import { BadGatewayException, BadRequestException, Injectable, NotFoundException, } from '@nestjs/common';
3
4
  import { CredentialsService } from '../credentials/credentials.service.js';
4
5
  import { EnvironmentsService } from '../environments/environments.service.js';
5
6
  import { IntegrationRegistry } from '../integrations/integration.registry.js';
@@ -19,6 +20,8 @@ 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
27
  proxy;
@@ -40,6 +43,8 @@ let ProxyService = class ProxyService {
40
43
  const integration = this.registry.find(target.provider);
41
44
  if (!integration)
42
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);
43
48
  // Independent reads, so they go together: the configuration this slug
44
49
  // names, and the person this owner names.
45
50
  const [configuration, userId] = await Promise.all([
@@ -55,8 +60,8 @@ let ProxyService = class ProxyService {
55
60
  const account = await this.proxy.findAccount(connection.accountId);
56
61
  if (!account)
57
62
  throw new NotFoundException('Connection not found');
58
- const credential = await this.resolver.resolve(account, integration, configuration);
59
- const baseUrl = integration.api.resolveBaseUrl?.({
63
+ const credential = await this.resolver.resolve(account, integration, configuration, tokenKind);
64
+ const resolved = integration.api.resolveBaseUrl?.({
60
65
  credential: {
61
66
  accessToken: credential.accessToken,
62
67
  tokenType: credential.tokenType,
@@ -66,10 +71,20 @@ let ProxyService = class ProxyService {
66
71
  // Jira path carries a cloudId — so passing nothing made those
67
72
  // providers unreachable by construction.
68
73
  metadata: asMetadata(connection.metadata),
69
- }) ?? integration.api.baseUrl;
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;
70
81
  if (!baseUrl) {
71
82
  throw new BadGatewayException('Provider has no resolvable base URL for this connection');
72
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);
73
88
  const targetUrl = this.buildTargetUrl(baseUrl, request);
74
89
  /*
75
90
  * Header names are compared lowercased, and the integration's headers are
@@ -141,3 +156,23 @@ function asMetadata(metadata) {
141
156
  ? metadata
142
157
  : undefined;
143
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.3.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.3.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.3.0",
121
- "@kosha-ai/integration-slack": "0.3.0"
120
+ "@kosha-ai/integration-github": "0.4.0",
121
+ "@kosha-ai/integration-slack": "0.4.0"
122
122
  }
123
123
  }