speedly 1.2.12 → 1.2.15
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/cjs/auth/auth.d.ts +2 -9
- package/dist/cjs/auth/auth.js +26 -80
- package/dist/cjs/auth/auth2.d.ts +18 -0
- package/dist/cjs/auth/auth2.js +93 -0
- package/dist/cjs/auth/types.d.ts +19 -0
- package/dist/cjs/auth/types.js +2 -0
- package/dist/esm/auth/auth.d.ts +2 -9
- package/dist/esm/auth/auth.js +26 -80
- package/dist/esm/auth/types.d.ts +19 -0
- package/dist/esm/auth/types.js +2 -0
- package/package.json +1 -1
package/dist/cjs/auth/auth.d.ts
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
declare const auth:
|
|
3
|
-
admin: {
|
|
4
|
-
role: string;
|
|
5
|
-
model: string;
|
|
6
|
-
};
|
|
7
|
-
jwtSecretEnv: string;
|
|
8
|
-
customValidator: (req: Request, key: string) => boolean;
|
|
9
|
-
}) => (req: Request, res: Response, next: NextFunction) => Promise<unknown>;
|
|
1
|
+
import * as Types from "./types";
|
|
2
|
+
declare const auth: Types.Auth;
|
|
10
3
|
export default auth;
|
package/dist/cjs/auth/auth.js
CHANGED
|
@@ -4,85 +4,31 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const getConfig_1 = __importDefault(require("../util/getConfig"));
|
|
7
|
-
const gConfig = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
next({ status: 405, json: { message: 'you don\'t have access to this section' } });
|
|
34
|
-
}
|
|
35
|
-
else
|
|
36
|
-
continue;
|
|
37
|
-
}
|
|
38
|
-
// اینجا اگر i === keys.length - 1 باشد یعنی آخرین آیتم هستی
|
|
39
|
-
// }else if (!req.cookies.AT_SECRET) {
|
|
40
|
-
// if (!handlerState.user || !handlerState.user?.handlers?.length) return res.status(403).json({ message: 'you dont have access for this section' })
|
|
41
|
-
// handlerState.user.handlers[0](req ,res , nextFunc(handlerState.user.handlers))
|
|
42
|
-
// }else {
|
|
43
|
-
// if(!handlerState.admin || !handlerState.admin?.handlers?.length) {
|
|
44
|
-
// if (!handlerState.user || !handlerState.user?.handlers?.length) return res.status(404).json({ message: 'route not found :(' })
|
|
45
|
-
// handlerState.user.handlers[0](req ,res , nextFunc(handlerState.user.handlers))
|
|
46
|
-
// } else {
|
|
47
|
-
// const tokenPayload = jwt.verify(req.cookies.AT_SECRET , process.env[gConfig.jwtSecretEnv])
|
|
48
|
-
// const adminDoc = await adminModel.findById(tokenPayload.id)
|
|
49
|
-
// if(!adminDoc)return res.status(403).json({ message: 'you don\'t have access for this section' })
|
|
50
|
-
// if(adminDoc.role !='OWNER' && adminDoc.role != config?.admin?.role ) return res.status(403).json({ message: 'you dont have access for this section' })
|
|
51
|
-
// req.admin = adminDoc
|
|
52
|
-
// handlerState.admin.handlers[0](req , res , nextFunc(handlerState.admin.handlers))
|
|
53
|
-
// }
|
|
54
|
-
// }
|
|
55
|
-
}
|
|
56
|
-
catch (error) {
|
|
57
|
-
console.log('auth', 42, error);
|
|
58
|
-
next({ status: 403, json: { message: (error instanceof Error ? error.message : 'error on authentication please login again') } });
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
holders.admin = (...handlers) => {
|
|
62
|
-
if (!Array.isArray(handlers))
|
|
63
|
-
throw new Error('handlers must be an array');
|
|
64
|
-
const hasConfig = typeof handlers[0] === 'object' && 'permission' in handlers[0];
|
|
65
|
-
const configObj = hasConfig ? handlers[0] : undefined;
|
|
66
|
-
const handlerFns = hasConfig ? handlers.slice(1) : handlers;
|
|
67
|
-
handlerState[`admin${configObj?.permission ? `:${configObj.permission}` : ''}`] = {
|
|
68
|
-
...(configObj ? { config: configObj } : {}),
|
|
69
|
-
handlers: handlerFns
|
|
70
|
-
};
|
|
71
|
-
return useAuth;
|
|
72
|
-
};
|
|
73
|
-
holders.user = (...handlers) => {
|
|
74
|
-
if (!Array.isArray(handlers))
|
|
75
|
-
throw new Error('handlers must be an array');
|
|
76
|
-
handlerState.user = { handlers };
|
|
77
|
-
return useAuth;
|
|
78
|
-
};
|
|
79
|
-
holders.any = (...handlers) => {
|
|
80
|
-
if (!Array.isArray(handlers))
|
|
81
|
-
throw new Error('handlers must be an array');
|
|
82
|
-
handlerState.any = { handlers };
|
|
83
|
-
return useAuth;
|
|
84
|
-
};
|
|
85
|
-
Object.assign(useAuth, holders);
|
|
86
|
-
return useAuth;
|
|
7
|
+
const gConfig = {
|
|
8
|
+
admin: { role: "ADMIN", model: "../models/admin" },
|
|
9
|
+
jwtSecretEnv: "JWT_KEY",
|
|
10
|
+
customValidator: (req, key) => {
|
|
11
|
+
return true;
|
|
12
|
+
},
|
|
13
|
+
...(0, getConfig_1.default)("auth"),
|
|
14
|
+
};
|
|
15
|
+
const executer = (authType) => async (req, res, next) => {
|
|
16
|
+
const accessResult = await gConfig?.customValidator?.(req, authType);
|
|
17
|
+
if (accessResult == null)
|
|
18
|
+
return next({ status: 401, json: { message: 'Unauthorized' } });
|
|
19
|
+
if (!accessResult)
|
|
20
|
+
return next({ status: 403, json: { message: 'Forbidden' } });
|
|
21
|
+
return next();
|
|
22
|
+
};
|
|
23
|
+
const auth = {
|
|
24
|
+
user: () => {
|
|
25
|
+
return executer("user");
|
|
26
|
+
},
|
|
27
|
+
admin: (config) => {
|
|
28
|
+
return executer(`admin${config?.permission ? `:${config.permission}` : ''}`);
|
|
29
|
+
},
|
|
30
|
+
any: () => {
|
|
31
|
+
return executer("any");
|
|
32
|
+
},
|
|
87
33
|
};
|
|
88
34
|
exports.default = auth;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
type Handler = (req: Request, res: Response, next: (errorMessage?: string) => unknown) => unknown;
|
|
3
|
+
type UseAuth = (req: Request, res: Response, next: NextFunction) => UseAuth | {
|
|
4
|
+
user?: (...handlers: Handler[]) => unknown;
|
|
5
|
+
admin?: (...handlers: [{
|
|
6
|
+
permission: string;
|
|
7
|
+
}, ...Handler[]] | Handler[]) => unknown;
|
|
8
|
+
any?: (...handlers: Handler[]) => unknown;
|
|
9
|
+
};
|
|
10
|
+
declare const auth: (config?: {
|
|
11
|
+
admin: {
|
|
12
|
+
role: string;
|
|
13
|
+
model: string;
|
|
14
|
+
};
|
|
15
|
+
jwtSecretEnv: string;
|
|
16
|
+
customValidator: (req: Request, key: string) => boolean;
|
|
17
|
+
}) => UseAuth;
|
|
18
|
+
export default auth;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const getConfig_1 = __importDefault(require("../util/getConfig"));
|
|
7
|
+
const gConfig = {
|
|
8
|
+
admin: { role: "ADMIN", model: "../models/admin" },
|
|
9
|
+
jwtSecretEnv: "JWT_KEY",
|
|
10
|
+
customValidator: (req, key) => {
|
|
11
|
+
return true;
|
|
12
|
+
},
|
|
13
|
+
...(0, getConfig_1.default)("auth"),
|
|
14
|
+
};
|
|
15
|
+
const holders = {};
|
|
16
|
+
const auth = (config = gConfig) => {
|
|
17
|
+
// const adminModel = require('../models/admin')
|
|
18
|
+
let handlerState = {};
|
|
19
|
+
let useAuth = async (req, res, next) => {
|
|
20
|
+
try {
|
|
21
|
+
const nextFunc = (handlers, index = 0) => (errorMessage = "") => {
|
|
22
|
+
if (errorMessage)
|
|
23
|
+
return next(errorMessage);
|
|
24
|
+
if (!handlers.length || !handlers[index + 1])
|
|
25
|
+
return next();
|
|
26
|
+
handlers[index + 1](req, res, nextFunc(handlers, index + 1));
|
|
27
|
+
};
|
|
28
|
+
const keys = Object.keys(handlerState);
|
|
29
|
+
for (let i = 0; i < keys.length; i++) {
|
|
30
|
+
const key = keys[i];
|
|
31
|
+
if (!handlerState[key]?.handlers?.length)
|
|
32
|
+
continue;
|
|
33
|
+
if (await gConfig?.customValidator?.(req, key)) {
|
|
34
|
+
return await handlerState[key].handlers[0](req, res, nextFunc(handlerState[key].handlers));
|
|
35
|
+
}
|
|
36
|
+
else if ((await gConfig?.customValidator?.(req, key)) == null) {
|
|
37
|
+
return next({ status: 401, json: { message: "unauthorized" } });
|
|
38
|
+
}
|
|
39
|
+
else if (i === keys.length - 1) {
|
|
40
|
+
next({
|
|
41
|
+
status: 405,
|
|
42
|
+
json: { message: "you don't have access to this section" },
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
else
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
console.log("auth", 42, error);
|
|
51
|
+
next({
|
|
52
|
+
status: 403,
|
|
53
|
+
json: {
|
|
54
|
+
message: error instanceof Error
|
|
55
|
+
? error.message
|
|
56
|
+
: "error on authentication please login again",
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
holders.admin = (...handlers) => {
|
|
62
|
+
if (!Array.isArray(handlers))
|
|
63
|
+
throw new Error("handlers must be an array");
|
|
64
|
+
const hasConfig = typeof handlers[0] === "object" && "permission" in handlers[0];
|
|
65
|
+
const configObj = hasConfig
|
|
66
|
+
? handlers[0]
|
|
67
|
+
: undefined;
|
|
68
|
+
const handlerFns = hasConfig
|
|
69
|
+
? handlers.slice(1)
|
|
70
|
+
: handlers;
|
|
71
|
+
handlerState[`admin${configObj?.permission ? `:${configObj.permission}` : ""}`] = {
|
|
72
|
+
...(configObj ? { config: configObj } : {}),
|
|
73
|
+
handlers: handlerFns,
|
|
74
|
+
};
|
|
75
|
+
return useAuth;
|
|
76
|
+
};
|
|
77
|
+
holders.user = (...handlers) => {
|
|
78
|
+
if (!Array.isArray(handlers))
|
|
79
|
+
throw new Error("handlers must be an array");
|
|
80
|
+
handlerState.user = { handlers };
|
|
81
|
+
return useAuth;
|
|
82
|
+
};
|
|
83
|
+
holders.any = (...handlers) => {
|
|
84
|
+
if (!Array.isArray(handlers))
|
|
85
|
+
throw new Error("handlers must be an array");
|
|
86
|
+
handlerState.any = { handlers };
|
|
87
|
+
return useAuth;
|
|
88
|
+
};
|
|
89
|
+
useAuth = Object.assign(useAuth, holders);
|
|
90
|
+
return useAuth;
|
|
91
|
+
};
|
|
92
|
+
console.log("auth", 81, typeof auth);
|
|
93
|
+
exports.default = auth;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
type Handler = (req: Request, res: Response, next: (errorMessage?: string) => unknown) => unknown;
|
|
3
|
+
type Executer = (authField: string) => (req: Request, res: Response, next: NextFunction) => unknown;
|
|
4
|
+
type Auth = {
|
|
5
|
+
user: () => Handler;
|
|
6
|
+
admin: (config?: {
|
|
7
|
+
permission: string;
|
|
8
|
+
}) => Handler;
|
|
9
|
+
any: () => Handler;
|
|
10
|
+
};
|
|
11
|
+
type ConfigType = {
|
|
12
|
+
customValidator?: (req: Request, key: string) => Promise<boolean | null>;
|
|
13
|
+
jwtSecretEnv?: string;
|
|
14
|
+
admin?: {
|
|
15
|
+
role: string;
|
|
16
|
+
model: string;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export { Handler, Executer, Auth, ConfigType };
|
package/dist/esm/auth/auth.d.ts
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
declare const auth:
|
|
3
|
-
admin: {
|
|
4
|
-
role: string;
|
|
5
|
-
model: string;
|
|
6
|
-
};
|
|
7
|
-
jwtSecretEnv: string;
|
|
8
|
-
customValidator: (req: Request, key: string) => boolean;
|
|
9
|
-
}) => (req: Request, res: Response, next: NextFunction) => Promise<unknown>;
|
|
1
|
+
import * as Types from "./types";
|
|
2
|
+
declare const auth: Types.Auth;
|
|
10
3
|
export default auth;
|
package/dist/esm/auth/auth.js
CHANGED
|
@@ -4,85 +4,31 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const getConfig_1 = __importDefault(require("../util/getConfig"));
|
|
7
|
-
const gConfig = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
next({ status: 405, json: { message: 'you don\'t have access to this section' } });
|
|
34
|
-
}
|
|
35
|
-
else
|
|
36
|
-
continue;
|
|
37
|
-
}
|
|
38
|
-
// اینجا اگر i === keys.length - 1 باشد یعنی آخرین آیتم هستی
|
|
39
|
-
// }else if (!req.cookies.AT_SECRET) {
|
|
40
|
-
// if (!handlerState.user || !handlerState.user?.handlers?.length) return res.status(403).json({ message: 'you dont have access for this section' })
|
|
41
|
-
// handlerState.user.handlers[0](req ,res , nextFunc(handlerState.user.handlers))
|
|
42
|
-
// }else {
|
|
43
|
-
// if(!handlerState.admin || !handlerState.admin?.handlers?.length) {
|
|
44
|
-
// if (!handlerState.user || !handlerState.user?.handlers?.length) return res.status(404).json({ message: 'route not found :(' })
|
|
45
|
-
// handlerState.user.handlers[0](req ,res , nextFunc(handlerState.user.handlers))
|
|
46
|
-
// } else {
|
|
47
|
-
// const tokenPayload = jwt.verify(req.cookies.AT_SECRET , process.env[gConfig.jwtSecretEnv])
|
|
48
|
-
// const adminDoc = await adminModel.findById(tokenPayload.id)
|
|
49
|
-
// if(!adminDoc)return res.status(403).json({ message: 'you don\'t have access for this section' })
|
|
50
|
-
// if(adminDoc.role !='OWNER' && adminDoc.role != config?.admin?.role ) return res.status(403).json({ message: 'you dont have access for this section' })
|
|
51
|
-
// req.admin = adminDoc
|
|
52
|
-
// handlerState.admin.handlers[0](req , res , nextFunc(handlerState.admin.handlers))
|
|
53
|
-
// }
|
|
54
|
-
// }
|
|
55
|
-
}
|
|
56
|
-
catch (error) {
|
|
57
|
-
console.log('auth', 42, error);
|
|
58
|
-
next({ status: 403, json: { message: (error instanceof Error ? error.message : 'error on authentication please login again') } });
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
holders.admin = (...handlers) => {
|
|
62
|
-
if (!Array.isArray(handlers))
|
|
63
|
-
throw new Error('handlers must be an array');
|
|
64
|
-
const hasConfig = typeof handlers[0] === 'object' && 'permission' in handlers[0];
|
|
65
|
-
const configObj = hasConfig ? handlers[0] : undefined;
|
|
66
|
-
const handlerFns = hasConfig ? handlers.slice(1) : handlers;
|
|
67
|
-
handlerState[`admin${configObj?.permission ? `:${configObj.permission}` : ''}`] = {
|
|
68
|
-
...(configObj ? { config: configObj } : {}),
|
|
69
|
-
handlers: handlerFns
|
|
70
|
-
};
|
|
71
|
-
return useAuth;
|
|
72
|
-
};
|
|
73
|
-
holders.user = (...handlers) => {
|
|
74
|
-
if (!Array.isArray(handlers))
|
|
75
|
-
throw new Error('handlers must be an array');
|
|
76
|
-
handlerState.user = { handlers };
|
|
77
|
-
return useAuth;
|
|
78
|
-
};
|
|
79
|
-
holders.any = (...handlers) => {
|
|
80
|
-
if (!Array.isArray(handlers))
|
|
81
|
-
throw new Error('handlers must be an array');
|
|
82
|
-
handlerState.any = { handlers };
|
|
83
|
-
return useAuth;
|
|
84
|
-
};
|
|
85
|
-
Object.assign(useAuth, holders);
|
|
86
|
-
return useAuth;
|
|
7
|
+
const gConfig = {
|
|
8
|
+
admin: { role: "ADMIN", model: "../models/admin" },
|
|
9
|
+
jwtSecretEnv: "JWT_KEY",
|
|
10
|
+
customValidator: (req, key) => {
|
|
11
|
+
return true;
|
|
12
|
+
},
|
|
13
|
+
...(0, getConfig_1.default)("auth"),
|
|
14
|
+
};
|
|
15
|
+
const executer = (authType) => async (req, res, next) => {
|
|
16
|
+
const accessResult = await gConfig?.customValidator?.(req, authType);
|
|
17
|
+
if (accessResult == null)
|
|
18
|
+
return next({ status: 401, json: { message: 'Unauthorized' } });
|
|
19
|
+
if (!accessResult)
|
|
20
|
+
return next({ status: 403, json: { message: 'Forbidden' } });
|
|
21
|
+
return next();
|
|
22
|
+
};
|
|
23
|
+
const auth = {
|
|
24
|
+
user: () => {
|
|
25
|
+
return executer("user");
|
|
26
|
+
},
|
|
27
|
+
admin: (config) => {
|
|
28
|
+
return executer(`admin${config?.permission ? `:${config.permission}` : ''}`);
|
|
29
|
+
},
|
|
30
|
+
any: () => {
|
|
31
|
+
return executer("any");
|
|
32
|
+
},
|
|
87
33
|
};
|
|
88
34
|
exports.default = auth;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
type Handler = (req: Request, res: Response, next: (errorMessage?: string) => unknown) => unknown;
|
|
3
|
+
type Executer = (authField: string) => (req: Request, res: Response, next: NextFunction) => unknown;
|
|
4
|
+
type Auth = {
|
|
5
|
+
user: () => Handler;
|
|
6
|
+
admin: (config?: {
|
|
7
|
+
permission: string;
|
|
8
|
+
}) => Handler;
|
|
9
|
+
any: () => Handler;
|
|
10
|
+
};
|
|
11
|
+
type ConfigType = {
|
|
12
|
+
customValidator?: (req: Request, key: string) => Promise<boolean | null>;
|
|
13
|
+
jwtSecretEnv?: string;
|
|
14
|
+
admin?: {
|
|
15
|
+
role: string;
|
|
16
|
+
model: string;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export { Handler, Executer, Auth, ConfigType };
|