@@ -21,8 +21,10 @@ use rmcp::transport::{
21
21
sse_server:: SseServerConfig ,
22
22
} ;
23
23
use serde:: { Deserialize , Serialize } ;
24
+ use serde_json:: Value ;
24
25
use tokio:: sync:: RwLock ;
25
26
use tokio_util:: sync:: CancellationToken ;
27
+ use tower_http:: cors:: { Any , CorsLayer } ;
26
28
use tracing:: { debug, error, info, warn} ;
27
29
use tracing_subscriber:: { layer:: SubscriberExt , util:: SubscriberInitExt } ;
28
30
use uuid:: Uuid ;
@@ -518,14 +520,23 @@ async fn validate_token_middleware(
518
520
519
521
// handle oauth server metadata request
520
522
async fn oauth_authorization_server ( ) -> impl IntoResponse {
523
+ let mut additional_fields = HashMap :: new ( ) ;
524
+ additional_fields. insert (
525
+ "response_types_supported" . into ( ) ,
526
+ Value :: Array ( vec ! [ Value :: String ( "code" . into( ) ) ] ) ,
527
+ ) ;
528
+ additional_fields. insert (
529
+ "code_challenge_methods_supported" . into ( ) ,
530
+ Value :: Array ( vec ! [ Value :: String ( "S256" . into( ) ) ] ) ,
531
+ ) ;
521
532
let metadata = AuthorizationMetadata {
522
533
authorization_endpoint : format ! ( "http://{}/oauth/authorize" , BIND_ADDRESS ) ,
523
534
token_endpoint : format ! ( "http://{}/oauth/token" , BIND_ADDRESS ) ,
524
535
scopes_supported : Some ( vec ! [ "profile" . to_string( ) , "email" . to_string( ) ] ) ,
525
536
registration_endpoint : format ! ( "http://{}/oauth/register" , BIND_ADDRESS ) ,
526
537
issuer : Some ( BIND_ADDRESS . to_string ( ) ) ,
527
538
jwks_uri : Some ( format ! ( "http://{}/oauth/jwks" , BIND_ADDRESS ) ) ,
528
- additional_fields : HashMap :: new ( ) ,
539
+ additional_fields,
529
540
} ;
530
541
debug ! ( "metadata: {:?}" , metadata) ;
531
542
( StatusCode :: OK , Json ( metadata) )
@@ -655,18 +666,33 @@ async fn main() -> Result<()> {
655
666
validate_token_middleware,
656
667
) ) ;
657
668
669
+ // Create CORS layer for the oauth authorization server endpoint
670
+ let cors_layer = CorsLayer :: new ( )
671
+ . allow_origin ( Any )
672
+ . allow_methods ( Any )
673
+ . allow_headers ( Any ) ;
674
+
675
+ // Create a sub-router for the oauth authorization server endpoint with CORS
676
+ let oauth_server_router = Router :: new ( )
677
+ . route (
678
+ "/.well-known/oauth-authorization-server" ,
679
+ get ( oauth_authorization_server) . options ( oauth_authorization_server) ,
680
+ )
681
+ . route ( "/oauth/token" , post ( oauth_token) . options ( oauth_token) )
682
+ . route (
683
+ "/oauth/register" ,
684
+ post ( oauth_register) . options ( oauth_register) ,
685
+ )
686
+ . layer ( cors_layer)
687
+ . with_state ( oauth_store. clone ( ) ) ;
688
+
658
689
// Create HTTP router with request logging middleware
659
690
let app = Router :: new ( )
660
691
. route ( "/" , get ( index) )
661
692
. route ( "/mcp" , get ( index) )
662
- . route (
663
- "/.well-known/oauth-authorization-server" ,
664
- get ( oauth_authorization_server) ,
665
- )
666
693
. route ( "/oauth/authorize" , get ( oauth_authorize) )
667
694
. route ( "/oauth/approve" , post ( oauth_approve) )
668
- . route ( "/oauth/token" , post ( oauth_token) )
669
- . route ( "/oauth/register" , post ( oauth_register) )
695
+ . merge ( oauth_server_router) // Merge the CORS-enabled oauth server router
670
696
// .merge(protected_sse_router)
671
697
. with_state ( oauth_store. clone ( ) )
672
698
. layer ( middleware:: from_fn ( log_request) ) ;
0 commit comments