@alanszp/express-common-fn 10.0.3 → 12.0.0
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/commonErrorsHandler.js +29 -14
- package/dist/commonErrorsHandler.js.map +1 -1
- package/node_modules/@alanszp/audit/dist/errors/AuditError.d.ts +2 -2
- package/node_modules/@alanszp/audit/dist/errors/AuditError.js +1 -1
- package/node_modules/@alanszp/audit/dist/errors/AuditError.js.map +1 -1
- package/node_modules/@alanszp/audit/dist/errors/MissingAuditFieldsError.d.ts +3 -3
- package/node_modules/@alanszp/audit/dist/errors/MissingAuditFieldsError.js +2 -2
- package/node_modules/@alanszp/audit/dist/errors/MissingAuditFieldsError.js.map +1 -1
- package/node_modules/@alanszp/audit/package.json +5 -4
- package/node_modules/@alanszp/audit/src/errors/AuditError.ts +2 -2
- package/node_modules/@alanszp/audit/src/errors/MissingAuditFieldsError.ts +4 -7
- package/node_modules/@alanszp/errors/dist/BaseError.js.map +1 -0
- package/node_modules/@alanszp/errors/dist/HttpRenderableError.d.ts +4 -0
- package/node_modules/@alanszp/errors/dist/HttpRenderableError.js +8 -0
- package/node_modules/@alanszp/errors/dist/HttpRenderableError.js.map +1 -0
- package/node_modules/@alanszp/errors/dist/InternalServerError.d.ts +8 -0
- package/node_modules/@alanszp/errors/dist/InternalServerError.js +24 -0
- package/node_modules/@alanszp/errors/dist/InternalServerError.js.map +1 -0
- package/node_modules/@alanszp/errors/dist/RenderableError.d.ts +15 -0
- package/node_modules/@alanszp/errors/dist/RenderableError.js +27 -0
- package/node_modules/@alanszp/errors/dist/RenderableError.js.map +1 -0
- package/node_modules/@alanszp/errors/dist/index.d.ts +4 -3
- package/node_modules/@alanszp/errors/dist/index.js +4 -3
- package/node_modules/@alanszp/errors/dist/index.js.map +1 -1
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/.gitignore +3 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/.npmignore +3 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/LICENSE +21 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/app/appIdentifier.js.map +1 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/app/index.d.ts +1 -0
- package/node_modules/@alanszp/errors/{dist/errors/http → node_modules/@alanszp/core/dist/app}/index.js +6 -6
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/app/index.js.map +1 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/assignKey.d.ts +2 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/assignKey.js +20 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/assignKey.js.map +1 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/editable/index.d.ts +2 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/editable/index.js +19 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/editable/index.js.map +1 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/editable/keepOrUpdate.d.ts +3 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/editable/keepOrUpdate.js +26 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/editable/keepOrUpdate.js.map +1 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/editable/types.d.ts +6 -0
- package/node_modules/@alanszp/errors/{dist/errors/RenderableError.js → node_modules/@alanszp/core/dist/editable/types.js} +1 -1
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/editable/types.js.map +1 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/index.d.ts +4 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/index.js +21 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/index.js.map +1 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/lists/ListResult.d.ts +7 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/lists/ListResult.js +8 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/lists/ListResult.js.map +1 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/lists/Orderable.d.ts +11 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/lists/Orderable.js +13 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/lists/Orderable.js.map +1 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/lists/Paginable.d.ts +14 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/lists/Paginable.js +22 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/lists/Paginable.js.map +1 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/lists/index.d.ts +3 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/lists/index.js +20 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/dist/lists/index.js.map +1 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/package.json +25 -0
- package/node_modules/@alanszp/errors/node_modules/@alanszp/core/tsconfig.json +15 -0
- package/node_modules/@alanszp/errors/package.json +7 -2
- package/node_modules/@alanszp/errors/src/HttpRenderableError.ts +5 -0
- package/node_modules/@alanszp/errors/src/{errors/http/InternalServerError.ts → InternalServerError.ts} +8 -10
- package/node_modules/@alanszp/errors/src/RenderableError.ts +37 -0
- package/node_modules/@alanszp/errors/src/index.ts +4 -3
- package/node_modules/@alanszp/express/dist/helpers/renderErrorJson.d.ts +4 -0
- package/node_modules/@alanszp/express/dist/helpers/renderErrorJson.js +32 -0
- package/node_modules/@alanszp/express/dist/helpers/renderErrorJson.js.map +1 -0
- package/node_modules/@alanszp/express/dist/index.d.ts +0 -1
- package/node_modules/@alanszp/express/dist/index.js +0 -1
- package/node_modules/@alanszp/express/dist/index.js.map +1 -1
- package/node_modules/@alanszp/express/dist/middlewares/authedForOrg.js +3 -4
- package/node_modules/@alanszp/express/dist/middlewares/authedForOrg.js.map +1 -1
- package/node_modules/@alanszp/express/dist/middlewares/authenticateUser.js +8 -8
- package/node_modules/@alanszp/express/dist/middlewares/authenticateUser.js.map +1 -1
- package/node_modules/@alanszp/express/dist/middlewares/createContext.js +2 -2
- package/node_modules/@alanszp/express/dist/middlewares/createContext.js.map +1 -1
- package/node_modules/@alanszp/express/dist/middlewares/createContext.test.js +3 -3
- package/node_modules/@alanszp/express/dist/middlewares/createContext.test.js.map +1 -1
- package/node_modules/@alanszp/express/dist/middlewares/hasRoles.js +3 -5
- package/node_modules/@alanszp/express/dist/middlewares/hasRoles.js.map +1 -1
- package/node_modules/@alanszp/express/dist/middlewares/jsonBodyParser.js +3 -4
- package/node_modules/@alanszp/express/dist/middlewares/jsonBodyParser.js.map +1 -1
- package/node_modules/@alanszp/express/dist/middlewares/returnInternalServerError.js +1 -2
- package/node_modules/@alanszp/express/dist/middlewares/returnInternalServerError.js.map +1 -1
- package/node_modules/@alanszp/express/dist/middlewares/returnNotFound.js +2 -3
- package/node_modules/@alanszp/express/dist/middlewares/returnNotFound.js.map +1 -1
- package/node_modules/@alanszp/express/dist/test/mocks/jwtUserMocks.js +7 -6
- package/node_modules/@alanszp/express/dist/test/mocks/jwtUserMocks.js.map +1 -1
- package/node_modules/@alanszp/express/package.json +7 -7
- package/node_modules/@alanszp/express/src/helpers/renderErrorJson.ts +29 -0
- package/node_modules/@alanszp/express/src/index.ts +0 -1
- package/node_modules/@alanszp/express/src/middlewares/authedForOrg.ts +3 -4
- package/node_modules/@alanszp/express/src/middlewares/authenticateUser.ts +18 -18
- package/node_modules/@alanszp/express/src/middlewares/createContext.test.ts +1 -1
- package/node_modules/@alanszp/express/src/middlewares/createContext.ts +1 -1
- package/node_modules/@alanszp/express/src/middlewares/hasRoles.ts +3 -5
- package/node_modules/@alanszp/express/src/middlewares/jsonBodyParser.ts +3 -4
- package/node_modules/@alanszp/express/src/middlewares/returnInternalServerError.ts +1 -2
- package/node_modules/@alanszp/express/src/middlewares/returnNotFound.ts +2 -3
- package/node_modules/@alanszp/express/src/test/mocks/jwtUserMocks.ts +6 -6
- package/node_modules/@alanszp/jwt/dist/JWTUser.d.ts +31 -0
- package/node_modules/@alanszp/jwt/dist/JWTUser.js +62 -0
- package/node_modules/@alanszp/jwt/dist/JWTUser.js.map +1 -0
- package/node_modules/@alanszp/jwt/dist/index.d.ts +1 -0
- package/node_modules/@alanszp/jwt/dist/index.js +1 -0
- package/node_modules/@alanszp/jwt/dist/index.js.map +1 -1
- package/node_modules/@alanszp/jwt/dist/jwt.d.ts +2 -3
- package/node_modules/@alanszp/jwt/dist/jwt.js +4 -26
- package/node_modules/@alanszp/jwt/dist/jwt.js.map +1 -1
- package/node_modules/@alanszp/jwt/dist/types.d.ts +3 -3
- package/node_modules/@alanszp/jwt/package.json +3 -2
- package/node_modules/@alanszp/jwt/src/JWTUser.ts +85 -0
- package/node_modules/@alanszp/jwt/src/index.ts +1 -0
- package/node_modules/@alanszp/jwt/src/jwt.ts +4 -29
- package/node_modules/@alanszp/jwt/src/types.ts +3 -3
- package/node_modules/@alanszp/logger/package.json +3 -2
- package/package.json +7 -6
- package/src/commonErrorsHandler.ts +35 -16
- package/node_modules/@alanszp/errors/dist/errors/BaseError.js.map +0 -1
- package/node_modules/@alanszp/errors/dist/errors/RenderableError.d.ts +0 -6
- package/node_modules/@alanszp/errors/dist/errors/RenderableError.js.map +0 -1
- package/node_modules/@alanszp/errors/dist/errors/http/BadRequestError.d.ts +0 -8
- package/node_modules/@alanszp/errors/dist/errors/http/BadRequestError.js +0 -20
- package/node_modules/@alanszp/errors/dist/errors/http/BadRequestError.js.map +0 -1
- package/node_modules/@alanszp/errors/dist/errors/http/HttpError.d.ts +0 -8
- package/node_modules/@alanszp/errors/dist/errors/http/HttpError.js +0 -18
- package/node_modules/@alanszp/errors/dist/errors/http/HttpError.js.map +0 -1
- package/node_modules/@alanszp/errors/dist/errors/http/InternalServerError.d.ts +0 -8
- package/node_modules/@alanszp/errors/dist/errors/http/InternalServerError.js +0 -32
- package/node_modules/@alanszp/errors/dist/errors/http/InternalServerError.js.map +0 -1
- package/node_modules/@alanszp/errors/dist/errors/http/NotFoundError.d.ts +0 -4
- package/node_modules/@alanszp/errors/dist/errors/http/NotFoundError.js +0 -11
- package/node_modules/@alanszp/errors/dist/errors/http/NotFoundError.js.map +0 -1
- package/node_modules/@alanszp/errors/dist/errors/http/UnauthorizedError.d.ts +0 -8
- package/node_modules/@alanszp/errors/dist/errors/http/UnauthorizedError.js +0 -20
- package/node_modules/@alanszp/errors/dist/errors/http/UnauthorizedError.js.map +0 -1
- package/node_modules/@alanszp/errors/dist/errors/http/index.d.ts +0 -5
- package/node_modules/@alanszp/errors/dist/errors/http/index.js.map +0 -1
- package/node_modules/@alanszp/errors/src/errors/RenderableError.ts +0 -7
- package/node_modules/@alanszp/errors/src/errors/http/BadRequestError.ts +0 -21
- package/node_modules/@alanszp/errors/src/errors/http/HttpError.ts +0 -19
- package/node_modules/@alanszp/errors/src/errors/http/NotFoundError.ts +0 -7
- package/node_modules/@alanszp/errors/src/errors/http/UnauthorizedError.ts +0 -21
- package/node_modules/@alanszp/errors/src/errors/http/index.ts +0 -5
- package/node_modules/@alanszp/express/dist/helpers/appIdentifier.js.map +0 -1
- package/node_modules/@alanszp/express/dist/views/errorView.d.ts +0 -2
- package/node_modules/@alanszp/express/dist/views/errorView.js +0 -14
- package/node_modules/@alanszp/express/dist/views/errorView.js.map +0 -1
- package/node_modules/@alanszp/express/src/helpers/appIdentifier.ts +0 -5
- package/node_modules/@alanszp/express/src/views/errorView.ts +0 -11
- /package/node_modules/@alanszp/errors/dist/{errors/BaseError.d.ts → BaseError.d.ts} +0 -0
- /package/node_modules/@alanszp/errors/dist/{errors/BaseError.js → BaseError.js} +0 -0
- /package/node_modules/@alanszp/{express/dist/helpers → errors/node_modules/@alanszp/core/dist/app}/appIdentifier.d.ts +0 -0
- /package/node_modules/@alanszp/{express/dist/helpers → errors/node_modules/@alanszp/core/dist/app}/appIdentifier.js +0 -0
- /package/node_modules/@alanszp/errors/src/{errors/BaseError.ts → BaseError.ts} +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/express",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "12.0.0",
|
|
4
4
|
"description": "Alan's express utils and middlewares.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -35,15 +35,15 @@
|
|
|
35
35
|
"typescript": "^4.3.4"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@alanszp/audit": "^
|
|
39
|
-
"@alanszp/errors": "^
|
|
40
|
-
"@alanszp/jwt": "^
|
|
41
|
-
"@alanszp/logger": "^
|
|
42
|
-
"@alanszp/shared-context": "^
|
|
38
|
+
"@alanszp/audit": "^12.0.0",
|
|
39
|
+
"@alanszp/errors": "^12.0.0",
|
|
40
|
+
"@alanszp/jwt": "^12.0.0",
|
|
41
|
+
"@alanszp/logger": "^12.0.0",
|
|
42
|
+
"@alanszp/shared-context": "^12.0.0",
|
|
43
43
|
"@babel/core": "^7.23.9",
|
|
44
44
|
"@paralleldrive/cuid2": "^2.2.2",
|
|
45
45
|
"body-parser": "^1.20.2",
|
|
46
46
|
"lodash": "^4.17.21"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "085a76a387bc0e272e09e9cb1b6f48f814512940"
|
|
49
49
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { appIdentifier } from "@alanszp/core";
|
|
2
|
+
import { RenderableView } from "@alanszp/errors";
|
|
3
|
+
|
|
4
|
+
export function render401Error(requiredChecks: string[]): RenderableView {
|
|
5
|
+
return {
|
|
6
|
+
code: "unauthorized_error",
|
|
7
|
+
message: "Unauthorized Error",
|
|
8
|
+
context: { requiredChecks },
|
|
9
|
+
origin: appIdentifier(),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function render404Error(): RenderableView {
|
|
14
|
+
return {
|
|
15
|
+
code: "not_found",
|
|
16
|
+
message: "Not Found",
|
|
17
|
+
context: {},
|
|
18
|
+
origin: appIdentifier(),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function render400Error(message: string): RenderableView {
|
|
23
|
+
return {
|
|
24
|
+
code: "bad_request",
|
|
25
|
+
message,
|
|
26
|
+
context: {},
|
|
27
|
+
origin: appIdentifier(),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { NotFoundError, UnauthorizedError } from "@alanszp/errors";
|
|
2
|
-
import { errorView } from "../views/errorView";
|
|
3
1
|
import { NextFunction, Response } from "express";
|
|
4
2
|
import { getRequestLogger } from "../helpers/getRequestLogger";
|
|
5
3
|
import { GenericRequest } from "../types/GenericRequest";
|
|
4
|
+
import { render401Error, render404Error } from "../helpers/renderErrorJson";
|
|
6
5
|
|
|
7
6
|
function response401(res: Response): void {
|
|
8
|
-
res.status(401).json(
|
|
7
|
+
res.status(401).json(render401Error(["jwt"]));
|
|
9
8
|
}
|
|
10
9
|
|
|
11
10
|
export function authForOrg(
|
|
@@ -46,6 +45,6 @@ export function authForOrg(
|
|
|
46
45
|
logger.info("middleware.authForOrg.error.noOrganization", {
|
|
47
46
|
error,
|
|
48
47
|
});
|
|
49
|
-
res.status(404).json(
|
|
48
|
+
res.status(404).json(render404Error());
|
|
50
49
|
}
|
|
51
50
|
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { JWTUser, verifyJWT, VerifyOptions } from "@alanszp/jwt";
|
|
2
|
-
import { UnauthorizedError } from "@alanszp/errors";
|
|
3
|
-
import { errorView } from "../views/errorView";
|
|
4
2
|
import { NextFunction, Response } from "express";
|
|
5
3
|
import { getRequestLogger } from "../helpers/getRequestLogger";
|
|
6
4
|
import { GenericRequest } from "../types/GenericRequest";
|
|
7
5
|
import { ILogger } from "@alanszp/logger";
|
|
8
6
|
import { compact, isEmpty, omit } from "lodash";
|
|
7
|
+
import { render401Error } from "../helpers/renderErrorJson";
|
|
9
8
|
|
|
10
9
|
function parseAuthorizationHeader(
|
|
11
10
|
authorization: string | undefined
|
|
@@ -89,14 +88,17 @@ const middlewareGetterByAuthType: Record<
|
|
|
89
88
|
user: "0",
|
|
90
89
|
org: "lara",
|
|
91
90
|
});
|
|
92
|
-
return Promise.resolve(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
91
|
+
return Promise.resolve(
|
|
92
|
+
new JWTUser({
|
|
93
|
+
id: "0",
|
|
94
|
+
employeeReference: "0",
|
|
95
|
+
organizationReference: "lara",
|
|
96
|
+
roles: [],
|
|
97
|
+
segmentReference: null,
|
|
98
|
+
// This will be changed in the near future to grab all permissions.
|
|
99
|
+
permissions: "MA==", // 0 in base64
|
|
100
|
+
})
|
|
101
|
+
);
|
|
100
102
|
} else {
|
|
101
103
|
return null;
|
|
102
104
|
}
|
|
@@ -140,13 +142,11 @@ export function createAuthContext<Options extends AuthOptions>(
|
|
|
140
142
|
res
|
|
141
143
|
.status(401)
|
|
142
144
|
.json(
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
])
|
|
149
|
-
)
|
|
145
|
+
render401Error([
|
|
146
|
+
authAttempts.includes(null)
|
|
147
|
+
? `Token invalid for methods ${authMethods}`
|
|
148
|
+
: `Token not set for methods ${authMethods}`,
|
|
149
|
+
])
|
|
150
150
|
);
|
|
151
151
|
return;
|
|
152
152
|
}
|
|
@@ -164,7 +164,7 @@ export function createAuthContext<Options extends AuthOptions>(
|
|
|
164
164
|
methods: AuthMethods,
|
|
165
165
|
error,
|
|
166
166
|
});
|
|
167
|
-
res.status(401).json(
|
|
167
|
+
res.status(401).json(render401Error(authMethods));
|
|
168
168
|
}
|
|
169
169
|
};
|
|
170
170
|
};
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
mockRequestWithBody,
|
|
9
9
|
mockResponse,
|
|
10
10
|
} from "../test/mocks/expressMocks";
|
|
11
|
-
import { appIdentifier } from "
|
|
11
|
+
import { appIdentifier } from "@alanszp/core";
|
|
12
12
|
jest.mock("@alanszp/shared-context");
|
|
13
13
|
|
|
14
14
|
const logger = createMockLogger({});
|
|
@@ -2,7 +2,7 @@ import { createId } from "@paralleldrive/cuid2";
|
|
|
2
2
|
import { NextFunction, Response } from "express";
|
|
3
3
|
import { ILogger } from "@alanszp/logger";
|
|
4
4
|
import { Audit } from "@alanszp/audit";
|
|
5
|
-
import { appIdentifier } from "
|
|
5
|
+
import { appIdentifier } from "@alanszp/core";
|
|
6
6
|
import { GenericRequest } from "../types/GenericRequest";
|
|
7
7
|
import { SharedContext } from "@alanszp/shared-context";
|
|
8
8
|
import { compact } from "lodash";
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import { UnauthorizedError } from "@alanszp/errors";
|
|
2
|
-
import { errorView } from "../views/errorView";
|
|
3
1
|
import { NextFunction, Response } from "express";
|
|
4
|
-
import { jwtUserHasRoles } from "@alanszp/jwt";
|
|
5
2
|
import { GenericRequest } from "../types/GenericRequest";
|
|
3
|
+
import { render401Error } from "../helpers/renderErrorJson";
|
|
6
4
|
|
|
7
5
|
function response401(res: Response): void {
|
|
8
|
-
res.status(401).json(
|
|
6
|
+
res.status(401).json(render401Error(["permissions"]));
|
|
9
7
|
}
|
|
10
8
|
|
|
11
9
|
export function hasRoles(
|
|
@@ -17,7 +15,7 @@ export function hasRoles(
|
|
|
17
15
|
return response401(res);
|
|
18
16
|
}
|
|
19
17
|
|
|
20
|
-
if (
|
|
18
|
+
if (jwtUser.hasRoles(roles)) {
|
|
21
19
|
return next();
|
|
22
20
|
}
|
|
23
21
|
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { json, OptionsJson } from "body-parser";
|
|
2
2
|
import { NextFunction, Response } from "express";
|
|
3
|
-
import { BadRequestError } from "@alanszp/errors";
|
|
4
|
-
import { errorView } from "../views/errorView";
|
|
5
3
|
import { GenericRequest } from "../types/GenericRequest";
|
|
4
|
+
import { render400Error } from "../helpers/renderErrorJson";
|
|
6
5
|
|
|
7
6
|
export function jsonBodyParser(options?: OptionsJson) {
|
|
8
7
|
const bodyParser = json({ limit: "1mb", ...options });
|
|
@@ -14,10 +13,10 @@ export function jsonBodyParser(options?: OptionsJson) {
|
|
|
14
13
|
try {
|
|
15
14
|
bodyParser(req, res, (error?: unknown) => {
|
|
16
15
|
if (!error) return next();
|
|
17
|
-
res.status(400).json(
|
|
16
|
+
res.status(400).json(render400Error("Malformed JSON"));
|
|
18
17
|
});
|
|
19
18
|
} catch (error: unknown) {
|
|
20
|
-
res.status(400).json(
|
|
19
|
+
res.status(400).json(render400Error("Malformed JSON"));
|
|
21
20
|
}
|
|
22
21
|
};
|
|
23
22
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { ErrorRequestHandler, NextFunction, Response } from "express";
|
|
2
2
|
import { InternalServerError } from "@alanszp/errors";
|
|
3
|
-
import { errorView } from "../views/errorView";
|
|
4
3
|
import { GenericRequest } from "../types/GenericRequest";
|
|
5
4
|
import { ILogger } from "@alanszp/logger";
|
|
6
5
|
|
|
@@ -11,6 +10,6 @@ export type GetInternalServerErrorMiddleware = (
|
|
|
11
10
|
export const returnInternalServerError: GetInternalServerErrorMiddleware =
|
|
12
11
|
(getLogger: () => ILogger) =>
|
|
13
12
|
(error: unknown, req: GenericRequest, res: Response, _next: NextFunction) => {
|
|
14
|
-
res.status(500).json(
|
|
13
|
+
res.status(500).json(new InternalServerError(error).toView());
|
|
15
14
|
getLogger().error("error_to_client", { error });
|
|
16
15
|
};
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { NextFunction, Response } from "express";
|
|
2
|
-
import { NotFoundError } from "@alanszp/errors";
|
|
3
|
-
import { errorView } from "../views/errorView";
|
|
4
2
|
import { GenericRequest } from "../types/GenericRequest";
|
|
3
|
+
import { render404Error } from "../helpers/renderErrorJson";
|
|
5
4
|
|
|
6
5
|
export function returnNotFound(
|
|
7
6
|
_req: GenericRequest,
|
|
8
7
|
res: Response,
|
|
9
8
|
_next: NextFunction
|
|
10
9
|
): void {
|
|
11
|
-
res.status(404).json(
|
|
10
|
+
res.status(404).json(render404Error());
|
|
12
11
|
}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { JWTUser } from "@alanszp/jwt";
|
|
2
2
|
|
|
3
|
-
export const userJwtUserMock: JWTUser = {
|
|
3
|
+
export const userJwtUserMock: JWTUser = new JWTUser({
|
|
4
4
|
id: "1",
|
|
5
5
|
employeeReference: "1",
|
|
6
6
|
organizationReference: "test",
|
|
7
7
|
roles: [],
|
|
8
|
-
permissions:
|
|
8
|
+
permissions: "MA==",
|
|
9
9
|
segmentReference: null,
|
|
10
|
-
};
|
|
10
|
+
});
|
|
11
11
|
|
|
12
|
-
export const laraJwtUserMock: JWTUser = {
|
|
12
|
+
export const laraJwtUserMock: JWTUser = new JWTUser({
|
|
13
13
|
id: "0",
|
|
14
14
|
employeeReference: "0",
|
|
15
15
|
organizationReference: "lara",
|
|
16
16
|
roles: [],
|
|
17
|
-
permissions:
|
|
17
|
+
permissions: "MA==",
|
|
18
18
|
segmentReference: null,
|
|
19
|
-
};
|
|
19
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { IJWTUser, JWTPayload } from "./types";
|
|
2
|
+
export declare class JWTUser implements IJWTUser {
|
|
3
|
+
id: string;
|
|
4
|
+
employeeReference: string | null;
|
|
5
|
+
organizationReference: string;
|
|
6
|
+
originalOrganizationReference: string | null;
|
|
7
|
+
roles: string[];
|
|
8
|
+
permissions: string;
|
|
9
|
+
segmentReference: string | null;
|
|
10
|
+
constructor({ id, employeeReference, organizationReference, roles, permissions, segmentReference, }: IJWTUser);
|
|
11
|
+
static fromPayload(payload: JWTPayload): JWTUser;
|
|
12
|
+
toTokenPayload(): JWTPayload;
|
|
13
|
+
/**
|
|
14
|
+
* Old way of checking for a role
|
|
15
|
+
* Will be replaced by permission checks
|
|
16
|
+
* @param role - role code to check
|
|
17
|
+
*/
|
|
18
|
+
hasRole(role: string): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Old way of checking for roles
|
|
21
|
+
* Will be replaced by permission checks
|
|
22
|
+
*/
|
|
23
|
+
hasRoles(validateRoles: string | string[]): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Check if user has permission to perform an action
|
|
26
|
+
* @note - not implemented, will be implemented in the next release
|
|
27
|
+
* @param permissionCode - permission code to check
|
|
28
|
+
* @returns boolean
|
|
29
|
+
*/
|
|
30
|
+
hasPermission(_permissionCode: string): boolean;
|
|
31
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JWTUser = void 0;
|
|
4
|
+
class JWTUser {
|
|
5
|
+
constructor({ id, employeeReference, organizationReference, roles, permissions, segmentReference, }) {
|
|
6
|
+
this.id = id;
|
|
7
|
+
this.employeeReference = employeeReference;
|
|
8
|
+
this.organizationReference = organizationReference;
|
|
9
|
+
this.roles = roles;
|
|
10
|
+
this.permissions = permissions;
|
|
11
|
+
this.segmentReference = segmentReference;
|
|
12
|
+
}
|
|
13
|
+
static fromPayload(payload) {
|
|
14
|
+
return new JWTUser({
|
|
15
|
+
id: payload.sub,
|
|
16
|
+
employeeReference: payload.ref,
|
|
17
|
+
organizationReference: payload.org,
|
|
18
|
+
roles: payload.rls,
|
|
19
|
+
permissions: payload.prms,
|
|
20
|
+
segmentReference: payload.seg || null,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
toTokenPayload() {
|
|
24
|
+
return {
|
|
25
|
+
sub: this.id,
|
|
26
|
+
ref: this.employeeReference,
|
|
27
|
+
org: this.organizationReference,
|
|
28
|
+
rls: this.roles,
|
|
29
|
+
prms: this.permissions,
|
|
30
|
+
seg: this.segmentReference,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Old way of checking for a role
|
|
35
|
+
* Will be replaced by permission checks
|
|
36
|
+
* @param role - role code to check
|
|
37
|
+
*/
|
|
38
|
+
hasRole(role) {
|
|
39
|
+
return this.roles.includes(role);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Old way of checking for roles
|
|
43
|
+
* Will be replaced by permission checks
|
|
44
|
+
*/
|
|
45
|
+
hasRoles(validateRoles) {
|
|
46
|
+
if (typeof validateRoles === "string") {
|
|
47
|
+
return this.hasRole(validateRoles);
|
|
48
|
+
}
|
|
49
|
+
return validateRoles.some((role) => this.hasRole(role));
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Check if user has permission to perform an action
|
|
53
|
+
* @note - not implemented, will be implemented in the next release
|
|
54
|
+
* @param permissionCode - permission code to check
|
|
55
|
+
* @returns boolean
|
|
56
|
+
*/
|
|
57
|
+
hasPermission(_permissionCode) {
|
|
58
|
+
throw new Error("Not implemented");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.JWTUser = JWTUser;
|
|
62
|
+
//# sourceMappingURL=JWTUser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JWTUser.js","sourceRoot":"","sources":["../src/JWTUser.ts"],"names":[],"mappings":";;;AAEA,MAAa,OAAO;IAelB,YAAY,EACV,EAAE,EACF,iBAAiB,EACjB,qBAAqB,EACrB,KAAK,EACL,WAAW,EACX,gBAAgB,GACP;QACT,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAC;QAC3C,IAAI,CAAC,qBAAqB,GAAG,qBAAqB,CAAC;QACnD,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;IAC3C,CAAC;IAED,MAAM,CAAC,WAAW,CAAC,OAAmB;QACpC,OAAO,IAAI,OAAO,CAAC;YACjB,EAAE,EAAE,OAAO,CAAC,GAAG;YACf,iBAAiB,EAAE,OAAO,CAAC,GAAG;YAC9B,qBAAqB,EAAE,OAAO,CAAC,GAAG;YAClC,KAAK,EAAE,OAAO,CAAC,GAAG;YAClB,WAAW,EAAE,OAAO,CAAC,IAAI;YACzB,gBAAgB,EAAE,OAAO,CAAC,GAAG,IAAI,IAAI;SACtC,CAAC,CAAC;IACL,CAAC;IAEM,cAAc;QACnB,OAAO;YACL,GAAG,EAAE,IAAI,CAAC,EAAE;YACZ,GAAG,EAAE,IAAI,CAAC,iBAAiB;YAC3B,GAAG,EAAE,IAAI,CAAC,qBAAqB;YAC/B,GAAG,EAAE,IAAI,CAAC,KAAK;YACf,IAAI,EAAE,IAAI,CAAC,WAAW;YACtB,GAAG,EAAE,IAAI,CAAC,gBAAgB;SAC3B,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACI,OAAO,CAAC,IAAY;QACzB,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC;IAED;;;OAGG;IACI,QAAQ,CAAC,aAAgC;QAC9C,IAAI,OAAO,aAAa,KAAK,QAAQ,EAAE;YACrC,OAAO,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;SACpC;QACD,OAAO,aAAa,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IAC1D,CAAC;IAED;;;;;OAKG;IACI,aAAa,CAAC,eAAuB;QAC1C,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;CACF;AAlFD,0BAkFC"}
|
|
@@ -11,5 +11,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
__exportStar(require("./types"), exports);
|
|
14
|
+
__exportStar(require("./JWTUser"), exports);
|
|
14
15
|
__exportStar(require("./jwt"), exports);
|
|
15
16
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,0CAAwB;AACxB,wCAAsB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,0CAAwB;AACxB,4CAA0B;AAC1B,wCAAsB"}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { KeyObject } from "crypto";
|
|
3
|
-
import type {
|
|
3
|
+
import type { SignOptions, VerifyOptions } from "./types";
|
|
4
|
+
import { JWTUser } from "./JWTUser";
|
|
4
5
|
export declare const JWT_ALGORITHM = "RS512";
|
|
5
6
|
export declare function privateKeyFromPem(key: string): KeyObject;
|
|
6
7
|
export declare function publicKeyFromPem(key: string): KeyObject;
|
|
7
8
|
export declare function withDefaultSignOptions(options?: Partial<SignOptions>): SignOptions;
|
|
8
9
|
export declare function withDefaultVerifyOptions(options?: Partial<VerifyOptions>): VerifyOptions;
|
|
9
10
|
export declare function generateJWT(privateKey: KeyObject | string, user: JWTUser, options?: Partial<SignOptions>): Promise<string>;
|
|
10
|
-
export declare function createTokenPayload(user: JWTUser): JWTPayload;
|
|
11
11
|
export declare function verifyJWT(publicKey: KeyObject | string, token: string, options?: Partial<VerifyOptions>): Promise<JWTUser>;
|
|
12
|
-
export declare function jwtUserHasRoles(jwtUser: JWTUser, roles: string | string[]): boolean;
|
|
@@ -9,9 +9,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
12
|
+
exports.verifyJWT = exports.generateJWT = exports.withDefaultVerifyOptions = exports.withDefaultSignOptions = exports.publicKeyFromPem = exports.privateKeyFromPem = exports.JWT_ALGORITHM = void 0;
|
|
13
13
|
const crypto_1 = require("crypto");
|
|
14
14
|
const jose_1 = require("jose");
|
|
15
|
+
const JWTUser_1 = require("./JWTUser");
|
|
15
16
|
exports.JWT_ALGORITHM = "RS512";
|
|
16
17
|
function privateKeyFromPem(key) {
|
|
17
18
|
return (0, crypto_1.createPrivateKey)({
|
|
@@ -39,7 +40,7 @@ function generateJWT(privateKey, user, options) {
|
|
|
39
40
|
return __awaiter(this, void 0, void 0, function* () {
|
|
40
41
|
const key = typeof privateKey === "string" ? privateKeyFromPem(privateKey) : privateKey;
|
|
41
42
|
const opts = withDefaultSignOptions(options);
|
|
42
|
-
return new jose_1.SignJWT(
|
|
43
|
+
return new jose_1.SignJWT(user.toTokenPayload())
|
|
43
44
|
.setProtectedHeader({ alg: exports.JWT_ALGORITHM })
|
|
44
45
|
.setIssuedAt()
|
|
45
46
|
.setIssuer(opts.issuer)
|
|
@@ -49,17 +50,6 @@ function generateJWT(privateKey, user, options) {
|
|
|
49
50
|
});
|
|
50
51
|
}
|
|
51
52
|
exports.generateJWT = generateJWT;
|
|
52
|
-
function createTokenPayload(user) {
|
|
53
|
-
return {
|
|
54
|
-
sub: user.id,
|
|
55
|
-
ref: user.employeeReference,
|
|
56
|
-
org: user.organizationReference,
|
|
57
|
-
rls: user.roles,
|
|
58
|
-
prms: user.permissions,
|
|
59
|
-
seg: user.segmentReference,
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
exports.createTokenPayload = createTokenPayload;
|
|
63
53
|
function verifyJWT(publicKey, token, options) {
|
|
64
54
|
return __awaiter(this, void 0, void 0, function* () {
|
|
65
55
|
const key = typeof publicKey === "string" ? publicKeyFromPem(publicKey) : publicKey;
|
|
@@ -70,20 +60,8 @@ function verifyJWT(publicKey, token, options) {
|
|
|
70
60
|
audience: opts.audience,
|
|
71
61
|
});
|
|
72
62
|
const payload = verify.payload;
|
|
73
|
-
return
|
|
74
|
-
id: payload.sub,
|
|
75
|
-
employeeReference: payload.ref,
|
|
76
|
-
organizationReference: payload.org,
|
|
77
|
-
roles: payload.rls,
|
|
78
|
-
permissions: payload.prms,
|
|
79
|
-
segmentReference: payload.seg || null,
|
|
80
|
-
};
|
|
63
|
+
return JWTUser_1.JWTUser.fromPayload(payload);
|
|
81
64
|
});
|
|
82
65
|
}
|
|
83
66
|
exports.verifyJWT = verifyJWT;
|
|
84
|
-
function jwtUserHasRoles(jwtUser, roles) {
|
|
85
|
-
const validateRoles = typeof roles === "string" ? [roles] : roles;
|
|
86
|
-
return validateRoles.some((role) => jwtUser.roles.includes(role));
|
|
87
|
-
}
|
|
88
|
-
exports.jwtUserHasRoles = jwtUserHasRoles;
|
|
89
67
|
//# sourceMappingURL=jwt.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jwt.js","sourceRoot":"","sources":["../src/jwt.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mCAAsE;AACtE,+BAA0C;
|
|
1
|
+
{"version":3,"file":"jwt.js","sourceRoot":"","sources":["../src/jwt.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mCAAsE;AACtE,+BAA0C;AAE1C,uCAAoC;AAEvB,QAAA,aAAa,GAAG,OAAO,CAAC;AAErC,SAAgB,iBAAiB,CAAC,GAAW;IAC3C,OAAO,IAAA,yBAAgB,EAAC;QACtB,GAAG;QACH,MAAM,EAAE,KAAK;KACd,CAAC,CAAC;AACL,CAAC;AALD,8CAKC;AAED,SAAgB,gBAAgB,CAAC,GAAW;IAC1C,OAAO,IAAA,wBAAe,EAAC;QACrB,GAAG;QACH,MAAM,EAAE,KAAK;KACd,CAAC,CAAC;AACL,CAAC;AALD,4CAKC;AAED,SAAgB,sBAAsB,CACpC,OAA8B;IAE9B,uBACE,MAAM,EAAE,OAAO,EACf,QAAQ,EAAE,KAAK,EACf,UAAU,EAAE,KAAK,IACd,OAAO,EACV;AACJ,CAAC;AATD,wDASC;AAED,SAAgB,wBAAwB,CACtC,OAAgC;IAEhC,uBACE,MAAM,EAAE,CAAC,OAAO,EAAE,QAAQ,CAAC,EAC3B,QAAQ,EAAE,KAAK,IACZ,OAAO,EACV;AACJ,CAAC;AARD,4DAQC;AAED,SAAsB,WAAW,CAC/B,UAA8B,EAC9B,IAAa,EACb,OAA8B;;QAE9B,MAAM,GAAG,GACP,OAAO,UAAU,KAAK,QAAQ,CAAC,CAAC,CAAC,iBAAiB,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC;QAE9E,MAAM,IAAI,GAAG,sBAAsB,CAAC,OAAO,CAAC,CAAC;QAE7C,OAAO,IAAI,cAAO,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC;aACtC,kBAAkB,CAAC,EAAE,GAAG,EAAE,qBAAa,EAAE,CAAC;aAC1C,WAAW,EAAE;aACb,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC;aACtB,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC;aAC1B,iBAAiB,CAAC,IAAI,CAAC,UAAU,CAAC;aAClC,IAAI,CAAC,GAAG,CAAC,CAAC;IACf,CAAC;CAAA;AAjBD,kCAiBC;AAED,SAAsB,SAAS,CAC7B,SAA6B,EAC7B,KAAa,EACb,OAAgC;;QAEhC,MAAM,GAAG,GACP,OAAO,SAAS,KAAK,QAAQ,CAAC,CAAC,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QAE1E,MAAM,IAAI,GAAG,wBAAwB,CAAC,OAAO,CAAC,CAAC;QAE/C,MAAM,MAAM,GAAG,MAAM,IAAA,gBAAS,EAAC,KAAK,EAAE,GAAG,EAAE;YACzC,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,UAAU,EAAE,CAAC,qBAAa,CAAC;YAC3B,QAAQ,EAAE,IAAI,CAAC,QAAQ;SACxB,CAAC,CAAC;QAEH,MAAM,OAAO,GAAG,MAAM,CAAC,OAAqB,CAAC;QAE7C,OAAO,iBAAO,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IACtC,CAAC;CAAA;AAnBD,8BAmBC"}
|
|
@@ -4,15 +4,15 @@ export interface JWTPayload extends LibPayload {
|
|
|
4
4
|
ref: string | null;
|
|
5
5
|
org: string;
|
|
6
6
|
rls: string[];
|
|
7
|
-
prms: string
|
|
7
|
+
prms: string;
|
|
8
8
|
seg: string | null;
|
|
9
9
|
}
|
|
10
|
-
export interface
|
|
10
|
+
export interface IJWTUser {
|
|
11
11
|
id: string;
|
|
12
12
|
employeeReference: string | null;
|
|
13
13
|
organizationReference: string;
|
|
14
14
|
roles: string[];
|
|
15
|
-
permissions: string
|
|
15
|
+
permissions: string;
|
|
16
16
|
segmentReference: string | null;
|
|
17
17
|
}
|
|
18
18
|
export interface SignOptions {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/jwt",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "12.0.0",
|
|
4
4
|
"description": "Alan's jwt validator & signer.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -25,5 +25,6 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"jose": "^5.2.2"
|
|
28
|
-
}
|
|
28
|
+
},
|
|
29
|
+
"gitHead": "085a76a387bc0e272e09e9cb1b6f48f814512940"
|
|
29
30
|
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { IJWTUser, JWTPayload } from "./types";
|
|
2
|
+
|
|
3
|
+
export class JWTUser implements IJWTUser {
|
|
4
|
+
id: string;
|
|
5
|
+
|
|
6
|
+
employeeReference: string | null;
|
|
7
|
+
|
|
8
|
+
organizationReference: string;
|
|
9
|
+
|
|
10
|
+
originalOrganizationReference: string | null;
|
|
11
|
+
|
|
12
|
+
roles: string[];
|
|
13
|
+
|
|
14
|
+
permissions: string;
|
|
15
|
+
|
|
16
|
+
segmentReference: string | null;
|
|
17
|
+
|
|
18
|
+
constructor({
|
|
19
|
+
id,
|
|
20
|
+
employeeReference,
|
|
21
|
+
organizationReference,
|
|
22
|
+
roles,
|
|
23
|
+
permissions,
|
|
24
|
+
segmentReference,
|
|
25
|
+
}: IJWTUser) {
|
|
26
|
+
this.id = id;
|
|
27
|
+
this.employeeReference = employeeReference;
|
|
28
|
+
this.organizationReference = organizationReference;
|
|
29
|
+
this.roles = roles;
|
|
30
|
+
this.permissions = permissions;
|
|
31
|
+
this.segmentReference = segmentReference;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static fromPayload(payload: JWTPayload): JWTUser {
|
|
35
|
+
return new JWTUser({
|
|
36
|
+
id: payload.sub,
|
|
37
|
+
employeeReference: payload.ref,
|
|
38
|
+
organizationReference: payload.org,
|
|
39
|
+
roles: payload.rls,
|
|
40
|
+
permissions: payload.prms,
|
|
41
|
+
segmentReference: payload.seg || null,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public toTokenPayload(): JWTPayload {
|
|
46
|
+
return {
|
|
47
|
+
sub: this.id,
|
|
48
|
+
ref: this.employeeReference,
|
|
49
|
+
org: this.organizationReference,
|
|
50
|
+
rls: this.roles,
|
|
51
|
+
prms: this.permissions,
|
|
52
|
+
seg: this.segmentReference,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Old way of checking for a role
|
|
58
|
+
* Will be replaced by permission checks
|
|
59
|
+
* @param role - role code to check
|
|
60
|
+
*/
|
|
61
|
+
public hasRole(role: string): boolean {
|
|
62
|
+
return this.roles.includes(role);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Old way of checking for roles
|
|
67
|
+
* Will be replaced by permission checks
|
|
68
|
+
*/
|
|
69
|
+
public hasRoles(validateRoles: string | string[]): boolean {
|
|
70
|
+
if (typeof validateRoles === "string") {
|
|
71
|
+
return this.hasRole(validateRoles);
|
|
72
|
+
}
|
|
73
|
+
return validateRoles.some((role) => this.hasRole(role));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Check if user has permission to perform an action
|
|
78
|
+
* @note - not implemented, will be implemented in the next release
|
|
79
|
+
* @param permissionCode - permission code to check
|
|
80
|
+
* @returns boolean
|
|
81
|
+
*/
|
|
82
|
+
public hasPermission(_permissionCode: string): boolean {
|
|
83
|
+
throw new Error("Not implemented");
|
|
84
|
+
}
|
|
85
|
+
}
|