feat: support custom authorization headers, fix #395

This commit is contained in:
xiaoli 2025-05-13 23:38:19 +08:00
commit 618f24a9ac
2 changed files with 24 additions and 1 deletions

View file

@ -297,7 +297,12 @@ export function useConnection({
bearerToken || (await serverAuthProvider.tokens())?.access_token;
if (token) {
const authHeaderName = headerName || "Authorization";
headers[authHeaderName] = `Bearer ${token}`;
headers[authHeaderName] = token;
// Add custom header name as a special request header to let the server know which header to pass through
if (headerName && headerName.toLowerCase() !== "authorization") {
headers["x-custom-auth-header"] = headerName;
}
}
// Create appropriate transport

View file

@ -91,6 +91,15 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
const value = req.headers[key];
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
}
// If the header "x-custom-auth-header" is present, use its value as the custom header name.
if (req.headers["x-custom-auth-header"] !== undefined) {
const customHeaderName = req.headers["x-custom-auth-header"] as string;
if (req.headers[customHeaderName.toLowerCase()] !== undefined) {
const value = req.headers[customHeaderName.toLowerCase()];
headers[customHeaderName] = Array.isArray(value) ? value[value.length - 1] : value as string;
}
}
console.log(`SSE transport: url=${url}, headers=${Object.keys(headers)}`);
@ -119,6 +128,15 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
const value = req.headers[key];
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
}
// If the header "x-custom-auth-header" is present, use its value as the custom header name.
if (req.headers["x-custom-auth-header"] !== undefined) {
const customHeaderName = req.headers["x-custom-auth-header"] as string;
if (req.headers[customHeaderName.toLowerCase()] !== undefined) {
const value = req.headers[customHeaderName.toLowerCase()];
headers[customHeaderName] = Array.isArray(value) ? value[value.length - 1] : value as string;
}
}
const transport = new StreamableHTTPClientTransport(
new URL(query.url as string),