@@ -707,147 +707,6 @@ describe("MCP OAuth provider", () => {
707707 ] ) ;
708708 } ) ;
709709
710- it ( "exchanges a captured loopback code under the same OAuth lease" , async ( ) => {
711- await withTempHome (
712- async ( ) => {
713- const authorizationUrl = new URL ( "https://auth.example.com/authorize" ) ;
714- authorizationUrl . searchParams . set ( "state" , "state-1" ) ;
715- authorizationUrl . searchParams . set ( "redirect_uri" , "http://127.0.0.1:43123/oauth/callback" ) ;
716- authMock . mockReset ( ) ;
717- authMock
718- . mockImplementationOnce ( async ( provider ) => {
719- await provider . saveCodeVerifier ?.( "verifier" ) ;
720- await provider . redirectToAuthorization ?.( authorizationUrl ) ;
721- return "REDIRECT" ;
722- } )
723- . mockImplementationOnce ( async ( provider , options ) => {
724- expect ( options . authorizationCode ) . toBe ( "captured-code" ) ;
725- expect ( provider . redirectUrl ) . toBe ( "http://127.0.0.1:43123/oauth/callback" ) ;
726- const leaseCount = openOpenClawStateDatabase ( )
727- . db . prepare ( "SELECT COUNT(*) AS count FROM state_leases WHERE scope = ?" )
728- . get ( "core:mcp-oauth" ) as { count : number } ;
729- expect ( leaseCount . count ) . toBe ( 1 ) ;
730- await provider . saveTokens ( { access_token : "access" , token_type : "Bearer" } ) ;
731- return "AUTHORIZED" ;
732- } ) ;
733-
734- await expect (
735- runMcpOAuthLogin ( {
736- serverName : "Remote Docs" ,
737- serverUrl : "https://mcp.example.com/mcp" ,
738- config : { redirectUrl : "http://127.0.0.1:43123/oauth/callback" } ,
739- onAuthorizationUrl : async ( url ) => {
740- expect ( url ) . toEqual ( authorizationUrl ) ;
741- return "captured-code" ;
742- } ,
743- } ) ,
744- ) . resolves . toBe ( "authorized" ) ;
745- expect ( authMock ) . toHaveBeenCalledTimes ( 2 ) ;
746- } ,
747- {
748- prefix : "openclaw-mcp-oauth-loopback-code-" ,
749- skipSessionCleanup : true ,
750- env : { OPENCLAW_CONFIG_PATH : undefined , OPENCLAW_STATE_DIR : undefined } ,
751- } ,
752- ) ;
753- } ) ;
754-
755- it ( "persists the localhost registration fallback before exchanging a captured code" , async ( ) => {
756- await withTempHome (
757- async ( ) => {
758- const serverName = "Calendly" ;
759- const serverUrl = "https://mcp.calendly.com/" ;
760- const storeKey = resolveMcpOAuthStoreKey ( serverName , serverUrl ) ;
761- const authorizationUrl = new URL ( "https://auth.example.com/authorize" ) ;
762- authorizationUrl . searchParams . set ( "state" , "state-1" ) ;
763- authorizationUrl . searchParams . set ( "redirect_uri" , "http://localhost:8989/oauth/callback" ) ;
764- authMock . mockReset ( ) ;
765- authMock
766- . mockRejectedValueOnce ( new Error ( "invalid_client_metadata: redirect_uri rejected" ) )
767- . mockImplementationOnce ( async ( provider ) => {
768- expect ( provider . redirectUrl ) . toBe ( "http://localhost:8989/oauth/callback" ) ;
769- await provider . saveCodeVerifier ?.( "verifier" ) ;
770- await provider . redirectToAuthorization ?.( authorizationUrl ) ;
771- return "REDIRECT" ;
772- } )
773- . mockImplementationOnce ( async ( provider , options ) => {
774- expect ( options . authorizationCode ) . toBe ( "captured-code" ) ;
775- expect ( provider . redirectUrl ) . toBe ( "http://localhost:8989/oauth/callback" ) ;
776- expect ( readMcpOAuthStore ( storeKey ) . redirectUrl ) . toBe (
777- "http://localhost:8989/oauth/callback" ,
778- ) ;
779- return "AUTHORIZED" ;
780- } ) ;
781-
782- await expect (
783- runMcpOAuthLogin ( {
784- serverName,
785- serverUrl,
786- onAuthorizationUrl : ( ) => "captured-code" ,
787- } ) ,
788- ) . resolves . toBe ( "authorized" ) ;
789- expect ( authMock ) . toHaveBeenCalledTimes ( 3 ) ;
790- } ,
791- {
792- prefix : "openclaw-mcp-oauth-loopback-fallback-" ,
793- skipSessionCleanup : true ,
794- env : { OPENCLAW_CONFIG_PATH : undefined , OPENCLAW_STATE_DIR : undefined } ,
795- } ,
796- ) ;
797- } ) ;
798-
799- it . each ( [
800- [ "busy listener" , Object . assign ( new Error ( "listen EADDRINUSE" ) , { code : "EADDRINUSE" } ) ] ,
801- [ "callback timeout" , new Error ( "OAuth callback timeout" ) ] ,
802- ] ) ( "persists localhost before a failed %s can fall back to --code" , async ( _label , error ) => {
803- await withTempHome (
804- async ( ) => {
805- const serverName = "Calendly" ;
806- const serverUrl = "https://mcp.calendly.com/" ;
807- const storeKey = resolveMcpOAuthStoreKey ( serverName , serverUrl ) ;
808- const authorizationUrl = new URL ( "https://auth.example.com/authorize" ) ;
809- authorizationUrl . searchParams . set ( "state" , "state-1" ) ;
810- authorizationUrl . searchParams . set ( "redirect_uri" , "http://localhost:8989/oauth/callback" ) ;
811- authMock . mockReset ( ) ;
812- authMock
813- . mockRejectedValueOnce ( new Error ( "invalid_client_metadata: redirect_uri rejected" ) )
814- . mockImplementationOnce ( async ( provider ) => {
815- await provider . saveCodeVerifier ?.( "verifier" ) ;
816- await provider . redirectToAuthorization ?.( authorizationUrl ) ;
817- return "REDIRECT" ;
818- } ) ;
819-
820- await expect (
821- runMcpOAuthLogin ( {
822- serverName,
823- serverUrl,
824- onAuthorizationUrl : ( ) => {
825- throw error ;
826- } ,
827- } ) ,
828- ) . rejects . toBe ( error ) ;
829- expect ( readMcpOAuthStore ( storeKey ) . redirectUrl ) . toBe (
830- "http://localhost:8989/oauth/callback" ,
831- ) ;
832-
833- authMock . mockReset ( ) ;
834- authMock . mockImplementationOnce ( async ( provider , options ) => {
835- expect ( options . authorizationCode ) . toBe ( "manual-code" ) ;
836- expect ( provider . redirectUrl ) . toBe ( "http://localhost:8989/oauth/callback" ) ;
837- return "AUTHORIZED" ;
838- } ) ;
839- await expect (
840- runMcpOAuthLogin ( { serverName, serverUrl, authorizationCode : "manual-code" } ) ,
841- ) . resolves . toBe ( "authorized" ) ;
842- } ,
843- {
844- prefix : "openclaw-mcp-oauth-loopback-recovery-" ,
845- skipSessionCleanup : true ,
846- env : { OPENCLAW_CONFIG_PATH : undefined , OPENCLAW_STATE_DIR : undefined } ,
847- } ,
848- ) ;
849- } ) ;
850-
851710 it ( "does not retry a code exchange redirect mismatch" , async ( ) => {
852711 authMock . mockReset ( ) ;
853712 authMock . mockRejectedValueOnce ( new Error ( "invalid_grant: redirect_uri mismatch" ) ) ;
@@ -895,30 +754,46 @@ describe("MCP OAuth provider", () => {
895754 it ( "persists localhost redirect for a later code exchange login" , async ( ) => {
896755 await withTempHome (
897756 async ( ) => {
757+ let finalAuthorizationUrl : URL | undefined ;
898758 authMock . mockReset ( ) ;
899759 authMock
900760 . mockRejectedValueOnce ( new Error ( "invalid_client_metadata: redirect_uri rejected" ) )
901761 . mockImplementationOnce ( async ( provider ) => {
902762 await provider . saveCodeVerifier ?.( "verifier" ) ;
763+ const authorizationUrl = new URL ( "https://auth.example.com/authorize" ) ;
764+ authorizationUrl . searchParams . set ( "redirect_uri" , String ( provider . redirectUrl ) ) ;
765+ authorizationUrl . searchParams . set ( "state" , "state-1234567890" ) ;
766+ await provider . redirectToAuthorization ?.( authorizationUrl ) ;
903767 return "REDIRECT" ;
904768 } ) ;
905769
906770 await expect (
907771 runMcpOAuthLogin ( {
908772 serverName : "Calendly" ,
909773 serverUrl : "https://mcp.calendly.com/" ,
910- onAuthorizationUrl : ( ) => { } ,
774+ onAuthorizationUrl : ( url ) => {
775+ finalAuthorizationUrl = url ;
776+ } ,
911777 } ) ,
912778 ) . resolves . toBe ( "redirect" ) ;
913779
780+ expect ( finalAuthorizationUrl ?. searchParams . get ( "redirect_uri" ) ) . toBe (
781+ "http://localhost:8989/oauth/callback" ,
782+ ) ;
783+
914784 const store = readMcpOAuthStore (
915785 resolveMcpOAuthStoreKey ( "Calendly" , "https://mcp.calendly.com/" ) ,
916786 ) ;
917787 expect ( store . redirectUrl ) . toBe ( "http://localhost:8989/oauth/callback" ) ;
918788 expect ( store . codeVerifier ) . toBe ( "verifier" ) ;
919789
920790 authMock . mockReset ( ) ;
921- authMock . mockResolvedValueOnce ( "AUTHORIZED" ) ;
791+ authMock . mockImplementationOnce ( async ( provider , options ) => {
792+ expect ( options . authorizationCode ) . toBe ( "code-123" ) ;
793+ expect ( provider . redirectUrl ) . toBe ( "http://localhost:8989/oauth/callback" ) ;
794+ expect ( await provider . codeVerifier ?.( ) ) . toBe ( "verifier" ) ;
795+ return "AUTHORIZED" ;
796+ } ) ;
922797 await runMcpOAuthLogin ( {
923798 serverName : "Calendly" ,
924799 serverUrl : "https://mcp.calendly.com/" ,
@@ -939,6 +814,74 @@ describe("MCP OAuth provider", () => {
939814 ) ;
940815 } ) ;
941816
817+ it ( "keeps a captured verifier bound to its login when another login overlaps" , async ( ) => {
818+ await withTempHome (
819+ async ( ) => {
820+ let firstSession : { codeVerifier : string ; redirectUrl : string } | undefined ;
821+ authMock . mockReset ( ) ;
822+ authMock . mockImplementationOnce ( async ( provider ) => {
823+ await provider . saveCodeVerifier ?.( "verifier-first" ) ;
824+ return "REDIRECT" ;
825+ } ) ;
826+
827+ await runMcpOAuthLogin ( {
828+ serverName : "Calendly" ,
829+ serverUrl : "https://mcp.calendly.com/" ,
830+ onAuthorizationUrl : ( ) => { } ,
831+ onAuthorizationSession : ( session ) => {
832+ firstSession = session ;
833+ } ,
834+ } ) ;
835+ expect ( firstSession ) . toEqual ( {
836+ codeVerifier : "verifier-first" ,
837+ redirectUrl : "http://127.0.0.1:8989/oauth/callback" ,
838+ } ) ;
839+
840+ authMock . mockImplementationOnce ( async ( provider ) => {
841+ await provider . saveCodeVerifier ?.( "verifier-second" ) ;
842+ return "REDIRECT" ;
843+ } ) ;
844+ await runMcpOAuthLogin ( {
845+ serverName : "Calendly" ,
846+ serverUrl : "https://mcp.calendly.com/" ,
847+ onAuthorizationUrl : ( ) => { } ,
848+ } ) ;
849+ expect (
850+ readMcpOAuthStore ( resolveMcpOAuthStoreKey ( "Calendly" , "https://mcp.calendly.com/" ) )
851+ . codeVerifier ,
852+ ) . toBe ( "verifier-second" ) ;
853+
854+ const captured = firstSession ;
855+ if ( ! captured ) {
856+ throw new Error ( "first login did not capture its authorization session" ) ;
857+ }
858+ authMock . mockImplementationOnce ( async ( provider , options ) => {
859+ expect ( options . authorizationCode ) . toBe ( "code-first" ) ;
860+ expect ( await provider . codeVerifier ( ) ) . toBe ( "verifier-first" ) ;
861+ expect ( provider . redirectUrl ) . toBe ( "http://127.0.0.1:8989/oauth/callback" ) ;
862+ return "AUTHORIZED" ;
863+ } ) ;
864+ await expect (
865+ runMcpOAuthLogin ( {
866+ serverName : "Calendly" ,
867+ serverUrl : "https://mcp.calendly.com/" ,
868+ config : { redirectUrl : captured . redirectUrl } ,
869+ authorizationCode : "code-first" ,
870+ codeVerifier : captured . codeVerifier ,
871+ } ) ,
872+ ) . resolves . toBe ( "authorized" ) ;
873+ } ,
874+ {
875+ prefix : "openclaw-mcp-oauth-overlap-" ,
876+ skipSessionCleanup : true ,
877+ env : {
878+ OPENCLAW_CONFIG_PATH : undefined ,
879+ OPENCLAW_STATE_DIR : undefined ,
880+ } ,
881+ } ,
882+ ) ;
883+ } ) ;
884+
942885 it ( "does not start hidden authorization flows without an authorization callback" , async ( ) => {
943886 // Normal agent/tool execution must not open browser auth flows implicitly;
944887 // operators use the explicit mcp login command instead.
0 commit comments