feat: enhance host API authentication handling and add regression tests
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
||||
} from './route-utils';
|
||||
|
||||
const DEFAULT_HOST_API_PORT = 13210;
|
||||
const HOST_API_UNAUTHORIZED_CODE = 'HOST_API_UNAUTHORIZED';
|
||||
|
||||
type StartHostApiServerOptions = {
|
||||
ctx: HostApiContext;
|
||||
@@ -50,21 +51,19 @@ export function startHostApiServer(options: StartHostApiServerOptions): Server {
|
||||
return;
|
||||
}
|
||||
|
||||
const bearerHeader = req.headers.authorization || '';
|
||||
const bearerToken = bearerHeader.startsWith('Bearer ')
|
||||
? bearerHeader.slice('Bearer '.length)
|
||||
: '';
|
||||
const token = (
|
||||
req.headers['x-host-api-token']
|
||||
|| requestUrl.searchParams.get('token')
|
||||
|| bearerToken
|
||||
);
|
||||
const headerToken = req.headers['x-host-api-token'];
|
||||
const token = typeof headerToken === 'string'
|
||||
? headerToken
|
||||
: Array.isArray(headerToken)
|
||||
? headerToken[0]
|
||||
: (requestUrl.searchParams.get('token') || '');
|
||||
|
||||
if (token !== hostApiToken) {
|
||||
sendJsonResponse(res, 401, {
|
||||
success: false,
|
||||
ok: false,
|
||||
error: 'Unauthorized',
|
||||
code: HOST_API_UNAUTHORIZED_CODE,
|
||||
error: 'Host API authentication failed',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user