Skip to content

Commit

Permalink
OAuth callback handler (not yet attached)
Browse files Browse the repository at this point in the history
  • Loading branch information
jspahrsummers committed Jan 24, 2025
1 parent 1c4ad60 commit 16cb596
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions client/src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 16cb596

Please sign in to comment.