Skip to content

Commit 1919a0e

Browse files
authored
Merge pull request #400 from Littly/fix/header
feat: support custom authorization headers, fix #395
2 parents 2c527ca + 021e6a4 commit 1919a0e

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

client/src/lib/hooks/useConnection.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,14 @@ export function useConnection({
304304
bearerToken || (await serverAuthProvider.tokens())?.access_token;
305305
if (token) {
306306
const authHeaderName = headerName || "Authorization";
307-
headers[authHeaderName] = `Bearer ${token}`;
307+
308+
// Add custom header name as a special request header to let the server know which header to pass through
309+
if (authHeaderName.toLowerCase() !== "authorization") {
310+
headers[authHeaderName] = token;
311+
headers["x-custom-auth-header"] = authHeaderName;
312+
} else {
313+
headers[authHeaderName] = `Bearer ${token}`;
314+
}
308315
}
309316

310317
// Create appropriate transport

server/src/index.ts

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,44 @@ const { values } = parseArgs({
4141
},
4242
});
4343

44+
// Function to get HTTP headers.
45+
// Supports only "sse" and "streamable-http" transport types.
46+
const getHttpHeaders = (
47+
req: express.Request,
48+
transportType: string,
49+
): HeadersInit => {
50+
const headers: HeadersInit = {
51+
Accept:
52+
transportType === "sse"
53+
? "text/event-stream"
54+
: "text/event-stream, application/json",
55+
};
56+
const defaultHeaders =
57+
transportType === "sse"
58+
? SSE_HEADERS_PASSTHROUGH
59+
: STREAMABLE_HTTP_HEADERS_PASSTHROUGH;
60+
61+
for (const key of defaultHeaders) {
62+
if (req.headers[key] === undefined) {
63+
continue;
64+
}
65+
66+
const value = req.headers[key];
67+
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
68+
}
69+
70+
// If the header "x-custom-auth-header" is present, use its value as the custom header name.
71+
if (req.headers["x-custom-auth-header"] !== undefined) {
72+
const customHeaderName = req.headers["x-custom-auth-header"] as string;
73+
const lowerCaseHeaderName = customHeaderName.toLowerCase();
74+
if (req.headers[lowerCaseHeaderName] !== undefined) {
75+
const value = req.headers[lowerCaseHeaderName];
76+
headers[customHeaderName] = value as string;
77+
}
78+
}
79+
return headers;
80+
};
81+
4482
const app = express();
4583
app.use(cors());
4684
app.use((req, res, next) => {
@@ -80,18 +118,8 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
80118
return transport;
81119
} else if (transportType === "sse") {
82120
const url = query.url as string;
83-
const headers: HeadersInit = {
84-
Accept: "text/event-stream",
85-
};
86121

87-
for (const key of SSE_HEADERS_PASSTHROUGH) {
88-
if (req.headers[key] === undefined) {
89-
continue;
90-
}
91-
92-
const value = req.headers[key];
93-
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
94-
}
122+
const headers = getHttpHeaders(req, transportType);
95123

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

@@ -108,18 +136,7 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
108136
console.log("Connected to SSE transport");
109137
return transport;
110138
} else if (transportType === "streamable-http") {
111-
const headers: HeadersInit = {
112-
Accept: "text/event-stream, application/json",
113-
};
114-
115-
for (const key of STREAMABLE_HTTP_HEADERS_PASSTHROUGH) {
116-
if (req.headers[key] === undefined) {
117-
continue;
118-
}
119-
120-
const value = req.headers[key];
121-
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
122-
}
139+
const headers = getHttpHeaders(req, transportType);
123140

124141
const transport = new StreamableHTTPClientTransport(
125142
new URL(query.url as string),

0 commit comments

Comments
 (0)