feat: add default Authorization Bearer header and clean up legacy auth

- Provide "Authorization: Bearer " as default starting header
- Remove legacy auth props from components
- Centralize migration logic and fix localStorage cleanup
- Update tests for new auth architecture
This commit is contained in:
jeetul.samaiya 2025-09-15 20:19:56 +05:30
commit e08ce8cee9
3 changed files with 45 additions and 62 deletions

View file

@ -649,9 +649,17 @@ describe("useConnection", () => {
});
test("preserves server Authorization header when proxy auth is configured", async () => {
const customHeaders: CustomHeaders = [
{
name: "Authorization",
value: "Bearer server-auth-token",
enabled: true,
},
];
const propsWithBothAuth = {
...defaultProps,
bearerToken: "server-auth-token",
customHeaders,
config: {
...DEFAULT_INSPECTOR_CONFIG,
MCP_PROXY_AUTH_TOKEN: {
@ -811,14 +819,18 @@ describe("useConnection", () => {
expect(headers).not.toHaveProperty("X-Disabled");
});
test("migrates from legacy bearerToken and headerName", async () => {
const propsWithLegacyAuth = {
test("handles migrated legacy auth via custom headers", async () => {
// Simulate what App.tsx would do - migrate legacy auth to custom headers
const customHeaders: CustomHeaders = [
{ name: "X-Custom-Auth", value: "legacy-token", enabled: true },
];
const propsWithMigratedAuth = {
...defaultProps,
bearerToken: "legacy-token",
headerName: "X-Custom-Auth",
customHeaders,
};
const { result } = renderHook(() => useConnection(propsWithLegacyAuth));
const { result } = renderHook(() => useConnection(propsWithMigratedAuth));
await act(async () => {
await result.current.connect();

View file

@ -57,7 +57,7 @@ import {
import { getMCPServerRequestTimeout } from "@/utils/configUtils";
import { InspectorConfig } from "../configurationTypes";
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
import { CustomHeaders, migrateFromLegacyAuth } from "../types/customHeaders";
import { CustomHeaders } from "../types/customHeaders";
interface UseConnectionOptions {
transportType: "stdio" | "sse" | "streamable-http";
@ -65,10 +65,7 @@ interface UseConnectionOptions {
args: string;
sseUrl: string;
env: Record<string, string>;
// Legacy auth support (for backward compatibility)
bearerToken?: string;
headerName?: string;
// New custom headers support
// Custom headers support
customHeaders?: CustomHeaders;
oauthClientId?: string;
oauthScope?: string;
@ -90,8 +87,6 @@ export function useConnection({
args,
sseUrl,
env,
bearerToken,
headerName,
customHeaders,
oauthClientId,
oauthScope,
@ -384,16 +379,8 @@ export function useConnection({
// Create an auth provider with the current server URL
const serverAuthProvider = new InspectorOAuthClientProvider(sseUrl);
// Handle custom headers (new approach) or legacy auth (backward compatibility)
let finalHeaders: CustomHeaders = [];
if (customHeaders && customHeaders.length > 0) {
// Use new custom headers approach
finalHeaders = customHeaders;
} else if (bearerToken || headerName) {
// Migrate from legacy auth approach
finalHeaders = migrateFromLegacyAuth(bearerToken, headerName);
}
// Use custom headers (migration is handled in App.tsx)
let finalHeaders: CustomHeaders = customHeaders || [];
// Add OAuth token if available and no custom headers are set
if (finalHeaders.length === 0) {

View file

@ -58,9 +58,7 @@ export const migrateFromLegacyAuth = (
bearerToken?: string,
headerName?: string,
): CustomHeaders => {
if (!bearerToken) {
return [];
}
return [createHeaderFromBearerToken(bearerToken, headerName)];
return bearerToken
? [createHeaderFromBearerToken(bearerToken, headerName)]
: [];
};