@lucaapp/service-utils 1.40.0 → 1.40.2
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/lib/api/endpoint.js
CHANGED
|
@@ -96,7 +96,7 @@ const handleMiddleware = async (middleware, context, request, response, debug) =
|
|
|
96
96
|
const handlerRequestQuery = ((await middleware.options.schemas?.query?.parseAsync(request.query)) ||
|
|
97
97
|
undefined);
|
|
98
98
|
const handlerRequestHeaders = ((await middleware.options.schemas?.headers?.parseAsync(request.headers)) || undefined);
|
|
99
|
-
const { ip, baseUrl, route, method } = request;
|
|
99
|
+
const { ip, baseUrl, originalUrl, route, method } = request;
|
|
100
100
|
const handlerRequest = {
|
|
101
101
|
body: handlerRequestBody,
|
|
102
102
|
params: handlerRequestParams,
|
|
@@ -104,6 +104,7 @@ const handleMiddleware = async (middleware, context, request, response, debug) =
|
|
|
104
104
|
headers: handlerRequestHeaders,
|
|
105
105
|
ip,
|
|
106
106
|
baseUrl,
|
|
107
|
+
originalUrl,
|
|
107
108
|
method,
|
|
108
109
|
path: route.path,
|
|
109
110
|
};
|
|
@@ -12,9 +12,10 @@ export type MiddlewareHandler<TResponseSchema, TRequestBodySchema, TRequestParam
|
|
|
12
12
|
headers: ZodSchemaOuput<TRequestHeadersSchema>;
|
|
13
13
|
ip: string;
|
|
14
14
|
baseUrl: string;
|
|
15
|
+
originalUrl: string;
|
|
15
16
|
path: string;
|
|
16
17
|
method: string;
|
|
17
|
-
}, send: (response: ExtractZodOutput<ArrayToUnion<TResponseSchema>>) => void, next: (context
|
|
18
|
+
}, send: (response: ExtractZodOutput<ArrayToUnion<TResponseSchema>>) => void, next: (context?: ZodSchemaOuput<TContextSchema>) => void) => Promise<void>;
|
|
18
19
|
export type MiddlewareOptions<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TContextSchema> = {
|
|
19
20
|
schemas?: {
|
|
20
21
|
body?: TRequestBodySchema;
|
|
@@ -2,6 +2,7 @@ import * as jose from 'jose';
|
|
|
2
2
|
import { HttpMethod } from 'types/http';
|
|
3
3
|
import { Request, RequestHandler } from 'express';
|
|
4
4
|
import { Service } from './service';
|
|
5
|
+
import { z } from 'zod';
|
|
5
6
|
type JWKS = {
|
|
6
7
|
keys: jose.JWK[];
|
|
7
8
|
};
|
|
@@ -25,6 +26,45 @@ declare class ServiceIdentity {
|
|
|
25
26
|
}>;
|
|
26
27
|
getIdentityJWKS: () => Promise<JWKS>;
|
|
27
28
|
requireServiceIdentity: (service: string) => RequestHandler;
|
|
29
|
+
requireServiceIdentityV3: (service: string) => import("../api/types/middleware").Middleware<({
|
|
30
|
+
status: 401;
|
|
31
|
+
description: string;
|
|
32
|
+
schema: z.ZodObject<{
|
|
33
|
+
error: z.ZodLiteral<import("../api/types/http").HTTPStatus.UNAUTHORIZED>;
|
|
34
|
+
message: z.ZodString;
|
|
35
|
+
}, "strip", z.ZodTypeAny, {
|
|
36
|
+
message: string;
|
|
37
|
+
error: import("../api/types/http").HTTPStatus.UNAUTHORIZED;
|
|
38
|
+
}, {
|
|
39
|
+
message: string;
|
|
40
|
+
error: import("../api/types/http").HTTPStatus.UNAUTHORIZED;
|
|
41
|
+
}>;
|
|
42
|
+
} | {
|
|
43
|
+
status: 403;
|
|
44
|
+
description: string;
|
|
45
|
+
schema: z.ZodObject<{
|
|
46
|
+
error: z.ZodLiteral<import("../api/types/http").HTTPStatus.FORBIDDEN>;
|
|
47
|
+
message: z.ZodString;
|
|
48
|
+
}, "strip", z.ZodTypeAny, {
|
|
49
|
+
message: string;
|
|
50
|
+
error: import("../api/types/http").HTTPStatus.FORBIDDEN;
|
|
51
|
+
}, {
|
|
52
|
+
message: string;
|
|
53
|
+
error: import("../api/types/http").HTTPStatus.FORBIDDEN;
|
|
54
|
+
}>;
|
|
55
|
+
})[], undefined, undefined, undefined, z.ZodObject<{
|
|
56
|
+
'X-Identity': z.ZodString;
|
|
57
|
+
}, "strip", z.ZodTypeAny, {
|
|
58
|
+
"X-Identity": string;
|
|
59
|
+
}, {
|
|
60
|
+
"X-Identity": string;
|
|
61
|
+
}>, z.ZodObject<{
|
|
62
|
+
payload: z.ZodAny;
|
|
63
|
+
}, "strip", z.ZodTypeAny, {
|
|
64
|
+
payload?: any;
|
|
65
|
+
}, {
|
|
66
|
+
payload?: any;
|
|
67
|
+
}>>;
|
|
28
68
|
identityJWKSRoute: RequestHandler;
|
|
29
69
|
}
|
|
30
70
|
export { ServiceIdentity };
|
|
@@ -30,9 +30,11 @@ exports.ServiceIdentity = void 0;
|
|
|
30
30
|
const jose = __importStar(require("jose"));
|
|
31
31
|
const url_1 = require("url");
|
|
32
32
|
const boom_1 = require("@hapi/boom");
|
|
33
|
+
const api_1 = require("../api");
|
|
33
34
|
const moment_1 = __importDefault(require("moment"));
|
|
34
35
|
const requestTracer_1 = require("../requestTracer");
|
|
35
36
|
const axios_1 = __importDefault(require("axios"));
|
|
37
|
+
const zod_1 = require("zod");
|
|
36
38
|
const JWT_ALGORITHM = 'ES256';
|
|
37
39
|
const JWT_HEADER_NAME = 'X-Identity';
|
|
38
40
|
const JWT_ALLOWED_ALGORITHMS = [JWT_ALGORITHM];
|
|
@@ -123,6 +125,29 @@ class ServiceIdentity {
|
|
|
123
125
|
request.body = payload.data;
|
|
124
126
|
next();
|
|
125
127
|
};
|
|
128
|
+
this.requireServiceIdentityV3 = (service) => (0, api_1.createMiddleware)({
|
|
129
|
+
schemas: {
|
|
130
|
+
headers: zod_1.z.object({
|
|
131
|
+
'X-Identity': zod_1.z.string(),
|
|
132
|
+
}),
|
|
133
|
+
context: zod_1.z.object({ payload: zod_1.z.any() }),
|
|
134
|
+
},
|
|
135
|
+
responses: [(0, api_1.unauthorizedResponse)(), (0, api_1.forbiddenResponse)()],
|
|
136
|
+
}, async (request, respond, next) => {
|
|
137
|
+
const { headers, originalUrl, method } = request;
|
|
138
|
+
const jwt = headers[JWT_HEADER_NAME];
|
|
139
|
+
if (typeof jwt !== 'string') {
|
|
140
|
+
return respond((0, api_1.unauthorized)());
|
|
141
|
+
}
|
|
142
|
+
const { payload } = await jose.jwtVerify(jwt, this.getRemoteJWKS(service), this.getJwtVerifyOptions(service));
|
|
143
|
+
if (request.originalUrl !== payload.url) {
|
|
144
|
+
return respond((0, api_1.forbidden)(`${originalUrl} !== ${payload.url}`));
|
|
145
|
+
}
|
|
146
|
+
if (method !== payload.method) {
|
|
147
|
+
return respond((0, api_1.forbidden)(`${method} !== ${payload.method}`));
|
|
148
|
+
}
|
|
149
|
+
next({ payload: payload.data });
|
|
150
|
+
});
|
|
126
151
|
this.identityJWKSRoute = async (_, response) => {
|
|
127
152
|
response.send(await this.getIdentityJWKS());
|
|
128
153
|
};
|