Skip to content

Commit 2636269

Browse files
Removing the duplicate logic
1 parent 2b05069 commit 2636269

File tree

2 files changed

+3
-115
lines changed

2 files changed

+3
-115
lines changed

src/client/auth.test.ts

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
isHttpsUrl
1717
} from './auth.js';
1818
import { InvalidClientMetadataError, ServerError } from '../server/auth/errors.js';
19-
import { AuthorizationServerMetadata, OAuthClientMetadata } from '../shared/auth.js';
19+
import { AuthorizationServerMetadata } from '../shared/auth.js';
2020
import { expect, vi, type Mock } from 'vitest';
2121

2222
// Mock pkce-challenge
@@ -1553,17 +1553,14 @@ describe('OAuth Authorization', () => {
15531553
});
15541554

15551555
describe('auth function', () => {
1556-
let clientMetadataScope: string | undefined = undefined;
1557-
15581556
const mockProvider: OAuthClientProvider = {
15591557
get redirectUrl() {
15601558
return 'http://localhost:3000/callback';
15611559
},
15621560
get clientMetadata() {
15631561
return {
15641562
redirect_uris: ['http://localhost:3000/callback'],
1565-
client_name: 'Test Client',
1566-
scope: clientMetadataScope
1563+
client_name: 'Test Client'
15671564
};
15681565
},
15691566
clientInformation: vi.fn(),
@@ -2473,91 +2470,6 @@ describe('OAuth Authorization', () => {
24732470
// Verify custom fetch was called for AS metadata discovery
24742471
expect(customFetch.mock.calls[1][0].toString()).toBe('https://auth.example.com/.well-known/oauth-authorization-server');
24752472
});
2476-
2477-
it('prioritizes provided scope over resourceMetadata.scope', async () => {
2478-
const providedScope = 'provided_scope';
2479-
(mockProvider.clientMetadata as OAuthClientMetadata).scope = 'client_metadata_scope';
2480-
2481-
mockFetch.mockImplementation(url => {
2482-
if (url.toString().includes('/.well-known/oauth-protected-resource')) {
2483-
return Promise.resolve({
2484-
ok: true,
2485-
status: 200,
2486-
json: async () => ({
2487-
resource: 'https://api.example.com/mcp-server',
2488-
scopes_supported: ['read', 'write'],
2489-
authorization_servers: ['https://auth.example.com']
2490-
})
2491-
});
2492-
}
2493-
return Promise.resolve({ ok: false, status: 404 });
2494-
});
2495-
2496-
await auth(mockProvider, {
2497-
serverUrl: 'https://api.example.com/mcp-server',
2498-
scope: providedScope
2499-
});
2500-
2501-
const redirectCall = (mockProvider.redirectToAuthorization as Mock).mock.calls[0];
2502-
const authUrl: URL = redirectCall[0];
2503-
expect(authUrl.searchParams.get('scope')).toBe(providedScope);
2504-
});
2505-
2506-
it('uses resourceMetadata.scope when provided scope is missing', async () => {
2507-
const resourceScope = 'resource_metadata_scope';
2508-
(mockProvider.clientMetadata as OAuthClientMetadata).scope = 'client_metadata_scope';
2509-
2510-
mockFetch.mockImplementation(url => {
2511-
if (url.toString().includes('/.well-known/oauth-protected-resource')) {
2512-
return Promise.resolve({
2513-
ok: true,
2514-
status: 200,
2515-
json: async () => ({
2516-
resource: 'https://api.example.com/mcp-server',
2517-
scopes_supported: ['resource_metadata_scope'],
2518-
authorization_servers: ['https://auth.example.com']
2519-
})
2520-
});
2521-
}
2522-
return Promise.resolve({ ok: false, status: 404 });
2523-
});
2524-
2525-
await auth(mockProvider, {
2526-
serverUrl: 'https://api.example.com/mcp-server'
2527-
});
2528-
2529-
const redirectCall = (mockProvider.redirectToAuthorization as Mock).mock.calls[0];
2530-
const authUrl: URL = redirectCall[0];
2531-
expect(authUrl.searchParams.get('scope')).toBe(resourceScope);
2532-
});
2533-
2534-
it('falls back to clientMetadata.scope when provided and resourceMetadata scopes are missing', async () => {
2535-
const expectedScope = 'client_metadata_scope';
2536-
clientMetadataScope = expectedScope;
2537-
2538-
mockFetch.mockImplementation(url => {
2539-
if (url.toString().includes('/.well-known/oauth-protected-resource')) {
2540-
return Promise.resolve({
2541-
ok: true,
2542-
status: 200,
2543-
json: async () => ({
2544-
resource: 'https://api.example.com/mcp-server',
2545-
resource_metadata_scope: [],
2546-
authorization_servers: ['https://auth.example.com']
2547-
})
2548-
});
2549-
}
2550-
return Promise.resolve({ ok: false, status: 404 });
2551-
});
2552-
2553-
await auth(mockProvider, {
2554-
serverUrl: 'https://api.example.com/mcp-server'
2555-
});
2556-
2557-
const redirectCall = (mockProvider.redirectToAuthorization as Mock).mock.calls[0];
2558-
const authUrl: URL = redirectCall[0];
2559-
expect(authUrl.searchParams.get('scope')).toBe(clientMetadataScope);
2560-
});
25612473
});
25622474

25632475
describe('exchangeAuthorization with multiple client authentication methods', () => {

src/client/auth.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ async function authInternal(
473473
clientInformation,
474474
state,
475475
redirectUrl: provider.redirectUrl,
476-
scope: selectScope(provider, resourceMetadata, scope),
476+
scope: scope || resourceMetadata?.scopes_supported?.join(' ') || provider.clientMetadata.scope,
477477
resource
478478
});
479479

@@ -483,30 +483,6 @@ async function authInternal(
483483
}
484484

485485
/**
486-
* Selects the appropriate OAuth scope to use.
487-
*
488-
* The priority order is:
489-
* 1. The provided `scope` argument (if available). The scope is usually provided by WWW-authenticate header.
490-
* 2. Protected Resource Metadata scope (if available)
491-
* 3. The `OAuthClientProvider.clientMetadata.scope` (if available)
492-
*/
493-
export function selectScope(
494-
provider: OAuthClientProvider,
495-
resourceMetadata?: OAuthProtectedResourceMetadata,
496-
scope?: string
497-
): string | undefined {
498-
if (scope) {
499-
return scope;
500-
}
501-
502-
const scopes = resourceMetadata?.scopes_supported;
503-
if (scopes && scopes.length > 0) {
504-
return scopes.join(' ');
505-
}
506-
507-
return provider.clientMetadata.scope;
508-
}
509-
510486
* SEP-991: URL-based Client IDs
511487
* Validate that the client_id is a valid URL with https scheme
512488
*/

0 commit comments

Comments
 (0)