Skip to content

Commit a439840

Browse files
committed
Mock handler
1 parent c67ef6c commit a439840

2 files changed

Lines changed: 28 additions & 70 deletions

File tree

samples/SecureWeatherClient/Program.cs

Lines changed: 17 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,24 @@ static async Task Main(string[] args)
1414
Console.WriteLine("==================================================");
1515
Console.WriteLine();
1616

17-
Console.WriteLine("Select authentication mode:");
18-
Console.WriteLine("1. Normal OAuth flow with browser");
19-
Console.WriteLine("2. Mock authentication (accepts any token for testing)");
20-
Console.Write("Enter your choice (1-2): ");
21-
var choice = Console.ReadLine()?.Trim();
17+
// Create the authorization config with HTTP listener
18+
var authConfig = new AuthorizationConfig
19+
{
20+
ClientId = "04f79824-ab56-4511-a7cb-d7deaea92dc0",
21+
Scopes = ["User.Read"]
22+
}.UseHttpListener(hostname: "localhost", listenPort: 1170);
2223

2324
// Create an HTTP client with OAuth handling
24-
DelegatingHandler oauthHandler;
25-
26-
if (choice == "2")
27-
{
28-
Console.WriteLine("\nUsing mock authentication for testing (no browser will open).\n");
29-
30-
// Create a mock OAuth handler that always returns a token
31-
oauthHandler = new MockOAuthHandler()
32-
{
33-
InnerHandler = new HttpClientHandler()
34-
};
35-
}
36-
else
25+
var oauthHandler = new OAuthDelegatingHandler(
26+
redirectUri: authConfig.RedirectUri,
27+
clientId: authConfig.ClientId,
28+
clientName: authConfig.ClientName,
29+
scopes: authConfig.Scopes,
30+
authorizationHandler: authConfig.AuthorizationHandler)
3731
{
38-
Console.WriteLine("\nUsing standard OAuth flow with browser authentication.\n");
39-
40-
// Create the authorization config with HTTP listener
41-
var authConfig = new AuthorizationConfig
42-
{
43-
ClientId = "04f79824-ab56-4511-a7cb-d7deaea92dc0",
44-
ClientName = "SecureWeatherClient",
45-
Scopes = ["weather.read"]
46-
}.UseHttpListener(hostname: "localhost", listenPort: 1170);
47-
48-
// Create an HTTP client with OAuth handling
49-
oauthHandler = new OAuthDelegatingHandler(
50-
redirectUri: authConfig.RedirectUri,
51-
clientId: authConfig.ClientId,
52-
clientName: authConfig.ClientName,
53-
scopes: authConfig.Scopes,
54-
authorizationHandler: authConfig.AuthorizationHandler)
55-
{
56-
// The OAuth handler needs an inner handler
57-
InnerHandler = new HttpClientHandler()
58-
};
59-
}
32+
// The OAuth handler needs an inner handler
33+
InnerHandler = new HttpClientHandler()
34+
};
6035

6136
var httpClient = new HttpClient(oauthHandler);
6237
var serverUrl = "http://localhost:7071/sse"; // Default server URL
@@ -71,13 +46,8 @@ static async Task Main(string[] args)
7146

7247
Console.WriteLine();
7348
Console.WriteLine($"Connecting to weather server at {serverUrl}...");
74-
75-
if (choice != "2")
76-
{
77-
Console.WriteLine("When prompted for authorization, a browser window will open automatically.");
78-
Console.WriteLine("Complete the authentication in the browser, and this application will continue automatically.");
79-
}
80-
49+
Console.WriteLine("When prompted for authorization, a browser window will open automatically.");
50+
Console.WriteLine("Complete the authentication in the browser, and this application will continue automatically.");
8151
Console.WriteLine();
8252

8353
try
@@ -127,21 +97,4 @@ static async Task Main(string[] args)
12797
Console.WriteLine("Press any key to exit...");
12898
Console.ReadKey();
12999
}
130-
}
131-
132-
/// <summary>
133-
/// A mock OAuth handler that always returns a predefined token without going through the OAuth flow.
134-
/// This is useful for testing without requiring a real OAuth server.
135-
/// </summary>
136-
public class MockOAuthHandler : DelegatingHandler
137-
{
138-
private readonly string _mockToken = "mock_test_token_" + Guid.NewGuid().ToString("N");
139-
140-
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
141-
{
142-
// Always attach the mock token to outgoing requests
143-
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _mockToken);
144-
145-
return base.SendAsync(request, cancellationToken);
146-
}
147100
}

samples/SecureWeatherServer/Program.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,19 @@
110110
Console.WriteLine("Starting MCP server with authorization at http://localhost:7071");
111111
Console.WriteLine("PRM Document URL: http://localhost:7071/.well-known/oauth-protected-resource");
112112

113+
Console.WriteLine();
114+
Console.WriteLine("Testing mode: Server will accept ANY non-empty token for authentication");
113115
Console.WriteLine();
114116
Console.WriteLine("To test the server:");
115117
Console.WriteLine("1. Use an MCP client that supports authorization");
116-
Console.WriteLine("2. When prompted for authorization, enter 'valid_token' to gain access");
117-
Console.WriteLine("3. Any other token value will be rejected with a 401 Unauthorized");
118+
Console.WriteLine("2. The server will accept any non-empty token sent by the client");
119+
Console.WriteLine("3. Tokens will be logged to the console for debugging");
118120
Console.WriteLine();
119121
Console.WriteLine("Press Ctrl+C to stop the server");
120122

121123
await app.RunAsync();
122124

123-
// Simple auth handler that validates a test token
125+
// Simple auth handler that accepts any non-empty token for testing
124126
// In a real app, you'd use a JWT handler or other proper authentication
125127
class SimpleAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
126128
{
@@ -149,12 +151,15 @@ protected override Task<AuthenticateResult> HandleAuthenticateAsync()
149151

150152
var token = headerValue["Bearer ".Length..].Trim();
151153

152-
// Validate the token - in a real app, this would validate a JWT
153-
if (token != "valid_token")
154+
// Accept any non-empty token for testing purposes
155+
if (string.IsNullOrEmpty(token))
154156
{
155-
return Task.FromResult(AuthenticateResult.Fail("Invalid token"));
157+
return Task.FromResult(AuthenticateResult.Fail("Token cannot be empty"));
156158
}
157159

160+
// Log the received token for debugging
161+
Console.WriteLine($"Received and accepted token: {token}");
162+
158163
// Create a claims identity with required claims
159164
var claims = new[]
160165
{

0 commit comments

Comments
 (0)