@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.
- package/dist/configurations/configurations.service.d.ts +6 -0
- package/dist/configurations/configurations.service.d.ts.map +1 -1
- package/dist/configurations/configurations.service.js +58 -1
- package/dist/configurations/dto/create-configuration.dto.d.ts +1 -0
- package/dist/configurations/dto/create-configuration.dto.d.ts.map +1 -1
- package/dist/configurations/dto/create-configuration.dto.js +10 -0
- package/dist/configurations/dto/update-configuration.dto.d.ts +1 -0
- package/dist/configurations/dto/update-configuration.dto.d.ts.map +1 -1
- package/dist/configurations/dto/update-configuration.dto.js +10 -0
- package/dist/connect/connect.controller.d.ts +11 -1
- package/dist/connect/connect.controller.d.ts.map +1 -1
- package/dist/connect/connect.controller.js +102 -13
- package/dist/connect/connect.repository.d.ts +11 -2
- package/dist/connect/connect.repository.d.ts.map +1 -1
- package/dist/connect/connect.repository.js +8 -3
- package/dist/connect/connect.service.d.ts +47 -0
- package/dist/connect/connect.service.d.ts.map +1 -1
- package/dist/connect/connect.service.js +269 -28
- package/dist/connect/dto/connect-credential.dto.d.ts +8 -0
- package/dist/connect/dto/connect-credential.dto.d.ts.map +1 -0
- package/dist/connect/dto/connect-credential.dto.js +54 -0
- package/dist/connect/dto/create-test-session.dto.d.ts +7 -0
- package/dist/connect/dto/create-test-session.dto.d.ts.map +1 -1
- package/dist/connect/dto/create-test-session.dto.js +36 -1
- package/dist/credentials/credentials.service.d.ts +9 -3
- package/dist/credentials/credentials.service.d.ts.map +1 -1
- package/dist/credentials/credentials.service.js +91 -26
- package/dist/crypto/credential-envelope.d.ts +27 -1
- package/dist/crypto/credential-envelope.d.ts.map +1 -1
- package/dist/crypto/credential-envelope.js +43 -0
- package/dist/integrations/client-context.d.ts +20 -0
- package/dist/integrations/client-context.d.ts.map +1 -0
- package/dist/integrations/client-context.js +23 -0
- package/dist/integrations/integrations.controller.d.ts +4 -0
- package/dist/integrations/integrations.controller.d.ts.map +1 -1
- package/dist/integrations/integrations.controller.js +2 -0
- package/dist/integrations/provider-store.d.ts +8 -2
- package/dist/integrations/provider-store.d.ts.map +1 -1
- package/dist/integrations/provider-store.js +82 -14
- package/dist/proxy/proxy.controller.d.ts +9 -1
- package/dist/proxy/proxy.controller.d.ts.map +1 -1
- package/dist/proxy/proxy.controller.js +34 -10
- package/dist/proxy/proxy.service.d.ts.map +1 -1
- package/dist/proxy/proxy.service.js +39 -4
- package/package.json +4 -4
|
@@ -1,20 +1,44 @@
|
|
|
1
1
|
var ConnectService_1;
|
|
2
2
|
import { __decorate, __metadata, __param } from "tslib";
|
|
3
|
-
import { BadRequestException, ConflictException, Inject, Injectable, Logger, NotFoundException, } from '@nestjs/common';
|
|
3
|
+
import { BadGatewayException, BadRequestException, ConflictException, Inject, Injectable, Logger, NotFoundException, } from '@nestjs/common';
|
|
4
4
|
import { randomBytes } from 'node:crypto';
|
|
5
|
-
import { generateCodeVerifier, httpFor } from '@kosha-ai/integration-sdk';
|
|
5
|
+
import { generateCodeVerifier, httpFor, isRedirectStrategy, } from '@kosha-ai/integration-sdk';
|
|
6
6
|
import { KOSHA_CONFIG } from '../config/tokens.js';
|
|
7
|
-
import { ENVELOPE_VERSION, expiryOf, } from '../crypto/credential-envelope.js';
|
|
7
|
+
import { ENVELOPE_VERSION, expiryOf, storedSecondaryToken, } from '../crypto/credential-envelope.js';
|
|
8
8
|
import { CryptoService } from '../crypto/crypto.service.js';
|
|
9
9
|
import { EnvironmentsService } from '../environments/environments.service.js';
|
|
10
|
+
import { clientContextFor } from '../integrations/client-context.js';
|
|
10
11
|
import { IntegrationRegistry } from '../integrations/integration.registry.js';
|
|
11
12
|
import { ProviderStore, } from '../integrations/provider-store.js';
|
|
12
13
|
import { ConnectRepository } from './connect.repository.js';
|
|
13
14
|
const SESSION_TTL_MS = 10 * 60 * 1000;
|
|
14
15
|
const STATE_TTL_MS = 10 * 60 * 1000;
|
|
16
|
+
export const MAX_LOGGED = 500;
|
|
17
|
+
/**
|
|
18
|
+
* A connect flow that ended without a credential, carrying where the person
|
|
19
|
+
* should be sent back to and why.
|
|
20
|
+
*/
|
|
21
|
+
export class ConnectFlowEnded extends Error {
|
|
22
|
+
status;
|
|
23
|
+
redirectUrl;
|
|
24
|
+
constructor(status, redirectUrl) {
|
|
25
|
+
super(status === 'denied'
|
|
26
|
+
? 'The end user declined at the provider'
|
|
27
|
+
: 'Connect flow expired');
|
|
28
|
+
this.status = status;
|
|
29
|
+
this.redirectUrl = redirectUrl;
|
|
30
|
+
this.name = 'ConnectFlowEnded';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
15
33
|
function randomToken() {
|
|
16
34
|
return randomBytes(32).toString('hex');
|
|
17
35
|
}
|
|
36
|
+
/** The exchange's secondary tokens, as the envelope stores them. */
|
|
37
|
+
function storedSecondary(tokens) {
|
|
38
|
+
if (!tokens.secondary?.length)
|
|
39
|
+
return undefined;
|
|
40
|
+
return Object.fromEntries(tokens.secondary.map((token) => [token.kind, storedSecondaryToken(token)]));
|
|
41
|
+
}
|
|
18
42
|
/**
|
|
19
43
|
* Drives the OAuth handshake that turns an end user's consent into a stored,
|
|
20
44
|
* encrypted credential.
|
|
@@ -49,6 +73,9 @@ let ConnectService = ConnectService_1 = class ConnectService {
|
|
|
49
73
|
}
|
|
50
74
|
const environment = await this.environments.get(organizationId, environmentId);
|
|
51
75
|
const integration = this.registry.get(input.provider);
|
|
76
|
+
// Refused here rather than when the end user opens the link, so the
|
|
77
|
+
// backend that asked for it is the one that hears about it.
|
|
78
|
+
redirectStrategyOf(integration);
|
|
52
79
|
const configuration = await this.resolveConfiguration(integration, environmentId, input);
|
|
53
80
|
// Nothing else sweeps these: `verification` is shared with email
|
|
54
81
|
// verification and password reset, so only this flow knows which rows are
|
|
@@ -80,10 +107,14 @@ let ConnectService = ConnectService_1 = class ConnectService {
|
|
|
80
107
|
* across the redirect.
|
|
81
108
|
*/
|
|
82
109
|
async buildAuthorizationRedirect(token) {
|
|
83
|
-
const
|
|
84
|
-
if (!
|
|
110
|
+
const taken = await this.connect.takeSession(token);
|
|
111
|
+
if (!taken) {
|
|
85
112
|
throw new NotFoundException('Connect session not found or expired');
|
|
86
113
|
}
|
|
114
|
+
if (taken.expired) {
|
|
115
|
+
throw new ConnectFlowEnded('expired', taken.flow.redirectUrl ?? null);
|
|
116
|
+
}
|
|
117
|
+
const session = taken.flow;
|
|
87
118
|
const integration = this.registry.get(session.provider);
|
|
88
119
|
const configuration = await this.store.findConfigurationWithCredentials(integration, session.configurationId);
|
|
89
120
|
if (!configuration) {
|
|
@@ -94,27 +125,89 @@ let ConnectService = ConnectService_1 = class ConnectService {
|
|
|
94
125
|
: undefined;
|
|
95
126
|
const state = randomToken();
|
|
96
127
|
await this.connect.putState(state, { ...session, codeVerifier }, new Date(Date.now() + STATE_TTL_MS));
|
|
97
|
-
|
|
98
|
-
|
|
128
|
+
const prepared = await this.prepareClient(integration, configuration);
|
|
129
|
+
return redirectStrategyOf(integration).buildAuthorizationUrl({
|
|
130
|
+
client: this.clientContextFor(integration, prepared),
|
|
99
131
|
state,
|
|
100
132
|
codeVerifier,
|
|
101
133
|
});
|
|
102
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Gives a provider whose endpoints and client are not known until an
|
|
137
|
+
* operator supplies a URL a chance to discover and register them, once per
|
|
138
|
+
* configuration. Persisted onto the configuration so refresh — which has no
|
|
139
|
+
* redirect to carry fresh discovery across — reads the same thing back.
|
|
140
|
+
*/
|
|
141
|
+
async prepareClient(integration, configuration) {
|
|
142
|
+
if (!integration.auth.prepareClient)
|
|
143
|
+
return configuration;
|
|
144
|
+
const { publicUrl, globalPrefix } = this.config.server;
|
|
145
|
+
const result = await integration.auth.prepareClient({
|
|
146
|
+
fields: configuration.fields,
|
|
147
|
+
redirectUri: `${publicUrl}/${globalPrefix}/connect/callback/${integration.key}`,
|
|
148
|
+
});
|
|
149
|
+
if (!result)
|
|
150
|
+
return configuration;
|
|
151
|
+
const existingCredentials = configuration.credentials
|
|
152
|
+
? JSON.parse(this.crypto.decrypt(configuration.credentials))
|
|
153
|
+
: {};
|
|
154
|
+
const mergedCredentials = result.credentials
|
|
155
|
+
? { ...existingCredentials, ...result.credentials }
|
|
156
|
+
: undefined;
|
|
157
|
+
const mergedFields = result.fields
|
|
158
|
+
? { ...configuration.fields, ...result.fields }
|
|
159
|
+
: configuration.fields;
|
|
160
|
+
const sealedCredentials = mergedCredentials
|
|
161
|
+
? this.crypto.encrypt(JSON.stringify(mergedCredentials))
|
|
162
|
+
: configuration.credentials;
|
|
163
|
+
await this.store.updateConfiguration(integration, configuration.id, {
|
|
164
|
+
fields: result.fields,
|
|
165
|
+
credentials: mergedCredentials ? sealedCredentials : undefined,
|
|
166
|
+
});
|
|
167
|
+
return {
|
|
168
|
+
...configuration,
|
|
169
|
+
fields: mergedFields,
|
|
170
|
+
credentials: sealedCredentials,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The person declined at the provider. Burns the state, so the flow cannot
|
|
175
|
+
* be completed afterwards, and says where to send them. A state from another
|
|
176
|
+
* provider's flow has nowhere to send them; one past its window is expired.
|
|
177
|
+
*
|
|
178
|
+
* The provider's reason is logged, never sent back: it is the only trace of
|
|
179
|
+
* a misconfigured provider, and the flow names where it happened. Both it
|
|
180
|
+
* and the provider come from the request, so they are logged quoted.
|
|
181
|
+
*/
|
|
182
|
+
async deny(provider, state, reason) {
|
|
183
|
+
const taken = state ? await this.connect.takeState(state) : null;
|
|
184
|
+
const flow = taken?.flow.provider === provider ? taken.flow : null;
|
|
185
|
+
this.logger.warn(`${JSON.stringify(provider.slice(0, MAX_LOGGED))} returned an error for ${flow
|
|
186
|
+
? `configuration ${flow.configurationId} in environment ${flow.environmentId}`
|
|
187
|
+
: 'an unknown connect flow'}: ${JSON.stringify(reason.slice(0, MAX_LOGGED))}`);
|
|
188
|
+
if (!taken || !flow)
|
|
189
|
+
return new ConnectFlowEnded('denied', null);
|
|
190
|
+
return new ConnectFlowEnded(taken.expired ? 'expired' : 'denied', flow.redirectUrl ?? null);
|
|
191
|
+
}
|
|
103
192
|
/** Handles the provider's redirect back to Kosha. */
|
|
104
193
|
async handleCallback(provider, code, state) {
|
|
105
|
-
const
|
|
106
|
-
if (!
|
|
194
|
+
const taken = await this.connect.takeState(state);
|
|
195
|
+
if (!taken) {
|
|
107
196
|
throw new NotFoundException('OAuth state not found or expired');
|
|
108
197
|
}
|
|
198
|
+
const flow = taken.flow;
|
|
109
199
|
if (flow.provider !== provider) {
|
|
110
200
|
throw new BadRequestException('Provider mismatch');
|
|
111
201
|
}
|
|
202
|
+
if (taken.expired) {
|
|
203
|
+
throw new ConnectFlowEnded('expired', flow.redirectUrl ?? null);
|
|
204
|
+
}
|
|
112
205
|
const integration = this.registry.get(provider);
|
|
113
206
|
const configuration = await this.store.findConfigurationWithCredentials(integration, flow.configurationId);
|
|
114
207
|
if (!configuration) {
|
|
115
208
|
throw new BadRequestException('Configuration is no longer available');
|
|
116
209
|
}
|
|
117
|
-
const tokens = await integration.
|
|
210
|
+
const tokens = await redirectStrategyOf(integration).exchangeCode({
|
|
118
211
|
client: this.clientContextFor(integration, configuration),
|
|
119
212
|
code,
|
|
120
213
|
codeVerifier: flow.codeVerifier,
|
|
@@ -122,12 +215,15 @@ let ConnectService = ConnectService_1 = class ConnectService {
|
|
|
122
215
|
const identity = await integration.identity.fetchAccount({
|
|
123
216
|
accessToken: tokens.accessToken,
|
|
124
217
|
http: httpFor(integration),
|
|
218
|
+
fields: configuration.fields,
|
|
219
|
+
tokens,
|
|
125
220
|
});
|
|
126
221
|
const user = await this.connect.upsertEndUser(flow.environmentSlug, flow.externalUserId);
|
|
127
222
|
const account = await this.writeCredential(flow, integration, {
|
|
128
223
|
userId: user.id,
|
|
129
224
|
accountId: identity.externalAccountId,
|
|
130
|
-
tokens,
|
|
225
|
+
envelope: this.envelopeFor(tokens),
|
|
226
|
+
scope: tokens.scope,
|
|
131
227
|
// What the provider told us about this account that a later proxied
|
|
132
228
|
// call needs — the Jira site, for one. Without storing it here, a
|
|
133
229
|
// provider with no fixed host has no resolvable base URL at all.
|
|
@@ -139,6 +235,75 @@ let ConnectService = ConnectService_1 = class ConnectService {
|
|
|
139
235
|
}));
|
|
140
236
|
return { connectionId: account.id, redirectUrl: flow.redirectUrl ?? null };
|
|
141
237
|
}
|
|
238
|
+
/**
|
|
239
|
+
* Creates a connection from a credential the end user already holds.
|
|
240
|
+
*
|
|
241
|
+
* The counterpart to the redirect flow: no connect session, no state to
|
|
242
|
+
* burn, no callback. The consuming app collects the key from its own user
|
|
243
|
+
* and posts it here, so that person needs no Kosha login and Kosha still
|
|
244
|
+
* owns the secret.
|
|
245
|
+
*
|
|
246
|
+
* The key is proved before anything is written, because a key that cannot
|
|
247
|
+
* name its own account is not one this connection can use.
|
|
248
|
+
*/
|
|
249
|
+
async connectWithCredential(organizationId, environmentId, input) {
|
|
250
|
+
const externalUserId = input.externalUserId?.trim();
|
|
251
|
+
if (!externalUserId) {
|
|
252
|
+
throw new BadRequestException('externalUserId is required');
|
|
253
|
+
}
|
|
254
|
+
const environment = await this.environments.get(organizationId, environmentId);
|
|
255
|
+
const integration = this.registry.get(input.provider);
|
|
256
|
+
if (isRedirectStrategy(integration.auth)) {
|
|
257
|
+
throw new BadRequestException(`${integration.key} sends the end user to the provider. Use /connect/sessions instead.`);
|
|
258
|
+
}
|
|
259
|
+
const configuration = await this.resolveConfiguration(integration, environmentId, input);
|
|
260
|
+
const supplied = suppliedCredential(integration, input.credential);
|
|
261
|
+
const envelope = envelopeForCredential(integration, supplied);
|
|
262
|
+
const identity = await this.proveCredential(integration, usableSecret(envelope), configuration.fields);
|
|
263
|
+
const user = await this.connect.upsertEndUser(environment.slug, externalUserId);
|
|
264
|
+
const account = await this.writeCredential({ environmentId, configurationId: configuration.id }, integration, {
|
|
265
|
+
userId: user.id,
|
|
266
|
+
accountId: identity.externalAccountId,
|
|
267
|
+
envelope,
|
|
268
|
+
metadata: identity.metadata,
|
|
269
|
+
});
|
|
270
|
+
await this.runHook(integration, () => integration.hooks?.onConnectionCreated?.({
|
|
271
|
+
connectionId: account.id,
|
|
272
|
+
organizationId,
|
|
273
|
+
}));
|
|
274
|
+
return { connectionId: account.id, redirectUrl: null };
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Asks the provider who the key belongs to, which is the only check
|
|
278
|
+
* available and the right one.
|
|
279
|
+
*
|
|
280
|
+
* The provider's own message is withheld, because it can echo the key back.
|
|
281
|
+
*/
|
|
282
|
+
async proveCredential(integration, key, fields) {
|
|
283
|
+
// A provider with no fixed host is reached at whatever the configuration
|
|
284
|
+
// named, which is also what it will be proxied to later.
|
|
285
|
+
const baseUrl = integration.api.resolveBaseUrl?.({
|
|
286
|
+
credential: { accessToken: key },
|
|
287
|
+
fields,
|
|
288
|
+
});
|
|
289
|
+
try {
|
|
290
|
+
return await integration.identity.fetchAccount({
|
|
291
|
+
accessToken: key,
|
|
292
|
+
http: httpFor(integration, baseUrl),
|
|
293
|
+
fields,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
catch (error) {
|
|
297
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
298
|
+
this.logger.warn(`Credential rejected by ${integration.key}: ${redact(message, key)}`);
|
|
299
|
+
// A transport failure is not the provider saying no, and telling the
|
|
300
|
+
// caller to check their key would be wrong.
|
|
301
|
+
if (looksLikeTransportFailure(error)) {
|
|
302
|
+
throw new BadGatewayException(`Could not reach ${integration.key} to check the credential.`);
|
|
303
|
+
}
|
|
304
|
+
throw new BadRequestException(`${integration.key} rejected that credential.`);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
142
307
|
/**
|
|
143
308
|
* Writes the credential, refusing when one is already there.
|
|
144
309
|
*
|
|
@@ -146,13 +311,12 @@ let ConnectService = ConnectService_1 = class ConnectService {
|
|
|
146
311
|
* connected, with no trace. Reconnecting means revoking first, which is a
|
|
147
312
|
* deliberate act by someone who can see what they are replacing.
|
|
148
313
|
*/
|
|
149
|
-
async writeCredential(
|
|
150
|
-
const
|
|
151
|
-
const
|
|
152
|
-
const accessTokenExpiresAt = expiryOf(envelope);
|
|
314
|
+
async writeCredential(target, integration, input) {
|
|
315
|
+
const accessToken = this.crypto.sealEnvelope(input.envelope);
|
|
316
|
+
const accessTokenExpiresAt = expiryOf(input.envelope);
|
|
153
317
|
// Read before writing, so a second connect is a clear refusal rather than
|
|
154
318
|
// the unique index surfacing as a 500. The index is still the backstop.
|
|
155
|
-
const existing = await this.store.findConnection(integration,
|
|
319
|
+
const existing = await this.store.findConnection(integration, target.environmentId, target.configurationId, input.userId);
|
|
156
320
|
if (existing) {
|
|
157
321
|
throw new ConflictException('This person is already connected to this configuration. Revoke that connection first.');
|
|
158
322
|
}
|
|
@@ -162,12 +326,12 @@ let ConnectService = ConnectService_1 = class ConnectService {
|
|
|
162
326
|
accountId: input.accountId,
|
|
163
327
|
accessToken,
|
|
164
328
|
accessTokenExpiresAt,
|
|
165
|
-
scope: input.
|
|
329
|
+
scope: input.scope ?? null,
|
|
166
330
|
}, (tx, accountId) => this.store.storeConnection(tx, integration, {
|
|
167
331
|
accountId,
|
|
168
332
|
userId: input.userId,
|
|
169
|
-
environmentId:
|
|
170
|
-
configurationId:
|
|
333
|
+
environmentId: target.environmentId,
|
|
334
|
+
configurationId: target.configurationId,
|
|
171
335
|
metadata: input.metadata,
|
|
172
336
|
}));
|
|
173
337
|
}
|
|
@@ -180,6 +344,7 @@ let ConnectService = ConnectService_1 = class ConnectService {
|
|
|
180
344
|
});
|
|
181
345
|
}
|
|
182
346
|
envelopeFor(tokens) {
|
|
347
|
+
const secondary = storedSecondary(tokens);
|
|
183
348
|
return {
|
|
184
349
|
v: ENVELOPE_VERSION,
|
|
185
350
|
type: 'oauth2',
|
|
@@ -191,21 +356,21 @@ let ConnectService = ConnectService_1 = class ConnectService {
|
|
|
191
356
|
expiresAt: new Date(Date.now() + tokens.expiresInSeconds * 1000).toISOString(),
|
|
192
357
|
}
|
|
193
358
|
: {}),
|
|
359
|
+
...(secondary ? { secondary } : {}),
|
|
194
360
|
};
|
|
195
361
|
}
|
|
196
362
|
clientContextFor(integration, configuration) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
redirectUri: `${publicUrl}/${globalPrefix}/connect/callback/${integration.key}`,
|
|
363
|
+
return clientContextFor({
|
|
364
|
+
integration,
|
|
365
|
+
configuration,
|
|
366
|
+
credentials: configuration.credentials
|
|
367
|
+
? JSON.parse(this.crypto.decrypt(configuration.credentials))
|
|
368
|
+
: {},
|
|
369
|
+
server: this.config.server,
|
|
205
370
|
// Declared by the package. An operator does not pick these, so there is
|
|
206
371
|
// nothing stored to drift from what the provider's console allows.
|
|
207
372
|
scopes: integration.auth.scopesRequested,
|
|
208
|
-
};
|
|
373
|
+
});
|
|
209
374
|
}
|
|
210
375
|
async resolveConfiguration(integration, environmentId, input) {
|
|
211
376
|
const slug = input.configurationSlug?.trim();
|
|
@@ -240,3 +405,79 @@ ConnectService = ConnectService_1 = __decorate([
|
|
|
240
405
|
IntegrationRegistry, Object])
|
|
241
406
|
], ConnectService);
|
|
242
407
|
export { ConnectService };
|
|
408
|
+
/** An auth type with no redirect has no connect flow of this shape to run. */
|
|
409
|
+
function redirectStrategyOf(integration) {
|
|
410
|
+
if (!isRedirectStrategy(integration.auth)) {
|
|
411
|
+
throw new BadRequestException(`${integration.key} does not send the end user to the provider, so it has no connect link.`);
|
|
412
|
+
}
|
|
413
|
+
return integration.auth;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* The one secret to store. The SDK hands a provider a single `accessToken`,
|
|
417
|
+
* so a provider asking for more than one value has nowhere to put the rest.
|
|
418
|
+
*/
|
|
419
|
+
/**
|
|
420
|
+
* What the end user supplied, checked against what the package declared.
|
|
421
|
+
*
|
|
422
|
+
* A key nothing declared is refused rather than ignored: silently dropping
|
|
423
|
+
* half of what somebody typed is how a connection is stored that cannot work.
|
|
424
|
+
*/
|
|
425
|
+
function suppliedCredential(integration, credential) {
|
|
426
|
+
const fields = integration.auth.credentialFields ?? [];
|
|
427
|
+
if (fields.length === 0) {
|
|
428
|
+
throw new BadRequestException(`${integration.key} declares no credential for an end user to supply.`);
|
|
429
|
+
}
|
|
430
|
+
const declared = new Set(fields.map((field) => field.key));
|
|
431
|
+
const unknown = Object.keys(credential ?? {}).filter((key) => !declared.has(key));
|
|
432
|
+
if (unknown.length > 0) {
|
|
433
|
+
throw new BadRequestException(`${integration.key} declares no credential field named ${unknown.join(' or ')}`);
|
|
434
|
+
}
|
|
435
|
+
const supplied = {};
|
|
436
|
+
for (const field of fields) {
|
|
437
|
+
const value = credential?.[field.key];
|
|
438
|
+
if (typeof value !== 'string' || value.trim() === '') {
|
|
439
|
+
if (field.required !== false) {
|
|
440
|
+
throw new BadRequestException(`credential.${field.key} is required`);
|
|
441
|
+
}
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
supplied[field.key] = value.trim();
|
|
445
|
+
}
|
|
446
|
+
return supplied;
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* What `fetchAccount` is given to prove the credential, which is the same
|
|
450
|
+
* string the proxy will later sign requests with.
|
|
451
|
+
*/
|
|
452
|
+
function usableSecret(envelope) {
|
|
453
|
+
return envelope.type === 'basic'
|
|
454
|
+
? Buffer.from(`${envelope.username}:${envelope.password}`).toString('base64')
|
|
455
|
+
: envelope.type === 'api_key'
|
|
456
|
+
? envelope.key
|
|
457
|
+
: '';
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* The envelope a supplied credential becomes, which is the auth type's answer
|
|
461
|
+
* rather than the schema's.
|
|
462
|
+
*/
|
|
463
|
+
function envelopeForCredential(integration, supplied) {
|
|
464
|
+
if (integration.auth.type === 'BASIC') {
|
|
465
|
+
return {
|
|
466
|
+
v: ENVELOPE_VERSION,
|
|
467
|
+
type: 'basic',
|
|
468
|
+
username: supplied['username'] ?? '',
|
|
469
|
+
password: supplied['password'] ?? '',
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
// Everything else hands over one secret, whatever the package calls it.
|
|
473
|
+
const [only] = Object.values(supplied);
|
|
474
|
+
return { v: ENVELOPE_VERSION, type: 'api_key', key: only ?? '' };
|
|
475
|
+
}
|
|
476
|
+
/** `fetch` reports a host it never reached as a TypeError with a cause. */
|
|
477
|
+
function looksLikeTransportFailure(error) {
|
|
478
|
+
return error instanceof TypeError;
|
|
479
|
+
}
|
|
480
|
+
/** Keeps the key out of the log, by value rather than by trusting call sites. */
|
|
481
|
+
function redact(message, key) {
|
|
482
|
+
return key ? message.split(key).join('[redacted]') : message;
|
|
483
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect-credential.dto.d.ts","sourceRoot":"","sources":["../../../src/connect/dto/connect-credential.dto.ts"],"names":[],"mappings":"AASA,qBAAa,wBAAwB;IAQnC,cAAc,EAAG,MAAM,CAAC;IAQxB,QAAQ,EAAG,MAAM,CAAC;IAQlB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAO3B,eAAe,CAAC,EAAE,MAAM,CAAC;IAQzB,UAAU,EAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACtC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
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 ConnectWithCredentialDto {
|
|
5
|
+
externalUserId;
|
|
6
|
+
provider;
|
|
7
|
+
configurationSlug;
|
|
8
|
+
configurationId;
|
|
9
|
+
credential;
|
|
10
|
+
}
|
|
11
|
+
__decorate([
|
|
12
|
+
ApiProperty({
|
|
13
|
+
example: 'user_9',
|
|
14
|
+
description: 'The consuming app’s own id for the person connecting.',
|
|
15
|
+
}),
|
|
16
|
+
IsString(),
|
|
17
|
+
MinLength(1),
|
|
18
|
+
MaxLength(255),
|
|
19
|
+
__metadata("design:type", String)
|
|
20
|
+
], ConnectWithCredentialDto.prototype, "externalUserId", void 0);
|
|
21
|
+
__decorate([
|
|
22
|
+
ApiProperty({
|
|
23
|
+
example: 'apollo',
|
|
24
|
+
description: 'Which provider. Must be one whose end user holds the key.',
|
|
25
|
+
}),
|
|
26
|
+
IsString(),
|
|
27
|
+
MinLength(1),
|
|
28
|
+
__metadata("design:type", String)
|
|
29
|
+
], ConnectWithCredentialDto.prototype, "provider", void 0);
|
|
30
|
+
__decorate([
|
|
31
|
+
ApiPropertyOptional({
|
|
32
|
+
example: 'apollo-main',
|
|
33
|
+
description: 'Configuration slug, as named in this environment.',
|
|
34
|
+
}),
|
|
35
|
+
IsOptional(),
|
|
36
|
+
IsString(),
|
|
37
|
+
__metadata("design:type", String)
|
|
38
|
+
], ConnectWithCredentialDto.prototype, "configurationSlug", void 0);
|
|
39
|
+
__decorate([
|
|
40
|
+
ApiPropertyOptional({
|
|
41
|
+
description: 'Configuration id, instead of the slug.',
|
|
42
|
+
}),
|
|
43
|
+
IsOptional(),
|
|
44
|
+
IsString(),
|
|
45
|
+
__metadata("design:type", String)
|
|
46
|
+
], ConnectWithCredentialDto.prototype, "configurationId", void 0);
|
|
47
|
+
__decorate([
|
|
48
|
+
ApiProperty({
|
|
49
|
+
description: 'What the end user supplied, keyed by the provider’s credentialFields.',
|
|
50
|
+
example: { credential: 'ap_live_…' },
|
|
51
|
+
}),
|
|
52
|
+
IsObject(),
|
|
53
|
+
__metadata("design:type", Object)
|
|
54
|
+
], ConnectWithCredentialDto.prototype, "credential", void 0);
|
|
@@ -8,4 +8,11 @@ export declare class CreateTestSessionDto {
|
|
|
8
8
|
configurationId?: string;
|
|
9
9
|
redirectUrl?: string;
|
|
10
10
|
}
|
|
11
|
+
/** The same, for a provider whose credential the operator pastes in. */
|
|
12
|
+
export declare class TestCredentialDto {
|
|
13
|
+
provider: string;
|
|
14
|
+
slug?: string;
|
|
15
|
+
configurationId?: string;
|
|
16
|
+
credential: Record<string, unknown>;
|
|
17
|
+
}
|
|
11
18
|
//# sourceMappingURL=create-test-session.dto.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-test-session.dto.d.ts","sourceRoot":"","sources":["../../../src/connect/dto/create-test-session.dto.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,qBAAa,oBAAoB;IAI/B,QAAQ,EAAG,MAAM,CAAC;IAKlB,IAAI,CAAC,EAAE,MAAM,CAAC;IAOd,eAAe,CAAC,EAAE,MAAM,CAAC;IAOzB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB"}
|
|
1
|
+
{"version":3,"file":"create-test-session.dto.d.ts","sourceRoot":"","sources":["../../../src/connect/dto/create-test-session.dto.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,qBAAa,oBAAoB;IAI/B,QAAQ,EAAG,MAAM,CAAC;IAKlB,IAAI,CAAC,EAAE,MAAM,CAAC;IAOd,eAAe,CAAC,EAAE,MAAM,CAAC;IAOzB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,wEAAwE;AACxE,qBAAa,iBAAiB;IAI5B,QAAQ,EAAG,MAAM,CAAC;IAKlB,IAAI,CAAC,EAAE,MAAM,CAAC;IAOd,eAAe,CAAC,EAAE,MAAM,CAAC;IAOzB,UAAU,EAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACtC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { __decorate, __metadata } from "tslib";
|
|
2
2
|
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
|
3
|
-
import { IsOptional, IsString, MinLength } from 'class-validator';
|
|
3
|
+
import { IsObject, IsOptional, IsString, MinLength } from 'class-validator';
|
|
4
4
|
/**
|
|
5
5
|
* The admin standing in for an end user, so an operator can verify a
|
|
6
6
|
* configuration without wiring up their backend first.
|
|
@@ -39,3 +39,38 @@ __decorate([
|
|
|
39
39
|
IsString(),
|
|
40
40
|
__metadata("design:type", String)
|
|
41
41
|
], CreateTestSessionDto.prototype, "redirectUrl", void 0);
|
|
42
|
+
/** The same, for a provider whose credential the operator pastes in. */
|
|
43
|
+
export class TestCredentialDto {
|
|
44
|
+
provider;
|
|
45
|
+
slug;
|
|
46
|
+
configurationId;
|
|
47
|
+
credential;
|
|
48
|
+
}
|
|
49
|
+
__decorate([
|
|
50
|
+
ApiProperty({ example: 'apollo', description: 'Which provider.' }),
|
|
51
|
+
IsString(),
|
|
52
|
+
MinLength(1),
|
|
53
|
+
__metadata("design:type", String)
|
|
54
|
+
], TestCredentialDto.prototype, "provider", void 0);
|
|
55
|
+
__decorate([
|
|
56
|
+
ApiPropertyOptional({ example: 'apollo-main' }),
|
|
57
|
+
IsOptional(),
|
|
58
|
+
IsString(),
|
|
59
|
+
__metadata("design:type", String)
|
|
60
|
+
], TestCredentialDto.prototype, "slug", void 0);
|
|
61
|
+
__decorate([
|
|
62
|
+
ApiPropertyOptional({
|
|
63
|
+
description: 'Configuration id, instead of the slug.',
|
|
64
|
+
}),
|
|
65
|
+
IsOptional(),
|
|
66
|
+
IsString(),
|
|
67
|
+
__metadata("design:type", String)
|
|
68
|
+
], TestCredentialDto.prototype, "configurationId", void 0);
|
|
69
|
+
__decorate([
|
|
70
|
+
ApiProperty({
|
|
71
|
+
description: 'Keyed by the provider’s credentialFields.',
|
|
72
|
+
example: { credential: 'ap_live_…' },
|
|
73
|
+
}),
|
|
74
|
+
IsObject(),
|
|
75
|
+
__metadata("design:type", Object)
|
|
76
|
+
], TestCredentialDto.prototype, "credential", void 0);
|
|
@@ -33,10 +33,16 @@ export declare class CredentialsService {
|
|
|
33
33
|
* the whole consent flow again for a token the provider was willing to
|
|
34
34
|
* renew.
|
|
35
35
|
*/
|
|
36
|
-
resolve(account: Account, integration: KoshaIntegration, configuration: ProviderConfiguration): Promise<ResolvedCredential>;
|
|
36
|
+
resolve(account: Account, integration: KoshaIntegration, configuration: ProviderConfiguration, tokenKind?: string): Promise<ResolvedCredential>;
|
|
37
37
|
private refresh;
|
|
38
|
-
/**
|
|
39
|
-
|
|
38
|
+
/**
|
|
39
|
+
* A secondary token expires and refreshes on its own, with its own refresh
|
|
40
|
+
* token. Renewing it leaves the primary, and its expiry, as they were.
|
|
41
|
+
*/
|
|
42
|
+
private resolveSecondary;
|
|
43
|
+
/** The refresh_token grant, for whichever stored token is being renewed. */
|
|
44
|
+
private requestRefresh;
|
|
45
|
+
private refreshFailed;
|
|
40
46
|
private openCredentials;
|
|
41
47
|
/**
|
|
42
48
|
* What an outbound request needs, whatever the credential's shape. A
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"credentials.service.d.ts","sourceRoot":"","sources":["../../src/credentials/credentials.service.ts"],"names":[],"mappings":"
|
|
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;IA6CrB;;;OAGG;YACW,gBAAgB;IA6D9B,4EAA4E;YAC9D,cAAc;IAsB5B,OAAO,CAAC,aAAa;IAcrB,OAAO,CAAC,eAAe;IAUvB;;;;OAIG;IACH,OAAO,CAAC,MAAM;YAgDA,qBAAqB;CAoBpC"}
|