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/index.cjs
CHANGED
|
@@ -161,26 +161,15 @@ function isPlainObject(value) {
|
|
|
161
161
|
var PLATFORM_ROLES = [
|
|
162
162
|
{
|
|
163
163
|
role: "platform_admin",
|
|
164
|
-
permissions: [
|
|
165
|
-
"projects.create",
|
|
166
|
-
"projects.read",
|
|
167
|
-
"projects.update",
|
|
168
|
-
"projects.delete",
|
|
169
|
-
"users.manage",
|
|
170
|
-
"api.manage"
|
|
171
|
-
]
|
|
164
|
+
permissions: []
|
|
172
165
|
},
|
|
173
166
|
{
|
|
174
167
|
role: "platform_manager",
|
|
175
|
-
permissions: [
|
|
176
|
-
"projects.read",
|
|
177
|
-
"projects.update",
|
|
178
|
-
"users.read"
|
|
179
|
-
]
|
|
168
|
+
permissions: []
|
|
180
169
|
},
|
|
181
170
|
{
|
|
182
171
|
role: "platform_user",
|
|
183
|
-
permissions: [
|
|
172
|
+
permissions: []
|
|
184
173
|
}
|
|
185
174
|
];
|
|
186
175
|
function getPermissionsForRoles(roles) {
|
|
@@ -238,17 +227,36 @@ function buildSession(payload) {
|
|
|
238
227
|
return session;
|
|
239
228
|
}
|
|
240
229
|
|
|
241
|
-
// src/models/
|
|
230
|
+
// src/models/rolePermission.model.ts
|
|
242
231
|
var import_mongoose = __toESM(require("mongoose"), 1);
|
|
232
|
+
var RolePermissionSchema = new import_mongoose.Schema(
|
|
233
|
+
{
|
|
234
|
+
orgId: { type: String, default: null, index: true },
|
|
235
|
+
role: { type: String, required: true },
|
|
236
|
+
permissions: { type: [String], default: [] }
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
timestamps: true
|
|
240
|
+
}
|
|
241
|
+
);
|
|
242
|
+
RolePermissionSchema.index({ orgId: 1, role: 1 }, { unique: true });
|
|
243
|
+
var RolePermissionModel = import_mongoose.default.model(
|
|
244
|
+
"RolePermission",
|
|
245
|
+
RolePermissionSchema,
|
|
246
|
+
"role_permissions"
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
// src/models/user.model.ts
|
|
250
|
+
var import_mongoose2 = __toESM(require("mongoose"), 1);
|
|
243
251
|
var import_uuid = require("uuid");
|
|
244
|
-
var MetadataSchema = new
|
|
252
|
+
var MetadataSchema = new import_mongoose2.default.Schema(
|
|
245
253
|
{
|
|
246
254
|
key: { type: String, required: true },
|
|
247
|
-
value: { type:
|
|
255
|
+
value: { type: import_mongoose2.default.Schema.Types.Mixed, required: true }
|
|
248
256
|
},
|
|
249
257
|
{ _id: false }
|
|
250
258
|
);
|
|
251
|
-
var OrgUserSchema = new
|
|
259
|
+
var OrgUserSchema = new import_mongoose2.default.Schema(
|
|
252
260
|
{
|
|
253
261
|
id: { type: String, default: (0, import_uuid.v4)(), index: true },
|
|
254
262
|
email: { type: String, required: true, unique: true },
|
|
@@ -265,7 +273,7 @@ var OrgUserSchema = new import_mongoose.default.Schema(
|
|
|
265
273
|
},
|
|
266
274
|
{ timestamps: true, collection: "users" }
|
|
267
275
|
);
|
|
268
|
-
var OrgUser =
|
|
276
|
+
var OrgUser = import_mongoose2.default.model("OrgUser", OrgUserSchema);
|
|
269
277
|
|
|
270
278
|
// src/utils/extract.ts
|
|
271
279
|
var import_cookie = require("cookie");
|
|
@@ -330,6 +338,27 @@ function verifyJwt(token) {
|
|
|
330
338
|
}
|
|
331
339
|
|
|
332
340
|
// src/middlewares/auth.middleware.ts
|
|
341
|
+
async function mergeRolePermissions(session) {
|
|
342
|
+
const roles = Array.isArray(session.roles) ? session.roles : [];
|
|
343
|
+
if (!roles.length) return;
|
|
344
|
+
const orgContexts = /* @__PURE__ */ new Set();
|
|
345
|
+
if (session.orgId) orgContexts.add(session.orgId);
|
|
346
|
+
if (session.org_id) orgContexts.add(session.org_id);
|
|
347
|
+
if (session.projectId) orgContexts.add(session.projectId);
|
|
348
|
+
orgContexts.add(null);
|
|
349
|
+
const docs = await RolePermissionModel.find({
|
|
350
|
+
orgId: { $in: Array.from(orgContexts) },
|
|
351
|
+
role: { $in: roles }
|
|
352
|
+
}).lean().exec();
|
|
353
|
+
const dynamic = /* @__PURE__ */ new Set();
|
|
354
|
+
for (const doc of docs) {
|
|
355
|
+
for (const perm of doc.permissions || []) {
|
|
356
|
+
if (perm) dynamic.add(perm);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
const existing = Array.isArray(session.permissions) ? session.permissions : [];
|
|
360
|
+
session.permissions = Array.from(/* @__PURE__ */ new Set([...existing, ...dynamic]));
|
|
361
|
+
}
|
|
333
362
|
function requireAuth() {
|
|
334
363
|
return async (req, res, next) => {
|
|
335
364
|
try {
|
|
@@ -346,26 +375,32 @@ function requireAuth() {
|
|
|
346
375
|
if (!user) {
|
|
347
376
|
return res.status(401).json({ error: "User not found" });
|
|
348
377
|
}
|
|
349
|
-
const
|
|
378
|
+
const session = buildSession({
|
|
350
379
|
sub: user.id.toString(),
|
|
351
380
|
email: user.email,
|
|
352
|
-
roles: user.roles || []
|
|
381
|
+
roles: user.roles || [],
|
|
382
|
+
orgId: user.orgId,
|
|
383
|
+
org_id: user.orgId,
|
|
384
|
+
projectId: user.projectId
|
|
353
385
|
});
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
386
|
+
session.authType = "api-key";
|
|
387
|
+
session.projectId = readProjectId(req) || user.projectId || void 0;
|
|
388
|
+
await mergeRolePermissions(session);
|
|
389
|
+
req.user = session;
|
|
357
390
|
return next();
|
|
391
|
+
} else {
|
|
392
|
+
const token = extractToken(req);
|
|
393
|
+
if (!token) {
|
|
394
|
+
return res.status(401).json({ error: "Missing token" });
|
|
395
|
+
}
|
|
396
|
+
const claims = await verifyJwt(token);
|
|
397
|
+
const session = buildSession(claims);
|
|
398
|
+
const pid = readProjectId(req);
|
|
399
|
+
if (pid) session.projectId = pid;
|
|
400
|
+
await mergeRolePermissions(session);
|
|
401
|
+
req.user = session;
|
|
402
|
+
next();
|
|
358
403
|
}
|
|
359
|
-
const token = extractToken(req);
|
|
360
|
-
if (!token) {
|
|
361
|
-
return res.status(401).json({ error: "Missing token" });
|
|
362
|
-
}
|
|
363
|
-
const claims = await verifyJwt(token);
|
|
364
|
-
const session = buildSession(claims);
|
|
365
|
-
const pid = readProjectId(req);
|
|
366
|
-
if (pid) session.projectId = pid;
|
|
367
|
-
req.user = session;
|
|
368
|
-
next();
|
|
369
404
|
} catch (e) {
|
|
370
405
|
res.status(401).json({ error: e?.message || "Unauthorized" });
|
|
371
406
|
}
|
|
@@ -378,7 +413,6 @@ function authorize(roles = []) {
|
|
|
378
413
|
if (!user) {
|
|
379
414
|
return res.status(401).json({ error: "Unauthorized" });
|
|
380
415
|
}
|
|
381
|
-
console.log(user, "user");
|
|
382
416
|
const have = new Set((user.roles || []).map(String));
|
|
383
417
|
const ok = roles.some((r) => have.has(r));
|
|
384
418
|
if (!ok) {
|
|
@@ -435,8 +469,8 @@ function validateSendInvite(req, res, next) {
|
|
|
435
469
|
}
|
|
436
470
|
|
|
437
471
|
// src/models/invite.model.ts
|
|
438
|
-
var
|
|
439
|
-
var InviteSchema = new
|
|
472
|
+
var import_mongoose3 = __toESM(require("mongoose"), 1);
|
|
473
|
+
var InviteSchema = new import_mongoose3.default.Schema(
|
|
440
474
|
{
|
|
441
475
|
id: { type: String, required: true, index: true },
|
|
442
476
|
email: { type: String, required: true },
|
|
@@ -454,15 +488,15 @@ var InviteSchema = new import_mongoose2.default.Schema(
|
|
|
454
488
|
},
|
|
455
489
|
{ timestamps: true, collection: "invites" }
|
|
456
490
|
);
|
|
457
|
-
var Invite =
|
|
491
|
+
var Invite = import_mongoose3.default.model("Invite", InviteSchema);
|
|
458
492
|
|
|
459
493
|
// src/services/auth-admin.service.ts
|
|
460
494
|
var import_bcrypt = __toESM(require("bcrypt"), 1);
|
|
461
495
|
var import_jsonwebtoken2 = __toESM(require("jsonwebtoken"), 1);
|
|
462
496
|
|
|
463
497
|
// src/models/client.model.ts
|
|
464
|
-
var
|
|
465
|
-
var ClientSchema = new
|
|
498
|
+
var import_mongoose4 = __toESM(require("mongoose"), 1);
|
|
499
|
+
var ClientSchema = new import_mongoose4.Schema(
|
|
466
500
|
{
|
|
467
501
|
clientId: {
|
|
468
502
|
type: String,
|
|
@@ -490,26 +524,7 @@ var ClientSchema = new import_mongoose3.Schema(
|
|
|
490
524
|
timestamps: true
|
|
491
525
|
}
|
|
492
526
|
);
|
|
493
|
-
var ClientModel =
|
|
494
|
-
|
|
495
|
-
// src/models/rolePermission.model.ts
|
|
496
|
-
var import_mongoose4 = __toESM(require("mongoose"), 1);
|
|
497
|
-
var RolePermissionSchema = new import_mongoose4.Schema(
|
|
498
|
-
{
|
|
499
|
-
orgId: { type: String, default: null, index: true },
|
|
500
|
-
role: { type: String, required: true },
|
|
501
|
-
permissions: { type: [String], default: [] }
|
|
502
|
-
},
|
|
503
|
-
{
|
|
504
|
-
timestamps: true
|
|
505
|
-
}
|
|
506
|
-
);
|
|
507
|
-
RolePermissionSchema.index({ orgId: 1, role: 1 }, { unique: true });
|
|
508
|
-
var RolePermissionModel = import_mongoose4.default.model(
|
|
509
|
-
"RolePermission",
|
|
510
|
-
RolePermissionSchema,
|
|
511
|
-
"role_permissions"
|
|
512
|
-
);
|
|
527
|
+
var ClientModel = import_mongoose4.default.models.Client || import_mongoose4.default.model("Client", ClientSchema);
|
|
513
528
|
|
|
514
529
|
// src/services/auth-admin.service.ts
|
|
515
530
|
var AuthAdminService = class {
|
|
@@ -1103,16 +1118,18 @@ async function sendRateLimitedEmail({
|
|
|
1103
1118
|
return { rateLimited: false };
|
|
1104
1119
|
}
|
|
1105
1120
|
function generateTokens(user) {
|
|
1106
|
-
const
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1121
|
+
const accessPayload = {
|
|
1122
|
+
sub: user.id.toString(),
|
|
1123
|
+
email: user.email,
|
|
1124
|
+
roles: user.roles || [],
|
|
1125
|
+
orgId: user.orgId || null,
|
|
1126
|
+
org_id: user.orgId || null,
|
|
1127
|
+
projectId: user.projectId || null,
|
|
1128
|
+
type: "user"
|
|
1129
|
+
};
|
|
1130
|
+
const accessToken = import_jsonwebtoken4.default.sign(accessPayload, process.env.JWT_SECRET, {
|
|
1131
|
+
expiresIn: "1h"
|
|
1132
|
+
});
|
|
1116
1133
|
const refreshToken = import_jsonwebtoken4.default.sign(
|
|
1117
1134
|
{ sub: user._id.toString() },
|
|
1118
1135
|
process.env.JWT_SECRET,
|
|
@@ -2002,27 +2019,44 @@ var AuthXSessionDecorator = (0, import_common4.createParamDecorator)(
|
|
|
2002
2019
|
var import_passport2 = require("passport");
|
|
2003
2020
|
var AuthXStrategy = class extends import_passport2.Strategy {
|
|
2004
2021
|
name = "authx";
|
|
2005
|
-
authenticate(req) {
|
|
2022
|
+
async authenticate(req) {
|
|
2006
2023
|
try {
|
|
2007
|
-
|
|
2008
|
-
const
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2024
|
+
const apiKey = req.headers["x-api-key"];
|
|
2025
|
+
const userId = req.headers["x-user-id"];
|
|
2026
|
+
if (apiKey) {
|
|
2027
|
+
if (apiKey !== process.env.SERVER_API_KEY) {
|
|
2028
|
+
return this.fail({ message: "Invalid API key" }, 401);
|
|
2029
|
+
}
|
|
2030
|
+
if (!userId) {
|
|
2031
|
+
return this.fail({ message: "User Id is required" }, 401);
|
|
2032
|
+
}
|
|
2033
|
+
const user = await OrgUser.findOne({
|
|
2034
|
+
id: userId,
|
|
2035
|
+
orgId: process.env.ORG_ID || null
|
|
2036
|
+
});
|
|
2037
|
+
if (!user) {
|
|
2038
|
+
return this.fail({ message: "User not found" }, 401);
|
|
2039
|
+
}
|
|
2040
|
+
const session = buildSession(user);
|
|
2018
2041
|
req.user = session;
|
|
2019
2042
|
return this.success(session);
|
|
2020
|
-
}
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2043
|
+
} else {
|
|
2044
|
+
const token = extractToken(req);
|
|
2045
|
+
if (!token) {
|
|
2046
|
+
return this.fail({ message: "Missing token" }, 401);
|
|
2047
|
+
}
|
|
2048
|
+
verifyJwt(token).then((claims) => {
|
|
2049
|
+
const session = buildSession(claims);
|
|
2050
|
+
req.user = session;
|
|
2051
|
+
return this.success(session);
|
|
2052
|
+
}).catch((error) => {
|
|
2053
|
+
return this.fail(
|
|
2054
|
+
{ message: error?.message || "Unauthorized" },
|
|
2055
|
+
401
|
|
2056
|
+
);
|
|
2057
|
+
});
|
|
2058
|
+
}
|
|
2024
2059
|
} catch (error) {
|
|
2025
|
-
console.log("AuthXStrategy.authenticate - exception caught:", error?.message || error);
|
|
2026
2060
|
return this.fail({ message: error?.message || "Unauthorized" }, 401);
|
|
2027
2061
|
}
|
|
2028
2062
|
}
|