Skip to content

Commit f3a3715

Browse files
committed
Consolidate some OAuth logic
1 parent a90d01e commit f3a3715

File tree

6 files changed

+19
-43
lines changed

6 files changed

+19
-43
lines changed

src/ModelContextProtocol/Auth/AuthorizationConfigExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static AuthorizationConfig UseHttpListener(
4949
openBrowser ??= DefaultOpenBrowser;
5050

5151
// Configure the handler
52-
config.AuthorizationHandler = OAuthAuthorizationHelpers.CreateHttpListenerCallback(
52+
config.AuthorizationHandler = OAuthHelpers.CreateHttpListenerCallback(
5353
openBrowser,
5454
hostname,
5555
listenPort,

src/ModelContextProtocol/Auth/McpClientExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ public static async Task<OAuthToken> HandleUnauthorizedResponseAsync(
9898
}
9999

100100
// Create OAuthAuthenticationService - use appropriate constructor based on whether we have a handler
101-
OAuthAuthenticationService authService = config.AuthorizationHandler != null
102-
? new OAuthAuthenticationService(config.AuthorizationHandler)
103-
: new OAuthAuthenticationService();
101+
OAuthService authService = config.AuthorizationHandler != null
102+
? new OAuthService(config.AuthorizationHandler)
103+
: new OAuthService();
104104

105105
// Get resource URI
106106
var resourceUri = response.RequestMessage?.RequestUri ?? throw new InvalidOperationException("Request URI is not available.");

src/ModelContextProtocol/Auth/OAuthAuthorizationHelpers.cs renamed to src/ModelContextProtocol/Auth/OAuthHelpers.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using ModelContextProtocol.Auth.Types;
2+
using ModelContextProtocol.Utils.Json;
33
using System.Net;
4-
using System.Net.Http;
54
using System.Net.Http.Headers;
65
using System.Text;
76
using System.Text.Json;
8-
using System.Threading;
9-
using System.Threading.Tasks;
10-
using ModelContextProtocol.Auth.Types;
11-
using ModelContextProtocol.Utils.Json;
127

138
namespace ModelContextProtocol.Auth;
149

1510
/// <summary>
1611
/// Provides helper methods for handling OAuth authorization.
1712
/// </summary>
18-
public static class OAuthAuthorizationHelpers
13+
public static class OAuthHelpers
1914
{
2015
private static readonly HttpClient _httpClient = new();
2116

src/ModelContextProtocol/Auth/OAuthAuthenticationService.cs renamed to src/ModelContextProtocol/Auth/OAuthService.cs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,23 @@ namespace ModelContextProtocol.Auth;
1111
/// <summary>
1212
/// Provides functionality for OAuth authentication in MCP clients.
1313
/// </summary>
14-
public class OAuthAuthenticationService
14+
public partial class OAuthService
1515
{
1616
private static readonly HttpClient _httpClient = new();
1717
private readonly Func<Uri, Task<string>>? _authorizationHandler;
1818

1919
/// <summary>
20-
/// Represents the PKCE code challenge and verifier for an authorization flow.
20+
/// Initializes a new instance of the <see cref="OAuthService"/> class.
2121
/// </summary>
22-
public class PkceValues
23-
{
24-
/// <summary>
25-
/// The code verifier used to generate the code challenge.
26-
/// </summary>
27-
public string CodeVerifier { get; }
28-
29-
/// <summary>
30-
/// The code challenge sent to the authorization server.
31-
/// </summary>
32-
public string CodeChallenge { get; }
33-
34-
/// <summary>
35-
/// Initializes a new instance of the <see cref="PkceValues"/> class.
36-
/// </summary>
37-
public PkceValues(string codeVerifier, string codeChallenge)
38-
{
39-
CodeVerifier = codeVerifier;
40-
CodeChallenge = codeChallenge;
41-
}
42-
}
43-
44-
/// <summary>
45-
/// Initializes a new instance of the <see cref="OAuthAuthenticationService"/> class.
46-
/// </summary>
47-
public OAuthAuthenticationService()
22+
public OAuthService()
4823
{
4924
}
5025

5126
/// <summary>
52-
/// Initializes a new instance of the <see cref="OAuthAuthenticationService"/> class with an authorization handler.
27+
/// Initializes a new instance of the <see cref="OAuthService"/> class with an authorization handler.
5328
/// </summary>
5429
/// <param name="authorizationHandler">A handler to invoke when authorization is required.</param>
55-
public OAuthAuthenticationService(Func<Uri, Task<string>> authorizationHandler)
30+
public OAuthService(Func<Uri, Task<string>> authorizationHandler)
5631
{
5732
_authorizationHandler = authorizationHandler ?? throw new ArgumentNullException(nameof(authorizationHandler));
5833
}

src/ModelContextProtocol/Auth/Types/AuthorizationCodeOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class AuthorizationCodeOptions
3838
/// <summary>
3939
/// PKCE values for the authorization flow.
4040
/// </summary>
41-
public OAuthAuthenticationService.PkceValues PkceValues { get; set; } = null!;
41+
public PkceValues PkceValues { get; set; } = null!;
4242

4343
/// <summary>
4444
/// A state value to protect against CSRF attacks.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ModelContextProtocol.Auth;
2+
3+
4+
/// <param name="CodeVerifier"> The code verifier used to generate the code challenge. </param>
5+
/// <param name="CodeChallenge"> The code challenge sent to the authorization server. </param>
6+
public record PkceValues(string CodeVerifier, string CodeChallenge);

0 commit comments

Comments
 (0)