response-standardizer 1.0.7 → 1.0.9
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/README.md +1 -0
- package/dist/index.js +2 -1
- package/dist/types.d.ts +4 -0
- package/package.json +1 -1
- package/src/index.ts +4 -3
- package/src/types.ts +5 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npm publish --access public
|
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ export const initKeycloak = async (config) => {
|
|
|
7
7
|
const KEYCLOAK_SERVICE = config.service ?? "localhost";
|
|
8
8
|
const KEYCLOAK_REALM = config.realm ?? "master";
|
|
9
9
|
const realmUrl = `http://${KEYCLOAK_SERVICE}/realms/${KEYCLOAK_REALM}`;
|
|
10
|
-
|
|
10
|
+
info(`Keycloak PublicKey Url: ${realmUrl}`);
|
|
11
11
|
const resp = await axios.get(realmUrl);
|
|
12
12
|
const key = resp.data.public_key;
|
|
13
13
|
KEYCLOAK_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----\n${key.match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
|
|
@@ -23,6 +23,7 @@ export const protect = (allowedRoles) => {
|
|
|
23
23
|
try {
|
|
24
24
|
const decoded = jwt.verify(token, KEYCLOAK_PUBLIC_KEY, { algorithms: ["RS256"] });
|
|
25
25
|
req.user = decoded;
|
|
26
|
+
req.token = token;
|
|
26
27
|
if (allowedRoles)
|
|
27
28
|
return role(req, res, next, allowedRoles);
|
|
28
29
|
next();
|
package/dist/types.d.ts
CHANGED
|
@@ -32,3 +32,7 @@ export interface RestResponseFunctions {
|
|
|
32
32
|
unauthorized: (req: Request, res: Response, message?: string) => void;
|
|
33
33
|
accessDenied: (req: Request, res: Response, message?: string) => void;
|
|
34
34
|
}
|
|
35
|
+
export interface AuthRequest extends Request {
|
|
36
|
+
user?: any;
|
|
37
|
+
token?: string;
|
|
38
|
+
}
|
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 } from "./types.js";
|
|
2
|
+
import { ErrorFields, PaginationMeta, StandardResponse, RestResponseFunctions, RestMiddlewareFunctions, AuthRequest } from "./types.js";
|
|
3
3
|
import { generateRequestId, getTimestamp } from "./utils.js";
|
|
4
4
|
import axios from "axios";
|
|
5
5
|
import jwt from "jsonwebtoken";
|
|
@@ -21,7 +21,7 @@ export const initKeycloak = async (config: { service?: string; realm?: string })
|
|
|
21
21
|
const KEYCLOAK_SERVICE = config.service ?? "localhost";
|
|
22
22
|
const KEYCLOAK_REALM = config.realm ?? "master";
|
|
23
23
|
const realmUrl = `http://${KEYCLOAK_SERVICE}/realms/${KEYCLOAK_REALM}`;
|
|
24
|
-
|
|
24
|
+
info(`Keycloak PublicKey Url: ${realmUrl}`);
|
|
25
25
|
const resp = await axios.get(realmUrl);
|
|
26
26
|
const key = resp.data.public_key;
|
|
27
27
|
KEYCLOAK_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----\n${key.match(/.{1,64}/g)?.join("\n")}\n-----END PUBLIC KEY-----`;
|
|
@@ -39,7 +39,8 @@ export const protect = (allowedRoles?: string[]) => {
|
|
|
39
39
|
|
|
40
40
|
try {
|
|
41
41
|
const decoded = jwt.verify(token, KEYCLOAK_PUBLIC_KEY, { algorithms: ["RS256"] });
|
|
42
|
-
(req as
|
|
42
|
+
(req as AuthRequest).user = decoded;
|
|
43
|
+
(req as AuthRequest).token = token;
|
|
43
44
|
if(allowedRoles)
|
|
44
45
|
return role(req, res, next, allowedRoles);
|
|
45
46
|
|
package/src/types.ts
CHANGED
|
@@ -53,3 +53,8 @@ export interface RestResponseFunctions {
|
|
|
53
53
|
unauthorized: (req: Request, res: Response, message?: string) => void;
|
|
54
54
|
accessDenied: (req: Request, res: Response, message?: string) => void;
|
|
55
55
|
}
|
|
56
|
+
|
|
57
|
+
export interface AuthRequest extends Request {
|
|
58
|
+
user?: any;
|
|
59
|
+
token?: string
|
|
60
|
+
}
|