fix: enhance OAuth token handling in useConnection hook to prevent infinite auth loops
This commit is contained in:
parent
861812e023
commit
17ace55bbf
2 changed files with 45 additions and 2 deletions
|
|
@ -913,6 +913,35 @@ describe("useConnection", () => {
|
||||||
expect(headers).toHaveProperty("Authorization", "Bearer mock-token");
|
expect(headers).toHaveProperty("Authorization", "Bearer mock-token");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("replaces empty Bearer token placeholder with OAuth token", async () => {
|
||||||
|
// This test prevents regression of the bug where default "Bearer " header
|
||||||
|
// prevented OAuth token injection, causing infinite auth loops
|
||||||
|
const customHeaders: CustomHeaders = [
|
||||||
|
{
|
||||||
|
name: "Authorization",
|
||||||
|
value: "Bearer ", // Empty Bearer token placeholder
|
||||||
|
enabled: true,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const propsWithEmptyBearer = {
|
||||||
|
...defaultProps,
|
||||||
|
customHeaders,
|
||||||
|
};
|
||||||
|
|
||||||
|
const { result } = renderHook(() => useConnection(propsWithEmptyBearer));
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
await result.current.connect();
|
||||||
|
});
|
||||||
|
|
||||||
|
const headers = mockSSETransport.options?.requestInit?.headers;
|
||||||
|
// Should replace the empty "Bearer " with actual OAuth token
|
||||||
|
expect(headers).toHaveProperty("Authorization", "Bearer mock-token");
|
||||||
|
// Should not have the x-custom-auth-headers since Authorization is standard
|
||||||
|
expect(headers).not.toHaveProperty("x-custom-auth-headers");
|
||||||
|
});
|
||||||
|
|
||||||
test("prioritizes custom headers over legacy auth", async () => {
|
test("prioritizes custom headers over legacy auth", async () => {
|
||||||
const customHeaders: CustomHeaders = [
|
const customHeaders: CustomHeaders = [
|
||||||
{ name: "Authorization", value: "Bearer custom-token", enabled: true },
|
{ name: "Authorization", value: "Bearer custom-token", enabled: true },
|
||||||
|
|
|
||||||
|
|
@ -400,11 +400,25 @@ export function useConnection({
|
||||||
// Use custom headers (migration is handled in App.tsx)
|
// Use custom headers (migration is handled in App.tsx)
|
||||||
let finalHeaders: CustomHeaders = customHeaders || [];
|
let finalHeaders: CustomHeaders = customHeaders || [];
|
||||||
|
|
||||||
// Add OAuth token if available and no custom headers are set
|
// Check if we need to inject OAuth token
|
||||||
if (finalHeaders.length === 0) {
|
// This handles both empty headers and default "Bearer " placeholder headers
|
||||||
|
const isEmptyAuthHeader = (header: CustomHeaders[number]) =>
|
||||||
|
header.name.trim().toLowerCase() === "authorization" &&
|
||||||
|
header.value.trim() === "Bearer";
|
||||||
|
|
||||||
|
const needsOAuthToken =
|
||||||
|
finalHeaders.length === 0 ||
|
||||||
|
finalHeaders.some(
|
||||||
|
(header) => header.enabled && isEmptyAuthHeader(header),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (needsOAuthToken) {
|
||||||
const oauthToken = (await serverAuthProvider.tokens())?.access_token;
|
const oauthToken = (await serverAuthProvider.tokens())?.access_token;
|
||||||
if (oauthToken) {
|
if (oauthToken) {
|
||||||
|
// Add the OAuth token
|
||||||
finalHeaders = [
|
finalHeaders = [
|
||||||
|
// Remove any existing Authorization headers with empty tokens
|
||||||
|
...finalHeaders.filter((header) => !isEmptyAuthHeader(header)),
|
||||||
{
|
{
|
||||||
name: "Authorization",
|
name: "Authorization",
|
||||||
value: `Bearer ${oauthToken}`,
|
value: `Bearer ${oauthToken}`,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue