@alanszp/express-common-fn 10.0.3 → 11.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/node_modules/@alanszp/audit/package.json +5 -4
- package/node_modules/@alanszp/errors/package.json +3 -2
- package/node_modules/@alanszp/express/dist/middlewares/authenticateUser.js +4 -3
- package/node_modules/@alanszp/express/dist/middlewares/authenticateUser.js.map +1 -1
- package/node_modules/@alanszp/express/dist/middlewares/hasRoles.js +1 -2
- package/node_modules/@alanszp/express/dist/middlewares/hasRoles.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/middlewares/authenticateUser.ts +11 -8
- package/node_modules/@alanszp/express/src/middlewares/hasRoles.ts +1 -2
- 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 +6 -6
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/audit",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "11.0.0",
|
|
4
4
|
"description": "Alan's audit util.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -19,11 +19,12 @@
|
|
|
19
19
|
"yalc-publish": "yarn run yalc publish"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@alanszp/errors": "^
|
|
23
|
-
"@alanszp/logger": "^
|
|
22
|
+
"@alanszp/errors": "^11.0.0",
|
|
23
|
+
"@alanszp/logger": "^11.0.0",
|
|
24
24
|
"lodash": "^4.17.21"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/node": "^20.11.17"
|
|
28
|
-
}
|
|
28
|
+
},
|
|
29
|
+
"gitHead": "60b62fa2fff34ef094ea9af69c2bed2373c70f64"
|
|
29
30
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/errors",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "11.0.0",
|
|
4
4
|
"description": "Alan's basic errors classes.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -22,5 +22,6 @@
|
|
|
22
22
|
"@types/node": "^20.11.17",
|
|
23
23
|
"ts-node": "^10.0.0",
|
|
24
24
|
"typescript": "^4.3.4"
|
|
25
|
-
}
|
|
25
|
+
},
|
|
26
|
+
"gitHead": "60b62fa2fff34ef094ea9af69c2bed2373c70f64"
|
|
26
27
|
}
|
|
@@ -54,14 +54,15 @@ const middlewareGetterByAuthType = {
|
|
|
54
54
|
user: "0",
|
|
55
55
|
org: "lara",
|
|
56
56
|
});
|
|
57
|
-
return Promise.resolve({
|
|
57
|
+
return Promise.resolve(new jwt_1.JWTUser({
|
|
58
58
|
id: "0",
|
|
59
59
|
employeeReference: "0",
|
|
60
60
|
organizationReference: "lara",
|
|
61
61
|
roles: [],
|
|
62
62
|
segmentReference: null,
|
|
63
|
-
permissions
|
|
64
|
-
|
|
63
|
+
// This will be changed in the near future to grab all permissions.
|
|
64
|
+
permissions: "MA==", // 0 in base64
|
|
65
|
+
}));
|
|
65
66
|
}
|
|
66
67
|
else {
|
|
67
68
|
return null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authenticateUser.js","sourceRoot":"","sources":["../../src/middlewares/authenticateUser.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,sCAAiE;AACjE,4CAAoD;AACpD,kDAA+C;AAE/C,kEAA+D;AAG/D,mCAAgD;AAEhD,SAAS,wBAAwB,CAC/B,aAAiC;IAEjC,IAAI,CAAC,aAAa;QAAE,OAAO,SAAS,CAAC;IACrC,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,KAAK,CAAC,GAAG,aAAa,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAEzD,IAAI,MAAM,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,SAAS,CAAC;IAE9D,OAAO,GAAG,CAAC;AACb,CAAC;AAED,IAAY,WAGX;AAHD,WAAY,WAAW;IACrB,0BAAW,CAAA;IACX,kCAAmB,CAAA;AACrB,CAAC,EAHW,WAAW,GAAX,mBAAW,KAAX,mBAAW,QAGtB;AA0BD,MAAM,0BAA0B,GAO5B;IACF,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,CACjB,GAA8B,EAC9B,OAA4C,EAC5C,MAAe,EACf,EAAE;QACF,IAAI;YACF,IAAI,CAAC,GAAG;gBAAE,OAAO,SAAS,CAAC;YAC3B,MAAM,OAAO,GAAG,MAAM,IAAA,eAAS,EAC7B,OAAO,CAAC,gBAAgB,CAAC,SAAS,EAClC,GAAG,EACH,IAAA,aAAI,EAAC,OAAO,CAAC,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AI,CACH,IAAA,qBAAS,EACP,IAAI,0BAAiB,CAAC;4BACpB,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC;gCACzB,CAAC,CAAC,6BAA6B,WAAW,EAAE;gCAC5C,CAAC,CAAC,6BAA6B,WAAW,EAAE;yBAC/C,CAAC,CACH,CACF,CAAC;wBACJ,OAAO;qBACR;oBAED,MAAM,OAAO,GAAY,sBAAsB,CAAC,CAAC,CAAC,CAAC;oBACnD,GAAG,CAAC,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC;oBAC9B,GAAG,CAAC,OAAO,CAAC,aAAa,CAAC,IAAI,CAC5B,OAAO,CAAC,iBAAiB,KAAK,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CACtD,CAAC;oBACF,IAAI,EAAE,CAAC;iBACR;gBAAC,OAAO,KAAc,EAAE;oBACvB,MAAM,CAAC,IAAI,CAAC,6BAA6B,EAAE;wBACzC,GAAG;wBACH,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,aAAa;wBAChC,OAAO,EAAE,WAAW;wBACpB,KAAK;qBACN,CAAC,CAAC;oBACH,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAA,qBAAS,EAAC,IAAI,0BAAiB,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;iBACrE;YACH,CAAC;SAAA,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AA7DD,8CA6DC"}
|
|
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.hasRoles = void 0;
|
|
4
4
|
const errors_1 = require("@alanszp/errors");
|
|
5
5
|
const errorView_1 = require("../views/errorView");
|
|
6
|
-
const jwt_1 = require("@alanszp/jwt");
|
|
7
6
|
function response401(res) {
|
|
8
7
|
res.status(401).json((0, errorView_1.errorView)(new errors_1.UnauthorizedError(["permissions"])));
|
|
9
8
|
}
|
|
@@ -13,7 +12,7 @@ function hasRoles(roles) {
|
|
|
13
12
|
if (!jwtUser) {
|
|
14
13
|
return response401(res);
|
|
15
14
|
}
|
|
16
|
-
if (
|
|
15
|
+
if (jwtUser.hasRoles(roles)) {
|
|
17
16
|
return next();
|
|
18
17
|
}
|
|
19
18
|
response401(res);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hasRoles.js","sourceRoot":"","sources":["../../src/middlewares/hasRoles.ts"],"names":[],"mappings":";;;AAAA,4CAAoD;AACpD,kDAA+C;
|
|
1
|
+
{"version":3,"file":"hasRoles.js","sourceRoot":"","sources":["../../src/middlewares/hasRoles.ts"],"names":[],"mappings":";;;AAAA,4CAAoD;AACpD,kDAA+C;AAI/C,SAAS,WAAW,CAAC,GAAa;IAChC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAA,qBAAS,EAAC,IAAI,0BAAiB,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;AAC1E,CAAC;AAED,SAAgB,QAAQ,CACtB,KAAwB;IAExB,OAAO,CAAC,GAAmB,EAAE,GAAa,EAAE,IAAkB,EAAE,EAAE;QAChE,MAAM,EAAE,OAAO,EAAE,GAAG,GAAG,CAAC,OAAO,CAAC;QAChC,IAAI,CAAC,OAAO,EAAE;YACZ,OAAO,WAAW,CAAC,GAAG,CAAC,CAAC;SACzB;QAED,IAAI,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE;YAC3B,OAAO,IAAI,EAAE,CAAC;SACf;QAED,WAAW,CAAC,GAAG,CAAC,CAAC;IACnB,CAAC,CAAC;AACJ,CAAC;AAfD,4BAeC"}
|
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.laraJwtUserMock = exports.userJwtUserMock = void 0;
|
|
4
|
-
|
|
4
|
+
const jwt_1 = require("@alanszp/jwt");
|
|
5
|
+
exports.userJwtUserMock = new jwt_1.JWTUser({
|
|
5
6
|
id: "1",
|
|
6
7
|
employeeReference: "1",
|
|
7
8
|
organizationReference: "test",
|
|
8
9
|
roles: [],
|
|
9
|
-
permissions:
|
|
10
|
+
permissions: "MA==",
|
|
10
11
|
segmentReference: null,
|
|
11
|
-
};
|
|
12
|
-
exports.laraJwtUserMock = {
|
|
12
|
+
});
|
|
13
|
+
exports.laraJwtUserMock = new jwt_1.JWTUser({
|
|
13
14
|
id: "0",
|
|
14
15
|
employeeReference: "0",
|
|
15
16
|
organizationReference: "lara",
|
|
16
17
|
roles: [],
|
|
17
|
-
permissions:
|
|
18
|
+
permissions: "MA==",
|
|
18
19
|
segmentReference: null,
|
|
19
|
-
};
|
|
20
|
+
});
|
|
20
21
|
//# sourceMappingURL=jwtUserMocks.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jwtUserMocks.js","sourceRoot":"","sources":["../../../src/test/mocks/jwtUserMocks.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"jwtUserMocks.js","sourceRoot":"","sources":["../../../src/test/mocks/jwtUserMocks.ts"],"names":[],"mappings":";;;AAAA,sCAAuC;AAE1B,QAAA,eAAe,GAAY,IAAI,aAAO,CAAC;IAClD,EAAE,EAAE,GAAG;IACP,iBAAiB,EAAE,GAAG;IACtB,qBAAqB,EAAE,MAAM;IAC7B,KAAK,EAAE,EAAE;IACT,WAAW,EAAE,MAAM;IACnB,gBAAgB,EAAE,IAAI;CACvB,CAAC,CAAC;AAEU,QAAA,eAAe,GAAY,IAAI,aAAO,CAAC;IAClD,EAAE,EAAE,GAAG;IACP,iBAAiB,EAAE,GAAG;IACtB,qBAAqB,EAAE,MAAM;IAC7B,KAAK,EAAE,EAAE;IACT,WAAW,EAAE,MAAM;IACnB,gBAAgB,EAAE,IAAI;CACvB,CAAC,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/express",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "11.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": "^11.0.0",
|
|
39
|
+
"@alanszp/errors": "^11.0.0",
|
|
40
|
+
"@alanszp/jwt": "^11.0.0",
|
|
41
|
+
"@alanszp/logger": "^11.0.0",
|
|
42
|
+
"@alanszp/shared-context": "^11.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": "60b62fa2fff34ef094ea9af69c2bed2373c70f64"
|
|
49
49
|
}
|
|
@@ -89,14 +89,17 @@ const middlewareGetterByAuthType: Record<
|
|
|
89
89
|
user: "0",
|
|
90
90
|
org: "lara",
|
|
91
91
|
});
|
|
92
|
-
return Promise.resolve(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
92
|
+
return Promise.resolve(
|
|
93
|
+
new JWTUser({
|
|
94
|
+
id: "0",
|
|
95
|
+
employeeReference: "0",
|
|
96
|
+
organizationReference: "lara",
|
|
97
|
+
roles: [],
|
|
98
|
+
segmentReference: null,
|
|
99
|
+
// This will be changed in the near future to grab all permissions.
|
|
100
|
+
permissions: "MA==", // 0 in base64
|
|
101
|
+
})
|
|
102
|
+
);
|
|
100
103
|
} else {
|
|
101
104
|
return null;
|
|
102
105
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { UnauthorizedError } from "@alanszp/errors";
|
|
2
2
|
import { errorView } from "../views/errorView";
|
|
3
3
|
import { NextFunction, Response } from "express";
|
|
4
|
-
import { jwtUserHasRoles } from "@alanszp/jwt";
|
|
5
4
|
import { GenericRequest } from "../types/GenericRequest";
|
|
6
5
|
|
|
7
6
|
function response401(res: Response): void {
|
|
@@ -17,7 +16,7 @@ export function hasRoles(
|
|
|
17
16
|
return response401(res);
|
|
18
17
|
}
|
|
19
18
|
|
|
20
|
-
if (
|
|
19
|
+
if (jwtUser.hasRoles(roles)) {
|
|
21
20
|
return next();
|
|
22
21
|
}
|
|
23
22
|
|
|
@@ -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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AC,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,K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C,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": "11.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": "60b62fa2fff34ef094ea9af69c2bed2373c70f64"
|
|
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
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createPublicKey, createPrivateKey, KeyObject } from "crypto";
|
|
2
2
|
import { SignJWT, jwtVerify } from "jose";
|
|
3
|
-
import type { JWTPayload,
|
|
3
|
+
import type { JWTPayload, SignOptions, VerifyOptions } from "./types";
|
|
4
|
+
import { JWTUser } from "./JWTUser";
|
|
4
5
|
|
|
5
6
|
export const JWT_ALGORITHM = "RS512";
|
|
6
7
|
|
|
@@ -49,7 +50,7 @@ export async function generateJWT(
|
|
|
49
50
|
|
|
50
51
|
const opts = withDefaultSignOptions(options);
|
|
51
52
|
|
|
52
|
-
return new SignJWT(
|
|
53
|
+
return new SignJWT(user.toTokenPayload())
|
|
53
54
|
.setProtectedHeader({ alg: JWT_ALGORITHM })
|
|
54
55
|
.setIssuedAt()
|
|
55
56
|
.setIssuer(opts.issuer)
|
|
@@ -58,17 +59,6 @@ export async function generateJWT(
|
|
|
58
59
|
.sign(key);
|
|
59
60
|
}
|
|
60
61
|
|
|
61
|
-
export function createTokenPayload(user: JWTUser): JWTPayload {
|
|
62
|
-
return {
|
|
63
|
-
sub: user.id,
|
|
64
|
-
ref: user.employeeReference,
|
|
65
|
-
org: user.organizationReference,
|
|
66
|
-
rls: user.roles,
|
|
67
|
-
prms: user.permissions,
|
|
68
|
-
seg: user.segmentReference,
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
62
|
export async function verifyJWT(
|
|
73
63
|
publicKey: KeyObject | string,
|
|
74
64
|
token: string,
|
|
@@ -87,20 +77,5 @@ export async function verifyJWT(
|
|
|
87
77
|
|
|
88
78
|
const payload = verify.payload as JWTPayload;
|
|
89
79
|
|
|
90
|
-
return
|
|
91
|
-
id: payload.sub,
|
|
92
|
-
employeeReference: payload.ref,
|
|
93
|
-
organizationReference: payload.org,
|
|
94
|
-
roles: payload.rls,
|
|
95
|
-
permissions: payload.prms,
|
|
96
|
-
segmentReference: payload.seg || null,
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function jwtUserHasRoles(
|
|
101
|
-
jwtUser: JWTUser,
|
|
102
|
-
roles: string | string[]
|
|
103
|
-
): boolean {
|
|
104
|
-
const validateRoles = typeof roles === "string" ? [roles] : roles;
|
|
105
|
-
return validateRoles.some((role) => jwtUser.roles.includes(role));
|
|
80
|
+
return JWTUser.fromPayload(payload);
|
|
106
81
|
}
|
|
@@ -5,17 +5,17 @@ export interface JWTPayload extends LibPayload {
|
|
|
5
5
|
ref: string | null;
|
|
6
6
|
org: string;
|
|
7
7
|
rls: string[];
|
|
8
|
-
prms: string
|
|
8
|
+
prms: string;
|
|
9
9
|
// segmentReference
|
|
10
10
|
seg: string | null;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export interface
|
|
13
|
+
export interface IJWTUser {
|
|
14
14
|
id: string;
|
|
15
15
|
employeeReference: string | null;
|
|
16
16
|
organizationReference: string;
|
|
17
17
|
roles: string[];
|
|
18
|
-
permissions: string
|
|
18
|
+
permissions: string;
|
|
19
19
|
segmentReference: string | null;
|
|
20
20
|
}
|
|
21
21
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/logger",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "11.0.0",
|
|
4
4
|
"description": "Alan's logger util.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -31,5 +31,6 @@
|
|
|
31
31
|
"ts-node": "^10.0.0",
|
|
32
32
|
"tslint": "^5.11.0",
|
|
33
33
|
"typescript": "^4.3.4"
|
|
34
|
-
}
|
|
34
|
+
},
|
|
35
|
+
"gitHead": "60b62fa2fff34ef094ea9af69c2bed2373c70f64"
|
|
35
36
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/express-common-fn",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "11.0.0",
|
|
4
4
|
"description": "Alan's express function to handle common errors and format uniformly.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
"yalc-publish": "yarn run yalc publish"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@alanszp/errors": "^
|
|
23
|
-
"@alanszp/express": "^
|
|
24
|
-
"@alanszp/logger": "^
|
|
25
|
-
"@alanszp/validations": "^
|
|
22
|
+
"@alanszp/errors": "^11.0.0",
|
|
23
|
+
"@alanszp/express": "^11.0.0",
|
|
24
|
+
"@alanszp/logger": "^11.0.0",
|
|
25
|
+
"@alanszp/validations": "^11.0.0"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
28
|
"express": "^4.17.1",
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"typeorm": "^0.2.34",
|
|
35
35
|
"typescript": "4.7.4"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "60b62fa2fff34ef094ea9af69c2bed2373c70f64"
|
|
38
38
|
}
|