OAuth callback handler (not yet attached)

This commit is contained in:
Justin Spahr-Summers
2025-01-24 11:37:35 +00:00
parent 1c4ad60354
commit 16cb59670c

View File

@@ -50,3 +50,35 @@ export async function startOAuthFlow(serverUrl: string): Promise<string> {
return authUrl.toString();
}
export async function handleOAuthCallback(serverUrl: string, code: string): Promise<string> {
// Get stored code verifier
const codeVerifier = sessionStorage.getItem('mcp_code_verifier');
if (!codeVerifier) {
throw new Error('No code verifier found');
}
// Discover OAuth endpoints
const metadata = await discoverOAuthMetadata(serverUrl);
// Exchange code for tokens
const response = await fetch(metadata.token_endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
grant_type: 'authorization_code',
code,
code_verifier: codeVerifier,
redirect_uri: window.location.origin + '/oauth/callback'
})
});
if (!response.ok) {
throw new Error('Token exchange failed');
}
const data = await response.json();
return data.access_token;
}