From a2cfc562b954e1aaa2623f052ab11ff52932ce73 Mon Sep 17 00:00:00 2001 From: Anton Alexeyev Date: Thu, 26 Jun 2025 14:56:55 +0700 Subject: [PATCH] Add error messages to the middleware (#9387) Signed-off-by: Anton Alexeyev --- services/billing/pod-billing/src/middleware.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/services/billing/pod-billing/src/middleware.ts b/services/billing/pod-billing/src/middleware.ts index a2f52967e0..efa55c3266 100644 --- a/services/billing/pod-billing/src/middleware.ts +++ b/services/billing/pod-billing/src/middleware.ts @@ -25,7 +25,7 @@ interface RequestWithAuth extends Request { export const withToken = (req: RequestWithAuth, res: Response, next: NextFunction): void => { const token = extractToken(req.headers) if (token === undefined || token == null) { - res.status(401).end() + res.status(401).json({ message: 'Token error' }).end() return } req.token = token @@ -34,11 +34,11 @@ export const withToken = (req: RequestWithAuth, res: Response, next: NextFunctio export const withAdmin = (req: RequestWithAuth, res: Response, next: NextFunction): void => { if (req.token === undefined || req.token == null) { - res.status(401).end() + res.status(401).json({ message: 'Token error' }).end() return } if (req.token.account !== systemAccountUuid && req.token.extra?.admin !== 'true') { - res.status(401).end() + res.status(401).json({ message: 'Admins only' }).end() return } next() @@ -50,18 +50,22 @@ export const withOwner = (req: RequestWithAuth, res: Response, next: NextFunctio const withOwnerAsync = async (req: RequestWithAuth, res: Response, next: NextFunction): Promise => { if (req.token === undefined || req.token == null) { - res.status(401).end() + res.status(401).json({ message: 'Token error' }).end() return } if ((req.token.workspace as string) !== req.params.workspace) { - res.status(401).end() + res.status(401).json({ message: 'Workspace mismatch' }).end() return } if (req.token.account !== systemAccountUuid && req.token.extra?.admin !== 'true') { const accountClient = getAccountClient(req.headers.authorization?.split(' ')[1]) const loginInfo = await accountClient.getLoginInfoByToken() - if (!('role' in loginInfo) || loginInfo.role !== AccountRole.Owner) { - res.status(401).end() + if (!('role' in loginInfo)) { + res.status(401).json({ message: 'Missing workspace role' }).end() + return + } + if (loginInfo.role !== AccountRole.Owner) { + res.status(401).json({ message: 'Workspace owners only' }).end() return } }