response-standardizer 1.0.0 → 1.0.1
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/index.d.ts +3 -0
- package/dist/index.js +47 -0
- package/package.json +4 -4
- package/src/index.ts +54 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Request, Response } from "express";
|
|
1
2
|
import { RestResponseFunctions, RestMiddlewareFunctions } from "./types.js";
|
|
2
3
|
declare global {
|
|
3
4
|
namespace Express {
|
|
@@ -11,5 +12,7 @@ declare global {
|
|
|
11
12
|
}
|
|
12
13
|
}
|
|
13
14
|
}
|
|
15
|
+
export declare const protect: (req: Request, res: Response, next: any) => void;
|
|
16
|
+
export declare const role: (allowedRoles: string[]) => (req: any, res: any, next: any) => void;
|
|
14
17
|
export declare const RestResponse: RestResponseFunctions;
|
|
15
18
|
export declare const RestMiddleware: RestMiddlewareFunctions;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,51 @@
|
|
|
1
1
|
import { generateRequestId, getTimestamp } from "./utils.js";
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
import jwt from "jsonwebtoken";
|
|
4
|
+
async function getKeycloakPublicKey() {
|
|
5
|
+
const realmUrl = `http://${process.env.KEYCLOAK_SERVICE ?? "localhost"}/realms/${process.env.KEYCLOAK_REALM ?? "master"}`;
|
|
6
|
+
console.log(`Keycloak PublicKey Url: ${realmUrl}`);
|
|
7
|
+
const resp = await axios.get(realmUrl);
|
|
8
|
+
const key = resp.data.public_key;
|
|
9
|
+
const pem = `-----BEGIN PUBLIC KEY-----\n${key.match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
|
|
10
|
+
return pem;
|
|
11
|
+
}
|
|
12
|
+
const KEYCLOAK_PUBLIC_KEY = await getKeycloakPublicKey();
|
|
13
|
+
export const protect = (req, res, next) => {
|
|
14
|
+
const authHeader = req.headers["authorization"];
|
|
15
|
+
if (!authHeader)
|
|
16
|
+
return RestResponse.unauthorized(req, res);
|
|
17
|
+
const token = authHeader.split(" ")[1];
|
|
18
|
+
if (!token)
|
|
19
|
+
return RestResponse.unauthorized(req, res, "Token malformed");
|
|
20
|
+
try {
|
|
21
|
+
const decoded = jwt.verify(token, KEYCLOAK_PUBLIC_KEY, { algorithms: ["RS256"] });
|
|
22
|
+
req.user = decoded;
|
|
23
|
+
next();
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
console.error(err);
|
|
27
|
+
return RestResponse.unauthorized(req, res, "Token is not valid");
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
export const role = (allowedRoles) => {
|
|
31
|
+
return (req, res, next) => {
|
|
32
|
+
const user = req.user;
|
|
33
|
+
if (!user) {
|
|
34
|
+
return RestResponse.unauthorized(req, res);
|
|
35
|
+
}
|
|
36
|
+
const realmRoles = (user?.realm_access?.roles ?? []).map((r) => r.toUpperCase());
|
|
37
|
+
const clientRoles = Object.values(user?.resource_access ?? {})
|
|
38
|
+
.flatMap((r) => r.roles ?? [])
|
|
39
|
+
.map((r) => r.toUpperCase());
|
|
40
|
+
const allowedUpper = allowedRoles.map(r => r.toUpperCase());
|
|
41
|
+
const allRoles = [...realmRoles, ...clientRoles];
|
|
42
|
+
const hasAccess = allowedUpper.some((role) => allRoles.includes(role));
|
|
43
|
+
if (!hasAccess) {
|
|
44
|
+
return RestResponse.accessDenied(req, res);
|
|
45
|
+
}
|
|
46
|
+
next();
|
|
47
|
+
};
|
|
48
|
+
};
|
|
2
49
|
const success = (req, res, data, message = null) => {
|
|
3
50
|
res.status(200).json({ data, message });
|
|
4
51
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "response-standardizer",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Express middleware to standardize API responses",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -18,17 +18,17 @@
|
|
|
18
18
|
"url": "https://gitlab.com/dezhnevesht-archive-software/response-standardizer/issues"
|
|
19
19
|
},
|
|
20
20
|
"homepage": "https://gitlab.com/dezhnevesht-archive-software/response-standardizer#readme",
|
|
21
|
-
|
|
22
21
|
"peerDependencies": {
|
|
23
22
|
"express": "^4 || ^5"
|
|
24
23
|
},
|
|
25
|
-
|
|
26
24
|
"dependencies": {
|
|
25
|
+
"axios": "^1.13.2",
|
|
26
|
+
"jsonwebtoken": "^9.0.2",
|
|
27
27
|
"uuid": "^9.0.1"
|
|
28
28
|
},
|
|
29
|
-
|
|
30
29
|
"devDependencies": {
|
|
31
30
|
"@types/express": "^5.0.5",
|
|
31
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
32
32
|
"@types/uuid": "^10.0.0",
|
|
33
33
|
"ts-node-dev": "^2.0.0",
|
|
34
34
|
"typescript": "^5.9.3"
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from "express";
|
|
2
2
|
import { ErrorFields, PaginationMeta, StandardResponse, RestResponseFunctions, RestMiddlewareFunctions } from "./types.js";
|
|
3
3
|
import { generateRequestId, getTimestamp } from "./utils.js";
|
|
4
|
+
import axios from "axios";
|
|
5
|
+
import jwt from "jsonwebtoken";
|
|
4
6
|
declare global {
|
|
5
7
|
namespace Express {
|
|
6
8
|
interface Response {
|
|
@@ -14,6 +16,58 @@ declare global {
|
|
|
14
16
|
}
|
|
15
17
|
}
|
|
16
18
|
|
|
19
|
+
async function getKeycloakPublicKey(): Promise<string> {
|
|
20
|
+
const realmUrl = `http://${process.env.KEYCLOAK_SERVICE??"localhost"}/realms/${process.env.KEYCLOAK_REALM??"master"}`;
|
|
21
|
+
console.log(`Keycloak PublicKey Url: ${realmUrl}`);
|
|
22
|
+
const resp = await axios.get(realmUrl);
|
|
23
|
+
const key = resp.data.public_key;
|
|
24
|
+
const pem = `-----BEGIN PUBLIC KEY-----\n${key.match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
|
|
25
|
+
return pem;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const KEYCLOAK_PUBLIC_KEY: any = await getKeycloakPublicKey();
|
|
29
|
+
|
|
30
|
+
export const protect = (req: Request, res: Response, next: any) => {
|
|
31
|
+
const authHeader = req.headers["authorization"];
|
|
32
|
+
if (!authHeader)
|
|
33
|
+
return RestResponse.unauthorized(req, res)
|
|
34
|
+
|
|
35
|
+
const token = authHeader.split(" ")[1];
|
|
36
|
+
if (!token)
|
|
37
|
+
return RestResponse.unauthorized(req, res, "Token malformed")
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
const decoded = jwt.verify(token, KEYCLOAK_PUBLIC_KEY, { algorithms: ["RS256"] });
|
|
41
|
+
(req as any).user = decoded;
|
|
42
|
+
next();
|
|
43
|
+
} catch (err) {
|
|
44
|
+
console.error(err)
|
|
45
|
+
return RestResponse.unauthorized(req, res, "Token is not valid")
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export const role = (allowedRoles: string[]) => {
|
|
49
|
+
return (req: any, res: any, next: any) => {
|
|
50
|
+
const user = req.user;
|
|
51
|
+
if (!user) {
|
|
52
|
+
return RestResponse.unauthorized(req, res);
|
|
53
|
+
}
|
|
54
|
+
const realmRoles: string[] = (user?.realm_access?.roles ?? []).map((r: string) => r.toUpperCase());
|
|
55
|
+
const clientRoles: string[] = Object.values(user?.resource_access ?? {})
|
|
56
|
+
.flatMap((r: any) => r.roles ?? [])
|
|
57
|
+
.map((r: string) => r.toUpperCase());
|
|
58
|
+
const allowedUpper = allowedRoles.map(r => r.toUpperCase());
|
|
59
|
+
const allRoles = [...realmRoles, ...clientRoles];
|
|
60
|
+
const hasAccess: boolean = allowedUpper.some((role: string) =>
|
|
61
|
+
allRoles.includes(role)
|
|
62
|
+
);
|
|
63
|
+
if (!hasAccess) {
|
|
64
|
+
return RestResponse.accessDenied(req, res)
|
|
65
|
+
}
|
|
66
|
+
next()
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
}
|
|
70
|
+
|
|
17
71
|
const success = <T>(req: Request, res: Response, data: T, message: string | null = null) => {
|
|
18
72
|
res.status(200).json({ data, message });
|
|
19
73
|
};
|