|
| 1 | +import type { Scenario, ConformanceCheck, ScenarioUrls } from '../../../types'; |
| 2 | +import { createAuthServer } from './helpers/createAuthServer'; |
| 3 | +import { createServer } from './helpers/createServer'; |
| 4 | +import { ServerLifecycle } from './helpers/serverLifecycle'; |
| 5 | +import { SpecReferences } from './spec-references'; |
| 6 | +import { MockTokenVerifier } from './helpers/mockTokenVerifier'; |
| 7 | + |
| 8 | +const PRE_REGISTERED_CLIENT_ID = 'pre-registered-client'; |
| 9 | +const PRE_REGISTERED_CLIENT_SECRET = 'pre-registered-secret'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Scenario: Pre-registration (static client credentials) |
| 13 | + * |
| 14 | + * Tests OAuth flow where the server does NOT support Dynamic Client Registration. |
| 15 | + * Clients must use pre-registered credentials passed via context. |
| 16 | + * |
| 17 | + * This tests the pre-registration approach described in the MCP spec: |
| 18 | + * https://modelcontextprotocol.io/specification/draft/basic/authorization#preregistration |
| 19 | + */ |
| 20 | +export class PreRegistrationScenario implements Scenario { |
| 21 | + name = 'auth/pre-registration'; |
| 22 | + description = |
| 23 | + 'Tests OAuth flow with pre-registered client credentials. Server does not support DCR.'; |
| 24 | + |
| 25 | + private authServer = new ServerLifecycle(); |
| 26 | + private server = new ServerLifecycle(); |
| 27 | + private checks: ConformanceCheck[] = []; |
| 28 | + |
| 29 | + async start(): Promise<ScenarioUrls> { |
| 30 | + this.checks = []; |
| 31 | + const tokenVerifier = new MockTokenVerifier(this.checks, []); |
| 32 | + |
| 33 | + const authApp = createAuthServer(this.checks, this.authServer.getUrl, { |
| 34 | + tokenVerifier, |
| 35 | + disableDynamicRegistration: true, |
| 36 | + tokenEndpointAuthMethodsSupported: ['client_secret_basic'], |
| 37 | + onTokenRequest: ({ authorizationHeader, timestamp }) => { |
| 38 | + // Verify client used pre-registered credentials via Basic auth |
| 39 | + if (!authorizationHeader?.startsWith('Basic ')) { |
| 40 | + this.checks.push({ |
| 41 | + id: 'pre-registration-auth', |
| 42 | + name: 'Pre-registration authentication', |
| 43 | + description: |
| 44 | + 'Client did not use Basic authentication with pre-registered credentials', |
| 45 | + status: 'FAILURE', |
| 46 | + timestamp, |
| 47 | + specReferences: [SpecReferences.MCP_PREREGISTRATION] |
| 48 | + }); |
| 49 | + return { |
| 50 | + error: 'invalid_client', |
| 51 | + errorDescription: 'Missing or invalid Authorization header', |
| 52 | + statusCode: 401 |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + const base64Credentials = authorizationHeader.slice(6); |
| 57 | + const credentials = Buffer.from(base64Credentials, 'base64').toString( |
| 58 | + 'utf-8' |
| 59 | + ); |
| 60 | + const [clientId, clientSecret] = credentials.split(':'); |
| 61 | + |
| 62 | + if ( |
| 63 | + clientId !== PRE_REGISTERED_CLIENT_ID || |
| 64 | + clientSecret !== PRE_REGISTERED_CLIENT_SECRET |
| 65 | + ) { |
| 66 | + this.checks.push({ |
| 67 | + id: 'pre-registration-auth', |
| 68 | + name: 'Pre-registration authentication', |
| 69 | + description: `Client used incorrect pre-registered credentials. Expected client_id '${PRE_REGISTERED_CLIENT_ID}', got '${clientId}'`, |
| 70 | + status: 'FAILURE', |
| 71 | + timestamp, |
| 72 | + specReferences: [SpecReferences.MCP_PREREGISTRATION], |
| 73 | + details: { |
| 74 | + expectedClientId: PRE_REGISTERED_CLIENT_ID, |
| 75 | + actualClientId: clientId |
| 76 | + } |
| 77 | + }); |
| 78 | + return { |
| 79 | + error: 'invalid_client', |
| 80 | + errorDescription: 'Invalid pre-registered credentials', |
| 81 | + statusCode: 401 |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + // Success - client used correct pre-registered credentials |
| 86 | + this.checks.push({ |
| 87 | + id: 'pre-registration-auth', |
| 88 | + name: 'Pre-registration authentication', |
| 89 | + description: |
| 90 | + 'Client correctly used pre-registered credentials when server does not support DCR', |
| 91 | + status: 'SUCCESS', |
| 92 | + timestamp, |
| 93 | + specReferences: [SpecReferences.MCP_PREREGISTRATION], |
| 94 | + details: { clientId } |
| 95 | + }); |
| 96 | + |
| 97 | + return { |
| 98 | + token: `test-token-prereg-${Date.now()}`, |
| 99 | + scopes: [] |
| 100 | + }; |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + await this.authServer.start(authApp); |
| 105 | + |
| 106 | + const app = createServer( |
| 107 | + this.checks, |
| 108 | + this.server.getUrl, |
| 109 | + this.authServer.getUrl, |
| 110 | + { |
| 111 | + prmPath: '/.well-known/oauth-protected-resource/mcp', |
| 112 | + requiredScopes: [], |
| 113 | + tokenVerifier |
| 114 | + } |
| 115 | + ); |
| 116 | + |
| 117 | + await this.server.start(app); |
| 118 | + |
| 119 | + return { |
| 120 | + serverUrl: `${this.server.getUrl()}/mcp`, |
| 121 | + context: { |
| 122 | + client_id: PRE_REGISTERED_CLIENT_ID, |
| 123 | + client_secret: PRE_REGISTERED_CLIENT_SECRET |
| 124 | + } |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + async stop() { |
| 129 | + await this.authServer.stop(); |
| 130 | + await this.server.stop(); |
| 131 | + } |
| 132 | + |
| 133 | + getChecks(): ConformanceCheck[] { |
| 134 | + // Ensure we have the pre-registration check |
| 135 | + const hasPreRegCheck = this.checks.some( |
| 136 | + (c) => c.id === 'pre-registration-auth' |
| 137 | + ); |
| 138 | + if (!hasPreRegCheck) { |
| 139 | + this.checks.push({ |
| 140 | + id: 'pre-registration-auth', |
| 141 | + name: 'Pre-registration authentication', |
| 142 | + description: 'Client did not make a token request', |
| 143 | + status: 'FAILURE', |
| 144 | + timestamp: new Date().toISOString(), |
| 145 | + specReferences: [SpecReferences.MCP_PREREGISTRATION] |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + return this.checks; |
| 150 | + } |
| 151 | +} |
0 commit comments