aaspai-authx 0.0.2 → 0.0.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/dist/express/index.cjs +87 -69
- package/dist/express/index.cjs.map +1 -1
- package/dist/express/index.js +87 -69
- package/dist/express/index.js.map +1 -1
- package/dist/index.cjs +121 -87
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +20 -20
- package/dist/index.d.ts +20 -20
- package/dist/index.js +121 -87
- package/dist/index.js.map +1 -1
- package/dist/nest/index.cjs +87 -69
- package/dist/nest/index.cjs.map +1 -1
- package/dist/nest/index.js +87 -69
- package/dist/nest/index.js.map +1 -1
- package/package.json +1 -1
package/dist/express/index.js
CHANGED
|
@@ -71,26 +71,15 @@ function isPlainObject(value) {
|
|
|
71
71
|
var PLATFORM_ROLES = [
|
|
72
72
|
{
|
|
73
73
|
role: "platform_admin",
|
|
74
|
-
permissions: [
|
|
75
|
-
"projects.create",
|
|
76
|
-
"projects.read",
|
|
77
|
-
"projects.update",
|
|
78
|
-
"projects.delete",
|
|
79
|
-
"users.manage",
|
|
80
|
-
"api.manage"
|
|
81
|
-
]
|
|
74
|
+
permissions: []
|
|
82
75
|
},
|
|
83
76
|
{
|
|
84
77
|
role: "platform_manager",
|
|
85
|
-
permissions: [
|
|
86
|
-
"projects.read",
|
|
87
|
-
"projects.update",
|
|
88
|
-
"users.read"
|
|
89
|
-
]
|
|
78
|
+
permissions: []
|
|
90
79
|
},
|
|
91
80
|
{
|
|
92
81
|
role: "platform_user",
|
|
93
|
-
permissions: [
|
|
82
|
+
permissions: []
|
|
94
83
|
}
|
|
95
84
|
];
|
|
96
85
|
function getPermissionsForRoles(roles) {
|
|
@@ -148,17 +137,36 @@ function buildSession(payload) {
|
|
|
148
137
|
return session;
|
|
149
138
|
}
|
|
150
139
|
|
|
140
|
+
// src/models/rolePermission.model.ts
|
|
141
|
+
import mongoose, { Schema } from "mongoose";
|
|
142
|
+
var RolePermissionSchema = new Schema(
|
|
143
|
+
{
|
|
144
|
+
orgId: { type: String, default: null, index: true },
|
|
145
|
+
role: { type: String, required: true },
|
|
146
|
+
permissions: { type: [String], default: [] }
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
timestamps: true
|
|
150
|
+
}
|
|
151
|
+
);
|
|
152
|
+
RolePermissionSchema.index({ orgId: 1, role: 1 }, { unique: true });
|
|
153
|
+
var RolePermissionModel = mongoose.model(
|
|
154
|
+
"RolePermission",
|
|
155
|
+
RolePermissionSchema,
|
|
156
|
+
"role_permissions"
|
|
157
|
+
);
|
|
158
|
+
|
|
151
159
|
// src/models/user.model.ts
|
|
152
|
-
import
|
|
160
|
+
import mongoose2 from "mongoose";
|
|
153
161
|
import { v4 as uuid } from "uuid";
|
|
154
|
-
var MetadataSchema = new
|
|
162
|
+
var MetadataSchema = new mongoose2.Schema(
|
|
155
163
|
{
|
|
156
164
|
key: { type: String, required: true },
|
|
157
|
-
value: { type:
|
|
165
|
+
value: { type: mongoose2.Schema.Types.Mixed, required: true }
|
|
158
166
|
},
|
|
159
167
|
{ _id: false }
|
|
160
168
|
);
|
|
161
|
-
var OrgUserSchema = new
|
|
169
|
+
var OrgUserSchema = new mongoose2.Schema(
|
|
162
170
|
{
|
|
163
171
|
id: { type: String, default: uuid(), index: true },
|
|
164
172
|
email: { type: String, required: true, unique: true },
|
|
@@ -175,7 +183,7 @@ var OrgUserSchema = new mongoose.Schema(
|
|
|
175
183
|
},
|
|
176
184
|
{ timestamps: true, collection: "users" }
|
|
177
185
|
);
|
|
178
|
-
var OrgUser =
|
|
186
|
+
var OrgUser = mongoose2.model("OrgUser", OrgUserSchema);
|
|
179
187
|
|
|
180
188
|
// src/utils/extract.ts
|
|
181
189
|
import { parse as parseCookie } from "cookie";
|
|
@@ -240,6 +248,27 @@ function verifyJwt(token) {
|
|
|
240
248
|
}
|
|
241
249
|
|
|
242
250
|
// src/middlewares/auth.middleware.ts
|
|
251
|
+
async function mergeRolePermissions(session) {
|
|
252
|
+
const roles = Array.isArray(session.roles) ? session.roles : [];
|
|
253
|
+
if (!roles.length) return;
|
|
254
|
+
const orgContexts = /* @__PURE__ */ new Set();
|
|
255
|
+
if (session.orgId) orgContexts.add(session.orgId);
|
|
256
|
+
if (session.org_id) orgContexts.add(session.org_id);
|
|
257
|
+
if (session.projectId) orgContexts.add(session.projectId);
|
|
258
|
+
orgContexts.add(null);
|
|
259
|
+
const docs = await RolePermissionModel.find({
|
|
260
|
+
orgId: { $in: Array.from(orgContexts) },
|
|
261
|
+
role: { $in: roles }
|
|
262
|
+
}).lean().exec();
|
|
263
|
+
const dynamic = /* @__PURE__ */ new Set();
|
|
264
|
+
for (const doc of docs) {
|
|
265
|
+
for (const perm of doc.permissions || []) {
|
|
266
|
+
if (perm) dynamic.add(perm);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
const existing = Array.isArray(session.permissions) ? session.permissions : [];
|
|
270
|
+
session.permissions = Array.from(/* @__PURE__ */ new Set([...existing, ...dynamic]));
|
|
271
|
+
}
|
|
243
272
|
function requireAuth() {
|
|
244
273
|
return async (req, res, next) => {
|
|
245
274
|
try {
|
|
@@ -256,26 +285,32 @@ function requireAuth() {
|
|
|
256
285
|
if (!user) {
|
|
257
286
|
return res.status(401).json({ error: "User not found" });
|
|
258
287
|
}
|
|
259
|
-
const
|
|
288
|
+
const session = buildSession({
|
|
260
289
|
sub: user.id.toString(),
|
|
261
290
|
email: user.email,
|
|
262
|
-
roles: user.roles || []
|
|
291
|
+
roles: user.roles || [],
|
|
292
|
+
orgId: user.orgId,
|
|
293
|
+
org_id: user.orgId,
|
|
294
|
+
projectId: user.projectId
|
|
263
295
|
});
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
296
|
+
session.authType = "api-key";
|
|
297
|
+
session.projectId = readProjectId(req) || user.projectId || void 0;
|
|
298
|
+
await mergeRolePermissions(session);
|
|
299
|
+
req.user = session;
|
|
267
300
|
return next();
|
|
301
|
+
} else {
|
|
302
|
+
const token = extractToken(req);
|
|
303
|
+
if (!token) {
|
|
304
|
+
return res.status(401).json({ error: "Missing token" });
|
|
305
|
+
}
|
|
306
|
+
const claims = await verifyJwt(token);
|
|
307
|
+
const session = buildSession(claims);
|
|
308
|
+
const pid = readProjectId(req);
|
|
309
|
+
if (pid) session.projectId = pid;
|
|
310
|
+
await mergeRolePermissions(session);
|
|
311
|
+
req.user = session;
|
|
312
|
+
next();
|
|
268
313
|
}
|
|
269
|
-
const token = extractToken(req);
|
|
270
|
-
if (!token) {
|
|
271
|
-
return res.status(401).json({ error: "Missing token" });
|
|
272
|
-
}
|
|
273
|
-
const claims = await verifyJwt(token);
|
|
274
|
-
const session = buildSession(claims);
|
|
275
|
-
const pid = readProjectId(req);
|
|
276
|
-
if (pid) session.projectId = pid;
|
|
277
|
-
req.user = session;
|
|
278
|
-
next();
|
|
279
314
|
} catch (e) {
|
|
280
315
|
res.status(401).json({ error: e?.message || "Unauthorized" });
|
|
281
316
|
}
|
|
@@ -329,8 +364,8 @@ function validateSendInvite(req, res, next) {
|
|
|
329
364
|
}
|
|
330
365
|
|
|
331
366
|
// src/models/invite.model.ts
|
|
332
|
-
import
|
|
333
|
-
var InviteSchema = new
|
|
367
|
+
import mongoose3 from "mongoose";
|
|
368
|
+
var InviteSchema = new mongoose3.Schema(
|
|
334
369
|
{
|
|
335
370
|
id: { type: String, required: true, index: true },
|
|
336
371
|
email: { type: String, required: true },
|
|
@@ -348,15 +383,15 @@ var InviteSchema = new mongoose2.Schema(
|
|
|
348
383
|
},
|
|
349
384
|
{ timestamps: true, collection: "invites" }
|
|
350
385
|
);
|
|
351
|
-
var Invite =
|
|
386
|
+
var Invite = mongoose3.model("Invite", InviteSchema);
|
|
352
387
|
|
|
353
388
|
// src/services/auth-admin.service.ts
|
|
354
389
|
import bcrypt from "bcrypt";
|
|
355
390
|
import jwt2 from "jsonwebtoken";
|
|
356
391
|
|
|
357
392
|
// src/models/client.model.ts
|
|
358
|
-
import
|
|
359
|
-
var ClientSchema = new
|
|
393
|
+
import mongoose4, { Schema as Schema2 } from "mongoose";
|
|
394
|
+
var ClientSchema = new Schema2(
|
|
360
395
|
{
|
|
361
396
|
clientId: {
|
|
362
397
|
type: String,
|
|
@@ -384,26 +419,7 @@ var ClientSchema = new Schema(
|
|
|
384
419
|
timestamps: true
|
|
385
420
|
}
|
|
386
421
|
);
|
|
387
|
-
var ClientModel =
|
|
388
|
-
|
|
389
|
-
// src/models/rolePermission.model.ts
|
|
390
|
-
import mongoose4, { Schema as Schema2 } from "mongoose";
|
|
391
|
-
var RolePermissionSchema = new Schema2(
|
|
392
|
-
{
|
|
393
|
-
orgId: { type: String, default: null, index: true },
|
|
394
|
-
role: { type: String, required: true },
|
|
395
|
-
permissions: { type: [String], default: [] }
|
|
396
|
-
},
|
|
397
|
-
{
|
|
398
|
-
timestamps: true
|
|
399
|
-
}
|
|
400
|
-
);
|
|
401
|
-
RolePermissionSchema.index({ orgId: 1, role: 1 }, { unique: true });
|
|
402
|
-
var RolePermissionModel = mongoose4.model(
|
|
403
|
-
"RolePermission",
|
|
404
|
-
RolePermissionSchema,
|
|
405
|
-
"role_permissions"
|
|
406
|
-
);
|
|
422
|
+
var ClientModel = mongoose4.models.Client || mongoose4.model("Client", ClientSchema);
|
|
407
423
|
|
|
408
424
|
// src/services/auth-admin.service.ts
|
|
409
425
|
var AuthAdminService = class {
|
|
@@ -997,16 +1013,18 @@ async function sendRateLimitedEmail({
|
|
|
997
1013
|
return { rateLimited: false };
|
|
998
1014
|
}
|
|
999
1015
|
function generateTokens(user) {
|
|
1000
|
-
const
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1016
|
+
const accessPayload = {
|
|
1017
|
+
sub: user.id.toString(),
|
|
1018
|
+
email: user.email,
|
|
1019
|
+
roles: user.roles || [],
|
|
1020
|
+
orgId: user.orgId || null,
|
|
1021
|
+
org_id: user.orgId || null,
|
|
1022
|
+
projectId: user.projectId || null,
|
|
1023
|
+
type: "user"
|
|
1024
|
+
};
|
|
1025
|
+
const accessToken = jwt4.sign(accessPayload, process.env.JWT_SECRET, {
|
|
1026
|
+
expiresIn: "1h"
|
|
1027
|
+
});
|
|
1010
1028
|
const refreshToken = jwt4.sign(
|
|
1011
1029
|
{ sub: user._id.toString() },
|
|
1012
1030
|
process.env.JWT_SECRET,
|