response-standardizer 1.0.2 → 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 +1 -3
- package/dist/index.js +21 -17
- package/package.json +1 -1
- package/src/index.ts +24 -19
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Request, Response } from "express";
|
|
2
1
|
import { RestResponseFunctions, RestMiddlewareFunctions } from "./types.js";
|
|
3
2
|
declare global {
|
|
4
3
|
namespace Express {
|
|
@@ -16,7 +15,6 @@ export declare const initKeycloak: (config: {
|
|
|
16
15
|
service?: string;
|
|
17
16
|
realm?: string;
|
|
18
17
|
}) => Promise<void>;
|
|
19
|
-
export declare const protect: (req:
|
|
20
|
-
export declare const role: (allowedRoles: string[]) => (req: any, res: any, next: any) => void;
|
|
18
|
+
export declare const protect: (allowedRoles?: string[]) => (req: any, res: any, next: any) => void | ((req: any, res: any, next: any) => void);
|
|
21
19
|
export declare const RestResponse: RestResponseFunctions;
|
|
22
20
|
export declare const RestMiddleware: RestMiddlewareFunctions;
|
package/dist/index.js
CHANGED
|
@@ -11,24 +11,28 @@ 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
|
-
export 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) => {
|
|
33
37
|
const user = req.user;
|
|
34
38
|
if (!user) {
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -26,25 +26,31 @@ 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
|
-
export 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) => {
|
|
49
55
|
const user = req.user;
|
|
50
56
|
if (!user) {
|
|
@@ -66,7 +72,6 @@ export const role = (allowedRoles: string[]) => {
|
|
|
66
72
|
}
|
|
67
73
|
|
|
68
74
|
}
|
|
69
|
-
|
|
70
75
|
const success = <T>(req: Request, res: Response, data: T, message: string | null = null) => {
|
|
71
76
|
res.status(200).json({ data, message });
|
|
72
77
|
};
|