@@ -19,6 +19,7 @@ import {
1919 isRejectedSecondaryBootstrap ,
2020 nextRejectedSecondaryBootstrap ,
2121 primaryRegistrationToRetainAfterTopologyRead ,
22+ prepareDesktopSshEnvironment ,
2223 provisionDesktopSshEnvironment ,
2324 readPrimaryEnvironmentTargetResult ,
2425 secondaryRegistrationsToRetainAfterTopologyRead ,
@@ -35,16 +36,30 @@ const TARGET: DesktopSshEnvironmentTarget = {
3536
3637function makeBridge (
3738 calls : string [ ] ,
38- options ?: { readonly failDescriptor ?: boolean } ,
39+ options ?: {
40+ readonly failDescriptor ?: boolean ;
41+ readonly savedTokenRejected ?: boolean ;
42+ readonly savedTokenPermissions ?: ReadonlyArray < string > ;
43+ } ,
3944) : DesktopBridge {
4045 return {
41- ensureSshEnvironment : async ( target : DesktopSshEnvironmentTarget ) => {
46+ ensureSshEnvironment : async (
47+ target : DesktopSshEnvironmentTarget ,
48+ ensureOptions ?: { issuePairingToken ?: boolean } ,
49+ ) => {
4250 calls . push ( "ensure" ) ;
4351 return {
4452 target,
4553 httpBaseUrl : "http://127.0.0.1:3201/" ,
4654 wsBaseUrl : "ws://127.0.0.1:3201/" ,
47- pairingToken : "pairing-token" ,
55+ pairingToken : ensureOptions ?. issuePairingToken === true ? "pairing-token" : null ,
56+ } ;
57+ } ,
58+ fetchSshSessionState : async ( ) => {
59+ calls . push ( "session" ) ;
60+ return {
61+ authenticated : options ?. savedTokenRejected !== true ,
62+ permissions : options ?. savedTokenPermissions ?? AuthStandardClientScopes ,
4863 } ;
4964 } ,
5065 fetchSshEnvironmentDescriptor : async ( ) => {
@@ -117,6 +132,74 @@ describe("desktop SSH pairing", () => {
117132 expect ( calls ) . toEqual ( [ "ensure" , "descriptor" ] ) ;
118133 } ) ,
119134 ) ;
135+
136+ it . effect ( "reconnects with the saved token while it holds every standard scope" , ( ) =>
137+ Effect . gen ( function * ( ) {
138+ const calls : string [ ] = [ ] ;
139+
140+ const prepared = yield * prepareDesktopSshEnvironment ( makeBridge ( calls ) , {
141+ target : TARGET ,
142+ expectedEnvironmentId : EnvironmentId . make ( "environment-ssh" ) ,
143+ bearerToken : "saved-token" ,
144+ } ) ;
145+
146+ expect ( prepared . bearerToken ) . toBe ( "saved-token" ) ;
147+ expect ( calls ) . toEqual ( [ "ensure" , "descriptor" , "session" ] ) ;
148+ } ) ,
149+ ) ;
150+
151+ it . effect ( "pairs again when the host no longer accepts the saved token" , ( ) =>
152+ Effect . gen ( function * ( ) {
153+ const calls : string [ ] = [ ] ;
154+
155+ const prepared = yield * prepareDesktopSshEnvironment (
156+ makeBridge ( calls , { savedTokenRejected : true } ) ,
157+ {
158+ target : TARGET ,
159+ expectedEnvironmentId : EnvironmentId . make ( "environment-ssh" ) ,
160+ bearerToken : "saved-token" ,
161+ } ,
162+ ) ;
163+
164+ expect ( prepared . bearerToken ) . toBe ( "bearer-token" ) ;
165+ expect ( calls ) . toEqual ( [ "ensure" , "descriptor" , "session" , "ensure" , "descriptor" , "token" ] ) ;
166+ } ) ,
167+ ) ;
168+
169+ it . effect ( "pairs again when the saved token predates a standard scope" , ( ) =>
170+ Effect . gen ( function * ( ) {
171+ const calls : string [ ] = [ ] ;
172+
173+ const prepared = yield * prepareDesktopSshEnvironment (
174+ makeBridge ( calls , {
175+ savedTokenPermissions : [ "orchestration:read" , "orchestration:operate" ] ,
176+ } ) ,
177+ {
178+ target : TARGET ,
179+ expectedEnvironmentId : EnvironmentId . make ( "environment-ssh" ) ,
180+ bearerToken : "saved-token" ,
181+ } ,
182+ ) ;
183+
184+ expect ( prepared . bearerToken ) . toBe ( "bearer-token" ) ;
185+ expect ( calls ) . toEqual ( [ "ensure" , "descriptor" , "session" , "ensure" , "descriptor" , "token" ] ) ;
186+ } ) ,
187+ ) ;
188+
189+ it . effect ( "stops before sending the saved token or pairing on a different machine" , ( ) =>
190+ Effect . gen ( function * ( ) {
191+ const calls : string [ ] = [ ] ;
192+
193+ const error = yield * prepareDesktopSshEnvironment ( makeBridge ( calls ) , {
194+ target : TARGET ,
195+ expectedEnvironmentId : EnvironmentId . make ( "some-other-machine" ) ,
196+ bearerToken : "saved-token" ,
197+ } ) . pipe ( Effect . flip ) ;
198+
199+ expect ( error ) . toMatchObject ( { reason : "configuration" } ) ;
200+ expect ( calls ) . toEqual ( [ "ensure" , "descriptor" ] ) ;
201+ } ) ,
202+ ) ;
120203} ) ;
121204
122205describe ( "desktop-local bearer cache" , ( ) => {
0 commit comments