diff --git a/src/Authentication/Authentication.Core/Interfaces/IGraphRequestPopContext.cs b/src/Authentication/Authentication.Core/Interfaces/IGraphRequestPopContext.cs index 7a154ea9f4..61551edb31 100644 --- a/src/Authentication/Authentication.Core/Interfaces/IGraphRequestPopContext.cs +++ b/src/Authentication/Authentication.Core/Interfaces/IGraphRequestPopContext.cs @@ -3,6 +3,7 @@ // ------------------------------------------------------------------------------ using Azure.Core; +using Azure.Core.Pipeline; using Azure.Identity; using System; using System.Net.Http; @@ -14,8 +15,7 @@ public interface IGraphRequestPopContext Uri Uri { get; set; } HttpMethod HttpMethod { get; set; } AccessToken AccessToken { get; set; } - PopTokenRequestContext PopTokenContext { get; set; } - Request Request { get; set; } + HttpPipeline PopPipeline { get; set; } InteractiveBrowserCredential PopInteractiveBrowserCredential { get; set; } } } \ No newline at end of file diff --git a/src/Authentication/Authentication.Core/Utilities/AuthenticationHelpers.cs b/src/Authentication/Authentication.Core/Utilities/AuthenticationHelpers.cs index a00dd5532e..1389bbb821 100644 --- a/src/Authentication/Authentication.Core/Utilities/AuthenticationHelpers.cs +++ b/src/Authentication/Authentication.Core/Utilities/AuthenticationHelpers.cs @@ -125,9 +125,10 @@ private static async Task GetInteractiveBrowserCre interactiveOptions.TokenCachePersistenceOptions = GetTokenCachePersistenceOptions(authContext); var interactiveBrowserCredential = new InteractiveBrowserCredential(interactiveOptions); + var popTokenRequestContext = new PopTokenRequestContext(); if (GraphSession.Instance.GraphOption.EnableATPoPForMSGraph) { - GraphSession.Instance.GraphRequestPopContext.PopTokenContext = await CreatePopTokenRequestContext(authContext); + popTokenRequestContext = await CreatePopTokenRequestContext(authContext); GraphSession.Instance.GraphRequestPopContext.PopInteractiveBrowserCredential = interactiveBrowserCredential; } @@ -142,7 +143,7 @@ private static async Task GetInteractiveBrowserCre authRecord = await Task.Run(() => { // Run the thread in MTA. - return interactiveBrowserCredential.AuthenticateAsync(GraphSession.Instance.GraphRequestPopContext.PopTokenContext, cancellationToken); + return interactiveBrowserCredential.AuthenticateAsync(popTokenRequestContext, cancellationToken); }); } else @@ -486,13 +487,13 @@ private static async Task CreatePopTokenRequestContext(I }); - var _popPipeline = HttpPipelineBuilder.Build(popPipelineOptions, new HttpPipelineTransportOptions()); - GraphSession.Instance.GraphRequestPopContext.Request = _popPipeline.CreateRequest(); - GraphSession.Instance.GraphRequestPopContext.Request.Method = RequestMethod.Parse(popMethod.Method.ToUpper()); - GraphSession.Instance.GraphRequestPopContext.Request.Uri.Reset(popResourceUri); + GraphSession.Instance.GraphRequestPopContext.PopPipeline = HttpPipelineBuilder.Build(popPipelineOptions, new HttpPipelineTransportOptions()); + var popRequest = GraphSession.Instance.GraphRequestPopContext.PopPipeline.CreateRequest(); + popRequest.Method = RequestMethod.Parse(popMethod.Method.ToUpper()); + popRequest.Uri.Reset(popResourceUri); // Refresh token logic --- end - var popContext = new PopTokenRequestContext(authContext.Scopes, isProofOfPossessionEnabled: true, proofOfPossessionNonce: WwwAuthenticateParameters.CreateFromAuthenticationHeaders(popResponse.Headers, "Pop").Nonce, request: GraphSession.Instance.GraphRequestPopContext.Request); + var popContext = new PopTokenRequestContext(authContext.Scopes, isProofOfPossessionEnabled: true, proofOfPossessionNonce: WwwAuthenticateParameters.CreateFromAuthenticationHeaders(popResponse.Headers, "Pop").Nonce, request: popRequest); return popContext; } } diff --git a/src/Authentication/Authentication/Handlers/AuthenticationHandler.cs b/src/Authentication/Authentication/Handlers/AuthenticationHandler.cs index b78ed1eb59..362a262abf 100644 --- a/src/Authentication/Authentication/Handlers/AuthenticationHandler.cs +++ b/src/Authentication/Authentication/Handlers/AuthenticationHandler.cs @@ -27,6 +27,8 @@ internal class AuthenticationHandler : DelegatingHandler private const string BearerAuthenticationScheme = "Bearer"; private const string PopAuthenticationScheme = "Pop"; private int MaxRetry { get; set; } = 1; + private PopTokenRequestContext popTokenRequestContext; + private Request popRequest = GraphSession.Instance.GraphRequestPopContext.PopPipeline.CreateRequest(); public AzureIdentityAccessTokenProvider AuthenticationProvider { get; set; } @@ -53,7 +55,7 @@ protected override async Task SendAsync(HttpRequestMessage // Continuous nonce extraction on each request if (GraphSession.Instance.GraphOption.EnableATPoPForMSGraph) { - GraphSession.Instance.GraphRequestPopContext.PopTokenContext = new PopTokenRequestContext(GraphSession.Instance.AuthContext.Scopes, isProofOfPossessionEnabled: true, proofOfPossessionNonce: WwwAuthenticateParameters.CreateFromAuthenticationHeaders(response.Headers, PopAuthenticationScheme).Nonce, request: GraphSession.Instance.GraphRequestPopContext.Request); + popTokenRequestContext = new PopTokenRequestContext(GraphSession.Instance.AuthContext.Scopes, isProofOfPossessionEnabled: true, proofOfPossessionNonce: WwwAuthenticateParameters.CreateFromAuthenticationHeaders(response.Headers, PopAuthenticationScheme).Nonce, request: popRequest); } // Check if response is a 401 & is not a streamed body (is buffered) @@ -76,14 +78,14 @@ private async Task AuthenticateRequestAsync(HttpRequestMessage httpRequestMessag { if (GraphSession.Instance.GraphOption.EnableATPoPForMSGraph) { - GraphSession.Instance.GraphRequestPopContext.Request.Method = RequestMethod.Parse(httpRequestMessage.Method.Method.ToUpper()); - GraphSession.Instance.GraphRequestPopContext.Request.Uri.Reset(httpRequestMessage.RequestUri); + popRequest.Method = RequestMethod.Parse(httpRequestMessage.Method.Method.ToUpper()); + popRequest.Uri.Reset(httpRequestMessage.RequestUri); foreach (var header in httpRequestMessage.Headers) { - GraphSession.Instance.GraphRequestPopContext.Request.Headers.Add(header.Key, header.Value.First()); + popRequest.Headers.Add(header.Key, header.Value.First()); } - var accessToken = await GraphSession.Instance.GraphRequestPopContext.PopInteractiveBrowserCredential.GetTokenAsync(GraphSession.Instance.GraphRequestPopContext.PopTokenContext, cancellationToken).ConfigureAwait(false); + var accessToken = await GraphSession.Instance.GraphRequestPopContext.PopInteractiveBrowserCredential.GetTokenAsync(popTokenRequestContext, cancellationToken).ConfigureAwait(false); httpRequestMessage.Headers.Authorization = new AuthenticationHeaderValue(PopAuthenticationScheme, accessToken.Token); } else diff --git a/src/Authentication/Authentication/Models/GraphRequestPopContext.cs b/src/Authentication/Authentication/Models/GraphRequestPopContext.cs index 0092543bb3..4be69f16e7 100644 --- a/src/Authentication/Authentication/Models/GraphRequestPopContext.cs +++ b/src/Authentication/Authentication/Models/GraphRequestPopContext.cs @@ -3,6 +3,7 @@ // ------------------------------------------------------------------------------ using Azure.Core; +using Azure.Core.Pipeline; using Azure.Identity; using System; using System.IO; @@ -15,8 +16,7 @@ internal class GraphRequestPopContext : IGraphRequestPopContext public Uri Uri { get; set; } public HttpMethod HttpMethod { get; set; } public AccessToken AccessToken { get; set; } - public PopTokenRequestContext PopTokenContext { get; set; } - public Request Request { get; set; } + public HttpPipeline PopPipeline { get; set; } public InteractiveBrowserCredential PopInteractiveBrowserCredential { get; set; } }