@mingjue8888/express-plus 1.2.2 → 1.2.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/has-permissions.ts +3 -8
- package/has-roles.ts +3 -5
- package/index.ts +9 -7
- package/package.json +11 -5
- package/README.md +0 -150
- package/id-generator.ts +0 -5
- package/login-account.ts +0 -11
- package/login-token.ts +0 -7
- package/password-encoder.ts +0 -9
- package/rbac0.ts +0 -9
package/has-permissions.ts
CHANGED
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import hasPermissionsByRBAC0 from "@mingjue8888/base-dependencies/rbac0/has-permissions";
|
|
2
2
|
import asyncMiddleware, {Middleware} from "./async-middleware";
|
|
3
3
|
import PermissionDeniedResponseError from "./permission-denied-response-error";
|
|
4
4
|
|
|
5
|
-
|
|
6
5
|
export default function hasPermissions(permissions: string[]): Middleware {
|
|
7
6
|
return asyncMiddleware(async function (req, _res) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
.flat();
|
|
11
|
-
|
|
12
|
-
const noPermission = _.intersection(permissions, userPermissions).length === 0;
|
|
13
|
-
if (noPermission) throw new PermissionDeniedResponseError("PermissionDeniedError", "You do not have permission");
|
|
7
|
+
if (!hasPermissionsByRBAC0(req.user, permissions))
|
|
8
|
+
throw new PermissionDeniedResponseError("PermissionDeniedError", "You do not have permission");
|
|
14
9
|
});
|
|
15
10
|
}
|
package/has-roles.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import hasRolesByRBAC0 from "@mingjue8888/base-dependencies/rbac0/has-roles";
|
|
2
2
|
import asyncMiddleware, {Middleware} from "./async-middleware";
|
|
3
3
|
import PermissionDeniedResponseError from "./permission-denied-response-error";
|
|
4
4
|
|
|
5
|
-
|
|
6
5
|
export default function hasRoles(roles: string[]): Middleware {
|
|
7
6
|
return asyncMiddleware(async function (req, _res) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (noRole) throw new PermissionDeniedResponseError("NotQualifiedError", "You do not meet the qualifications");
|
|
7
|
+
if (!hasRolesByRBAC0(req.user, roles))
|
|
8
|
+
throw new PermissionDeniedResponseError("NotQualifiedError", "You do not meet the qualifications");
|
|
11
9
|
});
|
|
12
10
|
}
|
package/index.ts
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
import "dotenv/config";
|
|
2
|
+
import {oneDay, oneSecond} from "@mingjue8888/base-dependencies/datetime";
|
|
2
3
|
import logger from "@mingjue8888/base-dependencies/logger";
|
|
3
|
-
import {
|
|
4
|
+
import {RBAC0Account} from "@mingjue8888/base-dependencies/rbac0";
|
|
4
5
|
import compression from "compression";
|
|
5
6
|
import cors from "cors";
|
|
6
7
|
import express from "express";
|
|
7
8
|
import rateLimit from "express-rate-limit";
|
|
8
9
|
import Joi from "joi";
|
|
9
|
-
import jwt from "jsonwebtoken";
|
|
10
|
+
import jwt, {JwtPayload} from "jsonwebtoken";
|
|
10
11
|
import _ from "lodash";
|
|
11
12
|
import morgan from "morgan";
|
|
12
13
|
import passport from "passport";
|
|
13
14
|
import {ExtractJwt, Strategy, StrategyOptions} from "passport-jwt";
|
|
14
15
|
import {Middleware} from "./async-middleware";
|
|
15
|
-
import LoginAccount from "./login-account";
|
|
16
|
-
import LoginToken from "./login-token";
|
|
17
16
|
import PermissionDeniedResponseError from "./permission-denied-response-error";
|
|
18
17
|
import ResponseError from "./response-error";
|
|
19
18
|
import Router from "./router";
|
|
@@ -25,6 +24,9 @@ type DeepArray<T> =
|
|
|
25
24
|
| T[]
|
|
26
25
|
| DeepArray<T>[];
|
|
27
26
|
|
|
27
|
+
type JWTPayload =
|
|
28
|
+
Required<Pick<JwtPayload, "exp" | "sub" | "iat">>;
|
|
29
|
+
|
|
28
30
|
type ResponseErrorConverter =
|
|
29
31
|
(err: Error) => ResponseError | void;
|
|
30
32
|
|
|
@@ -68,7 +70,7 @@ function errorHandler(
|
|
|
68
70
|
|
|
69
71
|
declare global {
|
|
70
72
|
namespace Express {
|
|
71
|
-
interface User extends
|
|
73
|
+
interface User extends RBAC0Account {}
|
|
72
74
|
interface Response {
|
|
73
75
|
signToken: (sub: string) => void;
|
|
74
76
|
body?: any;
|
|
@@ -86,7 +88,7 @@ interface EnvOptions {
|
|
|
86
88
|
}
|
|
87
89
|
|
|
88
90
|
interface StartupOptions extends EnvOptions {
|
|
89
|
-
authenticate?: <T extends
|
|
91
|
+
authenticate?: <T extends RBAC0Account>(token: JWTPayload) => Promise<T>;
|
|
90
92
|
middlewares?: Middleware[];
|
|
91
93
|
onError?: ResponseErrorConverter;
|
|
92
94
|
}
|
|
@@ -139,7 +141,7 @@ export default function app(routers: DeepArray<Router>, opt?: StartupOptions) {
|
|
|
139
141
|
ignoreExpiration: true,
|
|
140
142
|
}
|
|
141
143
|
|
|
142
|
-
const strategy = new Strategy(jwtStrategy, function (payload:
|
|
144
|
+
const strategy = new Strategy(jwtStrategy, function (payload: JWTPayload, next) {
|
|
143
145
|
if (!opt?.authenticate) {
|
|
144
146
|
next(new ServerErrorResponseError("AuthenticationError", "Missing authentication logic"), false);
|
|
145
147
|
return;
|
package/package.json
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mingjue8888/express-plus",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"repository": "github:mingjue8888/express-plus",
|
|
5
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"web",
|
|
7
|
+
"app",
|
|
8
|
+
"api",
|
|
9
|
+
"rest",
|
|
10
|
+
"restful",
|
|
11
|
+
"express",
|
|
12
|
+
"router"
|
|
13
|
+
],
|
|
6
14
|
"dependencies": {
|
|
7
|
-
"@mingjue8888/base-dependencies": "^1.
|
|
8
|
-
"bcrypt": "^6.0.0",
|
|
15
|
+
"@mingjue8888/base-dependencies": "^1.5.4",
|
|
9
16
|
"compression": "^1.8.2",
|
|
10
17
|
"cors": "^2.8.6",
|
|
11
18
|
"express-rate-limit": "^8.7.4",
|
|
@@ -15,7 +22,6 @@
|
|
|
15
22
|
"passport-jwt": "^4.0.1"
|
|
16
23
|
},
|
|
17
24
|
"devDependencies": {
|
|
18
|
-
"@types/bcrypt": "^6.0.0",
|
|
19
25
|
"@types/compression": "^1.8.1",
|
|
20
26
|
"@types/cors": "^2.8.19",
|
|
21
27
|
"@types/express": "^5.0.6",
|
package/README.md
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
# Usage
|
|
2
|
-
|
|
3
|
-
## Define User
|
|
4
|
-
|
|
5
|
-
```typescript
|
|
6
|
-
import LoginAccount from "@mingjue8888/express-plus/login-account";
|
|
7
|
-
|
|
8
|
-
export interface UserAccount extends LoginAccount {}
|
|
9
|
-
|
|
10
|
-
declare global {
|
|
11
|
-
namespace Express {
|
|
12
|
-
interface User extends UserAccount {}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## Define Error
|
|
18
|
-
|
|
19
|
-
```typescript
|
|
20
|
-
export class NotFoundUserError extends Error {}
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
## Write Router
|
|
24
|
-
|
|
25
|
-
```typescript
|
|
26
|
-
import Joi from "joi";
|
|
27
|
-
import asyncMiddleware from "@mingjue8888/express-plus/async-middleware";
|
|
28
|
-
import hasLogin from "@mingjue8888/express-plus/has-login";
|
|
29
|
-
import Router from "@mingjue8888/express-plus/router";
|
|
30
|
-
import validBody from "@mingjue8888/express-plus/valid-body";
|
|
31
|
-
import validParam from "@mingjue8888/express-plus/valid-param";
|
|
32
|
-
import validSend from "@mingjue8888/express-plus/valid-send";
|
|
33
|
-
|
|
34
|
-
// File ./routers/routerA.ts
|
|
35
|
-
const login: Router = {
|
|
36
|
-
method: "POST",
|
|
37
|
-
path: "/login",
|
|
38
|
-
middlewares: [
|
|
39
|
-
validBody({
|
|
40
|
-
username: Joi.string()
|
|
41
|
-
.required(),
|
|
42
|
-
|
|
43
|
-
password: Joi.string()
|
|
44
|
-
.regex(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$/)
|
|
45
|
-
.required()
|
|
46
|
-
}),
|
|
47
|
-
asyncMiddleware(async function (req, res) {
|
|
48
|
-
const username = req.valid.body.username as string;
|
|
49
|
-
const password = req.valid.body.password as string;
|
|
50
|
-
const id = "UUID";
|
|
51
|
-
res.signToken(id);
|
|
52
|
-
}),
|
|
53
|
-
validSend({
|
|
54
|
-
accessToken: Joi.string()
|
|
55
|
-
.required(),
|
|
56
|
-
}),
|
|
57
|
-
],
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export default [
|
|
61
|
-
login,
|
|
62
|
-
];
|
|
63
|
-
|
|
64
|
-
// File ./routers/routerB.ts
|
|
65
|
-
const findUser: Router = {
|
|
66
|
-
method: "GET",
|
|
67
|
-
path: "/users/:userId",
|
|
68
|
-
middlewares: [
|
|
69
|
-
hasLogin,
|
|
70
|
-
validParam({
|
|
71
|
-
userId: Joi.string()
|
|
72
|
-
.required(),
|
|
73
|
-
}),
|
|
74
|
-
asyncMiddleware(async function (req, res) {
|
|
75
|
-
throw new NotFoundUserError();
|
|
76
|
-
}),
|
|
77
|
-
validSend({
|
|
78
|
-
id: Joi.string()
|
|
79
|
-
.required(),
|
|
80
|
-
|
|
81
|
-
name: Joi.string()
|
|
82
|
-
.required(),
|
|
83
|
-
}),
|
|
84
|
-
],
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const findUserPosts: Router = {
|
|
88
|
-
method: "GET",
|
|
89
|
-
path: "/users/:userId/posts",
|
|
90
|
-
middlewares: [
|
|
91
|
-
hasLogin,
|
|
92
|
-
validParam({
|
|
93
|
-
userId: Joi.string()
|
|
94
|
-
.required(),
|
|
95
|
-
}),
|
|
96
|
-
asyncMiddleware(async function (req, res) {
|
|
97
|
-
res.body = {
|
|
98
|
-
posts: []
|
|
99
|
-
}
|
|
100
|
-
}),
|
|
101
|
-
validSend({
|
|
102
|
-
posts: Joi.array().items(Joi.object().keys({
|
|
103
|
-
id: Joi.string().required(),
|
|
104
|
-
content: Joi.string().required().default(""),
|
|
105
|
-
}))
|
|
106
|
-
})
|
|
107
|
-
],
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export default [
|
|
111
|
-
findUser,
|
|
112
|
-
findUserPosts,
|
|
113
|
-
];
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
## Start app
|
|
117
|
-
|
|
118
|
-
```typescript
|
|
119
|
-
import app from "@mingjue8888/express-plus";
|
|
120
|
-
import routerA from "./routers/routerA";
|
|
121
|
-
import routerB from "./routers/routerB";
|
|
122
|
-
import {NotFoundUserError} from "./errors";
|
|
123
|
-
import NotFoundResponseError from "@mingjue8888/express-plus/not-found-response-error";
|
|
124
|
-
|
|
125
|
-
const routers = [
|
|
126
|
-
routerA,
|
|
127
|
-
routerB,
|
|
128
|
-
];
|
|
129
|
-
|
|
130
|
-
app(routers, {
|
|
131
|
-
APP_PORT: 3000,
|
|
132
|
-
|
|
133
|
-
async authenticate<UserAccount>(token: LoginToken) {
|
|
134
|
-
return {
|
|
135
|
-
id: token.sub,
|
|
136
|
-
isLocked: false,
|
|
137
|
-
isSuspended: false,
|
|
138
|
-
isBanned: false,
|
|
139
|
-
roles: [],
|
|
140
|
-
} as UserAccount;
|
|
141
|
-
},
|
|
142
|
-
|
|
143
|
-
// Covert Error to ResponseError
|
|
144
|
-
onError(err: Error) {
|
|
145
|
-
if (err instanceof NotFoundUserError) {
|
|
146
|
-
return new NotFoundResponseError(err.constructor.name, "Not found user");
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
```
|
package/id-generator.ts
DELETED
package/login-account.ts
DELETED
package/login-token.ts
DELETED
package/password-encoder.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import bcrypt from "bcrypt";
|
|
2
|
-
|
|
3
|
-
export function encode(password: string): string {
|
|
4
|
-
return bcrypt.hashSync(password, bcrypt.genSaltSync());
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function check(password: string, encoded: string): boolean {
|
|
8
|
-
return bcrypt.compareSync(password, encoded);
|
|
9
|
-
}
|