response-standardizer 1.0.3 → 1.0.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/dist/index.d.ts +2 -2
- package/dist/index.js +20 -20
- package/dist/types.d.ts +0 -4
- package/package.json +1 -1
- package/src/index.ts +24 -22
- 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 | ((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,22 +11,26 @@ 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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
14
|
+
export const protect = (allowedRoles) => {
|
|
15
|
+
return (req, res, next) => {
|
|
16
|
+
const authHeader = req.headers["authorization"];
|
|
17
|
+
if (!authHeader)
|
|
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(allowedRoles);
|
|
27
|
+
next();
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
console.error(err);
|
|
31
|
+
return RestResponse.unauthorized(req, res, "Token is not valid");
|
|
32
|
+
}
|
|
33
|
+
};
|
|
30
34
|
};
|
|
31
35
|
const role = (allowedRoles) => {
|
|
32
36
|
return (req, res, next) => {
|
|
@@ -47,10 +51,6 @@ const role = (allowedRoles) => {
|
|
|
47
51
|
next();
|
|
48
52
|
};
|
|
49
53
|
};
|
|
50
|
-
export const Keycloak = {
|
|
51
|
-
protect,
|
|
52
|
-
role
|
|
53
|
-
};
|
|
54
54
|
const success = (req, res, data, message = null) => {
|
|
55
55
|
res.status(200).json({ data, message });
|
|
56
56
|
};
|
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,23 +26,29 @@ 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(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
53
|
const role = (allowedRoles: string[]) => {
|
|
48
54
|
return (req: any, res: any, next: any) => {
|
|
@@ -66,10 +72,6 @@ const role = (allowedRoles: string[]) => {
|
|
|
66
72
|
}
|
|
67
73
|
|
|
68
74
|
}
|
|
69
|
-
export const Keycloak: KeycloakFunctions = {
|
|
70
|
-
protect,
|
|
71
|
-
role
|
|
72
|
-
}
|
|
73
75
|
const success = <T>(req: Request, res: Response, data: T, message: string | null = null) => {
|
|
74
76
|
res.status(200).json({ data, message });
|
|
75
77
|
};
|
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>(
|