diff --git a/src/middlewares/auth.ts b/src/middlewares/auth.ts index 5aecde8..783e933 100644 --- a/src/middlewares/auth.ts +++ b/src/middlewares/auth.ts @@ -10,8 +10,14 @@ export async function expressAuthentication( ) { switch (securityName) { case "keycloak": - return keycloakAuth(request, scopes); + const authData = await keycloakAuth(request, scopes); + request.app.locals.logData.sessionId = authData.session_state; + return authData; default: - throw new HttpError(HttpStatus.NOT_IMPLEMENTED, "Unknown how to verify identity.", "unknowHowToVerify"); + throw new HttpError( + HttpStatus.NOT_IMPLEMENTED, + "Unknown how to verify identity.", + "unknowHowToVerify", + ); } } diff --git a/src/middlewares/log.ts b/src/middlewares/log.ts index 5e8eb01..d7ab8db 100644 --- a/src/middlewares/log.ts +++ b/src/middlewares/log.ts @@ -1,5 +1,6 @@ import { NextFunction, Request, Response } from "express"; import elasticsearch from "../services/elasticsearch"; +import { randomUUID } from "crypto"; if (!process.env.ELASTICSEARCH_INDEX) { throw new Error("Require ELASTICSEARCH_INDEX to store log."); @@ -50,7 +51,7 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) { host: req.hostname, sessionId: req.headers["x-session-id"], rtId: req.headers["x-rtid"], - tId: req.headers["x-tid"], + tId: randomUUID(), method: req.method, endpoint: req.url, responseCode: res.statusCode,