1515using System . Globalization ;
1616using System . IO ;
1717using System . Linq ;
18+ using System . Net . Http ;
19+ using System . Security . Claims ;
1820using System . Security . Cryptography . X509Certificates ;
21+ using System . Text . RegularExpressions ;
1922using System . Threading ;
2023using System . Threading . Tasks ;
2124
@@ -120,7 +123,9 @@ private static async Task<InteractiveBrowserCredential> GetInteractiveBrowserCre
120123 {
121124 if ( authContext is null )
122125 throw new AuthenticationException ( ErrorConstants . Message . MissingAuthContext ) ;
123- var interactiveOptions = IsWamSupported ( ) ? new InteractiveBrowserCredentialBrokerOptions ( WindowHandleUtlities . GetConsoleOrTerminalWindow ( ) ) : new InteractiveBrowserCredentialOptions ( ) ;
126+ var interactiveOptions = IsWamSupported ( ) ?
127+ new InteractiveBrowserCredentialBrokerOptions ( WindowHandleUtlities . GetConsoleOrTerminalWindow ( ) ) :
128+ new InteractiveBrowserCredentialOptions ( ) ;
124129 interactiveOptions . ClientId = authContext . ClientId ;
125130 interactiveOptions . TenantId = authContext . TenantId ?? "common" ;
126131 interactiveOptions . AuthorityHost = new Uri ( GetAuthorityUrl ( authContext ) ) ;
@@ -138,8 +143,21 @@ private static async Task<InteractiveBrowserCredential> GetInteractiveBrowserCre
138143 // Logic to implement ATPoP Authentication
139144 authRecord = await Task . Run ( ( ) =>
140145 {
146+ // Creating a Request to retrieve nonce value
147+ string popNonce = null ;
148+ var popNonceToken = "nonce=\" " ;
149+ Uri resourceUri = new Uri ( "https://canary.graph.microsoft.com/beta/me" ) ; //PPE (https://graph.microsoft-ppe.com) or Canary (https://canary.graph.microsoft.com) or (https://20.190.132.47/beta/me)
150+ HttpClient httpClient = new ( new HttpClientHandler { ServerCertificateCustomValidationCallback = ( _ , _ , _ , _ ) => true } ) ;
151+ HttpResponseMessage response = httpClient . SendAsync ( new HttpRequestMessage ( HttpMethod . Get , resourceUri ) ) . Result ;
152+
153+ // Find the WWW-Authenticate header in the response.
154+ var popChallenge = response . Headers . WwwAuthenticate . First ( wa => wa . Scheme == "PoP" ) ;
155+ var nonceStart = popChallenge . Parameter . IndexOf ( popNonceToken ) + popNonceToken . Length ;
156+ var nonceEnd = popChallenge . Parameter . IndexOf ( '"' , nonceStart ) ;
157+ popNonce = popChallenge . Parameter . Substring ( nonceStart , nonceEnd - nonceStart ) ;
158+
159+ // Refresh token logic --- start
141160 var popTokenAuthenticationPolicy = new PopTokenAuthenticationPolicy ( interactiveBrowserCredential as ISupportsProofOfPossession , $ "https://graph.microsoft.com/.default") ;
142-
143161 var pipelineOptions = new HttpPipelineOptions ( new PopClientOptions ( )
144162 {
145163 Diagnostics =
@@ -151,16 +169,19 @@ private static async Task<InteractiveBrowserCredential> GetInteractiveBrowserCre
151169 pipelineOptions . PerRetryPolicies . Add ( popTokenAuthenticationPolicy ) ;
152170
153171 var _pipeline = HttpPipelineBuilder . Build ( pipelineOptions , new HttpPipelineTransportOptions { ServerCertificateCustomValidationCallback = ( _ ) => true } ) ;
172+
154173 using var request = _pipeline . CreateRequest ( ) ;
155174 request . Method = RequestMethod . Get ;
156- request . Uri . Reset ( new Uri ( "https://20.190.132.47/beta/me" ) ) ;
157- var response = _pipeline . SendRequest ( request , cancellationToken ) ;
158- var message = new HttpMessage ( request , new ResponseClassifier ( ) ) ;
159-
175+ request . Uri . Reset ( resourceUri ) ;
176+
160177 // Manually invoke the authentication policy's process method
161- popTokenAuthenticationPolicy . ProcessAsync ( message , ReadOnlyMemory < HttpPipelinePolicy > . Empty ) ;
178+ popTokenAuthenticationPolicy . ProcessAsync ( new HttpMessage ( request , new ResponseClassifier ( ) ) , ReadOnlyMemory < HttpPipelinePolicy > . Empty ) ;
179+ // Refresh token logic --- end
180+
162181 // Run the thread in MTA.
163- return interactiveBrowserCredential . Authenticate ( new TokenRequestContext ( authContext . Scopes ) , cancellationToken ) ;
182+ var popContext = new PopTokenRequestContext ( authContext . Scopes , isProofOfPossessionEnabled : true , proofOfPossessionNonce : popNonce , request : request ) ;
183+ //var token = interactiveBrowserCredential.GetToken(popContext, cancellationToken);
184+ return interactiveBrowserCredential . Authenticate ( popContext , cancellationToken ) ;
164185 } ) ;
165186 }
166187 else
0 commit comments