Skip to content

Commit 048f1ac

Browse files
authored
fix(auth): url parse is not correct (#402)
The format method is not good for url.
1 parent 6853143 commit 048f1ac

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

crates/rmcp/src/transport/auth.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use thiserror::Error;
1616
use tokio::sync::{Mutex, RwLock};
1717
use tracing::{debug, error};
1818

19+
const DEFAULT_EXCHANGE_URL: &str = "http://localhost";
20+
1921
/// sse client with oauth2 authorization
2022
#[derive(Clone)]
2123
pub struct AuthClient<C> {
@@ -225,10 +227,17 @@ impl AuthorizationManager {
225227
// discard the path part, only keep scheme, host, port
226228
auth_base.set_path("");
227229

230+
// Helper function to create endpoint URL
231+
let create_endpoint = |path: &str| -> String {
232+
let mut url = auth_base.clone();
233+
url.set_path(path);
234+
url.to_string()
235+
};
236+
228237
Ok(AuthorizationMetadata {
229-
authorization_endpoint: format!("{}/authorize", auth_base),
230-
token_endpoint: format!("{}/token", auth_base),
231-
registration_endpoint: format!("{}/register", auth_base),
238+
authorization_endpoint: create_endpoint("authorize"),
239+
token_endpoint: create_endpoint("token"),
240+
registration_endpoint: create_endpoint("register"),
232241
issuer: None,
233242
jwks_uri: None,
234243
scopes_supported: None,
@@ -686,7 +695,7 @@ impl OAuthState {
686695
if let OAuthState::Unauthorized(manager) = self {
687696
let mut manager = std::mem::replace(
688697
manager,
689-
AuthorizationManager::new("http://localhost").await?,
698+
AuthorizationManager::new(DEFAULT_EXCHANGE_URL).await?,
690699
);
691700

692701
// write credentials
@@ -716,7 +725,7 @@ impl OAuthState {
716725
) -> Result<(), AuthError> {
717726
if let OAuthState::Unauthorized(mut manager) = std::mem::replace(
718727
self,
719-
OAuthState::Unauthorized(AuthorizationManager::new("http://localhost").await?),
728+
OAuthState::Unauthorized(AuthorizationManager::new(DEFAULT_EXCHANGE_URL).await?),
720729
) {
721730
debug!("start discovery");
722731
let metadata = manager.discover_metadata().await?;
@@ -736,7 +745,7 @@ impl OAuthState {
736745
pub async fn complete_authorization(&mut self) -> Result<(), AuthError> {
737746
if let OAuthState::Session(session) = std::mem::replace(
738747
self,
739-
OAuthState::Unauthorized(AuthorizationManager::new("http://localhost").await?),
748+
OAuthState::Unauthorized(AuthorizationManager::new(DEFAULT_EXCHANGE_URL).await?),
740749
) {
741750
*self = OAuthState::Authorized(session.auth_manager);
742751
Ok(())
@@ -748,7 +757,7 @@ impl OAuthState {
748757
pub async fn to_authorized_http_client(&mut self) -> Result<(), AuthError> {
749758
if let OAuthState::Authorized(manager) = std::mem::replace(
750759
self,
751-
OAuthState::Authorized(AuthorizationManager::new("http://localhost").await?),
760+
OAuthState::Authorized(AuthorizationManager::new(DEFAULT_EXCHANGE_URL).await?),
752761
) {
753762
*self = OAuthState::AuthorizedHttpClient(AuthorizedHttpClient::new(
754763
Arc::new(manager),

0 commit comments

Comments
 (0)