@@ -557,14 +557,54 @@ private string HandleBearerToken(string? bearerToken, ref string user, ref bool
557557 if ( doc . RootElement . TryGetProperty ( "access_token" , out var tokenElement ) )
558558 {
559559 bearerToken = tokenElement . GetString ( ) ;
560- Console . WriteLine ( "token exchange " + bearerToken ) ;
560+ Console . WriteLine ( "token exchange 2 " + bearerToken ) ;
561561 jwtSecurityToken = handler . ReadJwtToken ( bearerToken ) ;
562562
563563 iss = "" ;
564564 issClaim = jwtSecurityToken . Claims . Where ( c => c . Type == "iss" ) ;
565565 if ( issClaim . Any ( ) )
566566 {
567567 iss = issClaim . First ( ) . Value ;
568+
569+ var tokenExchange3 = System . Environment . GetEnvironmentVariable ( "TOKENEXCHANGE3" ) ;
570+
571+ if ( ! string . IsNullOrEmpty ( tokenExchange3 ) )
572+ {
573+ var audClaim = jwtSecurityToken . Claims . Where ( c => c . Type == "aud" ) ;
574+
575+ if ( audClaim . Any ( ) )
576+ {
577+ parameters = new Dictionary < string , string >
578+ {
579+ { "grant_type" , "urn:ietf:params:oauth:grant-type:token-exchange" } ,
580+ { "subject_token_type" , "urn:ietf:params:oauth:token-type:jwt" } ,
581+ { "requested_token_type" , "urn:ietf:params:oauth:token-type:access_token" } ,
582+ { "subject_token" , bearerToken }
583+ } ;
584+ request = new HttpRequestMessage ( HttpMethod . Post , $ "{ tokenExchange3 } /token")
585+ {
586+ Content = new FormUrlEncodedContent ( parameters )
587+ } ;
588+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/x-www-form-urlencoded" ) ;
589+
590+ response = client . SendAsync ( request ) ;
591+ content = response . GetAwaiter ( ) . GetResult ( ) . Content . ContentToString ( ) ;
592+
593+ bearerToken = "" ;
594+ jwtSecurityToken = null ;
595+ doc = JsonDocument . Parse ( content ) ;
596+ if ( doc . RootElement . TryGetProperty ( "access_token" , out var tokenElement2 ) )
597+ {
598+ bearerToken = tokenElement2 . GetString ( ) ;
599+ Console . WriteLine ( "token exchange 3 (with local idP) " + bearerToken ) ;
600+ jwtSecurityToken = handler . ReadJwtToken ( bearerToken ) ;
601+
602+ issClaim = jwtSecurityToken . Claims . Where ( c => c . Type == "iss" ) ;
603+
604+ iss = issClaim . First ( ) . Value ;
605+ }
606+ }
607+ }
568608 }
569609 }
570610 if ( jwtSecurityToken == null )
0 commit comments