@@ -466,6 +466,7 @@ export class EnvironmentAuth extends Context.Service<
466466 readonly createBrowserSession : (
467467 credential : string ,
468468 requestMetadata : AuthClientMetadata ,
469+ previousSessionToken ?: string ,
469470 ) => Effect . Effect <
470471 {
471472 readonly response : AuthBrowserSessionResult ;
@@ -786,12 +787,11 @@ export const make = Effect.gen(function* () {
786787 Effect . withSpan ( "EnvironmentAuth.getSessionState" ) ,
787788 ) ;
788789
789- const createBrowserSession : EnvironmentAuth [ "Service" ] [ "createBrowserSession" ] = (
790- credential ,
791- requestMetadata ,
792- ) => {
790+ const createBrowserSession : EnvironmentAuth [ "Service" ] [ "createBrowserSession" ] = Effect . fn (
791+ "EnvironmentAuth.createBrowserSession" ,
792+ ) ( function * ( credential , requestMetadata , previousSessionToken ) {
793793 if ( devAuth ?. matches ( credential ) ) {
794- return sessions . verify ( credential ) . pipe (
794+ return yield * sessions . verify ( credential ) . pipe (
795795 mapSessionVerificationErrors ,
796796 Effect . flatMap ( ( session ) =>
797797 DateTime . now . pipe (
@@ -814,38 +814,40 @@ export const make = Effect.gen(function* () {
814814 Effect . withSpan ( "EnvironmentAuth.createBrowserSession" ) ,
815815 ) ;
816816 }
817- return bootstrapCredentials . consume ( credential ) . pipe (
818- Effect . mapError ( toBootstrapExchangeError ) ,
819- Effect . flatMap ( ( grant ) =>
820- sessions
821- . issue ( {
822- method : "browser-session-cookie" ,
823- subject : grant . subject ,
824- scopes : grant . scopes ,
825- client : {
826- ...requestMetadata ,
827- ...( grant . label ? { label : grant . label } : { } ) ,
828- } ,
829- } )
830- . pipe (
831- Effect . mapError ( ( cause ) => new ServerAuthAuthenticatedSessionIssueError ( { cause } ) ) ,
832- ) ,
833- ) ,
834- Effect . map (
835- ( session ) =>
836- ( {
837- response : {
838- authenticated : true ,
839- scopes : session . scopes ,
840- sessionMethod : session . method ,
841- expiresAt : DateTime . toUtc ( session . expiresAt ) ,
842- } satisfies AuthBrowserSessionResult ,
843- sessionToken : session . token ,
844- } ) satisfies BootstrapExchangeResult ,
845- ) ,
846- Effect . withSpan ( "EnvironmentAuth.createBrowserSession" ) ,
847- ) ;
848- } ;
817+ const previousSession =
818+ previousSessionToken === undefined
819+ ? undefined
820+ : yield * sessions . verify ( previousSessionToken ) . pipe (
821+ Effect . catchIf ( SessionStore . isSessionCredentialInvalidError , ( ) => Effect . void ) ,
822+ Effect . mapError ( ( cause ) => new ServerAuthSessionCredentialValidationError ( { cause } ) ) ,
823+ ) ;
824+ const grant = yield * bootstrapCredentials
825+ . consume ( credential )
826+ . pipe ( Effect . mapError ( toBootstrapExchangeError ) ) ;
827+ const session = yield * sessions
828+ . issue ( {
829+ method : "browser-session-cookie" ,
830+ subject : grant . subject ,
831+ scopes : grant . scopes ,
832+ ...( previousSession ?. method === "browser-session-cookie"
833+ ? { replaceSessionId : previousSession . sessionId }
834+ : { } ) ,
835+ client : {
836+ ...requestMetadata ,
837+ ...( grant . label ? { label : grant . label } : { } ) ,
838+ } ,
839+ } )
840+ . pipe ( Effect . mapError ( ( cause ) => new ServerAuthAuthenticatedSessionIssueError ( { cause } ) ) ) ;
841+ return {
842+ response : {
843+ authenticated : true ,
844+ scopes : session . scopes ,
845+ sessionMethod : session . method ,
846+ expiresAt : DateTime . toUtc ( session . expiresAt ) ,
847+ } satisfies AuthBrowserSessionResult ,
848+ sessionToken : session . token ,
849+ } satisfies BootstrapExchangeResult ;
850+ } ) ;
849851
850852 type ResolvedBootstrapGrant = Pick <
851853 PairingGrantStore . BootstrapGrant ,
@@ -855,15 +857,24 @@ export const make = Effect.gen(function* () {
855857 } ;
856858 const resolveBootstrapGrant = (
857859 credential : string ,
858- input ?: { readonly proofKeyThumbprint ?: string } ,
860+ input ?: {
861+ readonly proofKeyThumbprint ?: string ;
862+ readonly requestedScopes ?: ReadonlyArray < AuthEnvironmentScope > ;
863+ } ,
859864 ) : Effect . Effect <
860865 ResolvedBootstrapGrant ,
861- ServerAuthInvalidCredentialError | ServerAuthInternalError
866+ ServerAuthInvalidCredentialError | ServerAuthInternalError | ServerAuthScopeNotGrantedError
862867 > => {
863868 if ( ! devAuth ?. matches ( credential ) ) {
864869 return bootstrapCredentials
865870 . consume ( credential , input )
866- . pipe ( Effect . mapError ( toBootstrapExchangeError ) ) ;
871+ . pipe (
872+ Effect . mapError ( ( cause ) =>
873+ cause . _tag === "BootstrapCredentialScopeNotGrantedError"
874+ ? new ServerAuthScopeNotGrantedError ( { } )
875+ : toBootstrapExchangeError ( cause ) ,
876+ ) ,
877+ ) ;
867878 }
868879 return sessions . verify ( credential ) . pipe (
869880 mapSessionVerificationErrors ,
@@ -879,14 +890,15 @@ export const make = Effect.gen(function* () {
879890 } ;
880891
881892 const exchangeBootstrapCredentialForAccessToken : EnvironmentAuth [ "Service" ] [ "exchangeBootstrapCredentialForAccessToken" ] =
882- ( credential , requestedScopes , requestMetadata , input ) =>
883- resolveBootstrapGrant ( credential , input ) . pipe (
893+ ( credential , requestedScopesInput , requestMetadata , input ) => {
894+ const requestedScopes = requestedScopesInput ?. length ? requestedScopesInput : undefined ;
895+ return resolveBootstrapGrant ( credential , {
896+ ...input ,
897+ ...( requestedScopes !== undefined ? { requestedScopes } : { } ) ,
898+ } ) . pipe (
884899 Effect . flatMap ( ( grant ) =>
885900 Effect . gen ( function * ( ) {
886901 const grantedScopes = requestedScopes ?? grant . scopes ;
887- if ( ! grantedScopes . every ( ( scope ) => grant . scopes . includes ( scope ) ) ) {
888- return yield * new ServerAuthScopeNotGrantedError ( { } ) ;
889- }
890902 return yield * sessions
891903 . issue ( {
892904 method : input ?. proofKeyThumbprint ? "dpop-access-token" : "bearer-access-token" ,
@@ -934,6 +946,7 @@ export const make = Effect.gen(function* () {
934946 ) ,
935947 Effect . withSpan ( "EnvironmentAuth.exchangeBootstrapCredentialForAccessToken" ) ,
936948 ) ;
949+ } ;
937950
938951 const issuePairingCredentialForSubject = ( input : {
939952 readonly scopes : ReadonlyArray < AuthEnvironmentScope > ;
@@ -1238,6 +1251,8 @@ export const make = Effect.gen(function* () {
12381251 Effect . catchTags ( {
12391252 ServerAuthInvalidCredentialError : ( ) =>
12401253 Effect . fail ( new ServerAuthMcpApprovalCodeError ( { reason : "unknown_or_used" } ) ) ,
1254+ ServerAuthScopeNotGrantedError : ( ) =>
1255+ Effect . fail ( new ServerAuthMcpApprovalCodeError ( { reason : "insufficient_scope" } ) ) ,
12411256 } ) ,
12421257 Effect . flatMap ( ( grant ) =>
12431258 grant . method !== "one-time-token" && grant . method !== "reusable-dev-token"
0 commit comments