@@ -67,7 +67,8 @@ impl CredentialRuntime {
6767 . await ?;
6868 handles
6969 . remove ( "gateway-access-token" )
70- . ok_or_else ( || Status :: internal ( "application credential handle missing" ) )
70+ . filter ( |handle| !handle. handle . is_empty ( ) )
71+ . ok_or_else ( || Status :: internal ( "empty or missing application credential handle" ) )
7172 }
7273
7374 pub async fn resolve_application_token (
@@ -452,6 +453,76 @@ mod tests {
452453 }
453454 }
454455
456+ #[ tokio:: test]
457+ async fn application_tokens_preserve_issuer_scope_and_backend_ownership ( ) {
458+ let storage = tempfile:: tempdir ( ) . unwrap ( ) ;
459+ let key_path = storage. path ( ) . join ( "key-encryption-key.bin" ) ;
460+ let store = Arc :: new ( crate :: persistence:: test_store ( ) . await ) ;
461+ let file = config_file ( & format ! (
462+ "[openshell.gateway.credential_storage]\n key_encryption_key_path = {}\n " ,
463+ toml_path( & key_path) ,
464+ ) ) ;
465+ let runtime =
466+ CredentialRuntime :: from_config_file_with_store ( & Config :: new ( None ) , Some ( & file) , store)
467+ . await
468+ . unwrap ( ) ;
469+ let handle = runtime
470+ . store_application_token ( "workspace-a" , "session-a" , "session-secret" )
471+ . await
472+ . unwrap ( ) ;
473+ assert_eq ! (
474+ runtime
475+ . resolve_application_token( "workspace-a" , "session-a" , handle. clone( ) )
476+ . await
477+ . unwrap( ) ,
478+ "session-secret"
479+ ) ;
480+ // Storage validates provider/key ownership; the gateway retains
481+ // workspace/session authorization before resolving its private handle.
482+ for ( provider, key) in [
483+ ( "another-provider" , "gateway-access-token" ) ,
484+ ( "gateway-service-session" , "another-key" ) ,
485+ ] {
486+ assert ! (
487+ runtime
488+ . resolve_secrets(
489+ SecretOwner :: provider( "workspace-a" , "service-session-session-a" , provider) ,
490+ & Handles :: from( [ ( key. into( ) , handle. clone( ) ) ] ) ,
491+ openshell_core:: time:: now_ms( ) ,
492+ )
493+ . await
494+ . is_err( )
495+ ) ;
496+ }
497+ // The explicit owner kind must preserve the already deployed driver scope.
498+ let resolved = runtime
499+ . resolve_secrets (
500+ SecretOwner :: provider (
501+ "workspace-a" ,
502+ "service-session-session-a" ,
503+ "gateway-service-session" ,
504+ ) ,
505+ & Handles :: from ( [ ( "gateway-access-token" . into ( ) , handle. clone ( ) ) ] ) ,
506+ openshell_core:: time:: now_ms ( ) ,
507+ )
508+ . await
509+ . unwrap ( ) ;
510+ assert_eq ! (
511+ resolved. values. get( "gateway-access-token" ) . unwrap( ) ,
512+ "session-secret"
513+ ) ;
514+ runtime
515+ . delete_application_token ( "workspace-a" , "session-a" , handle. clone ( ) )
516+ . await
517+ . unwrap ( ) ;
518+ assert ! (
519+ runtime
520+ . resolve_application_token( "workspace-a" , "session-a" , handle)
521+ . await
522+ . is_err( )
523+ ) ;
524+ }
525+
455526 #[ tokio:: test]
456527 async fn embedded_test_hooks_share_the_trait_backed_issuer_across_clones ( ) {
457528 let runtime = CredentialRuntime :: from_config (
0 commit comments