-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat(mcp): Persist OAuth credentials to keyring #4007
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8e88eb0
feat: Persist oauth credentials
jamadeo 8372bba
de-llmify
jamadeo 1c44dc0
Clear if they don't convert
jamadeo c75ef0b
Merge remote-tracking branch 'origin/main' into jackamadeo/persist-oauth
jamadeo a64a816
Allow a clippy violation
jamadeo 2e6b6d9
Merge remote-tracking branch 'origin/main' into jackamadeo/persist-oauth
jamadeo 6896845
rm the allow
jamadeo 3717a82
Clear them if they don't refresh
jamadeo 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
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
File renamed without changes.
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,72 @@ | ||
| use oauth2::{basic::BasicTokenType, EmptyExtraTokenFields, StandardTokenResponse}; | ||
| use reqwest::IntoUrl; | ||
| use rmcp::transport::{auth::OAuthState, AuthError}; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| use crate::config::Config; | ||
|
|
||
| #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| pub struct SerializableCredentials { | ||
| pub client_id: String, | ||
| pub token_response: Option<StandardTokenResponse<EmptyExtraTokenFields, BasicTokenType>>, | ||
| } | ||
|
|
||
| fn secret_key(name: &str) -> String { | ||
| format!("oauth_creds_{name}") | ||
| } | ||
|
|
||
| pub async fn save_credentials( | ||
| name: &str, | ||
| oauth_state: &OAuthState, | ||
| ) -> Result<(), Box<dyn std::error::Error>> { | ||
| let config = Config::global(); | ||
| let (client_id, token_response) = oauth_state.get_credentials().await?; | ||
|
|
||
| let credentials = SerializableCredentials { | ||
| client_id, | ||
| token_response, | ||
| }; | ||
|
|
||
| let value = serde_json::to_value(&credentials)?; | ||
| let key = secret_key(name); | ||
| config.set_secret(&key, value)?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| async fn load_credentials( | ||
| name: &str, | ||
| ) -> Result<SerializableCredentials, Box<dyn std::error::Error>> { | ||
| let config = Config::global(); | ||
| let key = secret_key(name); | ||
| let credentials: SerializableCredentials = config.get_secret(&key)?; | ||
|
|
||
| Ok(credentials) | ||
| } | ||
|
|
||
| pub fn clear_credentials(name: &str) -> Result<(), Box<dyn std::error::Error>> { | ||
| let config = Config::global(); | ||
|
|
||
| Ok(config.delete_secret(&secret_key(name))?) | ||
| } | ||
|
|
||
| pub async fn load_cached_state<U: IntoUrl>( | ||
| base_url: U, | ||
| name: &str, | ||
| ) -> Result<OAuthState, AuthError> { | ||
| let credentials = load_credentials(name) | ||
| .await | ||
| .map_err(|e| AuthError::InternalError(format!("Failed to load credentials: {}", e)))?; | ||
|
|
||
| if let Some(token_response) = credentials.token_response { | ||
| let mut oauth_state = OAuthState::new(base_url, None).await?; | ||
| oauth_state | ||
| .set_credentials(&credentials.client_id, token_response) | ||
| .await?; | ||
| Ok(oauth_state) | ||
| } else { | ||
| Err(AuthError::InternalError( | ||
| "No token response in cached credentials".to_string(), | ||
| )) | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how long are the credentials typically valid? maybe I am missing it but shouldn't there be a ttl for the data we store via keyring?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah they'll have an expiry, but that should be handled by the oauth mechanism anyway -- it might need to be refreshed after getting loaded but that should be fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested it out with an expired credential and indeed there was a bug! Added a call to refresh on load.