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.js
CHANGED
|
@@ -96,26 +96,15 @@ function isPlainObject(value) {
|
|
|
96
96
|
var PLATFORM_ROLES = [
|
|
97
97
|
{
|
|
98
98
|
role: "platform_admin",
|
|
99
|
-
permissions: [
|
|
100
|
-
"projects.create",
|
|
101
|
-
"projects.read",
|
|
102
|
-
"projects.update",
|
|
103
|
-
"projects.delete",
|
|
104
|
-
"users.manage",
|
|
105
|
-
"api.manage"
|
|
106
|
-
]
|
|
99
|
+
permissions: []
|
|
107
100
|
},
|
|
108
101
|
{
|
|
109
102
|
role: "platform_manager",
|
|
110
|
-
permissions: [
|
|
111
|
-
"projects.read",
|
|
112
|
-
"projects.update",
|
|
113
|
-
"users.read"
|
|
114
|
-
]
|
|
103
|
+
permissions: []
|
|
115
104
|
},
|
|
116
105
|
{
|
|
117
106
|
role: "platform_user",
|
|
118
|
-
permissions: [
|
|
107
|
+
permissions: []
|
|
119
108
|
}
|
|
120
109
|
];
|
|
121
110
|
function getPermissionsForRoles(roles) {
|
|
@@ -173,17 +162,36 @@ function buildSession(payload) {
|
|
|
173
162
|
return session;
|
|
174
163
|
}
|
|
175
164
|
|
|
165
|
+
// src/models/rolePermission.model.ts
|
|
166
|
+
import mongoose, { Schema } from "mongoose";
|
|
167
|
+
var RolePermissionSchema = new Schema(
|
|
168
|
+
{
|
|
169
|
+
orgId: { type: String, default: null, index: true },
|
|
170
|
+
role: { type: String, required: true },
|
|
171
|
+
permissions: { type: [String], default: [] }
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
timestamps: true
|
|
175
|
+
}
|
|
176
|
+
);
|
|
177
|
+
RolePermissionSchema.index({ orgId: 1, role: 1 }, { unique: true });
|
|
178
|
+
var RolePermissionModel = mongoose.model(
|
|
179
|
+
"RolePermission",
|
|
180
|
+
RolePermissionSchema,
|
|
181
|
+
"role_permissions"
|
|
182
|
+
);
|
|
183
|
+
|
|
176
184
|
// src/models/user.model.ts
|
|
177
|
-
import
|
|
185
|
+
import mongoose2 from "mongoose";
|
|
178
186
|
import { v4 as uuid } from "uuid";
|
|
179
|
-
var MetadataSchema = new
|
|
187
|
+
var MetadataSchema = new mongoose2.Schema(
|
|
180
188
|
{
|
|
181
189
|
key: { type: String, required: true },
|
|
182
|
-
value: { type:
|
|
190
|
+
value: { type: mongoose2.Schema.Types.Mixed, required: true }
|
|
183
191
|
},
|
|
184
192
|
{ _id: false }
|
|
185
193
|
);
|
|
186
|
-
var OrgUserSchema = new
|
|
194
|
+
var OrgUserSchema = new mongoose2.Schema(
|
|
187
195
|
{
|
|
188
196
|
id: { type: String, default: uuid(), index: true },
|
|
189
197
|
email: { type: String, required: true, unique: true },
|
|
@@ -200,7 +208,7 @@ var OrgUserSchema = new mongoose.Schema(
|
|
|
200
208
|
},
|
|
201
209
|
{ timestamps: true, collection: "users" }
|
|
202
210
|
);
|
|
203
|
-
var OrgUser =
|
|
211
|
+
var OrgUser = mongoose2.model("OrgUser", OrgUserSchema);
|
|
204
212
|
|
|
205
213
|
// src/utils/extract.ts
|
|
206
214
|
import { parse as parseCookie } from "cookie";
|
|
@@ -265,6 +273,27 @@ function verifyJwt(token) {
|
|
|
265
273
|
}
|
|
266
274
|
|
|
267
275
|
// src/middlewares/auth.middleware.ts
|
|
276
|
+
async function mergeRolePermissions(session) {
|
|
277
|
+
const roles = Array.isArray(session.roles) ? session.roles : [];
|
|
278
|
+
if (!roles.length) return;
|
|
279
|
+
const orgContexts = /* @__PURE__ */ new Set();
|
|
280
|
+
if (session.orgId) orgContexts.add(session.orgId);
|
|
281
|
+
if (session.org_id) orgContexts.add(session.org_id);
|
|
282
|
+
if (session.projectId) orgContexts.add(session.projectId);
|
|
283
|
+
orgContexts.add(null);
|
|
284
|
+
const docs = await RolePermissionModel.find({
|
|
285
|
+
orgId: { $in: Array.from(orgContexts) },
|
|
286
|
+
role: { $in: roles }
|
|
287
|
+
}).lean().exec();
|
|
288
|
+
const dynamic = /* @__PURE__ */ new Set();
|
|
289
|
+
for (const doc of docs) {
|
|
290
|
+
for (const perm of doc.permissions || []) {
|
|
291
|
+
if (perm) dynamic.add(perm);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
const existing = Array.isArray(session.permissions) ? session.permissions : [];
|
|
295
|
+
session.permissions = Array.from(/* @__PURE__ */ new Set([...existing, ...dynamic]));
|
|
296
|
+
}
|
|
268
297
|
function requireAuth() {
|
|
269
298
|
return async (req, res, next) => {
|
|
270
299
|
try {
|
|
@@ -281,26 +310,32 @@ function requireAuth() {
|
|
|
281
310
|
if (!user) {
|
|
282
311
|
return res.status(401).json({ error: "User not found" });
|
|
283
312
|
}
|
|
284
|
-
const
|
|
313
|
+
const session = buildSession({
|
|
285
314
|
sub: user.id.toString(),
|
|
286
315
|
email: user.email,
|
|
287
|
-
roles: user.roles || []
|
|
316
|
+
roles: user.roles || [],
|
|
317
|
+
orgId: user.orgId,
|
|
318
|
+
org_id: user.orgId,
|
|
319
|
+
projectId: user.projectId
|
|
288
320
|
});
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
321
|
+
session.authType = "api-key";
|
|
322
|
+
session.projectId = readProjectId(req) || user.projectId || void 0;
|
|
323
|
+
await mergeRolePermissions(session);
|
|
324
|
+
req.user = session;
|
|
292
325
|
return next();
|
|
326
|
+
} else {
|
|
327
|
+
const token = extractToken(req);
|
|
328
|
+
if (!token) {
|
|
329
|
+
return res.status(401).json({ error: "Missing token" });
|
|
330
|
+
}
|
|
331
|
+
const claims = await verifyJwt(token);
|
|
332
|
+
const session = buildSession(claims);
|
|
333
|
+
const pid = readProjectId(req);
|
|
334
|
+
if (pid) session.projectId = pid;
|
|
335
|
+
await mergeRolePermissions(session);
|
|
336
|
+
req.user = session;
|
|
337
|
+
next();
|
|
293
338
|
}
|
|
294
|
-
const token = extractToken(req);
|
|
295
|
-
if (!token) {
|
|
296
|
-
return res.status(401).json({ error: "Missing token" });
|
|
297
|
-
}
|
|
298
|
-
const claims = await verifyJwt(token);
|
|
299
|
-
const session = buildSession(claims);
|
|
300
|
-
const pid = readProjectId(req);
|
|
301
|
-
if (pid) session.projectId = pid;
|
|
302
|
-
req.user = session;
|
|
303
|
-
next();
|
|
304
339
|
} catch (e) {
|
|
305
340
|
res.status(401).json({ error: e?.message || "Unauthorized" });
|
|
306
341
|
}
|
|
@@ -313,7 +348,6 @@ function authorize(roles = []) {
|
|
|
313
348
|
if (!user) {
|
|
314
349
|
return res.status(401).json({ error: "Unauthorized" });
|
|
315
350
|
}
|
|
316
|
-
console.log(user, "user");
|
|
317
351
|
const have = new Set((user.roles || []).map(String));
|
|
318
352
|
const ok = roles.some((r) => have.has(r));
|
|
319
353
|
if (!ok) {
|
|
@@ -370,8 +404,8 @@ function validateSendInvite(req, res, next) {
|
|
|
370
404
|
}
|
|
371
405
|
|
|
372
406
|
// src/models/invite.model.ts
|
|
373
|
-
import
|
|
374
|
-
var InviteSchema = new
|
|
407
|
+
import mongoose3 from "mongoose";
|
|
408
|
+
var InviteSchema = new mongoose3.Schema(
|
|
375
409
|
{
|
|
376
410
|
id: { type: String, required: true, index: true },
|
|
377
411
|
email: { type: String, required: true },
|
|
@@ -389,15 +423,15 @@ var InviteSchema = new mongoose2.Schema(
|
|
|
389
423
|
},
|
|
390
424
|
{ timestamps: true, collection: "invites" }
|
|
391
425
|
);
|
|
392
|
-
var Invite =
|
|
426
|
+
var Invite = mongoose3.model("Invite", InviteSchema);
|
|
393
427
|
|
|
394
428
|
// src/services/auth-admin.service.ts
|
|
395
429
|
import bcrypt from "bcrypt";
|
|
396
430
|
import jwt2 from "jsonwebtoken";
|
|
397
431
|
|
|
398
432
|
// src/models/client.model.ts
|
|
399
|
-
import
|
|
400
|
-
var ClientSchema = new
|
|
433
|
+
import mongoose4, { Schema as Schema2 } from "mongoose";
|
|
434
|
+
var ClientSchema = new Schema2(
|
|
401
435
|
{
|
|
402
436
|
clientId: {
|
|
403
437
|
type: String,
|
|
@@ -425,26 +459,7 @@ var ClientSchema = new Schema(
|
|
|
425
459
|
timestamps: true
|
|
426
460
|
}
|
|
427
461
|
);
|
|
428
|
-
var ClientModel =
|
|
429
|
-
|
|
430
|
-
// src/models/rolePermission.model.ts
|
|
431
|
-
import mongoose4, { Schema as Schema2 } from "mongoose";
|
|
432
|
-
var RolePermissionSchema = new Schema2(
|
|
433
|
-
{
|
|
434
|
-
orgId: { type: String, default: null, index: true },
|
|
435
|
-
role: { type: String, required: true },
|
|
436
|
-
permissions: { type: [String], default: [] }
|
|
437
|
-
},
|
|
438
|
-
{
|
|
439
|
-
timestamps: true
|
|
440
|
-
}
|
|
441
|
-
);
|
|
442
|
-
RolePermissionSchema.index({ orgId: 1, role: 1 }, { unique: true });
|
|
443
|
-
var RolePermissionModel = mongoose4.model(
|
|
444
|
-
"RolePermission",
|
|
445
|
-
RolePermissionSchema,
|
|
446
|
-
"role_permissions"
|
|
447
|
-
);
|
|
462
|
+
var ClientModel = mongoose4.models.Client || mongoose4.model("Client", ClientSchema);
|
|
448
463
|
|
|
449
464
|
// src/services/auth-admin.service.ts
|
|
450
465
|
var AuthAdminService = class {
|
|
@@ -1038,16 +1053,18 @@ async function sendRateLimitedEmail({
|
|
|
1038
1053
|
return { rateLimited: false };
|
|
1039
1054
|
}
|
|
1040
1055
|
function generateTokens(user) {
|
|
1041
|
-
const
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1056
|
+
const accessPayload = {
|
|
1057
|
+
sub: user.id.toString(),
|
|
1058
|
+
email: user.email,
|
|
1059
|
+
roles: user.roles || [],
|
|
1060
|
+
orgId: user.orgId || null,
|
|
1061
|
+
org_id: user.orgId || null,
|
|
1062
|
+
projectId: user.projectId || null,
|
|
1063
|
+
type: "user"
|
|
1064
|
+
};
|
|
1065
|
+
const accessToken = jwt4.sign(accessPayload, process.env.JWT_SECRET, {
|
|
1066
|
+
expiresIn: "1h"
|
|
1067
|
+
});
|
|
1051
1068
|
const refreshToken = jwt4.sign(
|
|
1052
1069
|
{ sub: user._id.toString() },
|
|
1053
1070
|
process.env.JWT_SECRET,
|
|
@@ -1941,27 +1958,44 @@ var AuthXSessionDecorator = createParamDecorator(
|
|
|
1941
1958
|
import { Strategy } from "passport";
|
|
1942
1959
|
var AuthXStrategy = class extends Strategy {
|
|
1943
1960
|
name = "authx";
|
|
1944
|
-
authenticate(req) {
|
|
1961
|
+
async authenticate(req) {
|
|
1945
1962
|
try {
|
|
1946
|
-
|
|
1947
|
-
const
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1963
|
+
const apiKey = req.headers["x-api-key"];
|
|
1964
|
+
const userId = req.headers["x-user-id"];
|
|
1965
|
+
if (apiKey) {
|
|
1966
|
+
if (apiKey !== process.env.SERVER_API_KEY) {
|
|
1967
|
+
return this.fail({ message: "Invalid API key" }, 401);
|
|
1968
|
+
}
|
|
1969
|
+
if (!userId) {
|
|
1970
|
+
return this.fail({ message: "User Id is required" }, 401);
|
|
1971
|
+
}
|
|
1972
|
+
const user = await OrgUser.findOne({
|
|
1973
|
+
id: userId,
|
|
1974
|
+
orgId: process.env.ORG_ID || null
|
|
1975
|
+
});
|
|
1976
|
+
if (!user) {
|
|
1977
|
+
return this.fail({ message: "User not found" }, 401);
|
|
1978
|
+
}
|
|
1979
|
+
const session = buildSession(user);
|
|
1957
1980
|
req.user = session;
|
|
1958
1981
|
return this.success(session);
|
|
1959
|
-
}
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1982
|
+
} else {
|
|
1983
|
+
const token = extractToken(req);
|
|
1984
|
+
if (!token) {
|
|
1985
|
+
return this.fail({ message: "Missing token" }, 401);
|
|
1986
|
+
}
|
|
1987
|
+
verifyJwt(token).then((claims) => {
|
|
1988
|
+
const session = buildSession(claims);
|
|
1989
|
+
req.user = session;
|
|
1990
|
+
return this.success(session);
|
|
1991
|
+
}).catch((error) => {
|
|
1992
|
+
return this.fail(
|
|
1993
|
+
{ message: error?.message || "Unauthorized" },
|
|
1994
|
+
401
|
|
1995
|
+
);
|
|
1996
|
+
});
|
|
1997
|
+
}
|
|
1963
1998
|
} catch (error) {
|
|
1964
|
-
console.log("AuthXStrategy.authenticate - exception caught:", error?.message || error);
|
|
1965
1999
|
return this.fail({ message: error?.message || "Unauthorized" }, 401);
|
|
1966
2000
|
}
|
|
1967
2001
|
}
|