@@ -181,6 +181,119 @@ mod session {
181181 }
182182 }
183183
184+ impl Serialize for SecretJwt {
185+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
186+ where
187+ S : serde:: Serializer ,
188+ {
189+ serializer. serialize_str ( self . expose_secret ( ) )
190+ }
191+ }
192+
193+ impl < ' de > Deserialize < ' de > for SecretJwt {
194+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
195+ where
196+ D : serde:: Deserializer < ' de > ,
197+ {
198+ let value = String :: deserialize ( deserializer) ?;
199+ Self :: parse ( value) . map_err ( serde:: de:: Error :: custom)
200+ }
201+ }
202+
203+ /// Trusted launch input delivered only to `openshell-supervisor`.
204+ #[ derive( Clone , Serialize , Deserialize ) ]
205+ #[ serde( deny_unknown_fields) ]
206+ pub struct SupervisorAuthBundle {
207+ pub session_id : SandboxSessionId ,
208+ pub gateway_token : SecretJwt ,
209+ pub gateway_expires_at : i64 ,
210+ pub sandbox_token : SecretJwt ,
211+ pub sandbox_expires_at : i64 ,
212+ pub credential_epoch : CredentialEpoch ,
213+ }
214+
215+ /// Gateway-created authentication input trusted by a compute driver.
216+ ///
217+ /// Drivers split this structure: the supervisor receives `supervisor`,
218+ /// while the sandbox receives only the gateway identity and public keys.
219+ #[ derive( Clone , Serialize , Deserialize ) ]
220+ #[ serde( deny_unknown_fields) ]
221+ pub struct SandboxLaunchAuthentication {
222+ pub supervisor : SupervisorAuthBundle ,
223+ pub gateway_id : String ,
224+ pub verification_keys : Vec < SessionVerificationKey > ,
225+ }
226+
227+ impl SandboxLaunchAuthentication {
228+ pub fn validate ( & self ) -> Result < ( ) , SessionJwtError > {
229+ self . supervisor . validate ( ) ?;
230+ validate_gateway_id ( & self . gateway_id ) ?;
231+ if self . verification_keys . is_empty ( ) {
232+ return Err ( SessionJwtError :: NoVerificationKeys ) ;
233+ }
234+ let mut key_ids = std:: collections:: BTreeSet :: new ( ) ;
235+ for key in & self . verification_keys {
236+ validate_key_id ( key. key_id . clone ( ) ) ?;
237+ if key. public_key_pem . is_empty ( ) {
238+ return Err ( SessionJwtError :: InvalidVerificationKey ) ;
239+ }
240+ if !key_ids. insert ( key. key_id . as_str ( ) ) {
241+ return Err ( SessionJwtError :: DuplicateKeyId ) ;
242+ }
243+ }
244+ Ok ( ( ) )
245+ }
246+ }
247+
248+ impl fmt:: Debug for SandboxLaunchAuthentication {
249+ fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
250+ formatter
251+ . debug_struct ( "SandboxLaunchAuthentication" )
252+ . field ( "supervisor" , & self . supervisor )
253+ . field ( "gateway_id" , & self . gateway_id )
254+ . field (
255+ "verification_key_ids" ,
256+ & self
257+ . verification_keys
258+ . iter ( )
259+ . map ( |key| key. key_id . as_str ( ) )
260+ . collect :: < Vec < _ > > ( ) ,
261+ )
262+ . finish ( )
263+ }
264+ }
265+
266+ impl SupervisorAuthBundle {
267+ pub fn validate ( & self ) -> Result < ( ) , SessionJwtError > {
268+ if self . gateway_expires_at <= 0 || self . sandbox_expires_at <= 0 {
269+ return Err ( SessionJwtError :: InvalidLifetime ) ;
270+ }
271+ Ok ( ( ) )
272+ }
273+
274+ pub fn sandbox_bearer_slot ( & self ) -> Result < SessionBearerTokenSlot , SessionJwtError > {
275+ SessionBearerTokenSlot :: new (
276+ self . sandbox_token . clone ( ) ,
277+ self . sandbox_expires_at ,
278+ self . credential_epoch ,
279+ )
280+ }
281+ }
282+
283+ impl fmt:: Debug for SupervisorAuthBundle {
284+ fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
285+ formatter
286+ . debug_struct ( "SupervisorAuthBundle" )
287+ . field ( "session_id" , & self . session_id )
288+ . field ( "gateway_token" , & "[REDACTED]" )
289+ . field ( "gateway_expires_at" , & self . gateway_expires_at )
290+ . field ( "sandbox_token" , & "[REDACTED]" )
291+ . field ( "sandbox_expires_at" , & self . sandbox_expires_at )
292+ . field ( "credential_epoch" , & self . credential_epoch )
293+ . finish ( )
294+ }
295+ }
296+
184297 #[ derive( Clone , Debug ) ]
185298 pub struct MintedSessionToken {
186299 pub token : SecretJwt ,
@@ -206,6 +319,7 @@ mod session {
206319 struct StoredBearer {
207320 token : SecretJwt ,
208321 expires_at : i64 ,
322+ credential_epoch : CredentialEpoch ,
209323 }
210324
211325 impl SessionBearerTokenSlot {
@@ -216,21 +330,40 @@ mod session {
216330 }
217331 }
218332
219- pub fn new ( token : SecretJwt , expires_at : i64 ) -> Result < Self , SessionJwtError > {
333+ pub fn new (
334+ token : SecretJwt ,
335+ expires_at : i64 ,
336+ credential_epoch : CredentialEpoch ,
337+ ) -> Result < Self , SessionJwtError > {
220338 let slot = Self :: empty ( ) ;
221- slot. update ( token, expires_at) ?;
339+ slot. update ( token, expires_at, credential_epoch ) ?;
222340 Ok ( slot)
223341 }
224342
225- pub fn update ( & self , token : SecretJwt , expires_at : i64 ) -> Result < ( ) , SessionJwtError > {
343+ pub fn update (
344+ & self ,
345+ token : SecretJwt ,
346+ expires_at : i64 ,
347+ credential_epoch : CredentialEpoch ,
348+ ) -> Result < ( ) , SessionJwtError > {
226349 if expires_at <= 0 {
227350 return Err ( SessionJwtError :: InvalidLifetime ) ;
228351 }
229- * self
352+ let mut stored = self
230353 . inner
231354 . write ( )
232- . unwrap_or_else ( std:: sync:: PoisonError :: into_inner) =
233- Some ( StoredBearer { token, expires_at } ) ;
355+ . unwrap_or_else ( std:: sync:: PoisonError :: into_inner) ;
356+ if stored
357+ . as_ref ( )
358+ . is_some_and ( |current| credential_epoch <= current. credential_epoch )
359+ {
360+ return Err ( SessionJwtError :: StaleCredentialEpoch ) ;
361+ }
362+ * stored = Some ( StoredBearer {
363+ token,
364+ expires_at,
365+ credential_epoch,
366+ } ) ;
234367 Ok ( ( ) )
235368 }
236369
@@ -250,6 +383,15 @@ mod session {
250383 . map ( |stored| stored. expires_at )
251384 }
252385
386+ #[ must_use]
387+ pub fn credential_epoch ( & self ) -> Option < CredentialEpoch > {
388+ self . inner
389+ . read ( )
390+ . unwrap_or_else ( std:: sync:: PoisonError :: into_inner)
391+ . as_ref ( )
392+ . map ( |stored| stored. credential_epoch )
393+ }
394+
253395 pub fn authorization_metadata (
254396 & self ,
255397 ) -> Result < tonic:: metadata:: AsciiMetadataValue , SessionJwtError > {
@@ -278,6 +420,7 @@ mod session {
278420 formatter
279421 . debug_struct ( "SessionBearerTokenSlot" )
280422 . field ( "expires_at" , & self . expires_at ( ) )
423+ . field ( "credential_epoch" , & self . credential_epoch ( ) )
281424 . finish_non_exhaustive ( )
282425 }
283426 }
@@ -401,6 +544,8 @@ mod session {
401544 }
402545
403546 /// One accepted public key from the immutable sandbox verification bundle.
547+ #[ derive( Clone , Serialize , Deserialize ) ]
548+ #[ serde( deny_unknown_fields) ]
404549 pub struct SessionVerificationKey {
405550 pub key_id : String ,
406551 pub public_key_pem : Vec < u8 > ,
@@ -541,6 +686,8 @@ mod session {
541686 InvalidGatewayId ,
542687 #[ error( "credential epoch must be positive" ) ]
543688 InvalidCredentialEpoch ,
689+ #[ error( "credential epoch does not advance the active credential" ) ]
690+ StaleCredentialEpoch ,
544691 #[ error( "key ID is invalid" ) ]
545692 InvalidKeyId ,
546693 #[ error( "verification key IDs must be unique" ) ]
@@ -765,6 +912,41 @@ mod tests {
765912 assert ! ( !debug. contains( pair. sandbox. token. expose_secret( ) ) ) ;
766913 }
767914
915+ #[ test]
916+ fn supervisor_auth_bundle_round_trips_without_exposing_secrets_in_debug ( ) {
917+ let ( issuer, _gateway, _sandbox, identity) = fixture ( ) ;
918+ let pair = issuer
919+ . mint_pair ( & identity, CredentialEpoch :: new ( 7 ) . expect ( "epoch" ) )
920+ . expect ( "token pair" ) ;
921+ let bundle = SupervisorAuthBundle {
922+ session_id : identity. session_id ,
923+ gateway_token : pair. gateway . token ,
924+ gateway_expires_at : pair. gateway . expires_at ,
925+ sandbox_token : pair. sandbox . token ,
926+ sandbox_expires_at : pair. sandbox . expires_at ,
927+ credential_epoch : pair. credential_epoch ,
928+ } ;
929+
930+ let encoded = serde_json:: to_vec ( & bundle) . expect ( "serialize auth bundle" ) ;
931+ let decoded: SupervisorAuthBundle =
932+ serde_json:: from_slice ( & encoded) . expect ( "deserialize auth bundle" ) ;
933+ assert_eq ! ( decoded. session_id, bundle. session_id) ;
934+ assert_eq ! ( decoded. credential_epoch, bundle. credential_epoch) ;
935+ assert_eq ! (
936+ decoded. gateway_token. expose_secret( ) ,
937+ bundle. gateway_token. expose_secret( )
938+ ) ;
939+ assert_eq ! (
940+ decoded. sandbox_token. expose_secret( ) ,
941+ bundle. sandbox_token. expose_secret( )
942+ ) ;
943+
944+ let debug = format ! ( "{bundle:?}" ) ;
945+ assert ! ( !debug. contains( bundle. gateway_token. expose_secret( ) ) ) ;
946+ assert ! ( !debug. contains( bundle. sandbox_token. expose_secret( ) ) ) ;
947+ assert_eq ! ( debug. matches( "[REDACTED]" ) . count( ) , 2 ) ;
948+ }
949+
768950 #[ derive( Serialize ) ]
769951 struct AudienceArrayClaims < ' a > {
770952 iss : & ' a str ,
0 commit comments