-
Notifications
You must be signed in to change notification settings - Fork 571
Tokens can be cached beyond the lifetime of the (http) transport. #834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
halllo
wants to merge
6
commits into
modelcontextprotocol:main
Choose a base branch
from
halllo:tokencache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+536
−364
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
93f0d91
Tokens can be cached beyond the lifetime of the (http) transport.
halllo 26f80ef
Tests, ValueTasks, and dedicated type for caching.
halllo bd4f0ff
Type rename; alignment test
halllo de1dc78
Merge branch 'main' into tokencache
halter73 db341ad
Add OAuthTestBase and use it to test TokenCache
halter73 71bf435
Add back null forgiving operator that should be unnecessary
halter73 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/ModelContextProtocol.Core/Authentication/ITokenCache.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| namespace ModelContextProtocol.Authentication; | ||
|
|
||
| /// <summary> | ||
| /// Allows the client to cache access tokens beyond the lifetime of the transport. | ||
| /// </summary> | ||
| public interface ITokenCache | ||
| { | ||
| /// <summary> | ||
| /// Cache the token. After a new access token is acquired, this method is invoked to store it. | ||
| /// </summary> | ||
| ValueTask StoreTokensAsync(TokenContainer tokens, CancellationToken cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Get the cached token. This method is invoked for every request. | ||
| /// </summary> | ||
| ValueTask<TokenContainer?> GetTokensAsync(CancellationToken cancellationToken); | ||
| } |
27 changes: 27 additions & 0 deletions
27
src/ModelContextProtocol.Core/Authentication/InMemoryTokenCache.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
|
|
||
| namespace ModelContextProtocol.Authentication; | ||
|
|
||
| /// <summary> | ||
| /// Caches the token in-memory within this instance. | ||
| /// </summary> | ||
| internal class InMemoryTokenCache : ITokenCache | ||
| { | ||
| private TokenContainer? _tokens; | ||
|
|
||
| /// <summary> | ||
| /// Cache the token. | ||
| /// </summary> | ||
| public ValueTask StoreTokensAsync(TokenContainer tokens, CancellationToken cancellationToken) | ||
| { | ||
| _tokens = tokens; | ||
| return default; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Get the cached token. | ||
| /// </summary> | ||
| public ValueTask<TokenContainer?> GetTokensAsync(CancellationToken cancellationToken) | ||
| { | ||
| return new ValueTask<TokenContainer?>(_tokens); | ||
| } | ||
| } |
42 changes: 12 additions & 30 deletions
42
src/ModelContextProtocol.Core/Authentication/TokenContainer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,57 +1,39 @@ | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace ModelContextProtocol.Authentication; | ||
|
|
||
| /// <summary> | ||
| /// Represents a token response from the OAuth server. | ||
| /// Represents a cacheable combination of tokens ready to be used for authentication. | ||
| /// </summary> | ||
| internal sealed class TokenContainer | ||
| public sealed class TokenContainer | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the token type (typically "Bearer"). | ||
| /// </summary> | ||
| public required string TokenType { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the access token. | ||
| /// </summary> | ||
| [JsonPropertyName("access_token")] | ||
| public string AccessToken { get; set; } = string.Empty; | ||
| public required string AccessToken { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the refresh token. | ||
| /// </summary> | ||
| [JsonPropertyName("refresh_token")] | ||
| public string? RefreshToken { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the number of seconds until the access token expires. | ||
| /// </summary> | ||
| [JsonPropertyName("expires_in")] | ||
| public int ExpiresIn { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the extended expiration time in seconds. | ||
| /// </summary> | ||
| [JsonPropertyName("ext_expires_in")] | ||
| public int ExtExpiresIn { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the token type (typically "Bearer"). | ||
| /// </summary> | ||
| [JsonPropertyName("token_type")] | ||
| public string TokenType { get; set; } = string.Empty; | ||
| public int? ExpiresIn { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the scope of the access token. | ||
| /// </summary> | ||
| [JsonPropertyName("scope")] | ||
| public string Scope { get; set; } = string.Empty; | ||
| public string? Scope { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the timestamp when the token was obtained. | ||
| /// </summary> | ||
| [JsonIgnore] | ||
| public DateTimeOffset ObtainedAt { get; set; } | ||
| public required DateTimeOffset ObtainedAt { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the timestamp when the token expires, calculated from ObtainedAt and ExpiresIn. | ||
| /// </summary> | ||
| [JsonIgnore] | ||
| public DateTimeOffset ExpiresAt => ObtainedAt.AddSeconds(ExpiresIn); | ||
| internal bool IsExpired => ExpiresIn is not null && DateTimeOffset.UtcNow >= ObtainedAt.AddSeconds(ExpiresIn.Value); | ||
| } |
39 changes: 39 additions & 0 deletions
39
src/ModelContextProtocol.Core/Authentication/TokenResponse.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace ModelContextProtocol.Authentication; | ||
|
|
||
| /// <summary> | ||
| /// Represents a token response from the OAuth server. | ||
| /// </summary> | ||
| internal sealed class TokenResponse | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the access token. | ||
| /// </summary> | ||
| [JsonPropertyName("access_token")] | ||
| public required string AccessToken { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the refresh token. | ||
| /// </summary> | ||
| [JsonPropertyName("refresh_token")] | ||
| public string? RefreshToken { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the number of seconds until the access token expires. | ||
| /// </summary> | ||
| [JsonPropertyName("expires_in")] | ||
| public int? ExpiresIn { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the token type (typically "Bearer"). | ||
| /// </summary> | ||
| [JsonPropertyName("token_type")] | ||
| public required string TokenType { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the scope of the access token. | ||
| /// </summary> | ||
| [JsonPropertyName("scope")] | ||
| public string? Scope { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.