@btffamily/chekaam 1.0.5 → 1.0.6
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/build/middleware/auth.mw.d.ts +1 -1
- package/build/middleware/auth.mw.js +9 -30
- package/build/middleware/db.mw.d.ts +3 -3
- package/build/middleware/db.mw.js +41 -20
- package/build/middleware/redis.mw.js +4 -4
- package/build/middleware/role.mw.d.ts +2 -1
- package/build/middleware/role.mw.js +7 -9
- package/package.json +2 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { IPermitOptions, IRedisConnOptions, IVerifyJWT } from '../utils/interfaces.util';
|
|
2
2
|
export declare const protect: (token: string, secret: string) => IVerifyJWT;
|
|
3
|
-
export declare const authorize: (roles: Array<string>, userRoles: Array<any>, authType: string, authDB: string,
|
|
3
|
+
export declare const authorize: (roles: Array<string>, userRoles: Array<any>, authType: string, authDB: string, _options: IRedisConnOptions) => Promise<boolean>;
|
|
4
4
|
export declare const permit: (data: IPermitOptions) => Promise<boolean>;
|
|
@@ -11,16 +11,11 @@ const auth_service_1 = __importDefault(require("../services/auth.service"));
|
|
|
11
11
|
const checkRole = (roleIds, roles) => {
|
|
12
12
|
let flag = false;
|
|
13
13
|
let mappedRoles = roles.map((x) => {
|
|
14
|
-
|
|
15
|
-
return x._id;
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
return x;
|
|
19
|
-
}
|
|
14
|
+
return x?._id ?? x?.id ?? x;
|
|
20
15
|
});
|
|
21
16
|
for (let i = 0; i < roleIds.length; i++) {
|
|
22
17
|
for (let j = 0; j < mappedRoles.length; j++) {
|
|
23
|
-
if (roleIds[i]
|
|
18
|
+
if (roleIds[i] != null && mappedRoles[j] != null && roleIds[i].toString() === mappedRoles[j].toString()) {
|
|
24
19
|
flag = true;
|
|
25
20
|
}
|
|
26
21
|
}
|
|
@@ -45,30 +40,14 @@ const protect = (token, secret) => {
|
|
|
45
40
|
return result;
|
|
46
41
|
};
|
|
47
42
|
exports.protect = protect;
|
|
48
|
-
const authorize = async (roles, userRoles, authType, authDB,
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const conn = await (0, db_mw_1.connectDB)(authType, authDB);
|
|
52
|
-
const allRoles = await (0, role_mw_1.getRoles)({ conn, roles });
|
|
53
|
-
// get authorized IDs //
|
|
54
|
-
const roleIds = allRoles.map((role) => {
|
|
55
|
-
if (role !== undefined && role !== null) {
|
|
56
|
-
return role._id;
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
// check if user roles matches the authorized roles
|
|
60
|
-
const flag = checkRole(roleIds, userRoles);
|
|
61
|
-
if (flag) {
|
|
62
|
-
result = true;
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
result = false;
|
|
66
|
-
}
|
|
43
|
+
const authorize = async (roles, userRoles, authType, authDB, _options) => {
|
|
44
|
+
if (!Array.isArray(roles) || !roles.length || !Array.isArray(userRoles) || !userRoles.length) {
|
|
45
|
+
return false;
|
|
67
46
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return
|
|
47
|
+
const conn = await (0, db_mw_1.connectDB)(authType, authDB);
|
|
48
|
+
const allRoles = await (0, role_mw_1.getRoles)({ conn, roles });
|
|
49
|
+
const roleIds = allRoles.map((role) => role._id);
|
|
50
|
+
return checkRole(roleIds, userRoles);
|
|
72
51
|
};
|
|
73
52
|
exports.authorize = authorize;
|
|
74
53
|
const permit = async (data) => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Connection, Collection } from 'mongoose';
|
|
2
2
|
export declare const connectDB: (authType: string, authDB: string) => Promise<any>;
|
|
3
3
|
export declare const getModel: (data: {
|
|
4
|
-
conn:
|
|
4
|
+
conn: Connection;
|
|
5
5
|
name: string;
|
|
6
|
-
}) => Promise<
|
|
6
|
+
}) => Promise<Collection>;
|
|
@@ -5,39 +5,60 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.getModel = exports.connectDB = void 0;
|
|
7
7
|
const mongoose_1 = __importDefault(require("mongoose"));
|
|
8
|
-
|
|
8
|
+
const connections = new Map();
|
|
9
9
|
const options = {
|
|
10
|
-
autoIndex:
|
|
11
|
-
maxPoolSize:
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
serverSelectionTimeoutMS: 60000,
|
|
16
|
-
family: 4,
|
|
10
|
+
autoIndex: false,
|
|
11
|
+
maxPoolSize: 10,
|
|
12
|
+
connectTimeoutMS: 10000,
|
|
13
|
+
serverSelectionTimeoutMS: 10000,
|
|
14
|
+
family: 4
|
|
17
15
|
};
|
|
18
16
|
const cert = `${__dirname.split('middleware')[0]}src/ca-certificate.crt`;
|
|
19
17
|
// console.log(__dirname);
|
|
20
18
|
const connectDB = async (authType, authDB) => {
|
|
19
|
+
if (!['development', 'production', 'staging', 'cloud'].includes(authType)) {
|
|
20
|
+
throw new Error('Authentication type is required');
|
|
21
|
+
}
|
|
22
|
+
if (!authDB) {
|
|
23
|
+
throw new Error('Authentication database URI is required');
|
|
24
|
+
}
|
|
25
|
+
const key = JSON.stringify([authType, authDB]);
|
|
26
|
+
const existing = connections.get(key);
|
|
27
|
+
if (existing) {
|
|
28
|
+
return existing;
|
|
29
|
+
}
|
|
30
|
+
const settings = { ...options };
|
|
31
|
+
if (authType === 'cloud') {
|
|
32
|
+
settings.tls = true;
|
|
33
|
+
settings.tlsCAFile = cert;
|
|
34
|
+
}
|
|
35
|
+
const connection = mongoose_1.default.createConnection(authDB, settings);
|
|
36
|
+
const pending = connection.asPromise();
|
|
37
|
+
connections.set(key, pending);
|
|
38
|
+
connection.once('close', () => {
|
|
39
|
+
if (connections.get(key) === pending) {
|
|
40
|
+
connections.delete(key);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
21
43
|
try {
|
|
22
|
-
|
|
23
|
-
|
|
44
|
+
return await pending;
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (connections.get(key) === pending) {
|
|
48
|
+
connections.delete(key);
|
|
24
49
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
connection = mongoose_1.default.createConnection(cloudDBString, options);
|
|
50
|
+
try {
|
|
51
|
+
await connection.destroy();
|
|
28
52
|
}
|
|
29
|
-
|
|
30
|
-
|
|
53
|
+
catch {
|
|
54
|
+
// Preserve the original connection failure if cleanup also fails.
|
|
31
55
|
}
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
catch (err) {
|
|
35
|
-
throw new Error(`${err}`);
|
|
56
|
+
throw error;
|
|
36
57
|
}
|
|
37
58
|
};
|
|
38
59
|
exports.connectDB = connectDB;
|
|
39
60
|
const getModel = async (data) => {
|
|
40
61
|
const { conn, name } = data;
|
|
41
|
-
return
|
|
62
|
+
return conn.collection(name.toLowerCase());
|
|
42
63
|
};
|
|
43
64
|
exports.getModel = getModel;
|
|
@@ -14,10 +14,10 @@ class redisWrapper {
|
|
|
14
14
|
// console.log(colors.yellow.inverse('Connected to REDIS'));
|
|
15
15
|
}
|
|
16
16
|
async keepData(data, exp) {
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
const { key, value } = data;
|
|
18
|
+
const parsed = JSON.stringify(value);
|
|
19
|
+
await this.client.set(key, parsed, {
|
|
20
|
+
EX: exp
|
|
21
21
|
});
|
|
22
22
|
}
|
|
23
23
|
async fetchData(key) {
|
|
@@ -3,17 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getRoles = void 0;
|
|
4
4
|
const db_mw_1 = require("./db.mw");
|
|
5
5
|
const getRoles = async (data) => {
|
|
6
|
-
let result = [];
|
|
7
6
|
const { conn, roles } = data;
|
|
7
|
+
if (!roles.length) {
|
|
8
|
+
return [];
|
|
9
|
+
}
|
|
8
10
|
const Role = await (0, db_mw_1.getModel)({ conn, name: 'roles' });
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (role) {
|
|
13
|
-
result.push(role);
|
|
14
|
-
}
|
|
11
|
+
return Role.find({
|
|
12
|
+
name: {
|
|
13
|
+
$in: roles
|
|
15
14
|
}
|
|
16
|
-
}
|
|
17
|
-
return result;
|
|
15
|
+
}).toArray();
|
|
18
16
|
};
|
|
19
17
|
exports.getRoles = getRoles;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@btffamily/chekaam",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "common node.js codebase for chekaam platform",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"express": "^4.17.1",
|
|
41
41
|
"jsonwebtoken": "^8.5.1",
|
|
42
42
|
"moment": "^2.30.1",
|
|
43
|
-
"mongoose": "^8.
|
|
43
|
+
"mongoose": "^8.24.6",
|
|
44
44
|
"node-nats-streaming": "^0.3.2",
|
|
45
45
|
"redis": "^4.0.6",
|
|
46
46
|
"slugify": "^1.6.6",
|