15
15
using System . Globalization ;
16
16
using System . IO ;
17
17
using System . Linq ;
18
+ using System . Net . Http ;
19
+ using System . Security . Claims ;
18
20
using System . Security . Cryptography . X509Certificates ;
21
+ using System . Text . RegularExpressions ;
19
22
using System . Threading ;
20
23
using System . Threading . Tasks ;
21
24
@@ -120,7 +123,9 @@ private static async Task<InteractiveBrowserCredential> GetInteractiveBrowserCre
120
123
{
121
124
if ( authContext is null )
122
125
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 ( ) ;
124
129
interactiveOptions . ClientId = authContext . ClientId ;
125
130
interactiveOptions . TenantId = authContext . TenantId ?? "common" ;
126
131
interactiveOptions . AuthorityHost = new Uri ( GetAuthorityUrl ( authContext ) ) ;
@@ -138,8 +143,21 @@ private static async Task<InteractiveBrowserCredential> GetInteractiveBrowserCre
138
143
// Logic to implement ATPoP Authentication
139
144
authRecord = await Task . Run ( ( ) =>
140
145
{
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
141
160
var popTokenAuthenticationPolicy = new PopTokenAuthenticationPolicy ( interactiveBrowserCredential as ISupportsProofOfPossession , $ "https://graph.microsoft.com/.default") ;
142
-
143
161
var pipelineOptions = new HttpPipelineOptions ( new PopClientOptions ( )
144
162
{
145
163
Diagnostics =
@@ -151,16 +169,19 @@ private static async Task<InteractiveBrowserCredential> GetInteractiveBrowserCre
151
169
pipelineOptions . PerRetryPolicies . Add ( popTokenAuthenticationPolicy ) ;
152
170
153
171
var _pipeline = HttpPipelineBuilder . Build ( pipelineOptions , new HttpPipelineTransportOptions { ServerCertificateCustomValidationCallback = ( _ ) => true } ) ;
172
+
154
173
using var request = _pipeline . CreateRequest ( ) ;
155
174
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
+
160
177
// 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
+
162
181
// 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 ) ;
164
185
} ) ;
165
186
}
166
187
else
0 commit comments