Skip to content

Commit c60cb25

Browse files
committed
feat: support custom authorization headers, fix #395
1 parent 5e8e78c commit c60cb25

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

client/src/lib/hooks/useConnection.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,12 @@ export function useConnection({
297297
bearerToken || (await serverAuthProvider.tokens())?.access_token;
298298
if (token) {
299299
const authHeaderName = headerName || "Authorization";
300-
headers[authHeaderName] = `Bearer ${token}`;
300+
headers[authHeaderName] = token;
301+
302+
// Add custom header name as a special request header to let the server know which header to pass through
303+
if (headerName && headerName.toLowerCase() !== "authorization") {
304+
headers["x-custom-auth-header"] = headerName;
305+
}
301306
}
302307

303308
// Create appropriate transport

server/src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
9191
const value = req.headers[key];
9292
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
9393
}
94+
95+
// If the header "x-custom-auth-header" is present, use its value as the custom header name.
96+
if (req.headers["x-custom-auth-header"] !== undefined) {
97+
const customHeaderName = req.headers["x-custom-auth-header"] as string;
98+
if (req.headers[customHeaderName.toLowerCase()] !== undefined) {
99+
const value = req.headers[customHeaderName.toLowerCase()];
100+
headers[customHeaderName] = Array.isArray(value) ? value[value.length - 1] : value as string;
101+
}
102+
}
94103

95104
console.log(`SSE transport: url=${url}, headers=${Object.keys(headers)}`);
96105

@@ -119,6 +128,15 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
119128
const value = req.headers[key];
120129
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
121130
}
131+
132+
// If the header "x-custom-auth-header" is present, use its value as the custom header name.
133+
if (req.headers["x-custom-auth-header"] !== undefined) {
134+
const customHeaderName = req.headers["x-custom-auth-header"] as string;
135+
if (req.headers[customHeaderName.toLowerCase()] !== undefined) {
136+
const value = req.headers[customHeaderName.toLowerCase()];
137+
headers[customHeaderName] = Array.isArray(value) ? value[value.length - 1] : value as string;
138+
}
139+
}
122140

123141
const transport = new StreamableHTTPClientTransport(
124142
new URL(query.url as string),

0 commit comments

Comments
 (0)