Skip to content

Commit

Permalink
Resolving comments regarding static instance
Browse files Browse the repository at this point in the history
  • Loading branch information
FehintolaObafemi committed Jul 15, 2024
1 parent 43589bf commit ff194cb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// ------------------------------------------------------------------------------

using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Identity;
using System;
using System.Net.Http;
Expand All @@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ private static async Task<InteractiveBrowserCredential> 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;
}

Expand All @@ -142,7 +143,7 @@ private static async Task<InteractiveBrowserCredential> GetInteractiveBrowserCre
authRecord = await Task.Run(() =>
{
// Run the thread in MTA.
return interactiveBrowserCredential.AuthenticateAsync(GraphSession.Instance.GraphRequestPopContext.PopTokenContext, cancellationToken);
return interactiveBrowserCredential.AuthenticateAsync(popTokenRequestContext, cancellationToken);
});
}
else
Expand Down Expand Up @@ -486,13 +487,13 @@ private static async Task<PopTokenRequestContext> 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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -53,7 +55,7 @@ protected override async Task<HttpResponseMessage> 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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// ------------------------------------------------------------------------------

using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Identity;
using System;
using System.IO;
Expand All @@ -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; }
}

Expand Down

0 comments on commit ff194cb

Please sign in to comment.