1616using System . IO ;
1717using System . Linq ;
1818using System . Net . Http ;
19+ using System . Net . Http . Headers ;
1920using System . Security . Cryptography . X509Certificates ;
2021using System . Threading ;
2122using System . Threading . Tasks ;
@@ -88,12 +89,6 @@ private static bool IsWamSupported()
8889 return GraphSession . Instance . GraphOption . EnableWAMForMSGraph && SharedUtilities . IsWindowsPlatform ( ) ;
8990 }
9091
91- //Check to see if ATPoP is Supported
92- public static bool IsATPoPSupported ( )
93- {
94- return GraphSession . Instance . GraphOption . EnableATPoPForMSGraph ;
95- }
96-
9792 private static async Task < TokenCredential > GetClientSecretCredentialAsync ( IAuthContext authContext )
9893 {
9994 if ( authContext is null )
@@ -130,27 +125,24 @@ private static async Task<InteractiveBrowserCredential> GetInteractiveBrowserCre
130125 interactiveOptions . TokenCachePersistenceOptions = GetTokenCachePersistenceOptions ( authContext ) ;
131126
132127 var interactiveBrowserCredential = new InteractiveBrowserCredential ( interactiveOptions ) ;
133- if ( IsATPoPSupported ( ) )
128+ if ( GraphSession . Instance . GraphOption . EnableATPoPForMSGraph )
134129 {
135- GraphSession . Instance . GraphRequestProofofPossession . PopTokenContext = CreatePopTokenRequestContext ( authContext ) ;
136- GraphSession . Instance . GraphRequestProofofPossession . BrowserCredential = interactiveBrowserCredential ;
130+ GraphSession . Instance . GraphRequestPopContext . PopTokenContext = await CreatePopTokenRequestContext ( authContext ) ;
131+ GraphSession . Instance . GraphRequestPopContext . PopInteractiveBrowserCredential = interactiveBrowserCredential ;
137132 }
138133
139134 if ( ! File . Exists ( Constants . AuthRecordPath ) )
140135 {
141136 AuthenticationRecord authRecord ;
142- //var interactiveBrowserCredential = new InteractiveBrowserCredential(interactiveOptions);
143137 if ( IsWamSupported ( ) )
144138 {
145139 // Adding a scenario to account for Access Token Proof of Possession
146- if ( IsATPoPSupported ( ) )
140+ if ( GraphSession . Instance . GraphOption . EnableATPoPForMSGraph )
147141 {
148- // Logic to implement ATPoP Authentication
149142 authRecord = await Task . Run ( ( ) =>
150143 {
151144 // Run the thread in MTA.
152- //GraphSession.Instance.GraphRequestProofofPossession.AccessToken = interactiveBrowserCredential.GetTokenAsync(GraphSession.Instance.GraphRequestProofofPossession.PopTokenContext, cancellationToken).Result;
153- return interactiveBrowserCredential . AuthenticateAsync ( GraphSession . Instance . GraphRequestProofofPossession . PopTokenContext , cancellationToken ) ;
145+ return interactiveBrowserCredential . AuthenticateAsync ( GraphSession . Instance . GraphRequestPopContext . PopTokenContext , cancellationToken ) ;
154146 } ) ;
155147 }
156148 else
@@ -478,19 +470,16 @@ public static Task DeleteAuthRecordAsync()
478470 return Task . CompletedTask ;
479471 }
480472
481- public static PopTokenRequestContext CreatePopTokenRequestContext ( IAuthContext authContext )
473+ private static async Task < PopTokenRequestContext > CreatePopTokenRequestContext ( IAuthContext authContext )
482474 {
483475 // Creating a httpclient that would handle all pop calls
484- Uri popResourceUri = GraphSession . Instance . GraphRequestProofofPossession . Uri ?? new Uri ( "https://graph.microsoft.com/beta/organization" ) ; //PPE (https://graph.microsoft-ppe.com) or Canary (https://canary.graph.microsoft.com) or (https://20.190.132.47/beta/me)
476+ Uri popResourceUri = GraphSession . Instance . GraphRequestPopContext . Uri ?? new Uri ( "https://graph.microsoft.com/beta/organization" ) ; //PPE (https://graph.microsoft-ppe.com) or Canary (https://canary.graph.microsoft.com) or (https://20.190.132.47/beta/me)
485477 HttpClient popHttpClient = new ( new HttpClientHandler ( ) ) ;
486478
487- // Find the WWW-Authenticate header in the response.
488- var popMethod = GraphSession . Instance . GraphRequestProofofPossession . HttpMethod ?? HttpMethod . Get ;
489- var popResponse = popHttpClient . SendAsync ( new HttpRequestMessage ( popMethod , popResourceUri ) ) . Result ;
490- var popChallenge = popResponse . Headers . WwwAuthenticate . First ( wa => wa . Scheme == "PoP" ) ;
491- var nonceStart = popChallenge . Parameter . IndexOf ( "nonce=\" " ) + "nonce=\" " . Length ;
492- var nonceEnd = popChallenge . Parameter . IndexOf ( '"' , nonceStart ) ;
493- GraphSession . Instance . GraphRequestProofofPossession . ProofofPossessionNonce = popChallenge . Parameter . Substring ( nonceStart , nonceEnd - nonceStart ) ;
479+ // Find the nonce in the WWW-Authenticate header in the response.
480+ var popMethod = GraphSession . Instance . GraphRequestPopContext . HttpMethod ?? HttpMethod . Get ;
481+ var popResponse = await popHttpClient . SendAsync ( new HttpRequestMessage ( popMethod , popResourceUri ) ) ;
482+ GraphSession . Instance . GraphRequestPopContext . ProofofPossessionNonce = WwwAuthenticateParameters . CreateFromAuthenticationHeaders ( popResponse . Headers , "Pop" ) . Nonce ;
494483
495484 // Refresh token logic --- start
496485 var popPipelineOptions = new HttpPipelineOptions ( new PopClientOptions ( )
@@ -499,12 +488,12 @@ public static PopTokenRequestContext CreatePopTokenRequestContext(IAuthContext a
499488 } ) ;
500489
501490 var _popPipeline = HttpPipelineBuilder . Build ( popPipelineOptions , new HttpPipelineTransportOptions ( ) ) ;
502- GraphSession . Instance . GraphRequestProofofPossession . Request = _popPipeline . CreateRequest ( ) ;
503- GraphSession . Instance . GraphRequestProofofPossession . Request . Method = ConvertToAzureRequestMethod ( popMethod ) ;
504- GraphSession . Instance . GraphRequestProofofPossession . Request . Uri . Reset ( popResourceUri ) ;
491+ GraphSession . Instance . GraphRequestPopContext . Request = _popPipeline . CreateRequest ( ) ;
492+ GraphSession . Instance . GraphRequestPopContext . Request . Method = ConvertToAzureRequestMethod ( popMethod ) ;
493+ GraphSession . Instance . GraphRequestPopContext . Request . Uri . Reset ( popResourceUri ) ;
505494
506495 // Refresh token logic --- end
507- var popContext = new PopTokenRequestContext ( authContext . Scopes , isProofOfPossessionEnabled : true , proofOfPossessionNonce : GraphSession . Instance . GraphRequestProofofPossession . ProofofPossessionNonce , request : GraphSession . Instance . GraphRequestProofofPossession . Request ) ;
496+ var popContext = new PopTokenRequestContext ( authContext . Scopes , isProofOfPossessionEnabled : true , proofOfPossessionNonce : GraphSession . Instance . GraphRequestPopContext . ProofofPossessionNonce , request : GraphSession . Instance . GraphRequestPopContext . Request ) ;
508497 return popContext ;
509498 }
510499 public static RequestMethod ConvertToAzureRequestMethod ( HttpMethod httpMethod )
0 commit comments