@@ -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 ( "\n Using 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 ( "\n Using 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}
0 commit comments