mirror of
https://github.com/OneUptime/oneuptime
synced 2024-11-22 15:24:55 +00:00
raises proper exception when token is invalid or empty
This commit is contained in:
parent
dc4721f878
commit
f2c6321216
@ -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);
|
||||
}
|
||||
|
@ -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.]`
|
||||
);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user