@kosha-ai/server 0.5.4 → 0.6.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.
@@ -21,6 +21,7 @@ export declare class ConnectController {
21
21
  * the app collects it and posts it here, and a connection exists at once.
22
22
  */
23
23
  connectWithCredential(request: ApiKeyRequest, body: ConnectWithCredentialDto): Promise<import("./connect.service.js").CallbackResult>;
24
+ connectWithClientCredentials(request: ApiKeyRequest, body: CreateConnectSessionDto): Promise<import("./connect.service.js").CallbackResult>;
24
25
  /**
25
26
  * Admin-only: runs the same flow with the signed-in admin standing in for an
26
27
  * end user, so an operator can verify a configuration without wiring up
@@ -1 +1 @@
1
- {"version":3,"file":"connect.controller.d.ts","sourceRoot":"","sources":["../../src/connect/connect.controller.ts"],"names":[],"mappings":"AAsBA,OAAO,EAGL,KAAK,WAAW,EACjB,MAAM,8BAA8B,CAAC;AACtC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACxC,OAAO,EAAE,KAAK,aAAa,EAAgB,MAAM,0BAA0B,CAAC;AAE5E,OAAO,EAEL,cAAc,EAEf,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAC3E,OAAO,EAAE,uBAAuB,EAAE,MAAM,qCAAqC,CAAC;AAC9E,OAAO,EACL,oBAAoB,EACpB,iBAAiB,EAClB,MAAM,kCAAkC,CAAC;AAE1C,qBAEa,iBAAiB;IAGhB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAFpC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAsC;gBAEhC,OAAO,EAAE,cAAc;IAEpD;;;;;OAKG;IAcH,aAAa,CACJ,OAAO,EAAE,aAAa,EACrB,IAAI,EAAE,uBAAuB;IAevC;;;OAGG;IAcH,qBAAqB,CACZ,OAAO,EAAE,aAAa,EACrB,IAAI,EAAE,wBAAwB;IAexC;;;;OAIG;IAOH,iBAAiB,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qBAAqB,CACZ,OAAO,EAAE,aAAa,EACrB,IAAI,EAAE,wBAAwB;IAyBxC,4BAA4B,CACnB,OAAO,EAAE,aAAa,EACrB,IAAI,EAAE,uBAAuB;IAcvC;;;;OAIG;IAOH,iBAAiB,CACJ,OAAO,EAAE,WAAW,EACP,aAAa,EAAE,MAAM,EACrC,IAAI,EAAE,oBAAoB;IAepC,wEAAwE;IAOxE,cAAc,CACD,OAAO,EAAE,WAAW,EACP,aAAa,EAAE,MAAM,EACrC,IAAI,EAAE,iBAAiB;IAejC,uEAAuE;IAIjE,KAAK,CAAiB,KAAK,EAAE,MAAM,EAAS,GAAG,EAAE,QAAQ;IAuB/D,2EAA2E;YAC7D,MAAM;IAiBpB,kDAAkD;IAI5C,QAAQ,CACO,QAAQ,EAAE,MAAM,EACpB,IAAI,EAAE,MAAM,GAAG,SAAS,EACvB,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,aAAa,EAAE,MAAM,GAAG,SAAS,EACrB,wBAAwB,EAAE,MAAM,GAAG,SAAS,EACjE,GAAG,EAAE,QAAQ;CAyDvB"}
@@ -37,6 +37,12 @@ let ConnectController = ConnectController_1 = class ConnectController {
37
37
  }
38
38
  return this.connect.connectWithCredential(request.organizationId, request.environmentId, body);
39
39
  }
40
+ connectWithClientCredentials(request, body) {
41
+ if (!request.environmentId) {
42
+ throw new BadRequestException('This route needs an API key, which is what names the environment');
43
+ }
44
+ return this.connect.connectWithClientCredentials(request.organizationId, request.environmentId, body);
45
+ }
40
46
  /**
41
47
  * Admin-only: runs the same flow with the signed-in admin standing in for an
42
48
  * end user, so an operator can verify a configuration without wiring up
@@ -67,6 +73,12 @@ let ConnectController = ConnectController_1 = class ConnectController {
67
73
  res.redirect(await this.connect.buildAuthorizationRedirect(token));
68
74
  }
69
75
  catch (error) {
76
+ if (error instanceof ConnectFlowEnded && error.status === 'success') {
77
+ if (redirectBack(res, error.redirectUrl, error.status))
78
+ return;
79
+ res.send('Connected. You can close this window.');
80
+ return;
81
+ }
70
82
  this.logger.warn(`Connect start rejected: ${error instanceof Error ? error.message : error}`);
71
83
  if (error instanceof ConnectFlowEnded &&
72
84
  redirectBack(res, error.redirectUrl, error.status)) {
@@ -169,6 +181,22 @@ __decorate([
169
181
  __metadata("design:paramtypes", [Object, ConnectWithCredentialDto]),
170
182
  __metadata("design:returntype", void 0)
171
183
  ], ConnectController.prototype, "connectWithCredential", null);
184
+ __decorate([
185
+ Post('client-credentials'),
186
+ AllowAnonymous(),
187
+ UseGuards(OrgAuthGuard),
188
+ ApiSecurity('x-api-key'),
189
+ ApiOperation({
190
+ summary: 'Connect an organization with an OAuth client-credentials grant',
191
+ description: 'Mints a server-to-server access token without redirecting an end user. Use a stable organization owner id in externalUserId.',
192
+ }),
193
+ ApiCreatedResponse({ description: 'The organization connection created.' }),
194
+ __param(0, Req()),
195
+ __param(1, Body()),
196
+ __metadata("design:type", Function),
197
+ __metadata("design:paramtypes", [Object, CreateConnectSessionDto]),
198
+ __metadata("design:returntype", void 0)
199
+ ], ConnectController.prototype, "connectWithClientCredentials", null);
172
200
  __decorate([
173
201
  Post('sessions/test/:environmentId'),
174
202
  ApiOperation({
@@ -22,9 +22,9 @@ export interface CallbackResult {
22
22
  * should be sent back to and why.
23
23
  */
24
24
  export declare class ConnectFlowEnded extends Error {
25
- readonly status: 'denied' | 'expired';
25
+ readonly status: 'success' | 'denied' | 'expired';
26
26
  readonly redirectUrl: string | null;
27
- constructor(status: 'denied' | 'expired', redirectUrl: string | null);
27
+ constructor(status: 'success' | 'denied' | 'expired', redirectUrl: string | null);
28
28
  }
29
29
  /**
30
30
  * Drives the OAuth handshake that turns an end user's consent into a stored,
@@ -84,6 +84,13 @@ export declare class ConnectService {
84
84
  * name its own account is not one this connection can use.
85
85
  */
86
86
  connectWithCredential(organizationId: string, environmentId: string, input: ConnectWithCredentialDto): Promise<CallbackResult>;
87
+ /**
88
+ * Creates an organization-owned connection using an OAuth client-credentials
89
+ * grant. There is no browser redirect: the client secret stays in the
90
+ * configuration, and the short-lived access token is stored on the
91
+ * connection so the credential resolver can mint a replacement on expiry.
92
+ */
93
+ connectWithClientCredentials(organizationId: string, environmentId: string, input: CreateConnectSessionDto): Promise<CallbackResult>;
87
94
  /**
88
95
  * Asks the provider who the key belongs to, which is the only check
89
96
  * available and the right one.
@@ -1 +1 @@
1
- {"version":3,"file":"connect.service.d.ts","sourceRoot":"","sources":["../../src/connect/connect.service.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAKV,QAAQ,EACT,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AASrE,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,yCAAyC,CAAC;AAE9E,OAAO,EAAE,mBAAmB,EAAE,MAAM,yCAAyC,CAAC;AAC9E,OAAO,EACL,aAAa,EAEd,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAChF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,qCAAqC,CAAC;AAInF,eAAO,MAAM,UAAU,MAAM,CAAC;AAE9B,MAAM,WAAW,oBAAoB;IACnC,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,IAAI,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5B;AAED;;;GAGG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IAEvC,QAAQ,CAAC,MAAM,EAAE,QAAQ,GAAG,SAAS;IACrC,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI;gBAD1B,MAAM,EAAE,QAAQ,GAAG,SAAS,EAC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IA0D1B;;;;;OAKG;IACG,4BAA4B,CAChC,cAAc,EAAE,MAAM,EACtB,aAAa,EAAE,MAAM,EACrB,KAAK,EAAE,uBAAuB,GAC7B,OAAO,CAAC,cAAc,CAAC;IA+D1B;;;;;OAKG;YACW,eAAe;IAqC7B;;;;;;OAMG;YACW,eAAe;IAgD7B,oEAAoE;IAC9D,iBAAiB,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;IAS3E,OAAO,CAAC,WAAW;IAmBnB,OAAO,CAAC,gBAAgB;YAoBV,oBAAoB;IA8BlC,sEAAsE;YACxD,OAAO;CActB"}
@@ -22,9 +22,11 @@ export class ConnectFlowEnded extends Error {
22
22
  status;
23
23
  redirectUrl;
24
24
  constructor(status, redirectUrl) {
25
- super(status === 'denied'
26
- ? 'The end user declined at the provider'
27
- : 'Connect flow expired');
25
+ super(status === 'success'
26
+ ? 'Connection created'
27
+ : status === 'denied'
28
+ ? 'The end user declined at the provider'
29
+ : 'Connect flow expired');
28
30
  this.status = status;
29
31
  this.redirectUrl = redirectUrl;
30
32
  this.name = 'ConnectFlowEnded';
@@ -120,6 +122,18 @@ let ConnectService = ConnectService_1 = class ConnectService {
120
122
  if (!configuration) {
121
123
  throw new BadRequestException('Configuration is no longer available');
122
124
  }
125
+ // Keep existing connect URLs compatible with configurations that use an
126
+ // organization-wide client-credentials grant. There is no provider
127
+ // redirect for these configurations, so complete the connection here and
128
+ // let the normal start handler return the caller to its redirect URL.
129
+ if (integration.auth.clientCredentialsOnly?.(configuration.fields)) {
130
+ await this.connectWithClientCredentials(session.organizationId, session.environmentId, {
131
+ externalUserId: session.externalUserId,
132
+ provider: session.provider,
133
+ configurationId: session.configurationId,
134
+ });
135
+ throw new ConnectFlowEnded('success', session.redirectUrl ?? null);
136
+ }
123
137
  const codeVerifier = integration.auth.pkce
124
138
  ? generateCodeVerifier()
125
139
  : undefined;
@@ -273,6 +287,50 @@ let ConnectService = ConnectService_1 = class ConnectService {
273
287
  }));
274
288
  return { connectionId: account.id, redirectUrl: null };
275
289
  }
290
+ /**
291
+ * Creates an organization-owned connection using an OAuth client-credentials
292
+ * grant. There is no browser redirect: the client secret stays in the
293
+ * configuration, and the short-lived access token is stored on the
294
+ * connection so the credential resolver can mint a replacement on expiry.
295
+ */
296
+ async connectWithClientCredentials(organizationId, environmentId, input) {
297
+ const externalUserId = input.externalUserId?.trim();
298
+ if (!externalUserId) {
299
+ throw new BadRequestException('externalUserId is required');
300
+ }
301
+ const environment = await this.environments.get(organizationId, environmentId);
302
+ const integration = this.registry.get(input.provider);
303
+ if (typeof integration.auth.clientCredentials !== 'function') {
304
+ throw new BadRequestException(`${integration.key} does not support client-credentials OAuth.`);
305
+ }
306
+ const configuration = await this.resolveConfiguration(integration, environmentId, input);
307
+ const configured = await this.store.findConfigurationWithCredentials(integration, configuration.id);
308
+ if (!configured) {
309
+ throw new NotFoundException('Configuration not found');
310
+ }
311
+ const tokens = await integration.auth.clientCredentials({
312
+ client: this.clientContextFor(integration, configured),
313
+ });
314
+ const identity = await integration.identity.fetchAccount({
315
+ accessToken: tokens.accessToken,
316
+ http: httpFor(integration),
317
+ fields: configured.fields,
318
+ tokens,
319
+ });
320
+ const user = await this.connect.upsertEndUser(environment.slug, externalUserId);
321
+ const account = await this.writeCredential({ environmentId, configurationId: configured.id }, integration, {
322
+ userId: user.id,
323
+ accountId: identity.externalAccountId,
324
+ envelope: this.envelopeFor(tokens),
325
+ scope: tokens.scope,
326
+ metadata: identity.metadata,
327
+ });
328
+ await this.runHook(integration, () => integration.hooks?.onConnectionCreated?.({
329
+ connectionId: account.id,
330
+ organizationId,
331
+ }));
332
+ return { connectionId: account.id, redirectUrl: null };
333
+ }
276
334
  /**
277
335
  * Asks the provider who the key belongs to, which is the only check
278
336
  * available and the right one.
@@ -35,6 +35,7 @@ export declare class CredentialsService {
35
35
  */
36
36
  resolve(account: Account, integration: KoshaIntegration, configuration: ProviderConfiguration, tokenKind?: string): Promise<ResolvedCredential>;
37
37
  private refresh;
38
+ private mintClientCredentials;
38
39
  /**
39
40
  * A secondary token expires and refreshes on its own, with its own refresh
40
41
  * token. Renewing it leaves the primary, and its expiry, as they were.
@@ -1 +1 @@
1
- {"version":3,"file":"credentials.service.d.ts","sourceRoot":"","sources":["../../src/credentials/credentials.service.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,gBAAgB,EAAY,MAAM,2BAA2B,CAAC;AAE5E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAGrE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAQ/E,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAC7D,OAAO,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AAEpE;;;;;;;GAOG;AACH,MAAM,WAAW,kBAAkB;IACjC,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,SAAS,EAAE,IAAI,GAAG,IAAI,CAAC;IACvB,mEAAmE;IACnE,SAAS,EAAE,OAAO,CAAC;CACpB;AAED,qBACa,kBAAkB;IAI3B,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,WAAW;IACN,OAAO,CAAC,QAAQ,CAAC,MAAM;IAL/C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuC;gBAG3C,MAAM,EAAE,aAAa,EACrB,WAAW,EAAE,qBAAqB,EACZ,MAAM,EAAE,mBAAmB;IAGpE;;;;;;;OAOG;IACG,OAAO,CACX,OAAO,EAAE,OAAO,EAChB,WAAW,EAAE,gBAAgB,EAC7B,aAAa,EAAE,qBAAqB,EACpC,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,kBAAkB,CAAC;YA2ChB,OAAO;IA6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AAC;YAkDhB,OAAO;YA6CP,qBAAqB;IA6CnC;;;OAGG;YACW,gBAAgB;IA6D9B,4EAA4E;YAC9D,cAAc;IAsB5B,OAAO,CAAC,aAAa;IAcrB,OAAO,CAAC,eAAe;IAUvB;;;;OAIG;IACH,OAAO,CAAC,MAAM;YAgDA,qBAAqB;CAoBpC"}
@@ -41,6 +41,10 @@ let CredentialsService = CredentialsService_1 = class CredentialsService {
41
41
  return this.usable(envelope, expiresAt, false);
42
42
  const refreshToken = envelope.type === 'oauth2' ? envelope.refreshToken : undefined;
43
43
  const canRefresh = typeof integration.auth.refresh === 'function' && refreshToken;
44
+ if (!canRefresh &&
45
+ typeof integration.auth.clientCredentials === 'function') {
46
+ return this.mintClientCredentials(account, integration, configuration);
47
+ }
44
48
  if (!canRefresh) {
45
49
  throw new UnauthorizedException('Connection token expired; the end user must reconnect');
46
50
  }
@@ -71,6 +75,39 @@ let CredentialsService = CredentialsService_1 = class CredentialsService {
71
75
  throw this.refreshFailed(integration, error);
72
76
  }
73
77
  }
78
+ async mintClientCredentials(account, integration, configuration) {
79
+ try {
80
+ const tokens = await integration.auth.clientCredentials?.({
81
+ client: clientContextFor({
82
+ integration,
83
+ configuration,
84
+ credentials: this.openCredentials(configuration),
85
+ server: this.config.server,
86
+ scopes: integration.auth.scopesRequested,
87
+ }),
88
+ });
89
+ if (!tokens)
90
+ throw new Error('Client-credentials request returned no tokens');
91
+ const envelope = {
92
+ v: ENVELOPE_VERSION,
93
+ type: 'oauth2',
94
+ accessToken: tokens.accessToken,
95
+ ...(tokens.tokenType ? { tokenType: tokens.tokenType } : {}),
96
+ ...(tokens.expiresInSeconds
97
+ ? {
98
+ expiresAt: new Date(Date.now() + tokens.expiresInSeconds * 1000).toISOString(),
99
+ }
100
+ : {}),
101
+ };
102
+ const expiresAt = expiryOf(envelope);
103
+ await this.credentials.replaceEnvelope(account.id, this.crypto.sealEnvelope(envelope), expiresAt, tokens.scope ?? undefined);
104
+ await this.runTokenRefreshedHook(account.id, integration, tokens);
105
+ return this.usable(envelope, expiresAt, true);
106
+ }
107
+ catch (error) {
108
+ throw this.refreshFailed(integration, error);
109
+ }
110
+ }
74
111
  /**
75
112
  * A secondary token expires and refreshes on its own, with its own refresh
76
113
  * token. Renewing it leaves the primary, and its expiry, as they were.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kosha-ai/server",
3
- "version": "0.5.4",
3
+ "version": "0.6.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.5.4"
112
+ "@kosha-ai/integration-sdk": "0.6.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.5.4",
121
- "@kosha-ai/integration-slack": "0.5.4"
120
+ "@kosha-ai/integration-github": "0.6.0",
121
+ "@kosha-ai/integration-slack": "0.6.0"
122
122
  }
123
123
  }