@nest-boot/auth 8.0.5-beta.3 → 8.0.5-beta.4
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/README.md +13 -7
- package/dist/abilities/auth.ability.d.ts +6 -0
- package/dist/abilities/{user.ability.js → auth.ability.js} +4 -4
- package/dist/abilities/auth.ability.js.map +1 -0
- package/dist/abilities/index.d.ts +1 -2
- package/dist/abilities/index.js +1 -2
- package/dist/abilities/index.js.map +1 -1
- package/dist/auth-module-options.interface.d.ts +3 -0
- package/dist/auth.guard.d.ts +3 -9
- package/dist/auth.guard.js +13 -38
- package/dist/auth.guard.js.map +1 -1
- package/dist/auth.module.js +0 -3
- package/dist/auth.module.js.map +1 -1
- package/dist/decorators/can.decorator.d.ts +3 -11
- package/dist/decorators/can.decorator.js +8 -14
- package/dist/decorators/can.decorator.js.map +1 -1
- package/dist/decorators/index.d.ts +0 -2
- package/dist/decorators/index.js +0 -2
- package/dist/decorators/index.js.map +1 -1
- package/dist/entities/account.entity.d.ts +1 -1
- package/dist/entities/account.entity.js +1 -1
- package/dist/entities/invitation.entity.d.ts +2 -0
- package/dist/entities/invitation.entity.js +9 -0
- package/dist/entities/invitation.entity.js.map +1 -1
- package/dist/entities/member.entity.d.ts +2 -0
- package/dist/entities/member.entity.js +9 -0
- package/dist/entities/member.entity.js.map +1 -1
- package/dist/entities/workspace-api-key.entity.d.ts +2 -0
- package/dist/entities/workspace-api-key.entity.js +9 -0
- package/dist/entities/workspace-api-key.entity.js.map +1 -1
- package/dist/index.d.ts +4 -8
- package/dist/index.js +4 -8
- package/dist/index.js.map +1 -1
- package/dist/infrastructure/api-key-lifecycle.d.ts +14 -0
- package/dist/infrastructure/api-key-lifecycle.js +19 -0
- package/dist/infrastructure/api-key-lifecycle.js.map +1 -1
- package/dist/infrastructure/auth-ability.factory.d.ts +4 -9
- package/dist/infrastructure/auth-ability.factory.js +23 -21
- package/dist/infrastructure/auth-ability.factory.js.map +1 -1
- package/dist/infrastructure/auth-service.providers.js +9 -11
- package/dist/infrastructure/auth-service.providers.js.map +1 -1
- package/dist/infrastructure/request-identity.d.ts +16 -4
- package/dist/infrastructure/request-identity.js +61 -40
- package/dist/infrastructure/request-identity.js.map +1 -1
- package/dist/interfaces/ability-context.interface.d.ts +16 -0
- package/dist/interfaces/ability-context.interface.js +2 -0
- package/dist/interfaces/ability-context.interface.js.map +1 -0
- package/dist/interfaces/ability-rules.interface.d.ts +8 -2
- package/dist/interfaces/auth-module-user-options.interface.d.ts +0 -3
- package/dist/interfaces/auth-module-workspace-options.interface.d.ts +0 -3
- package/dist/interfaces/{user-can-metadata.interface.d.ts → can-metadata.interface.d.ts} +2 -2
- package/dist/interfaces/can-metadata.interface.js +2 -0
- package/dist/interfaces/can-metadata.interface.js.map +1 -0
- package/dist/permission.constants.d.ts +2 -4
- package/dist/permission.constants.js +2 -4
- package/dist/permission.constants.js.map +1 -1
- package/dist/resolvers/auth.resolver.d.ts +2 -4
- package/dist/resolvers/auth.resolver.js +5 -16
- package/dist/resolvers/auth.resolver.js.map +1 -1
- package/dist/services/account.service.d.ts +1 -3
- package/dist/services/account.service.js +4 -7
- package/dist/services/account.service.js.map +1 -1
- package/dist/services/invitation.service.d.ts +1 -3
- package/dist/services/invitation.service.js +32 -32
- package/dist/services/invitation.service.js.map +1 -1
- package/dist/services/member.service.d.ts +1 -3
- package/dist/services/member.service.js +50 -51
- package/dist/services/member.service.js.map +1 -1
- package/dist/services/session.service.d.ts +1 -4
- package/dist/services/session.service.js +11 -15
- package/dist/services/session.service.js.map +1 -1
- package/dist/services/user-api-key.service.d.ts +3 -7
- package/dist/services/user-api-key.service.js +52 -73
- package/dist/services/user-api-key.service.js.map +1 -1
- package/dist/services/user-deletion.service.js.map +1 -1
- package/dist/services/user.service.d.ts +1 -3
- package/dist/services/user.service.js +38 -42
- package/dist/services/user.service.js.map +1 -1
- package/dist/services/workspace-api-key.service.d.ts +3 -7
- package/dist/services/workspace-api-key.service.js +50 -71
- package/dist/services/workspace-api-key.service.js.map +1 -1
- package/dist/services/workspace.service.d.ts +1 -3
- package/dist/services/workspace.service.js +22 -24
- package/dist/services/workspace.service.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/build-ability-callback.type.d.ts +4 -0
- package/dist/types/build-ability-callback.type.js +2 -0
- package/dist/types/build-ability-callback.type.js.map +1 -0
- package/dist/utils/api-key-permissions.util.d.ts +2 -0
- package/dist/utils/api-key-permissions.util.js +9 -0
- package/dist/utils/api-key-permissions.util.js.map +1 -1
- package/dist/utils/assert-can.util.d.ts +3 -0
- package/dist/utils/assert-can.util.js +8 -0
- package/dist/utils/assert-can.util.js.map +1 -0
- package/dist/utils/build-request-ability.util.d.ts +3 -6
- package/dist/utils/build-request-ability.util.js +25 -15
- package/dist/utils/build-request-ability.util.js.map +1 -1
- package/dist/utils/can.util.d.ts +2 -3
- package/dist/utils/can.util.js +9 -7
- package/dist/utils/can.util.js.map +1 -1
- package/dist/utils/extend-ability.util.d.ts +5 -1
- package/dist/utils/extend-ability.util.js +13 -6
- package/dist/utils/extend-ability.util.js.map +1 -1
- package/dist/utils/get-ability.util.d.ts +5 -0
- package/dist/utils/get-ability.util.js +20 -0
- package/dist/utils/get-ability.util.js.map +1 -0
- package/dist/utils/permission-grants.util.d.ts +9 -0
- package/dist/utils/permission-grants.util.js +25 -0
- package/dist/utils/permission-grants.util.js.map +1 -0
- package/package.json +9 -9
- package/dist/abilities/user.ability.d.ts +0 -8
- package/dist/abilities/user.ability.js.map +0 -1
- package/dist/abilities/workspace.ability.d.ts +0 -8
- package/dist/abilities/workspace.ability.js +0 -13
- package/dist/abilities/workspace.ability.js.map +0 -1
- package/dist/decorators/user-can.decorator.d.ts +0 -13
- package/dist/decorators/user-can.decorator.js +0 -21
- package/dist/decorators/user-can.decorator.js.map +0 -1
- package/dist/decorators/workspace-can.decorator.d.ts +0 -13
- package/dist/decorators/workspace-can.decorator.js +0 -21
- package/dist/decorators/workspace-can.decorator.js.map +0 -1
- package/dist/interfaces/can-options.interface.d.ts +0 -5
- package/dist/interfaces/can-options.interface.js +0 -2
- package/dist/interfaces/can-options.interface.js.map +0 -1
- package/dist/interfaces/user-can-metadata.interface.js +0 -2
- package/dist/interfaces/user-can-metadata.interface.js.map +0 -1
- package/dist/interfaces/workspace-can-metadata.interface.d.ts +0 -9
- package/dist/interfaces/workspace-can-metadata.interface.js +0 -2
- package/dist/interfaces/workspace-can-metadata.interface.js.map +0 -1
- package/dist/services/access-control.service.d.ts +0 -54
- package/dist/services/access-control.service.js +0 -208
- package/dist/services/access-control.service.js.map +0 -1
- package/dist/types/build-user-ability-callback.type.d.ts +0 -10
- package/dist/types/build-user-ability-callback.type.js +0 -2
- package/dist/types/build-user-ability-callback.type.js.map +0 -1
- package/dist/types/build-workspace-ability-callback.type.d.ts +0 -10
- package/dist/types/build-workspace-ability-callback.type.js +0 -2
- package/dist/types/build-workspace-ability-callback.type.js.map +0 -1
- package/dist/utils/get-user-ability.util.d.ts +0 -3
- package/dist/utils/get-user-ability.util.js +0 -14
- package/dist/utils/get-user-ability.util.js.map +0 -1
- package/dist/utils/get-workspace-ability.util.d.ts +0 -3
- package/dist/utils/get-workspace-ability.util.js +0 -14
- package/dist/utils/get-workspace-ability.util.js.map +0 -1
- package/dist/utils/user-can.util.d.ts +0 -3
- package/dist/utils/user-can.util.js +0 -9
- package/dist/utils/user-can.util.js.map +0 -1
- package/dist/utils/workspace-can.util.d.ts +0 -3
- package/dist/utils/workspace-can.util.js +0 -10
- package/dist/utils/workspace-can.util.js.map +0 -1
|
@@ -11,63 +11,62 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
11
11
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
12
|
};
|
|
13
13
|
var UserApiKeyService_1;
|
|
14
|
-
import { EntityManager, Reference
|
|
14
|
+
import { EntityManager, Reference } from "@mikro-orm/core";
|
|
15
15
|
import { ConnectionManager, } from "@nest-boot/graphql-connection";
|
|
16
16
|
import { ForbiddenException, Inject, Injectable, Logger, NotFoundException, } from "@nestjs/common";
|
|
17
17
|
import { MODULE_OPTIONS_TOKEN } from "../auth.module-definition.js";
|
|
18
18
|
import { UserApiKeyConnection } from "../connections/user-api-key.connection-definition.js";
|
|
19
19
|
import { UserApiKey } from "../entities/user-api-key.entity.js";
|
|
20
20
|
import { ApiKeyLifecycle } from "../infrastructure/api-key-lifecycle.js";
|
|
21
|
-
import {
|
|
22
|
-
import { resolveApiKeyPermissionCatalog } from "../utils/api-key-permissions.util.js";
|
|
23
|
-
import {
|
|
21
|
+
import { RequestIdentity } from "../infrastructure/request-identity.js";
|
|
22
|
+
import { normalizeApiKeyPermissions, resolveApiKeyPermissionCatalog, } from "../utils/api-key-permissions.util.js";
|
|
23
|
+
import { assertCan } from "../utils/assert-can.util.js";
|
|
24
|
+
import { assertApiKeyPermissionCeiling, assertPermissionCeiling, canGrantPermissions, } from "../utils/permission-grants.util.js";
|
|
24
25
|
import { resolveAuthCatalog } from "../utils/resolve-auth-catalog.util.js";
|
|
25
26
|
import { resolveUserPermissions } from "../utils/resolve-effective-permissions.util.js";
|
|
26
|
-
import {
|
|
27
|
+
import { resolveRequestPermissions } from "../utils/resolve-request-permissions.util.js";
|
|
27
28
|
/** Manages user-owned API keys within the current request's authorization scope. */
|
|
28
29
|
let UserApiKeyService = UserApiKeyService_1 = class UserApiKeyService {
|
|
29
30
|
em;
|
|
30
31
|
authOptions;
|
|
31
|
-
accessControlService;
|
|
32
32
|
logger = new Logger(UserApiKeyService_1.name);
|
|
33
33
|
/** Creates an API-key domain service. */
|
|
34
34
|
constructor(
|
|
35
35
|
/** MikroORM entity manager used for API-key persistence. */
|
|
36
|
-
em, authOptions
|
|
36
|
+
em, authOptions) {
|
|
37
37
|
this.em = em;
|
|
38
38
|
this.authOptions = authOptions;
|
|
39
|
-
this.accessControlService = accessControlService;
|
|
40
39
|
}
|
|
41
40
|
/** Lists user-key grants subject to configuration and the current credential's ceiling. */
|
|
42
41
|
getUserApiKeyPermissions(user) {
|
|
43
|
-
|
|
42
|
+
RequestIdentity.assertCurrentUser(user);
|
|
44
43
|
const { permissions, allowed, defaults } = resolveApiKeyPermissionCatalog(this.authOptions, "user");
|
|
45
44
|
const allowedSet = new Set(allowed);
|
|
46
45
|
const userPermissions = new Set(resolveAuthCatalog(this.authOptions, "user").permissions);
|
|
47
|
-
const ceiling = this.
|
|
46
|
+
const ceiling = resolveRequestPermissions(this.authOptions).apiKey;
|
|
48
47
|
return permissions.map((permission) => ({
|
|
49
48
|
permission,
|
|
50
49
|
default: defaults.includes(permission),
|
|
51
50
|
grantable: allowedSet.has(permission) &&
|
|
52
51
|
(!userPermissions.has(permission) ||
|
|
53
|
-
this.
|
|
52
|
+
canGrantPermissions(this.authOptions, "user", [permission])) &&
|
|
54
53
|
(ceiling === null || ceiling.includes(permission)),
|
|
55
54
|
}));
|
|
56
55
|
}
|
|
57
56
|
/** Returns a user-owned API key when it belongs to the current user. */
|
|
58
57
|
async getUserApiKey(id, user) {
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
RequestIdentity.assertCurrentUser(user);
|
|
59
|
+
assertCan("read", UserApiKey);
|
|
61
60
|
const apiKey = await this.getVisibleApiKey(id, user);
|
|
62
61
|
if (apiKey) {
|
|
63
|
-
|
|
62
|
+
assertCan("read", apiKey);
|
|
64
63
|
}
|
|
65
64
|
return apiKey;
|
|
66
65
|
}
|
|
67
66
|
/** Paginates current-user keys after applying ownership and permission ceilings. */
|
|
68
67
|
async getUserApiKeyConnection(user, args) {
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
RequestIdentity.assertCurrentUser(user);
|
|
69
|
+
assertCan("read", UserApiKey);
|
|
71
70
|
const where = this.getOwnedListFilter(user);
|
|
72
71
|
const connection = await new ConnectionManager(this.em).find(UserApiKeyConnection, args, {
|
|
73
72
|
where,
|
|
@@ -75,76 +74,67 @@ let UserApiKeyService = UserApiKeyService_1 = class UserApiKeyService {
|
|
|
75
74
|
});
|
|
76
75
|
// Reject the whole page rather than silently changing cursor pagination.
|
|
77
76
|
for (const { node } of connection.edges) {
|
|
78
|
-
|
|
77
|
+
assertCan("read", node);
|
|
79
78
|
}
|
|
80
79
|
return connection;
|
|
81
80
|
}
|
|
82
81
|
/** Creates an API key owned by a user. */
|
|
83
82
|
async createUserApiKey(user, options) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const permissions = this.
|
|
87
|
-
? resolveApiKeyPermissionCatalog(this.authOptions, "user").defaults
|
|
88
|
-
: (options.permissions ?? []));
|
|
83
|
+
RequestIdentity.assertCurrentUser(user);
|
|
84
|
+
assertCan("write", UserApiKey);
|
|
85
|
+
const permissions = normalizeApiKeyPermissions(this.authOptions, "user", options.permissions);
|
|
89
86
|
this.assertUserPermissionCeiling(user, permissions);
|
|
90
87
|
return await this.createKey(user, options, permissions);
|
|
91
88
|
}
|
|
92
89
|
/** Updates an API key owned by the current user. */
|
|
93
90
|
async updateUserApiKey(id, input) {
|
|
94
|
-
|
|
91
|
+
assertCan("write", UserApiKey);
|
|
95
92
|
const apiKey = await this.findWritableApiKey(id);
|
|
96
|
-
|
|
97
|
-
const user = this.
|
|
93
|
+
assertCan("write", apiKey);
|
|
94
|
+
const user = this.unwrapUser(apiKey);
|
|
98
95
|
const permissions = input.permissions === undefined
|
|
99
96
|
? undefined
|
|
100
|
-
: this.
|
|
97
|
+
: normalizeApiKeyPermissions(this.authOptions, "user", input.permissions ?? []);
|
|
101
98
|
// Allow disabling stale grants without replacing them.
|
|
102
99
|
if (input.enabled !== false || permissions !== undefined) {
|
|
103
|
-
const finalPermissions = permissions ??
|
|
100
|
+
const finalPermissions = permissions ??
|
|
101
|
+
normalizeApiKeyPermissions(this.authOptions, "user", apiKey.permissions ?? []);
|
|
104
102
|
this.assertUserPermissionCeiling(user, finalPermissions);
|
|
105
103
|
}
|
|
106
104
|
return await ApiKeyLifecycle.update(this.em, this.authOptions, apiKey, input, permissions);
|
|
107
105
|
}
|
|
108
106
|
/** Deletes an API key owned by the current user. */
|
|
109
107
|
async deleteUserApiKey(id) {
|
|
110
|
-
|
|
108
|
+
assertCan("write", UserApiKey);
|
|
111
109
|
const apiKey = await this.findWritableApiKey(id);
|
|
112
|
-
|
|
110
|
+
assertCan("write", apiKey);
|
|
113
111
|
return await ApiKeyLifecycle.delete(this.em, this.authOptions, apiKey);
|
|
114
112
|
}
|
|
115
|
-
async createKey(
|
|
116
|
-
ApiKeyLifecycle.
|
|
117
|
-
const prefix = options.prefix ?? process.env.API_KEY_PREFIX ?? "sk";
|
|
118
|
-
const plaintextApiKey = generateApiKey(prefix);
|
|
113
|
+
async createKey(user, options, permissions) {
|
|
114
|
+
const { apiKey, data } = ApiKeyLifecycle.prepareCreation(options, permissions);
|
|
119
115
|
const entity = await this.em.transactional(async (em) => {
|
|
120
116
|
const entity = em.create(UserApiKey, {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
key: hashApiKey(plaintextApiKey),
|
|
124
|
-
name: options.name,
|
|
125
|
-
user: owner,
|
|
126
|
-
permissions,
|
|
127
|
-
prefix,
|
|
128
|
-
start: plaintextApiKey.slice(0, 8),
|
|
117
|
+
...data,
|
|
118
|
+
user,
|
|
129
119
|
});
|
|
130
|
-
|
|
120
|
+
assertCan("write", entity);
|
|
131
121
|
await em.persist(entity).flush();
|
|
132
122
|
return entity;
|
|
133
123
|
}, { clear: true });
|
|
134
124
|
this.logger.log("API key created", {
|
|
135
125
|
apiKeyId: entity.id,
|
|
136
|
-
ownerId:
|
|
126
|
+
ownerId: user.id,
|
|
137
127
|
ownerType: "user",
|
|
138
128
|
});
|
|
139
|
-
return { apiKey
|
|
129
|
+
return { apiKey, entity };
|
|
140
130
|
}
|
|
141
|
-
async getVisibleApiKey(id,
|
|
131
|
+
async getVisibleApiKey(id, user) {
|
|
142
132
|
const apiKey = await this.em.findOne(UserApiKey, {
|
|
143
|
-
$and: [{ id }, this.getOwnedListFilter(
|
|
133
|
+
$and: [{ id }, this.getOwnedListFilter(user)],
|
|
144
134
|
}, { populate: ["user"], exclude: ["key"] });
|
|
145
135
|
if (apiKey) {
|
|
146
|
-
this.
|
|
147
|
-
this.
|
|
136
|
+
this.assertUser(apiKey, user);
|
|
137
|
+
assertApiKeyPermissionCeiling(this.authOptions, apiKey.permissions ?? []);
|
|
148
138
|
}
|
|
149
139
|
return apiKey;
|
|
150
140
|
}
|
|
@@ -157,51 +147,40 @@ let UserApiKeyService = UserApiKeyService_1 = class UserApiKeyService {
|
|
|
157
147
|
if (!apiKey) {
|
|
158
148
|
throw new NotFoundException("API key not found");
|
|
159
149
|
}
|
|
160
|
-
this.
|
|
161
|
-
this.
|
|
150
|
+
RequestIdentity.assertCurrentUser(this.unwrapUser(apiKey));
|
|
151
|
+
assertApiKeyPermissionCeiling(this.authOptions, apiKey.permissions ?? []);
|
|
162
152
|
return apiKey;
|
|
163
153
|
}
|
|
164
|
-
getOwnedListFilter(
|
|
165
|
-
const ceiling = this.
|
|
154
|
+
getOwnedListFilter(user) {
|
|
155
|
+
const ceiling = resolveRequestPermissions(this.authOptions).apiKey;
|
|
166
156
|
return {
|
|
167
|
-
user
|
|
157
|
+
user,
|
|
168
158
|
...(ceiling !== null ? { permissions: { $contained: ceiling } } : {}),
|
|
169
159
|
};
|
|
170
160
|
}
|
|
171
|
-
|
|
172
|
-
const
|
|
173
|
-
if (
|
|
161
|
+
assertUser(apiKey, expectedUser) {
|
|
162
|
+
const user = this.unwrapUser(apiKey);
|
|
163
|
+
if (user.id !== expectedUser.id) {
|
|
174
164
|
throw new ForbiddenException("You are not allowed to access this API key");
|
|
175
165
|
}
|
|
176
166
|
}
|
|
177
|
-
|
|
167
|
+
unwrapUser(apiKey) {
|
|
168
|
+
if (!apiKey.user)
|
|
169
|
+
throw new ForbiddenException("API key owner is missing");
|
|
178
170
|
return Reference.unwrapReference(apiKey.user);
|
|
179
171
|
}
|
|
180
|
-
normalizePermissions(permissions) {
|
|
181
|
-
const { permissions: availablePermissions, allowed } = resolveApiKeyPermissionCatalog(this.authOptions, "user");
|
|
182
|
-
const normalizedPermissions = normalizeAuthPermissions(permissions, availablePermissions, "User API key");
|
|
183
|
-
this.assertPermissionCeiling(normalizedPermissions, allowed, "API key permissions exceed configured allowedPermissions");
|
|
184
|
-
return normalizedPermissions;
|
|
185
|
-
}
|
|
186
172
|
assertUserPermissionCeiling(user, permissions) {
|
|
187
173
|
const userPermissionCatalog = new Set(resolveAuthCatalog(this.authOptions, "user").permissions);
|
|
188
174
|
const requestedUserPermissions = permissions.filter((permission) => userPermissionCatalog.has(permission));
|
|
189
175
|
const effectivePermissions = resolveUserPermissions(this.authOptions, user);
|
|
190
|
-
|
|
191
|
-
this.
|
|
192
|
-
}
|
|
193
|
-
assertPermissionCeiling(requestedPermissions, effectivePermissions, message) {
|
|
194
|
-
const effectivePermissionSet = new Set(effectivePermissions);
|
|
195
|
-
const excessivePermissions = requestedPermissions.filter((permission) => !effectivePermissionSet.has(permission));
|
|
196
|
-
if (excessivePermissions.length > 0) {
|
|
197
|
-
throw new ForbiddenException(`${message}: ${excessivePermissions.join(", ")}`);
|
|
198
|
-
}
|
|
176
|
+
assertPermissionCeiling(requestedUserPermissions, effectivePermissions, "User API key permissions exceed owner permissions");
|
|
177
|
+
assertApiKeyPermissionCeiling(this.authOptions, permissions);
|
|
199
178
|
}
|
|
200
179
|
};
|
|
201
180
|
UserApiKeyService = UserApiKeyService_1 = __decorate([
|
|
202
181
|
Injectable(),
|
|
203
182
|
__param(1, Inject(MODULE_OPTIONS_TOKEN)),
|
|
204
|
-
__metadata("design:paramtypes", [EntityManager, Object
|
|
183
|
+
__metadata("design:paramtypes", [EntityManager, Object])
|
|
205
184
|
], UserApiKeyService);
|
|
206
185
|
export { UserApiKeyService };
|
|
207
186
|
//# sourceMappingURL=user-api-key.service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user-api-key.service.js","sourceRoot":"","sources":["../../src/services/user-api-key.service.ts"],"names":[],"mappings":";;;;;;;;;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B,CAAC,CAAC;QAC3E,OAAO,SAAS,CAAC,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAChD,CAAC;IAEO,2BAA2B,CACjC,IAAU,EACV,WAA8B;QAE9B,MAAM,qBAAqB,GAAG,IAAI,GAAG,CACnC,kBAAkB,CAAC,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC,WAAW,CACzD,CAAC;QACF,MAAM,wBAAwB,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE,CACjE,qBAAqB,CAAC,GAAG,CAAC,UAAU,CAAC,CACtC,CAAC;QACF,MAAM,oBAAoB,GAAG,sBAAsB,CAAC,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;QAE5E,uBAAuB,CACrB,wBAAwB,EACxB,oBAAoB,EACpB,mDAAmD,CACpD,CAAC;QACF,6BAA6B,CAAC,IAAI,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;IAC/D,CAAC;CACF,CAAA;AAzOY,iBAAiB;IAD7B,UAAU,EAAE;IAQR,WAAA,MAAM,CAAC,oBAAoB,CAAC,CAAA;qCADN,aAAa;GAN3B,iBAAiB,CAyO7B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user-deletion.service.js","sourceRoot":"","sources":["../../src/services/user-deletion.service.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"user-deletion.service.js","sourceRoot":"","sources":["../../src/services/user-deletion.service.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAE/D,OAAO,EAAE,IAAI,EAAE,MAAM,4BAA4B,CAAC;AAElD,6EAA6E;AAEtE,IAAM,mBAAmB,GAAzB,MAAM,mBAAmB;IAED;IAD7B,sDAAsD;IACtD,YAA6B,EAAiB;QAAjB,OAAE,GAAF,EAAE,CAAe;IAAG,CAAC;IAElD;;;;;OAKG;IACH,KAAK,CAAC,UAAU,CACd,MAAc,EACd,YAAkC;QAElC,MAAM,MAAM,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,iBAAiB,EAAE,CAAC;QAC/D,OAAO,MAAM,IAAI,CAAC,EAAE,CAAC,aAAa,CAChC,KAAK,EAAE,EAAE,EAAE,EAAE;YACX,MAAM,IAAI,GAAG,MAAM,EAAE,CAAC,OAAO,CAC3B,IAAI,EACJ,EAAE,EAAE,EAAE,MAAM,EAAE,EACd,MAAM;gBACJ,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE;gBACnB,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,CAAC,iBAAiB,EAAE,CAC7D,CAAC;YACF,IAAI,CAAC,IAAI;gBAAE,OAAO,IAAI,CAAC;YAEvB,MAAM,YAAY,EAAE,EAAE,CAAC;YACvB,yEAAyE;YACzE,MAAM,KAAK,GAAG,MAAM,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE;gBACxC,EAAE,EAAE,MAAM;aACX,CAAC,CAAC;YACH,IAAI,KAAK,KAAK,CAAC;gBAAE,MAAM,IAAI,iBAAiB,CAAC,gBAAgB,CAAC,CAAC;YAC/D,OAAO,IAAI,CAAC;QACd,CAAC,EACD,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAC9B,CAAC;IACJ,CAAC;CACF,CAAA;AArCY,mBAAmB;IAD/B,UAAU,EAAE;qCAGsB,aAAa;GAFnC,mBAAmB,CAqC/B"}
|
|
@@ -12,19 +12,17 @@ import type { UpdateUserOptions } from "../interfaces/update-user-options.interf
|
|
|
12
12
|
import type { UserHasPermissionsOptions } from "../interfaces/user-has-permissions-options.interface.js";
|
|
13
13
|
import type { UserPermissionOption } from "../objects/user-permission-option.object.js";
|
|
14
14
|
import type { UserRoleOption } from "../objects/user-role-option.object.js";
|
|
15
|
-
import { AccessControlService } from "./access-control.service.js";
|
|
16
15
|
/** User management implemented with the built-in MikroORM entities. */
|
|
17
16
|
export declare class UserService {
|
|
18
17
|
/** MikroORM entity manager used for authentication persistence. */
|
|
19
18
|
private readonly em;
|
|
20
19
|
private readonly options;
|
|
21
20
|
private readonly hashService;
|
|
22
|
-
private readonly accessControlService;
|
|
23
21
|
private readonly userDeletionService;
|
|
24
22
|
/** Creates a new UserService instance. */
|
|
25
23
|
constructor(
|
|
26
24
|
/** MikroORM entity manager used for authentication persistence. */
|
|
27
|
-
em: EntityManager, options: AuthModuleOptions, hashService: HashService,
|
|
25
|
+
em: EntityManager, options: AuthModuleOptions, hashService: HashService, userDeletionService: {
|
|
28
26
|
deleteUser(userId: string, beforeDelete?: () => Promise<void>): Promise<User | null>;
|
|
29
27
|
});
|
|
30
28
|
/** Creates a user and its credential account atomically. */
|
|
@@ -11,7 +11,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
11
11
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
12
|
};
|
|
13
13
|
import { randomBytes } from "node:crypto";
|
|
14
|
-
import { EntityManager, Reference
|
|
14
|
+
import { EntityManager, Reference } from "@mikro-orm/core";
|
|
15
15
|
import { ConnectionManager, } from "@nest-boot/graphql-connection";
|
|
16
16
|
import { HashService } from "@nest-boot/hash";
|
|
17
17
|
import { RequestContext } from "@nest-boot/request-context";
|
|
@@ -25,11 +25,12 @@ import { UserApiKey } from "../entities/user-api-key.entity.js";
|
|
|
25
25
|
import { RequestIdentity } from "../infrastructure/request-identity.js";
|
|
26
26
|
import { RevokedAuthenticationException } from "../infrastructure/revoked-authentication.exception.js";
|
|
27
27
|
import { DEFAULT_USER_ADMIN_ROLES, DEFAULT_USER_ROLE, } from "../user.constants.js";
|
|
28
|
+
import { assertCan } from "../utils/assert-can.util.js";
|
|
28
29
|
import { listAuthPermissions, normalizeAuthPermissions, normalizeAuthRoles, resolveAuthPermissions, } from "../utils/auth-role.util.js";
|
|
29
30
|
import { getCurrentApiKey } from "../utils/get-current-api-key.util.js";
|
|
31
|
+
import { assertCanGrantPermissions, canGrantPermissions, } from "../utils/permission-grants.util.js";
|
|
30
32
|
import { resolveAuthCatalog } from "../utils/resolve-auth-catalog.util.js";
|
|
31
33
|
import { resolveUserPermissions } from "../utils/resolve-effective-permissions.util.js";
|
|
32
|
-
import { AccessControlService } from "./access-control.service.js";
|
|
33
34
|
import { UserDeletionService } from "./user-deletion.service.js";
|
|
34
35
|
const CREDENTIAL_ISSUER = "local:credential";
|
|
35
36
|
const CREDENTIAL_PROVIDER_ID = "credential";
|
|
@@ -44,31 +45,29 @@ let UserService = class UserService {
|
|
|
44
45
|
em;
|
|
45
46
|
options;
|
|
46
47
|
hashService;
|
|
47
|
-
accessControlService;
|
|
48
48
|
userDeletionService;
|
|
49
49
|
/** Creates a new UserService instance. */
|
|
50
50
|
constructor(
|
|
51
51
|
/** MikroORM entity manager used for authentication persistence. */
|
|
52
|
-
em, options, hashService,
|
|
52
|
+
em, options, hashService, userDeletionService) {
|
|
53
53
|
this.em = em;
|
|
54
54
|
this.options = options;
|
|
55
55
|
this.hashService = hashService;
|
|
56
|
-
this.accessControlService = accessControlService;
|
|
57
56
|
this.userDeletionService = userDeletionService;
|
|
58
57
|
}
|
|
59
58
|
/** Creates a user and its credential account atomically. */
|
|
60
59
|
async createUser(input) {
|
|
61
|
-
|
|
60
|
+
assertCan("create", User);
|
|
62
61
|
if (input.roles !== undefined) {
|
|
63
|
-
|
|
62
|
+
assertCan("set-roles", User);
|
|
64
63
|
}
|
|
65
64
|
if (input.permissions !== undefined) {
|
|
66
|
-
|
|
65
|
+
assertCan("set-permissions", User);
|
|
67
66
|
}
|
|
68
67
|
this.assertPasswordLength(input.password);
|
|
69
68
|
const permissions = this.normalizePermissions(input.permissions ?? []);
|
|
70
69
|
const roles = this.normalizeRoles(input.roles ?? [this.defaultRole]);
|
|
71
|
-
this.
|
|
70
|
+
assertCanGrantPermissions(this.options, "user", resolveAuthPermissions(roles, permissions, this.roles));
|
|
72
71
|
const email = input.email.trim().toLowerCase();
|
|
73
72
|
const password = await this.hashPassword(input.password);
|
|
74
73
|
return await this.em.transactional(async (em) => {
|
|
@@ -79,12 +78,12 @@ let UserService = class UserService {
|
|
|
79
78
|
permissions,
|
|
80
79
|
roles,
|
|
81
80
|
});
|
|
82
|
-
|
|
81
|
+
assertCan("create", user);
|
|
83
82
|
if (input.roles !== undefined) {
|
|
84
|
-
|
|
83
|
+
assertCan("set-roles", user);
|
|
85
84
|
}
|
|
86
85
|
if (input.permissions !== undefined) {
|
|
87
|
-
|
|
86
|
+
assertCan("set-permissions", user);
|
|
88
87
|
}
|
|
89
88
|
em.persist(user);
|
|
90
89
|
await em.flush();
|
|
@@ -103,31 +102,31 @@ let UserService = class UserService {
|
|
|
103
102
|
}
|
|
104
103
|
/** Gets a user by identifier within the request's RLS scope. */
|
|
105
104
|
async getUser(userId) {
|
|
106
|
-
|
|
105
|
+
assertCan("read", User);
|
|
107
106
|
const user = await this.em.findOne(User, {
|
|
108
107
|
id: userId,
|
|
109
108
|
});
|
|
110
109
|
if (user)
|
|
111
|
-
|
|
110
|
+
assertCan("read", user);
|
|
112
111
|
return user;
|
|
113
112
|
}
|
|
114
113
|
/** Gets a user by normalized email within the request's RLS scope. */
|
|
115
114
|
async getUserByEmail(email) {
|
|
116
|
-
|
|
115
|
+
assertCan("read", User);
|
|
117
116
|
const user = await this.em.findOne(User, {
|
|
118
117
|
email: email.trim().toLowerCase(),
|
|
119
118
|
});
|
|
120
119
|
if (user)
|
|
121
|
-
|
|
120
|
+
assertCan("read", user);
|
|
122
121
|
return user;
|
|
123
122
|
}
|
|
124
123
|
/** Updates mutable user fields. */
|
|
125
124
|
async updateUser(user, input) {
|
|
126
125
|
user = await this.resolveUserForAction(user, "update");
|
|
127
|
-
|
|
126
|
+
assertCan("update", user);
|
|
128
127
|
const data = this.createUserUpdateData(input);
|
|
129
128
|
if (input.email !== undefined || input.emailVerified !== undefined) {
|
|
130
|
-
|
|
129
|
+
assertCan("set-email", user);
|
|
131
130
|
}
|
|
132
131
|
this.assertAuthorizationCanCommit(user);
|
|
133
132
|
const previous = {
|
|
@@ -150,9 +149,9 @@ let UserService = class UserService {
|
|
|
150
149
|
/** Replaces a user's application permissions. */
|
|
151
150
|
async setUserPermissions(user, permissions) {
|
|
152
151
|
user = await this.resolveUserForAction(user, "set-permissions");
|
|
153
|
-
|
|
152
|
+
assertCan("set-permissions", user);
|
|
154
153
|
const normalized = this.normalizePermissions(permissions);
|
|
155
|
-
this.
|
|
154
|
+
assertCanGrantPermissions(this.options, "user", normalized);
|
|
156
155
|
this.assertAuthorizationCanCommit(user);
|
|
157
156
|
const previous = user.permissions;
|
|
158
157
|
user.permissions = normalized;
|
|
@@ -169,9 +168,9 @@ let UserService = class UserService {
|
|
|
169
168
|
/** Replaces the roles assigned to a user. */
|
|
170
169
|
async setUserRoles(user, roleNames) {
|
|
171
170
|
user = await this.resolveUserForAction(user, "set-roles");
|
|
172
|
-
|
|
171
|
+
assertCan("set-roles", user);
|
|
173
172
|
const roles = this.normalizeRoles(roleNames);
|
|
174
|
-
this.
|
|
173
|
+
assertCanGrantPermissions(this.options, "user", resolveAuthPermissions(roles, [], this.roles));
|
|
175
174
|
this.assertAuthorizationCanCommit(user);
|
|
176
175
|
const previous = user.roles;
|
|
177
176
|
user.roles = roles;
|
|
@@ -208,7 +207,7 @@ let UserService = class UserService {
|
|
|
208
207
|
async resolveUserForAction(user, action) {
|
|
209
208
|
if (typeof user !== "string")
|
|
210
209
|
return user;
|
|
211
|
-
|
|
210
|
+
assertCan(action, User);
|
|
212
211
|
const entity = await this.em.findOne(User, { id: user }, { refresh: true });
|
|
213
212
|
if (!entity)
|
|
214
213
|
throw new NotFoundException("User not found");
|
|
@@ -216,20 +215,18 @@ let UserService = class UserService {
|
|
|
216
215
|
}
|
|
217
216
|
/** Lists all configured roles with the current principal's grant availability. */
|
|
218
217
|
listRoles() {
|
|
219
|
-
|
|
218
|
+
assertCan("set-roles", User);
|
|
220
219
|
return Object.entries(this.roles).map(([role, permissions]) => ({
|
|
221
220
|
role,
|
|
222
|
-
grantable: this.
|
|
221
|
+
grantable: canGrantPermissions(this.options, "user", permissions),
|
|
223
222
|
}));
|
|
224
223
|
}
|
|
225
224
|
/** Lists all configured permissions with the current principal's grant availability. */
|
|
226
225
|
listPermissions() {
|
|
227
|
-
|
|
226
|
+
assertCan("set-permissions", User);
|
|
228
227
|
return listAuthPermissions(this.permissions).map((permission) => ({
|
|
229
228
|
permission,
|
|
230
|
-
grantable: this.
|
|
231
|
-
permission,
|
|
232
|
-
]),
|
|
229
|
+
grantable: canGrantPermissions(this.options, "user", [permission]),
|
|
233
230
|
}));
|
|
234
231
|
}
|
|
235
232
|
/** Resolves permissions inherited from roles plus direct user permissions. */
|
|
@@ -238,17 +235,17 @@ let UserService = class UserService {
|
|
|
238
235
|
}
|
|
239
236
|
/** Paginates users without bypassing application RLS. */
|
|
240
237
|
async getUserConnection(args) {
|
|
241
|
-
|
|
238
|
+
assertCan("read", User);
|
|
242
239
|
const connection = await new ConnectionManager(this.em).find(UserConnection, args);
|
|
243
240
|
for (const { node } of connection.edges) {
|
|
244
|
-
|
|
241
|
+
assertCan("read", node);
|
|
245
242
|
}
|
|
246
243
|
return connection;
|
|
247
244
|
}
|
|
248
245
|
/** Bans a user and immediately revokes all of their sessions. */
|
|
249
246
|
async banUser(user, input = {}) {
|
|
250
247
|
user = await this.resolveUserForAction(user, "ban");
|
|
251
|
-
|
|
248
|
+
assertCan("ban", user);
|
|
252
249
|
this.assertIdentityRevocationCanCommit(user);
|
|
253
250
|
const banExpiresAt = input.banExpiresIn === undefined
|
|
254
251
|
? null
|
|
@@ -295,7 +292,7 @@ let UserService = class UserService {
|
|
|
295
292
|
/** Removes a user's ban. */
|
|
296
293
|
async unbanUser(user) {
|
|
297
294
|
user = await this.resolveUserForAction(user, "ban");
|
|
298
|
-
|
|
295
|
+
assertCan("ban", user);
|
|
299
296
|
this.assertAuthorizationCanCommit(user);
|
|
300
297
|
const previous = {
|
|
301
298
|
banned: user.banned,
|
|
@@ -317,11 +314,11 @@ let UserService = class UserService {
|
|
|
317
314
|
}
|
|
318
315
|
/** Creates a session that impersonates another user. */
|
|
319
316
|
async impersonateUser(administrator, user, input = {}) {
|
|
320
|
-
|
|
317
|
+
RequestIdentity.assertCurrentUser(administrator);
|
|
321
318
|
user = await this.resolveUserForAction(user, "impersonate");
|
|
322
|
-
|
|
319
|
+
assertCan("impersonate", user);
|
|
323
320
|
if (this.isAdmin(user)) {
|
|
324
|
-
|
|
321
|
+
assertCan("impersonate-admin", user);
|
|
325
322
|
}
|
|
326
323
|
if (this.isActivelyBanned(user)) {
|
|
327
324
|
throw new ForbiddenException("Banned users cannot be impersonated");
|
|
@@ -338,7 +335,7 @@ let UserService = class UserService {
|
|
|
338
335
|
}
|
|
339
336
|
/** Ends impersonation and creates a replacement administrator session. */
|
|
340
337
|
async stopImpersonating(currentSession, input = {}) {
|
|
341
|
-
|
|
338
|
+
RequestIdentity.assertCurrentSession(currentSession);
|
|
342
339
|
const impersonatedByReference = currentSession.impersonatedBy;
|
|
343
340
|
if (!impersonatedByReference)
|
|
344
341
|
return null;
|
|
@@ -370,7 +367,7 @@ let UserService = class UserService {
|
|
|
370
367
|
/** Permanently deletes a user and all dependent authentication records. */
|
|
371
368
|
async deleteUser(user) {
|
|
372
369
|
user = await this.resolveUserForAction(user, "delete");
|
|
373
|
-
|
|
370
|
+
assertCan("delete", user);
|
|
374
371
|
this.assertIdentityRevocationCanCommit(user);
|
|
375
372
|
const deleted = await this.userDeletionService.deleteUser(String(user.id));
|
|
376
373
|
if (deleted === null)
|
|
@@ -382,7 +379,7 @@ let UserService = class UserService {
|
|
|
382
379
|
/** Sets or replaces a user's credential password. */
|
|
383
380
|
async setUserPassword(user, newPassword) {
|
|
384
381
|
user = await this.resolveUserForAction(user, "set-password");
|
|
385
|
-
|
|
382
|
+
assertCan("set-password", user);
|
|
386
383
|
this.assertPasswordLength(newPassword);
|
|
387
384
|
const password = await this.hashPassword(newPassword);
|
|
388
385
|
await this.em.transactional(async (em) => {
|
|
@@ -477,9 +474,8 @@ let UserService = class UserService {
|
|
|
477
474
|
UserService = __decorate([
|
|
478
475
|
Injectable(),
|
|
479
476
|
__param(1, Inject(MODULE_OPTIONS_TOKEN)),
|
|
480
|
-
__param(
|
|
481
|
-
__metadata("design:paramtypes", [EntityManager, Object, HashService,
|
|
482
|
-
AccessControlService, Object])
|
|
477
|
+
__param(3, Inject(UserDeletionService)),
|
|
478
|
+
__metadata("design:paramtypes", [EntityManager, Object, HashService, Object])
|
|
483
479
|
], UserService);
|
|
484
480
|
export { UserService };
|
|
485
481
|
//# sourceMappingURL=user.service.js.map
|