@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.
- package/dist/auth/api-key.guard.d.ts +7 -2
- package/dist/auth/api-key.guard.d.ts.map +1 -1
- package/dist/auth/api-key.guard.js +28 -8
- package/dist/auth/auth.factory.d.ts +15 -1
- package/dist/auth/auth.factory.d.ts.map +1 -1
- package/dist/auth/auth.factory.js +43 -2
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +24 -6
- package/dist/configurations/configurations.controller.d.ts +12 -136
- package/dist/configurations/configurations.controller.d.ts.map +1 -1
- package/dist/configurations/configurations.controller.js +65 -28
- package/dist/configurations/configurations.module.d.ts.map +1 -1
- package/dist/configurations/configurations.module.js +2 -0
- package/dist/configurations/configurations.service.d.ts +32 -200
- package/dist/configurations/configurations.service.d.ts.map +1 -1
- package/dist/configurations/configurations.service.js +98 -170
- package/dist/configurations/dto/create-configuration.dto.d.ts +7 -0
- package/dist/configurations/dto/create-configuration.dto.d.ts.map +1 -0
- package/dist/configurations/dto/create-configuration.dto.js +44 -0
- package/dist/configurations/dto/update-configuration.dto.d.ts +5 -0
- package/dist/configurations/dto/update-configuration.dto.d.ts.map +1 -0
- package/dist/configurations/dto/update-configuration.dto.js +23 -0
- package/dist/connect/connect.controller.d.ts +6 -8
- package/dist/connect/connect.controller.d.ts.map +1 -1
- package/dist/connect/connect.controller.js +46 -15
- package/dist/connect/connect.module.d.ts.map +1 -1
- package/dist/connect/connect.module.js +4 -3
- package/dist/connect/connect.repository.d.ts +78 -0
- package/dist/connect/connect.repository.d.ts.map +1 -0
- package/dist/connect/connect.repository.js +134 -0
- package/dist/connect/connect.service.d.ts +29 -21
- package/dist/connect/connect.service.d.ts.map +1 -1
- package/dist/connect/connect.service.js +157 -168
- package/dist/connect/dto/create-connect-session.dto.d.ts +8 -0
- package/dist/connect/dto/create-connect-session.dto.d.ts.map +1 -0
- package/dist/connect/dto/create-connect-session.dto.js +54 -0
- package/dist/connect/dto/create-test-session.dto.d.ts +11 -0
- package/dist/connect/dto/create-test-session.dto.d.ts.map +1 -0
- package/dist/connect/dto/create-test-session.dto.js +41 -0
- package/dist/connections/connections.controller.d.ts +10 -62
- package/dist/connections/connections.controller.d.ts.map +1 -1
- package/dist/connections/connections.controller.js +41 -17
- package/dist/connections/connections.module.d.ts.map +1 -1
- package/dist/connections/connections.module.js +4 -1
- package/dist/connections/connections.repository.d.ts +31 -0
- package/dist/connections/connections.repository.d.ts.map +1 -0
- package/dist/connections/connections.repository.js +44 -0
- package/dist/connections/connections.service.d.ts +58 -68
- package/dist/connections/connections.service.d.ts.map +1 -1
- package/dist/connections/connections.service.js +137 -55
- package/dist/connections/dto/list-connections.dto.d.ts +7 -0
- package/dist/connections/dto/list-connections.dto.d.ts.map +1 -0
- package/dist/connections/dto/list-connections.dto.js +47 -0
- package/dist/connections/environment-connections.controller.d.ts +27 -0
- package/dist/connections/environment-connections.controller.d.ts.map +1 -0
- package/dist/connections/environment-connections.controller.js +83 -0
- package/dist/connections/environment-connections.module.d.ts +11 -0
- package/dist/connections/environment-connections.module.d.ts.map +1 -0
- package/dist/connections/environment-connections.module.js +21 -0
- package/dist/credentials/credentials.module.d.ts.map +1 -1
- package/dist/credentials/credentials.module.js +2 -4
- package/dist/credentials/credentials.repository.d.ts +11 -0
- package/dist/credentials/credentials.repository.d.ts.map +1 -0
- package/dist/credentials/credentials.repository.js +28 -0
- package/dist/credentials/credentials.service.d.ts +25 -31
- package/dist/credentials/credentials.service.d.ts.map +1 -1
- package/dist/credentials/credentials.service.js +100 -62
- package/dist/crypto/credential-envelope.d.ts +56 -0
- package/dist/crypto/credential-envelope.d.ts.map +1 -0
- package/dist/crypto/credential-envelope.js +70 -0
- package/dist/crypto/crypto.service.d.ts +8 -0
- package/dist/crypto/crypto.service.d.ts.map +1 -1
- package/dist/crypto/crypto.service.js +12 -0
- package/dist/egress/dto/mint-grant.dto.d.ts +7 -0
- package/dist/egress/dto/mint-grant.dto.d.ts.map +1 -0
- package/dist/egress/dto/mint-grant.dto.js +44 -0
- package/dist/egress/egress.controller.d.ts +2 -8
- package/dist/egress/egress.controller.d.ts.map +1 -1
- package/dist/egress/egress.controller.js +27 -6
- package/dist/egress/egress.listener.d.ts.map +1 -1
- package/dist/egress/egress.listener.js +6 -6
- package/dist/egress/egress.module.d.ts.map +1 -1
- package/dist/egress/egress.module.js +10 -2
- package/dist/egress/egress.repository.d.ts +65 -0
- package/dist/egress/egress.repository.d.ts.map +1 -0
- package/dist/egress/egress.repository.js +103 -0
- package/dist/egress/grants.service.d.ts +23 -78
- package/dist/egress/grants.service.d.ts.map +1 -1
- package/dist/egress/grants.service.js +44 -104
- package/dist/environments/dto/create-environment.dto.d.ts +6 -0
- package/dist/environments/dto/create-environment.dto.d.ts.map +1 -0
- package/dist/environments/dto/create-environment.dto.js +34 -0
- package/dist/environments/dto/update-environment.dto.d.ts +5 -0
- package/dist/environments/dto/update-environment.dto.d.ts.map +1 -0
- package/dist/environments/dto/update-environment.dto.js +24 -0
- package/dist/environments/environments.controller.d.ts +53 -0
- package/dist/environments/environments.controller.d.ts.map +1 -0
- package/dist/environments/environments.controller.js +101 -0
- package/dist/environments/environments.module.d.ts +3 -0
- package/dist/environments/environments.module.d.ts.map +1 -0
- package/dist/environments/environments.module.js +15 -0
- package/dist/environments/environments.repository.d.ts +37 -0
- package/dist/environments/environments.repository.d.ts.map +1 -0
- package/dist/environments/environments.repository.js +82 -0
- package/dist/environments/environments.service.d.ts +43 -0
- package/dist/environments/environments.service.d.ts.map +1 -0
- package/dist/environments/environments.service.js +124 -0
- package/dist/generated/prisma/browser.d.ts +11 -39
- package/dist/generated/prisma/browser.d.ts.map +1 -1
- package/dist/generated/prisma/client.d.ts +11 -39
- package/dist/generated/prisma/client.d.ts.map +1 -1
- package/dist/generated/prisma/commonInputTypes.d.ts +50 -146
- package/dist/generated/prisma/commonInputTypes.d.ts.map +1 -1
- package/dist/generated/prisma/enums.d.ts +1 -21
- package/dist/generated/prisma/enums.d.ts.map +1 -1
- package/dist/generated/prisma/enums.js +1 -18
- package/dist/generated/prisma/internal/class.d.ts +16 -60
- package/dist/generated/prisma/internal/class.d.ts.map +1 -1
- package/dist/generated/prisma/internal/class.js +4 -4
- package/dist/generated/prisma/internal/prismaNamespace.d.ts +166 -583
- package/dist/generated/prisma/internal/prismaNamespace.d.ts.map +1 -1
- package/dist/generated/prisma/internal/prismaNamespace.js +25 -104
- package/dist/generated/prisma/internal/prismaNamespaceBrowser.d.ts +26 -111
- package/dist/generated/prisma/internal/prismaNamespaceBrowser.d.ts.map +1 -1
- package/dist/generated/prisma/internal/prismaNamespaceBrowser.js +25 -104
- package/dist/generated/prisma/models/EgressGrant.d.ts +131 -134
- package/dist/generated/prisma/models/EgressGrant.d.ts.map +1 -1
- package/dist/generated/prisma/models/Environment.d.ts +1386 -0
- package/dist/generated/prisma/models/Environment.d.ts.map +1 -0
- package/dist/generated/prisma/models/Invitation.d.ts +42 -1
- package/dist/generated/prisma/models/Invitation.d.ts.map +1 -1
- package/dist/generated/prisma/models/Organization.d.ts +122 -408
- package/dist/generated/prisma/models/Organization.d.ts.map +1 -1
- package/dist/generated/prisma/models/Session.d.ts +36 -1
- package/dist/generated/prisma/models/Session.d.ts.map +1 -1
- package/dist/generated/prisma/models/Team.d.ts +1498 -0
- package/dist/generated/prisma/models/Team.d.ts.map +1 -0
- package/dist/generated/prisma/models/TeamMember.d.ts +1281 -0
- package/dist/generated/prisma/models/TeamMember.d.ts.map +1 -0
- package/dist/generated/prisma/models/User.d.ts +278 -0
- package/dist/generated/prisma/models/User.d.ts.map +1 -1
- package/dist/generated/prisma/models.d.ts +3 -7
- package/dist/generated/prisma/models.d.ts.map +1 -1
- package/dist/index.d.ts +14 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -2
- package/dist/integrations/integrations.controller.d.ts +4 -1
- package/dist/integrations/integrations.controller.d.ts.map +1 -1
- package/dist/integrations/integrations.controller.js +1 -0
- package/dist/integrations/integrations.module.js +3 -3
- package/dist/integrations/provider-store.d.ts +104 -0
- package/dist/integrations/provider-store.d.ts.map +1 -0
- package/dist/integrations/provider-store.js +234 -0
- package/dist/kosha.module.d.ts.map +1 -1
- package/dist/kosha.module.js +9 -3
- package/dist/proxy/proxy.controller.d.ts +12 -3
- package/dist/proxy/proxy.controller.d.ts.map +1 -1
- package/dist/proxy/proxy.controller.js +31 -10
- package/dist/proxy/proxy.module.d.ts.map +1 -1
- package/dist/proxy/proxy.module.js +4 -2
- package/dist/proxy/proxy.repository.d.ts +21 -0
- package/dist/proxy/proxy.repository.d.ts.map +1 -0
- package/dist/proxy/proxy.repository.js +37 -0
- package/dist/proxy/proxy.service.d.ts +15 -5
- package/dist/proxy/proxy.service.d.ts.map +1 -1
- package/dist/proxy/proxy.service.js +63 -26
- package/package.json +7 -4
- package/prisma/migrations/{20260908000000_init → 20261008090000_init}/migration.sql +99 -16
- package/prisma/schema.prisma +56 -202
- package/dist/generated/prisma/models/Configuration.d.ts +0 -2018
- package/dist/generated/prisma/models/Configuration.d.ts.map +0 -1
- package/dist/generated/prisma/models/ConnectSession.d.ts +0 -1606
- package/dist/generated/prisma/models/ConnectSession.d.ts.map +0 -1
- package/dist/generated/prisma/models/Connection.d.ts +0 -1949
- package/dist/generated/prisma/models/Connection.d.ts.map +0 -1
- package/dist/generated/prisma/models/ConnectionCredential.d.ts +0 -1293
- package/dist/generated/prisma/models/ConnectionCredential.d.ts.map +0 -1
- package/dist/generated/prisma/models/ConnectionCredential.js +0 -1
- package/dist/generated/prisma/models/EndUser.d.ts +0 -1666
- package/dist/generated/prisma/models/EndUser.d.ts.map +0 -1
- package/dist/generated/prisma/models/EndUser.js +0 -1
- package/dist/generated/prisma/models/OAuthState.d.ts +0 -1404
- package/dist/generated/prisma/models/OAuthState.d.ts.map +0 -1
- package/dist/generated/prisma/models/OAuthState.js +0 -1
- package/dist/generated/prisma/models/Provider.d.ts +0 -1462
- package/dist/generated/prisma/models/Provider.d.ts.map +0 -1
- package/dist/generated/prisma/models/Provider.js +0 -1
- package/dist/integrations/provider-sync.service.d.ts +0 -24
- package/dist/integrations/provider-sync.service.d.ts.map +0 -1
- package/dist/integrations/provider-sync.service.js +0 -74
- package/prisma/migrations/20260908161813_add_connect_domain/migration.sql +0 -173
- package/prisma/migrations/20260916000000_add_configuration_slug/migration.sql +0 -15
- package/prisma/migrations/20260921000000_add_egress_grant/migration.sql +0 -31
- /package/dist/generated/prisma/models/{Configuration.js → Environment.js} +0 -0
- /package/dist/generated/prisma/models/{ConnectSession.js → Team.js} +0 -0
- /package/dist/generated/prisma/models/{Connection.js → TeamMember.js} +0 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
2
|
+
import { Controller, Delete, Get, Param, Query } from '@nestjs/common';
|
|
3
|
+
import { ApiNotFoundResponse, ApiOkResponse, ApiOperation, ApiTags, } from '@nestjs/swagger';
|
|
4
|
+
import { Session } from '@thallesp/nestjs-better-auth';
|
|
5
|
+
import { getActiveOrganizationId } from '../common/active-organization.js';
|
|
6
|
+
import { ConnectionsService } from './connections.service.js';
|
|
7
|
+
import { ListConnectionsDto } from './dto/list-connections.dto.js';
|
|
8
|
+
/**
|
|
9
|
+
* The admin's view of connections, nested under the environment they belong
|
|
10
|
+
* to — the same shape as `ConfigurationsController`.
|
|
11
|
+
*
|
|
12
|
+
* Separate from `ConnectionsController` because the two have different
|
|
13
|
+
* audiences and different authorization rules. That one serves a consuming
|
|
14
|
+
* app's backend over `x-api-key`, where the key names the environment. This
|
|
15
|
+
* serves a signed-in operator, whose teams decide which environments they may
|
|
16
|
+
* reach. Keeping them apart means the rule is chosen by the route rather than
|
|
17
|
+
* by a branch inside a handler, so a later route cannot quietly pick up the
|
|
18
|
+
* wrong one.
|
|
19
|
+
*/
|
|
20
|
+
let EnvironmentConnectionsController = class EnvironmentConnectionsController {
|
|
21
|
+
connections;
|
|
22
|
+
constructor(connections) {
|
|
23
|
+
this.connections = connections;
|
|
24
|
+
}
|
|
25
|
+
list(session, environmentId, filters) {
|
|
26
|
+
return this.connections.listForMember(getActiveOrganizationId(session), session.user.id, environmentId, filters);
|
|
27
|
+
}
|
|
28
|
+
get(session, environmentId, provider, id) {
|
|
29
|
+
return this.connections.getForMember(getActiveOrganizationId(session), session.user.id, environmentId, provider, id);
|
|
30
|
+
}
|
|
31
|
+
revoke(session, environmentId, provider, id) {
|
|
32
|
+
return this.connections.revokeForMember(getActiveOrganizationId(session), session.user.id, environmentId, provider, id);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
__decorate([
|
|
36
|
+
Get(),
|
|
37
|
+
ApiOperation({
|
|
38
|
+
summary: 'List the connections in one environment',
|
|
39
|
+
description: 'Returns connections from every installed provider, newest first. Tokens and secrets are never included.',
|
|
40
|
+
}),
|
|
41
|
+
ApiOkResponse({ description: 'Connections in this environment.' }),
|
|
42
|
+
__param(0, Session()),
|
|
43
|
+
__param(1, Param('environmentId')),
|
|
44
|
+
__param(2, Query()),
|
|
45
|
+
__metadata("design:type", Function),
|
|
46
|
+
__metadata("design:paramtypes", [Object, String, ListConnectionsDto]),
|
|
47
|
+
__metadata("design:returntype", void 0)
|
|
48
|
+
], EnvironmentConnectionsController.prototype, "list", null);
|
|
49
|
+
__decorate([
|
|
50
|
+
Get(':provider/:id'),
|
|
51
|
+
ApiOperation({ summary: 'Get one connection' }),
|
|
52
|
+
ApiNotFoundResponse({
|
|
53
|
+
description: 'No such connection in an environment you can reach.',
|
|
54
|
+
}),
|
|
55
|
+
__param(0, Session()),
|
|
56
|
+
__param(1, Param('environmentId')),
|
|
57
|
+
__param(2, Param('provider')),
|
|
58
|
+
__param(3, Param('id')),
|
|
59
|
+
__metadata("design:type", Function),
|
|
60
|
+
__metadata("design:paramtypes", [Object, String, String, String]),
|
|
61
|
+
__metadata("design:returntype", void 0)
|
|
62
|
+
], EnvironmentConnectionsController.prototype, "get", null);
|
|
63
|
+
__decorate([
|
|
64
|
+
Delete(':provider/:id'),
|
|
65
|
+
ApiOperation({
|
|
66
|
+
summary: 'Revoke a connection',
|
|
67
|
+
description: 'Deletes the credential and the provider’s connection row. Kosha logins are never affected.',
|
|
68
|
+
}),
|
|
69
|
+
ApiOkResponse({ description: 'The revoked connection id.' }),
|
|
70
|
+
__param(0, Session()),
|
|
71
|
+
__param(1, Param('environmentId')),
|
|
72
|
+
__param(2, Param('provider')),
|
|
73
|
+
__param(3, Param('id')),
|
|
74
|
+
__metadata("design:type", Function),
|
|
75
|
+
__metadata("design:paramtypes", [Object, String, String, String]),
|
|
76
|
+
__metadata("design:returntype", void 0)
|
|
77
|
+
], EnvironmentConnectionsController.prototype, "revoke", null);
|
|
78
|
+
EnvironmentConnectionsController = __decorate([
|
|
79
|
+
ApiTags('connections'),
|
|
80
|
+
Controller('environments/:environmentId/connections'),
|
|
81
|
+
__metadata("design:paramtypes", [ConnectionsService])
|
|
82
|
+
], EnvironmentConnectionsController);
|
|
83
|
+
export { EnvironmentConnectionsController };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The admin's connections routes, beside the machine-facing ones.
|
|
3
|
+
*
|
|
4
|
+
* A module rather than a second controller inside `ConnectionsModule`: the two
|
|
5
|
+
* serve different audiences under different authentication, and keeping them
|
|
6
|
+
* in separate modules is what a Rails namespace would be. The service and
|
|
7
|
+
* repository are shared, so nothing is duplicated but the routing.
|
|
8
|
+
*/
|
|
9
|
+
export declare class EnvironmentConnectionsModule {
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=environment-connections.module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"environment-connections.module.d.ts","sourceRoot":"","sources":["../../src/connections/environment-connections.module.ts"],"names":[],"mappings":"AAIA;;;;;;;GAOG;AACH,qBAIa,4BAA4B;CAAG"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { Module } from '@nestjs/common';
|
|
3
|
+
import { ConnectionsModule } from './connections.module.js';
|
|
4
|
+
import { EnvironmentConnectionsController } from './environment-connections.controller.js';
|
|
5
|
+
/**
|
|
6
|
+
* The admin's connections routes, beside the machine-facing ones.
|
|
7
|
+
*
|
|
8
|
+
* A module rather than a second controller inside `ConnectionsModule`: the two
|
|
9
|
+
* serve different audiences under different authentication, and keeping them
|
|
10
|
+
* in separate modules is what a Rails namespace would be. The service and
|
|
11
|
+
* repository are shared, so nothing is duplicated but the routing.
|
|
12
|
+
*/
|
|
13
|
+
let EnvironmentConnectionsModule = class EnvironmentConnectionsModule {
|
|
14
|
+
};
|
|
15
|
+
EnvironmentConnectionsModule = __decorate([
|
|
16
|
+
Module({
|
|
17
|
+
imports: [ConnectionsModule],
|
|
18
|
+
controllers: [EnvironmentConnectionsController],
|
|
19
|
+
})
|
|
20
|
+
], EnvironmentConnectionsModule);
|
|
21
|
+
export { EnvironmentConnectionsModule };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"credentials.module.d.ts","sourceRoot":"","sources":["../../src/credentials/credentials.module.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"credentials.module.d.ts","sourceRoot":"","sources":["../../src/credentials/credentials.module.ts"],"names":[],"mappings":"AAIA,qBAIa,iBAAiB;CAAG"}
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { __decorate } from "tslib";
|
|
2
2
|
import { Module } from '@nestjs/common';
|
|
3
|
-
import {
|
|
4
|
-
import { ConnectModule } from '../connect/connect.module.js';
|
|
3
|
+
import { CredentialsRepository } from './credentials.repository.js';
|
|
5
4
|
import { CredentialsService } from './credentials.service.js';
|
|
6
5
|
let CredentialsModule = class CredentialsModule {
|
|
7
6
|
};
|
|
8
7
|
CredentialsModule = __decorate([
|
|
9
8
|
Module({
|
|
10
|
-
|
|
11
|
-
providers: [CredentialsService],
|
|
9
|
+
providers: [CredentialsService, CredentialsRepository],
|
|
12
10
|
exports: [CredentialsService],
|
|
13
11
|
})
|
|
14
12
|
], CredentialsModule);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { PrismaService } from '../prisma/prisma.service.js';
|
|
2
|
+
export declare class CredentialsRepository {
|
|
3
|
+
private readonly prisma;
|
|
4
|
+
constructor(prisma: PrismaService);
|
|
5
|
+
/**
|
|
6
|
+
* Replaces the envelope in place, after a refresh minted a new token. The
|
|
7
|
+
* grant is rewritten too: a provider may report a different one.
|
|
8
|
+
*/
|
|
9
|
+
replaceEnvelope(id: string, accessToken: string, accessTokenExpiresAt: Date | null, scope?: string | null): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=credentials.repository.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credentials.repository.d.ts","sourceRoot":"","sources":["../../src/credentials/credentials.repository.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D,qBACa,qBAAqB;IACpB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,aAAa;IAElD;;;OAGG;IACG,eAAe,CACnB,EAAE,EAAE,MAAM,EACV,WAAW,EAAE,MAAM,EACnB,oBAAoB,EAAE,IAAI,GAAG,IAAI,EACjC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,GACpB,OAAO,CAAC,IAAI,CAAC;CAUjB"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { __decorate, __metadata } from "tslib";
|
|
2
|
+
import { Injectable } from '@nestjs/common';
|
|
3
|
+
import { PrismaService } from '../prisma/prisma.service.js';
|
|
4
|
+
let CredentialsRepository = class CredentialsRepository {
|
|
5
|
+
prisma;
|
|
6
|
+
constructor(prisma) {
|
|
7
|
+
this.prisma = prisma;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Replaces the envelope in place, after a refresh minted a new token. The
|
|
11
|
+
* grant is rewritten too: a provider may report a different one.
|
|
12
|
+
*/
|
|
13
|
+
async replaceEnvelope(id, accessToken, accessTokenExpiresAt, scope) {
|
|
14
|
+
await this.prisma.account.update({
|
|
15
|
+
where: { id },
|
|
16
|
+
data: {
|
|
17
|
+
accessToken,
|
|
18
|
+
accessTokenExpiresAt,
|
|
19
|
+
...(scope === undefined ? {} : { scope }),
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
CredentialsRepository = __decorate([
|
|
25
|
+
Injectable(),
|
|
26
|
+
__metadata("design:paramtypes", [PrismaService])
|
|
27
|
+
], CredentialsRepository);
|
|
28
|
+
export { CredentialsRepository };
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import type { KoshaIntegration } from '@kosha-ai/integration-sdk';
|
|
2
|
+
import type { ResolvedKoshaConfig } from '../config/kosha-config.js';
|
|
3
|
+
import type { ProviderConfiguration } from '../integrations/provider-store.js';
|
|
3
4
|
import { CryptoService } from '../crypto/crypto.service.js';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
5
|
+
import type { Account } from '../generated/prisma/client.js';
|
|
6
|
+
import { CredentialsRepository } from './credentials.repository.js';
|
|
6
7
|
/**
|
|
7
|
-
* Turning a stored
|
|
8
|
+
* Turning a stored credential into something that can sign an outbound
|
|
9
|
+
* request.
|
|
8
10
|
*
|
|
9
11
|
* Its own service rather than a private method on the proxy, because the REST
|
|
10
12
|
* proxy and the egress broker both need this and two copies could disagree
|
|
11
|
-
* about when a
|
|
13
|
+
* about when a credential is dead.
|
|
12
14
|
*/
|
|
13
15
|
export interface ResolvedCredential {
|
|
14
16
|
accessToken: string;
|
|
@@ -17,39 +19,31 @@ export interface ResolvedCredential {
|
|
|
17
19
|
/** True when we minted a new token, so the old one is now dead. */
|
|
18
20
|
refreshed: boolean;
|
|
19
21
|
}
|
|
20
|
-
export interface ResolvableConnection {
|
|
21
|
-
id: string;
|
|
22
|
-
configurationId: string;
|
|
23
|
-
configuration: {
|
|
24
|
-
provider: {
|
|
25
|
-
key: string;
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
credential: {
|
|
29
|
-
accessToken: string;
|
|
30
|
-
refreshToken: string | null;
|
|
31
|
-
tokenType: string | null;
|
|
32
|
-
expiresAt: Date | null;
|
|
33
|
-
} | null;
|
|
34
|
-
}
|
|
35
22
|
export declare class CredentialsService {
|
|
36
|
-
private readonly prisma;
|
|
37
23
|
private readonly crypto;
|
|
38
|
-
private readonly
|
|
39
|
-
private readonly
|
|
40
|
-
private readonly configurations;
|
|
24
|
+
private readonly credentials;
|
|
25
|
+
private readonly config;
|
|
41
26
|
private readonly logger;
|
|
42
|
-
constructor(
|
|
27
|
+
constructor(crypto: CryptoService, credentials: CredentialsRepository, config: ResolvedKoshaConfig);
|
|
43
28
|
/**
|
|
44
29
|
* Returns a usable access token, refreshing first when the stored one has
|
|
45
|
-
* expired and the provider supports
|
|
30
|
+
* expired and the provider supports it.
|
|
46
31
|
*
|
|
47
|
-
* Without this an expired token is a dead end for the end user: they
|
|
48
|
-
*
|
|
32
|
+
* Without this an expired token is a dead end for the end user: they walk
|
|
33
|
+
* the whole consent flow again for a token the provider was willing to
|
|
49
34
|
* renew.
|
|
50
35
|
*/
|
|
51
|
-
|
|
36
|
+
resolve(account: Account, integration: KoshaIntegration, configuration: ProviderConfiguration): Promise<ResolvedCredential>;
|
|
37
|
+
private refresh;
|
|
38
|
+
/** Where the provider sends the end user back. */
|
|
39
|
+
private callbackUrlFor;
|
|
40
|
+
private openCredentials;
|
|
41
|
+
/**
|
|
42
|
+
* What an outbound request needs, whatever the credential's shape. A
|
|
43
|
+
* client-credentials provider carries no token of its own: the integration
|
|
44
|
+
* signs from its configuration, so there is nothing to hand it here.
|
|
45
|
+
*/
|
|
46
|
+
private usable;
|
|
52
47
|
private runTokenRefreshedHook;
|
|
53
|
-
private markExpired;
|
|
54
48
|
}
|
|
55
49
|
//# sourceMappingURL=credentials.service.d.ts.map
|
|
@@ -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":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAElE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAErE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAM/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,GACnC,OAAO,CAAC,kBAAkB,CAAC;YAyBhB,OAAO;IA4DrB,kDAAkD;IAClD,OAAO,CAAC,cAAc;IAKtB,OAAO,CAAC,eAAe;IAUvB;;;;OAIG;IACH,OAAO,CAAC,MAAM;YAqCA,qBAAqB;CAoBpC"}
|
|
@@ -1,84 +1,130 @@
|
|
|
1
1
|
var CredentialsService_1;
|
|
2
|
-
import { __decorate, __metadata } from "tslib";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
2
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
3
|
+
import { Injectable, Logger, UnauthorizedException } from '@nestjs/common';
|
|
4
|
+
import { Inject } from '@nestjs/common';
|
|
5
|
+
import { KOSHA_CONFIG } from '../config/tokens.js';
|
|
6
|
+
import { ENVELOPE_VERSION, expiryOf, } from '../crypto/credential-envelope.js';
|
|
6
7
|
import { CryptoService } from '../crypto/crypto.service.js';
|
|
7
|
-
import {
|
|
8
|
-
import { PrismaService } from '../prisma/prisma.service.js';
|
|
8
|
+
import { CredentialsRepository } from './credentials.repository.js';
|
|
9
9
|
let CredentialsService = CredentialsService_1 = class CredentialsService {
|
|
10
|
-
prisma;
|
|
11
10
|
crypto;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
configurations;
|
|
11
|
+
credentials;
|
|
12
|
+
config;
|
|
15
13
|
logger = new Logger(CredentialsService_1.name);
|
|
16
|
-
constructor(
|
|
17
|
-
this.prisma = prisma;
|
|
14
|
+
constructor(crypto, credentials, config) {
|
|
18
15
|
this.crypto = crypto;
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
this.configurations = configurations;
|
|
16
|
+
this.credentials = credentials;
|
|
17
|
+
this.config = config;
|
|
22
18
|
}
|
|
23
19
|
/**
|
|
24
20
|
* Returns a usable access token, refreshing first when the stored one has
|
|
25
|
-
* expired and the provider supports
|
|
21
|
+
* expired and the provider supports it.
|
|
26
22
|
*
|
|
27
|
-
* Without this an expired token is a dead end for the end user: they
|
|
28
|
-
*
|
|
23
|
+
* Without this an expired token is a dead end for the end user: they walk
|
|
24
|
+
* the whole consent flow again for a token the provider was willing to
|
|
29
25
|
* renew.
|
|
30
26
|
*/
|
|
31
|
-
async
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
throw new ForbiddenException('Connection has no stored credential');
|
|
27
|
+
async resolve(account, integration, configuration) {
|
|
28
|
+
if (!account.accessToken) {
|
|
29
|
+
throw new UnauthorizedException('Connection has no stored credential');
|
|
35
30
|
}
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
const integration = this.registry.get(connection.configuration.provider.key);
|
|
46
|
-
const canRefresh = typeof integration.auth.refresh === 'function' && credential.refreshToken;
|
|
31
|
+
const envelope = this.crypto.openEnvelope(account.accessToken);
|
|
32
|
+
const expiresAt = expiryOf(envelope);
|
|
33
|
+
const expired = expiresAt !== null && expiresAt <= new Date();
|
|
34
|
+
if (!expired)
|
|
35
|
+
return this.usable(envelope, expiresAt, false);
|
|
36
|
+
const refreshToken = envelope.type === 'oauth2' ? envelope.refreshToken : undefined;
|
|
37
|
+
const canRefresh = typeof integration.auth.refresh === 'function' && refreshToken;
|
|
47
38
|
if (!canRefresh) {
|
|
48
|
-
await this.markExpired(connection.id, 'Access token expired');
|
|
49
39
|
throw new UnauthorizedException('Connection token expired; the end user must reconnect');
|
|
50
40
|
}
|
|
51
|
-
|
|
41
|
+
return this.refresh(account, integration, configuration, refreshToken);
|
|
42
|
+
}
|
|
43
|
+
async refresh(account, integration, configuration, refreshToken) {
|
|
44
|
+
const credentials = this.openCredentials(configuration);
|
|
52
45
|
try {
|
|
53
46
|
const tokens = await integration.auth.refresh?.({
|
|
54
47
|
client: {
|
|
55
|
-
clientId:
|
|
56
|
-
clientSecret:
|
|
57
|
-
redirectUri:
|
|
58
|
-
|
|
59
|
-
|
|
48
|
+
clientId: String(credentials['clientId'] ?? ''),
|
|
49
|
+
clientSecret: String(credentials['clientSecret'] ?? ''),
|
|
50
|
+
redirectUri: this.callbackUrlFor(integration.key),
|
|
51
|
+
// The refresh_token grant carries no scope; the provider returns
|
|
52
|
+
// whatever the original consent granted.
|
|
53
|
+
scopes: [],
|
|
60
54
|
},
|
|
61
|
-
refreshToken
|
|
55
|
+
refreshToken,
|
|
62
56
|
});
|
|
63
57
|
if (!tokens)
|
|
64
58
|
throw new Error('Refresh returned no tokens');
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
59
|
+
const envelope = {
|
|
60
|
+
v: ENVELOPE_VERSION,
|
|
61
|
+
type: 'oauth2',
|
|
68
62
|
accessToken: tokens.accessToken,
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
63
|
+
...(tokens.refreshToken ? { refreshToken: tokens.refreshToken } : {}),
|
|
64
|
+
...(tokens.tokenType ? { tokenType: tokens.tokenType } : {}),
|
|
65
|
+
...(tokens.expiresInSeconds
|
|
66
|
+
? {
|
|
67
|
+
expiresAt: new Date(Date.now() + tokens.expiresInSeconds * 1000).toISOString(),
|
|
68
|
+
}
|
|
69
|
+
: {}),
|
|
74
70
|
};
|
|
71
|
+
const expiresAt = expiryOf(envelope);
|
|
72
|
+
await this.credentials.replaceEnvelope(account.id, this.crypto.sealEnvelope(envelope), expiresAt, tokens.scope ?? undefined);
|
|
73
|
+
await this.runTokenRefreshedHook(account.id, integration, tokens);
|
|
74
|
+
return this.usable(envelope, expiresAt, true);
|
|
75
75
|
}
|
|
76
76
|
catch (error) {
|
|
77
|
-
|
|
78
|
-
await this.markExpired(connection.id, `Token refresh failed: ${message}`);
|
|
77
|
+
this.logger.warn(`Refresh failed for ${integration.key}: ${error instanceof Error ? error.message : error}`);
|
|
79
78
|
throw new UnauthorizedException('Connection token expired and could not be refreshed; the end user must reconnect');
|
|
80
79
|
}
|
|
81
80
|
}
|
|
81
|
+
/** Where the provider sends the end user back. */
|
|
82
|
+
callbackUrlFor(provider) {
|
|
83
|
+
const { publicUrl, globalPrefix } = this.config.server;
|
|
84
|
+
return `${publicUrl}/${globalPrefix}/connect/callback/${provider}`;
|
|
85
|
+
}
|
|
86
|
+
openCredentials(configuration) {
|
|
87
|
+
if (!configuration.credentials)
|
|
88
|
+
return {};
|
|
89
|
+
return JSON.parse(this.crypto.decrypt(configuration.credentials));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* What an outbound request needs, whatever the credential's shape. A
|
|
93
|
+
* client-credentials provider carries no token of its own: the integration
|
|
94
|
+
* signs from its configuration, so there is nothing to hand it here.
|
|
95
|
+
*/
|
|
96
|
+
usable(envelope, expiresAt, refreshed) {
|
|
97
|
+
switch (envelope.type) {
|
|
98
|
+
case 'oauth2':
|
|
99
|
+
return {
|
|
100
|
+
accessToken: envelope.accessToken,
|
|
101
|
+
tokenType: envelope.tokenType ?? null,
|
|
102
|
+
expiresAt,
|
|
103
|
+
refreshed,
|
|
104
|
+
};
|
|
105
|
+
case 'api_key':
|
|
106
|
+
return {
|
|
107
|
+
accessToken: envelope.key,
|
|
108
|
+
tokenType: null,
|
|
109
|
+
expiresAt: null,
|
|
110
|
+
refreshed,
|
|
111
|
+
};
|
|
112
|
+
case 'app':
|
|
113
|
+
return {
|
|
114
|
+
accessToken: envelope.installationId,
|
|
115
|
+
tokenType: null,
|
|
116
|
+
expiresAt: null,
|
|
117
|
+
refreshed,
|
|
118
|
+
};
|
|
119
|
+
case 'oauth2_cc':
|
|
120
|
+
return {
|
|
121
|
+
accessToken: '',
|
|
122
|
+
tokenType: null,
|
|
123
|
+
expiresAt: null,
|
|
124
|
+
refreshed,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
}
|
|
82
128
|
async runTokenRefreshedHook(connectionId, integration, tokens) {
|
|
83
129
|
const hook = integration.hooks?.onTokenRefreshed;
|
|
84
130
|
if (!hook)
|
|
@@ -90,19 +136,11 @@ let CredentialsService = CredentialsService_1 = class CredentialsService {
|
|
|
90
136
|
this.logger.error(`onTokenRefreshed hook failed for "${integration.key}": ${error instanceof Error ? error.message : error}`);
|
|
91
137
|
}
|
|
92
138
|
}
|
|
93
|
-
async markExpired(connectionId, reason) {
|
|
94
|
-
await this.prisma.connection.update({
|
|
95
|
-
where: { id: connectionId },
|
|
96
|
-
data: { status: 'EXPIRED', errorMessage: reason },
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
139
|
};
|
|
100
140
|
CredentialsService = CredentialsService_1 = __decorate([
|
|
101
141
|
Injectable(),
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
ConnectService,
|
|
106
|
-
ConfigurationsService])
|
|
142
|
+
__param(2, Inject(KOSHA_CONFIG)),
|
|
143
|
+
__metadata("design:paramtypes", [CryptoService,
|
|
144
|
+
CredentialsRepository, Object])
|
|
107
145
|
], CredentialsService);
|
|
108
146
|
export { CredentialsService };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a stored credential actually is.
|
|
3
|
+
*
|
|
4
|
+
* `account.accessToken` holds one of these, encrypted — not a bare token. The
|
|
5
|
+
* database knows nothing about auth types, so the discriminator travels with
|
|
6
|
+
* the secret rather than sitting in a column, and a provider that needs
|
|
7
|
+
* different fields needs no migration.
|
|
8
|
+
*/
|
|
9
|
+
export declare const ENVELOPE_VERSION = 1;
|
|
10
|
+
interface Versioned {
|
|
11
|
+
v: typeof ENVELOPE_VERSION;
|
|
12
|
+
}
|
|
13
|
+
/** A token pair from an authorization-code flow. */
|
|
14
|
+
export interface OAuth2Envelope extends Versioned {
|
|
15
|
+
type: 'oauth2';
|
|
16
|
+
accessToken: string;
|
|
17
|
+
refreshToken?: string;
|
|
18
|
+
tokenType?: string;
|
|
19
|
+
/** ISO 8601. Mirrored into `account.accessTokenExpiresAt` on write. */
|
|
20
|
+
expiresAt?: string;
|
|
21
|
+
}
|
|
22
|
+
/** A key the end user pasted, such as Apollo's. */
|
|
23
|
+
export interface ApiKeyEnvelope extends Versioned {
|
|
24
|
+
type: 'api_key';
|
|
25
|
+
key: string;
|
|
26
|
+
}
|
|
27
|
+
/** A GitHub App install: tokens are minted per call from the installation. */
|
|
28
|
+
export interface AppEnvelope extends Versioned {
|
|
29
|
+
type: 'app';
|
|
30
|
+
installationId: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Client credentials, like Zoom's. The secret lives entirely in the
|
|
34
|
+
* configuration, so the connection records nothing but its own existence.
|
|
35
|
+
*/
|
|
36
|
+
export interface ClientCredentialsEnvelope extends Versioned {
|
|
37
|
+
type: 'oauth2_cc';
|
|
38
|
+
}
|
|
39
|
+
export type CredentialEnvelope = OAuth2Envelope | ApiKeyEnvelope | AppEnvelope | ClientCredentialsEnvelope;
|
|
40
|
+
export type CredentialEnvelopeType = CredentialEnvelope['type'];
|
|
41
|
+
export declare class InvalidEnvelopeError extends Error {
|
|
42
|
+
constructor(reason: string);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Rejects rather than guesses. A credential we cannot read is a connection the
|
|
46
|
+
* end user must redo, and that is a better outcome than sending a provider
|
|
47
|
+
* something we mis-parsed.
|
|
48
|
+
*/
|
|
49
|
+
export declare function parseEnvelope(json: string): CredentialEnvelope;
|
|
50
|
+
/**
|
|
51
|
+
* The expiry to mirror into `account.accessTokenExpiresAt`, so the refresh
|
|
52
|
+
* sweep can find expiring credentials without decrypting any of them.
|
|
53
|
+
*/
|
|
54
|
+
export declare function expiryOf(envelope: CredentialEnvelope): Date | null;
|
|
55
|
+
export {};
|
|
56
|
+
//# sourceMappingURL=credential-envelope.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential-envelope.d.ts","sourceRoot":"","sources":["../../src/crypto/credential-envelope.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,eAAO,MAAM,gBAAgB,IAAI,CAAC;AAElC,UAAU,SAAS;IACjB,CAAC,EAAE,OAAO,gBAAgB,CAAC;CAC5B;AAED,oDAAoD;AACpD,MAAM,WAAW,cAAe,SAAQ,SAAS;IAC/C,IAAI,EAAE,QAAQ,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,uEAAuE;IACvE,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,mDAAmD;AACnD,MAAM,WAAW,cAAe,SAAQ,SAAS;IAC/C,IAAI,EAAE,SAAS,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;CACb;AAED,8EAA8E;AAC9E,MAAM,WAAW,WAAY,SAAQ,SAAS;IAC5C,IAAI,EAAE,KAAK,CAAC;IACZ,cAAc,EAAE,MAAM,CAAC;CACxB;AAED;;;GAGG;AACH,MAAM,WAAW,yBAA0B,SAAQ,SAAS;IAC1D,IAAI,EAAE,WAAW,CAAC;CACnB;AAED,MAAM,MAAM,kBAAkB,GAC5B,cAAc,GAAG,cAAc,GAAG,WAAW,GAAG,yBAAyB,CAAC;AAE5E,MAAM,MAAM,sBAAsB,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;AAEhE,qBAAa,oBAAqB,SAAQ,KAAK;gBACjC,MAAM,EAAE,MAAM;CAI3B;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,kBAAkB,CA6C9D;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,kBAAkB,GAAG,IAAI,GAAG,IAAI,CAKlE"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a stored credential actually is.
|
|
3
|
+
*
|
|
4
|
+
* `account.accessToken` holds one of these, encrypted — not a bare token. The
|
|
5
|
+
* database knows nothing about auth types, so the discriminator travels with
|
|
6
|
+
* the secret rather than sitting in a column, and a provider that needs
|
|
7
|
+
* different fields needs no migration.
|
|
8
|
+
*/
|
|
9
|
+
export const ENVELOPE_VERSION = 1;
|
|
10
|
+
export class InvalidEnvelopeError extends Error {
|
|
11
|
+
constructor(reason) {
|
|
12
|
+
super(`Stored credential is unusable: ${reason}`);
|
|
13
|
+
this.name = 'InvalidEnvelopeError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Rejects rather than guesses. A credential we cannot read is a connection the
|
|
18
|
+
* end user must redo, and that is a better outcome than sending a provider
|
|
19
|
+
* something we mis-parsed.
|
|
20
|
+
*/
|
|
21
|
+
export function parseEnvelope(json) {
|
|
22
|
+
let value;
|
|
23
|
+
try {
|
|
24
|
+
value = JSON.parse(json);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
throw new InvalidEnvelopeError('not JSON');
|
|
28
|
+
}
|
|
29
|
+
if (typeof value !== 'object' || value === null) {
|
|
30
|
+
throw new InvalidEnvelopeError('not an object');
|
|
31
|
+
}
|
|
32
|
+
const envelope = value;
|
|
33
|
+
if (envelope.v !== ENVELOPE_VERSION) {
|
|
34
|
+
throw new InvalidEnvelopeError(`version ${String(envelope.v)} is not supported, expected ${ENVELOPE_VERSION}`);
|
|
35
|
+
}
|
|
36
|
+
switch (envelope.type) {
|
|
37
|
+
case 'oauth2':
|
|
38
|
+
if (!isNonEmptyString(envelope.accessToken)) {
|
|
39
|
+
throw new InvalidEnvelopeError('oauth2 envelope has no accessToken');
|
|
40
|
+
}
|
|
41
|
+
return envelope;
|
|
42
|
+
case 'api_key':
|
|
43
|
+
if (!isNonEmptyString(envelope.key)) {
|
|
44
|
+
throw new InvalidEnvelopeError('api_key envelope has no key');
|
|
45
|
+
}
|
|
46
|
+
return envelope;
|
|
47
|
+
case 'app':
|
|
48
|
+
if (!isNonEmptyString(envelope.installationId)) {
|
|
49
|
+
throw new InvalidEnvelopeError('app envelope has no installationId');
|
|
50
|
+
}
|
|
51
|
+
return envelope;
|
|
52
|
+
case 'oauth2_cc':
|
|
53
|
+
return envelope;
|
|
54
|
+
default:
|
|
55
|
+
throw new InvalidEnvelopeError(`unknown type "${String(envelope.type)}"`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The expiry to mirror into `account.accessTokenExpiresAt`, so the refresh
|
|
60
|
+
* sweep can find expiring credentials without decrypting any of them.
|
|
61
|
+
*/
|
|
62
|
+
export function expiryOf(envelope) {
|
|
63
|
+
if (envelope.type !== 'oauth2' || !envelope.expiresAt)
|
|
64
|
+
return null;
|
|
65
|
+
const expiry = new Date(envelope.expiresAt);
|
|
66
|
+
return Number.isNaN(expiry.getTime()) ? null : expiry;
|
|
67
|
+
}
|
|
68
|
+
function isNonEmptyString(value) {
|
|
69
|
+
return typeof value === 'string' && value.length > 0;
|
|
70
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ResolvedKoshaConfig } from '../config/kosha-config.js';
|
|
2
|
+
import { type CredentialEnvelope } from './credential-envelope.js';
|
|
2
3
|
export declare class MissingEncryptionKeyError extends Error {
|
|
3
4
|
constructor();
|
|
4
5
|
}
|
|
@@ -16,6 +17,13 @@ export declare class CryptoService {
|
|
|
16
17
|
/** `iv.authTag.ciphertext`, each part base64. */
|
|
17
18
|
encrypt(plaintext: string): string;
|
|
18
19
|
decrypt(payload: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* A credential goes to rest as an encrypted envelope carrying its own type
|
|
22
|
+
* and version, so the column it lives in needs to know neither.
|
|
23
|
+
*/
|
|
24
|
+
sealEnvelope(envelope: CredentialEnvelope): string;
|
|
25
|
+
/** Throws rather than returning a half-understood credential. */
|
|
26
|
+
openEnvelope(payload: string): CredentialEnvelope;
|
|
19
27
|
private requireKey;
|
|
20
28
|
}
|
|
21
29
|
//# sourceMappingURL=crypto.service.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.service.d.ts","sourceRoot":"","sources":["../../src/crypto/crypto.service.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"crypto.service.d.ts","sourceRoot":"","sources":["../../src/crypto/crypto.service.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAErE,OAAO,EAEL,KAAK,kBAAkB,EACxB,MAAM,0BAA0B,CAAC;AAMlC,qBAAa,yBAA0B,SAAQ,KAAK;;CAOnD;AAED;;;;;;GAMG;AACH,qBACa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAS;gBAEI,MAAM,EAAE,mBAAmB;IAgB7D,IAAI,YAAY,IAAI,OAAO,CAE1B;IAED,iDAAiD;IACjD,OAAO,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM;IAgBlC,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAoBhC;;;OAGG;IACH,YAAY,CAAC,QAAQ,EAAE,kBAAkB,GAAG,MAAM;IAIlD,iEAAiE;IACjE,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,kBAAkB;IAIjD,OAAO,CAAC,UAAU;CAInB"}
|