-
Notifications
You must be signed in to change notification settings - Fork 9
[auth] Scenarios for scope selection #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e37c97f
wip scope change
pcarleton aa16772
rm useless bool
pcarleton ef31f40
fix-me: package changes for linking
pcarleton 53ae690
negative test
pcarleton 70fcc3a
negative test for multiple scopes
pcarleton 194fbae
fix displays of warnings
pcarleton 7f94fed
wip step-up
pcarleton 8b378be
negative test for step up auth
pcarleton 8d24f02
move warning into main request flow
pcarleton ab7ecd5
clean up more checks
pcarleton 258d859
cleanup step-up scenario
pcarleton 778c933
cleanup and simplify
pcarleton e7185ff
cleanup middleware
pcarleton 7bab0bb
inline negative tests
pcarleton ec554f5
inline the other test
pcarleton 8961318
refactor logging and inlining for standalone execution too
pcarleton ee9015e
skip pending tests
pcarleton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import { Client } from '@modelcontextprotocol/sdk/client/index.js'; | ||
| import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; | ||
| import { | ||
| auth, | ||
| UnauthorizedError | ||
| } from '@modelcontextprotocol/sdk/client/auth.js'; | ||
| import type { FetchLike } from '@modelcontextprotocol/sdk/shared/transport.js'; | ||
| import { withOAuthRetry } from './helpers/withOAuthRetry.js'; | ||
| import { ConformanceOAuthProvider } from './helpers/ConformanceOAuthProvider.js'; | ||
| import { runAsCli } from './helpers/cliRunner.js'; | ||
| import { logger } from './helpers/logger.js'; | ||
|
|
||
| /** | ||
| * Broken client that always uses root-based PRM discovery. | ||
| * BUG: Ignores the resource_metadata URL from WWW-Authenticate header. | ||
| */ | ||
| export async function runClient(serverUrl: string): Promise<void> { | ||
| const handle401Broken = async ( | ||
| response: Response, | ||
| provider: ConformanceOAuthProvider, | ||
| next: FetchLike, | ||
| serverUrl: string | URL | ||
| ): Promise<void> => { | ||
| // BUG: Use root-based PRM discovery exclusively | ||
| const resourceMetadataUrl = new URL( | ||
| '/.well-known/oauth-protected-resource', | ||
| typeof serverUrl === 'string' ? serverUrl : serverUrl.origin | ||
| ); | ||
|
|
||
| let result = await auth(provider, { | ||
| serverUrl, | ||
| resourceMetadataUrl, | ||
| fetchFn: next | ||
| }); | ||
|
|
||
| if (result === 'REDIRECT') { | ||
| const authorizationCode = await provider.getAuthCode(); | ||
| result = await auth(provider, { | ||
| serverUrl, | ||
| resourceMetadataUrl, | ||
| authorizationCode, | ||
| fetchFn: next | ||
| }); | ||
| if (result !== 'AUTHORIZED') { | ||
| throw new UnauthorizedError( | ||
| `Authentication failed with result: ${result}` | ||
| ); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const client = new Client( | ||
| { name: 'test-auth-client-broken', version: '1.0.0' }, | ||
| { capabilities: {} } | ||
| ); | ||
|
|
||
| const oauthFetch = withOAuthRetry( | ||
| 'test-auth-client-broken', | ||
| new URL(serverUrl), | ||
| handle401Broken | ||
| )(fetch); | ||
|
|
||
| const transport = new StreamableHTTPClientTransport(new URL(serverUrl), { | ||
| fetch: oauthFetch | ||
| }); | ||
|
|
||
| await client.connect(transport); | ||
| logger.debug('✅ Successfully connected to MCP server'); | ||
|
|
||
| await client.listTools(); | ||
| logger.debug('✅ Successfully listed tools'); | ||
|
|
||
| await transport.close(); | ||
| logger.debug('✅ Connection closed successfully'); | ||
| } | ||
|
|
||
| runAsCli(runClient, import.meta.url, 'auth-test-bad-prm <server-url>'); |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import { Client } from '@modelcontextprotocol/sdk/client/index.js'; | ||
| import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; | ||
| import { | ||
| auth, | ||
| extractWWWAuthenticateParams, | ||
| UnauthorizedError | ||
| } from '@modelcontextprotocol/sdk/client/auth.js'; | ||
| import type { FetchLike } from '@modelcontextprotocol/sdk/shared/transport.js'; | ||
| import { withOAuthRetry } from './helpers/withOAuthRetry.js'; | ||
| import { ConformanceOAuthProvider } from './helpers/ConformanceOAuthProvider.js'; | ||
| import { runAsCli } from './helpers/cliRunner.js'; | ||
| import { logger } from './helpers/logger.js'; | ||
|
|
||
| /** | ||
| * Broken client that only responds to 401, not 403. | ||
| * BUG: Ignores 403 responses which are used for step-up auth. | ||
| */ | ||
| export async function runClient(serverUrl: string): Promise<void> { | ||
| const handle401Broken = async ( | ||
| response: Response, | ||
| provider: ConformanceOAuthProvider, | ||
| next: FetchLike, | ||
| serverUrl: string | URL | ||
| ): Promise<void> => { | ||
| // BUG: Only respond to 401, not 403 | ||
| if (response.status !== 401) { | ||
| return; | ||
| } | ||
|
|
||
| const { resourceMetadataUrl, scope } = | ||
| extractWWWAuthenticateParams(response); | ||
| let result = await auth(provider, { | ||
| serverUrl, | ||
| resourceMetadataUrl, | ||
| scope, | ||
| fetchFn: next | ||
| }); | ||
|
|
||
| if (result === 'REDIRECT') { | ||
| const authorizationCode = await provider.getAuthCode(); | ||
| result = await auth(provider, { | ||
| serverUrl, | ||
| resourceMetadataUrl, | ||
| scope, | ||
| authorizationCode, | ||
| fetchFn: next | ||
| }); | ||
| if (result !== 'AUTHORIZED') { | ||
| throw new UnauthorizedError( | ||
| `Authentication failed with result: ${result}` | ||
| ); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const client = new Client( | ||
| { name: 'test-auth-client-broken', version: '1.0.0' }, | ||
| { capabilities: {} } | ||
| ); | ||
|
|
||
| const oauthFetch = withOAuthRetry( | ||
| 'test-auth-client-broken', | ||
| new URL(serverUrl), | ||
| handle401Broken | ||
| )(fetch); | ||
|
|
||
| const transport = new StreamableHTTPClientTransport(new URL(serverUrl), { | ||
| fetch: oauthFetch | ||
| }); | ||
|
|
||
| await client.connect(transport); | ||
| logger.debug('✅ Successfully connected to MCP server'); | ||
|
|
||
| await client.listTools(); | ||
| logger.debug('✅ Successfully listed tools'); | ||
|
|
||
| // Call tool to trigger step-up auth | ||
| await client.callTool({ name: 'test-tool', arguments: {} }); | ||
| logger.debug('✅ Successfully called tool'); | ||
|
|
||
| await transport.close(); | ||
| logger.debug('✅ Connection closed successfully'); | ||
| } | ||
|
|
||
| runAsCli(runClient, import.meta.url, 'auth-test-ignore-403 <server-url>'); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import { Client } from '@modelcontextprotocol/sdk/client/index.js'; | ||
| import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; | ||
| import { | ||
| auth, | ||
| extractWWWAuthenticateParams, | ||
| UnauthorizedError | ||
| } from '@modelcontextprotocol/sdk/client/auth.js'; | ||
| import type { FetchLike } from '@modelcontextprotocol/sdk/shared/transport.js'; | ||
| import { withOAuthRetry } from './helpers/withOAuthRetry.js'; | ||
| import { ConformanceOAuthProvider } from './helpers/ConformanceOAuthProvider.js'; | ||
| import { runAsCli } from './helpers/cliRunner.js'; | ||
| import { logger } from './helpers/logger.js'; | ||
|
|
||
| /** | ||
| * Broken client that ignores the scope from WWW-Authenticate header. | ||
| * BUG: Doesn't pass the scope parameter from the 401 response. | ||
| */ | ||
| export async function runClient(serverUrl: string): Promise<void> { | ||
| const handle401Broken = async ( | ||
| response: Response, | ||
| provider: ConformanceOAuthProvider, | ||
| next: FetchLike, | ||
| serverUrl: string | URL | ||
| ): Promise<void> => { | ||
| // BUG: Don't read the scope from the header | ||
| const { resourceMetadataUrl } = extractWWWAuthenticateParams(response); | ||
| let result = await auth(provider, { | ||
| serverUrl, | ||
| resourceMetadataUrl, | ||
| // scope deliberately omitted | ||
| fetchFn: next | ||
| }); | ||
|
|
||
| if (result === 'REDIRECT') { | ||
| const authorizationCode = await provider.getAuthCode(); | ||
| result = await auth(provider, { | ||
| serverUrl, | ||
| resourceMetadataUrl, | ||
| authorizationCode, | ||
| fetchFn: next | ||
| }); | ||
| if (result !== 'AUTHORIZED') { | ||
| throw new UnauthorizedError( | ||
| `Authentication failed with result: ${result}` | ||
| ); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const client = new Client( | ||
| { name: 'test-auth-client-broken', version: '1.0.0' }, | ||
| { capabilities: {} } | ||
| ); | ||
|
|
||
| const oauthFetch = withOAuthRetry( | ||
| 'test-auth-client-broken', | ||
| new URL(serverUrl), | ||
| handle401Broken | ||
| )(fetch); | ||
|
|
||
| const transport = new StreamableHTTPClientTransport(new URL(serverUrl), { | ||
| fetch: oauthFetch | ||
| }); | ||
|
|
||
| await client.connect(transport); | ||
| logger.debug('✅ Successfully connected to MCP server'); | ||
|
|
||
| await client.listTools(); | ||
| logger.debug('✅ Successfully listed tools'); | ||
|
|
||
| await transport.close(); | ||
| logger.debug('✅ Connection closed successfully'); | ||
| } | ||
|
|
||
| runAsCli(runClient, import.meta.url, 'auth-test-ignore-scope <server-url>'); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more of a style nit: all these "broken" examples look very similar (identical?) except for the specific way in which they're "broken".
Could consider having a single implementation and having the specific way it's actually broken be a param to
createBrokenHandler, a higher level function that creates thehandle401broken.Could make this less verbose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potentially the well-behaved client could also share the same implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we prefer explicitness over conciseness though for conformance tests, so definitely just a thought 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed on the repetitiveness, i've been resisting the urge to de-dupe too aggressively in order to let abstractions fall out from a few more iterations on tests before committing to one.