response-standardizer 1.0.3 → 1.0.5
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 +2 -2
- package/dist/index.js +31 -33
- package/dist/types.d.ts +0 -4
- package/package.json +1 -1
- package/src/index.ts +26 -27
- package/src/types.ts +0 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RestResponseFunctions, RestMiddlewareFunctions
|
|
1
|
+
import { RestResponseFunctions, RestMiddlewareFunctions } from "./types.js";
|
|
2
2
|
declare global {
|
|
3
3
|
namespace Express {
|
|
4
4
|
interface Response {
|
|
@@ -15,6 +15,6 @@ export declare const initKeycloak: (config: {
|
|
|
15
15
|
service?: string;
|
|
16
16
|
realm?: string;
|
|
17
17
|
}) => Promise<void>;
|
|
18
|
-
export declare const
|
|
18
|
+
export declare const protect: (allowedRoles?: string[]) => (req: any, res: any, next: any) => void;
|
|
19
19
|
export declare const RestResponse: RestResponseFunctions;
|
|
20
20
|
export declare const RestMiddleware: RestMiddlewareFunctions;
|
package/dist/index.js
CHANGED
|
@@ -11,45 +11,43 @@ export const initKeycloak = async (config) => {
|
|
|
11
11
|
const key = resp.data.public_key;
|
|
12
12
|
KEYCLOAK_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----\n${key.match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
|
|
13
13
|
};
|
|
14
|
-
const protect = (
|
|
15
|
-
const authHeader = req.headers["authorization"];
|
|
16
|
-
if (!authHeader)
|
|
17
|
-
return RestResponse.unauthorized(req, res);
|
|
18
|
-
const token = authHeader.split(" ")[1];
|
|
19
|
-
if (!token)
|
|
20
|
-
return RestResponse.unauthorized(req, res, "Token malformed");
|
|
21
|
-
try {
|
|
22
|
-
const decoded = jwt.verify(token, KEYCLOAK_PUBLIC_KEY, { algorithms: ["RS256"] });
|
|
23
|
-
req.user = decoded;
|
|
24
|
-
next();
|
|
25
|
-
}
|
|
26
|
-
catch (err) {
|
|
27
|
-
console.error(err);
|
|
28
|
-
return RestResponse.unauthorized(req, res, "Token is not valid");
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
const role = (allowedRoles) => {
|
|
14
|
+
export const protect = (allowedRoles) => {
|
|
32
15
|
return (req, res, next) => {
|
|
33
|
-
const
|
|
34
|
-
if (!
|
|
16
|
+
const authHeader = req.headers["authorization"];
|
|
17
|
+
if (!authHeader)
|
|
35
18
|
return RestResponse.unauthorized(req, res);
|
|
19
|
+
const token = authHeader.split(" ")[1];
|
|
20
|
+
if (!token)
|
|
21
|
+
return RestResponse.unauthorized(req, res, "Token malformed");
|
|
22
|
+
try {
|
|
23
|
+
const decoded = jwt.verify(token, KEYCLOAK_PUBLIC_KEY, { algorithms: ["RS256"] });
|
|
24
|
+
req.user = decoded;
|
|
25
|
+
if (allowedRoles)
|
|
26
|
+
return role(req, res, next, allowedRoles);
|
|
27
|
+
next();
|
|
36
28
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
.
|
|
40
|
-
.map((r) => r.toUpperCase());
|
|
41
|
-
const allowedUpper = allowedRoles.map(r => r.toUpperCase());
|
|
42
|
-
const allRoles = [...realmRoles, ...clientRoles];
|
|
43
|
-
const hasAccess = allowedUpper.some((role) => allRoles.includes(role));
|
|
44
|
-
if (!hasAccess) {
|
|
45
|
-
return RestResponse.accessDenied(req, res);
|
|
29
|
+
catch (err) {
|
|
30
|
+
console.error(err);
|
|
31
|
+
return RestResponse.unauthorized(req, res, "Token is not valid");
|
|
46
32
|
}
|
|
47
|
-
next();
|
|
48
33
|
};
|
|
49
34
|
};
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
35
|
+
const role = (req, res, next, allowedRoles) => {
|
|
36
|
+
const user = req.user;
|
|
37
|
+
if (!user) {
|
|
38
|
+
return RestResponse.unauthorized(req, res);
|
|
39
|
+
}
|
|
40
|
+
const realmRoles = (user?.realm_access?.roles ?? []).map((r) => r.toUpperCase());
|
|
41
|
+
const clientRoles = Object.values(user?.resource_access ?? {})
|
|
42
|
+
.flatMap((r) => r.roles ?? [])
|
|
43
|
+
.map((r) => r.toUpperCase());
|
|
44
|
+
const allowedUpper = allowedRoles.map(r => r.toUpperCase());
|
|
45
|
+
const allRoles = [...realmRoles, ...clientRoles];
|
|
46
|
+
const hasAccess = allowedUpper.some((role) => allRoles.includes(role));
|
|
47
|
+
if (!hasAccess) {
|
|
48
|
+
return RestResponse.accessDenied(req, res);
|
|
49
|
+
}
|
|
50
|
+
next();
|
|
53
51
|
};
|
|
54
52
|
const success = (req, res, data, message = null) => {
|
|
55
53
|
res.status(200).json({ data, message });
|
package/dist/types.d.ts
CHANGED
|
@@ -22,10 +22,6 @@ export type Middleware = (req: Request, res: Response, next: NextFunction) => vo
|
|
|
22
22
|
export interface RestMiddlewareFunctions {
|
|
23
23
|
responseHandlerMiddleware: <T>(req: Request, res: Response, next: NextFunction) => void;
|
|
24
24
|
}
|
|
25
|
-
export interface KeycloakFunctions {
|
|
26
|
-
protect: (req: Request, res: Response, next: any) => void;
|
|
27
|
-
role: (allowedRoles: string[]) => void;
|
|
28
|
-
}
|
|
29
25
|
export interface RestResponseFunctions {
|
|
30
26
|
success: <T>(req: Request, res: Response, data: T, message?: string | null) => void;
|
|
31
27
|
paginate: <T>(req: Request, res: Response, data: T, pagination: PaginationMeta, message?: string | null) => void;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from "express";
|
|
2
|
-
import { ErrorFields, PaginationMeta, StandardResponse, RestResponseFunctions, RestMiddlewareFunctions
|
|
2
|
+
import { ErrorFields, PaginationMeta, StandardResponse, RestResponseFunctions, RestMiddlewareFunctions } from "./types.js";
|
|
3
3
|
import { generateRequestId, getTimestamp } from "./utils.js";
|
|
4
4
|
import axios from "axios";
|
|
5
5
|
import jwt from "jsonwebtoken";
|
|
@@ -26,27 +26,32 @@ export const initKeycloak = async (config: { service?: string; realm?: string })
|
|
|
26
26
|
KEYCLOAK_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----\n${key.match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
const protect = (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
29
|
+
export const protect = (allowedRoles?: string[]) => {
|
|
30
|
+
return (req: any, res: any, 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
|
+
if(allowedRoles)
|
|
43
|
+
return role(req, res, next, allowedRoles);
|
|
44
|
+
|
|
45
|
+
next();
|
|
46
|
+
} catch (err) {
|
|
47
|
+
console.error(err)
|
|
48
|
+
return RestResponse.unauthorized(req, res, "Token is not valid")
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
46
52
|
}
|
|
47
|
-
const role = (allowedRoles: string[]) => {
|
|
48
|
-
|
|
49
|
-
const user = req.user;
|
|
53
|
+
const role = (req: any, res: any, next: any, allowedRoles: string[]) => {
|
|
54
|
+
const user = req.user;
|
|
50
55
|
if (!user) {
|
|
51
56
|
return RestResponse.unauthorized(req, res);
|
|
52
57
|
}
|
|
@@ -63,12 +68,6 @@ const role = (allowedRoles: string[]) => {
|
|
|
63
68
|
return RestResponse.accessDenied(req, res)
|
|
64
69
|
}
|
|
65
70
|
next()
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
export const Keycloak: KeycloakFunctions = {
|
|
70
|
-
protect,
|
|
71
|
-
role
|
|
72
71
|
}
|
|
73
72
|
const success = <T>(req: Request, res: Response, data: T, message: string | null = null) => {
|
|
74
73
|
res.status(200).json({ data, message });
|
package/src/types.ts
CHANGED
|
@@ -27,10 +27,6 @@ export type Middleware = (req: Request, res: Response, next: NextFunction) => vo
|
|
|
27
27
|
export interface RestMiddlewareFunctions {
|
|
28
28
|
responseHandlerMiddleware: <T>(req: Request, res: Response, next: NextFunction) => void
|
|
29
29
|
}
|
|
30
|
-
export interface KeycloakFunctions {
|
|
31
|
-
protect:(req: Request, res: Response, next: any) => void;
|
|
32
|
-
role: (allowedRoles: string[]) => void;
|
|
33
|
-
}
|
|
34
30
|
export interface RestResponseFunctions {
|
|
35
31
|
success: <T>(req: Request, res: Response, data: T, message?: string | null) => void;
|
|
36
32
|
paginate: <T>(
|