raises proper exception when token is invalid or empty

This commit is contained in:
Hammad 2023-10-24 16:22:45 +05:00
parent dc4721f878
commit f2c6321216
2 changed files with 6 additions and 6 deletions

View File

@ -17,12 +17,12 @@ export default class BearerTokenAuthorization {
try {
req = req as OneUptimeRequest;
if (req.headers['authorization'] || req.headers['Authorization']) {
if (req.headers?.['authorization'] || req.headers?.['Authorization']) {
let token: string | undefined | Array<string> =
req.headers['authorization'] ||
req.headers['Authorization'];
token = token?.toString().replace('Bearer ', '');
if (token) {
token = token.toString().replace('Bearer ', '');
const tokenData: JSONObject =
JSONWebToken.decodeJsonPayload(token);
@ -33,7 +33,7 @@ export default class BearerTokenAuthorization {
}
}
throw new NotAuthorizedException('Invalid bearer token.');
throw new NotAuthorizedException('Invalid bearer token, or bearer token not provided.');
} catch (err) {
next(err);
}

View File

@ -62,7 +62,7 @@ describe('BearerTokenAuthorization', () => {
next
);
expect(next.mock.calls[0][0]).toMatchInlineSnapshot(
`[Error: Invalid bearer token.]`
`[Error: Invalid bearer token, or bearer token not provided.]`
);
});
it('calls next with exception if token is invalid', () => {
@ -80,7 +80,7 @@ describe('BearerTokenAuthorization', () => {
next
);
expect(next.mock.calls[0][0]).toMatchInlineSnapshot(
`[JsonWebTokenError: jwt must be provided]`
`[Error: Invalid bearer token, or bearer token not provided.]`
);
});
it('calls next with exception if token header is not present', () => {
@ -95,7 +95,7 @@ describe('BearerTokenAuthorization', () => {
next
);
expect(next.mock.calls[0][0]).toMatchInlineSnapshot(
`[TypeError: Cannot read properties of undefined (reading 'authorization')]`
`[Error: Invalid bearer token, or bearer token not provided.]`
);
});
});