Skip to content

Commit 0d0c631

Browse files
pcarletonclaude
andcommitted
Refactor token endpoint auth tests to use shared createAuthServer
Extend createAuthServer helper with: - tokenEndpointAuthMethodsSupported option for metadata - onTokenRequest callback now receives full Request object - onRegistrationRequest callback for custom client credentials This eliminates the duplicate auth server implementation in token-endpoint-auth.ts and reduces code by ~140 lines. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 8a59560 commit 0d0c631

File tree

2 files changed

+94
-235
lines changed

2 files changed

+94
-235
lines changed

src/scenarios/client/auth/helpers/createAuthServer.ts

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,21 @@ export interface AuthServerOptions {
1010
loggingEnabled?: boolean;
1111
routePrefix?: string;
1212
scopesSupported?: string[];
13+
tokenEndpointAuthMethodsSupported?: string[];
1314
tokenVerifier?: MockTokenVerifier;
14-
onTokenRequest?: (requestData: {
15-
scope?: string;
16-
grantType: string;
17-
timestamp: string;
18-
}) => { token: string; scopes: string[] };
15+
onTokenRequest?: (
16+
req: Request,
17+
timestamp: string
18+
) => { token: string; scopes: string[] } | void;
1919
onAuthorizationRequest?: (requestData: {
2020
scope?: string;
2121
timestamp: string;
2222
}) => void;
23+
onRegistrationRequest?: (req: Request) => {
24+
clientId: string;
25+
clientSecret?: string;
26+
tokenEndpointAuthMethod?: string;
27+
};
2328
}
2429

2530
export function createAuthServer(
@@ -33,9 +38,11 @@ export function createAuthServer(
3338
loggingEnabled = true,
3439
routePrefix = '',
3540
scopesSupported,
41+
tokenEndpointAuthMethodsSupported = ['none'],
3642
tokenVerifier,
3743
onTokenRequest,
38-
onAuthorizationRequest
44+
onAuthorizationRequest,
45+
onRegistrationRequest
3946
} = options;
4047

4148
// Track scopes from the most recent authorization request
@@ -85,7 +92,7 @@ export function createAuthServer(
8592
response_types_supported: ['code'],
8693
grant_types_supported: ['authorization_code', 'refresh_token'],
8794
code_challenge_methods_supported: ['S256'],
88-
token_endpoint_auth_methods_supported: ['none']
95+
token_endpoint_auth_methods_supported: tokenEndpointAuthMethodsSupported
8996
};
9097

9198
// Add scopes_supported if provided
@@ -141,7 +148,6 @@ export function createAuthServer(
141148

142149
app.post(authRoutes.token_endpoint, (req: Request, res: Response) => {
143150
const timestamp = new Date().toISOString();
144-
const requestedScope = req.body.scope;
145151

146152
checks.push({
147153
id: 'token-request',
@@ -160,13 +166,11 @@ export function createAuthServer(
160166
let scopes: string[] = lastAuthorizationScopes;
161167

162168
if (onTokenRequest) {
163-
const result = onTokenRequest({
164-
scope: requestedScope,
165-
grantType: req.body.grant_type,
166-
timestamp
167-
});
168-
token = result.token;
169-
scopes = result.scopes;
169+
const result = onTokenRequest(req, timestamp);
170+
if (result) {
171+
token = result.token;
172+
scopes = result.scopes;
173+
}
170174
}
171175

172176
// Register token with verifier if provided
@@ -183,6 +187,17 @@ export function createAuthServer(
183187
});
184188

185189
app.post(authRoutes.registration_endpoint, (req: Request, res: Response) => {
190+
let clientId = 'test-client-id';
191+
let clientSecret: string | undefined = 'test-client-secret';
192+
let tokenEndpointAuthMethod: string | undefined;
193+
194+
if (onRegistrationRequest) {
195+
const result = onRegistrationRequest(req);
196+
clientId = result.clientId;
197+
clientSecret = result.clientSecret;
198+
tokenEndpointAuthMethod = result.tokenEndpointAuthMethod;
199+
}
200+
186201
checks.push({
187202
id: 'client-registration',
188203
name: 'ClientRegistration',
@@ -192,15 +207,19 @@ export function createAuthServer(
192207
specReferences: [SpecReferences.MCP_DCR],
193208
details: {
194209
endpoint: '/register',
195-
clientName: req.body.client_name
210+
clientName: req.body.client_name,
211+
...(tokenEndpointAuthMethod && { tokenEndpointAuthMethod })
196212
}
197213
});
198214

199215
res.status(201).json({
200-
client_id: 'test-client-id',
201-
client_secret: 'test-client-secret',
216+
client_id: clientId,
217+
...(clientSecret && { client_secret: clientSecret }),
202218
client_name: req.body.client_name || 'test-client',
203-
redirect_uris: req.body.redirect_uris || []
219+
redirect_uris: req.body.redirect_uris || [],
220+
...(tokenEndpointAuthMethod && {
221+
token_endpoint_auth_method: tokenEndpointAuthMethod
222+
})
204223
});
205224
});
206225

0 commit comments

Comments
 (0)