Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add abstract method to MicrosoftIdentityAuthenticationBaseMessageHandler so subclasses can more easily override #1675

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;

using Microsoft.Extensions.Options;
using Microsoft.Identity.Client;

namespace Microsoft.Identity.Web
{
Expand All @@ -29,6 +30,22 @@ public MicrosoftIdentityAppAuthenticationMessageHandler(
{
}

/// <inheritdoc/>
protected override async Task<AuthenticationResult> GetTokenAsync(MicrosoftIdentityAuthenticationMessageHandlerOptions options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

return await TokenAcquisition.GetAuthenticationResultForAppAsync(
options.Scopes!,
options.AuthenticationScheme,
options.Tenant,
options.TokenAcquisitionOptions)
jennyf19 marked this conversation as resolved.
Show resolved Hide resolved
.ConfigureAwait(false);
}

/// <inheritdoc/>
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Expand All @@ -41,12 +58,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
// authenticate
var options = GetOptionsForRequest(request);

var authResult = await TokenAcquisition.GetAuthenticationResultForAppAsync(
options.Scopes!,
options.AuthenticationScheme,
options.Tenant,
options.TokenAcquisitionOptions)
.ConfigureAwait(false);
var authResult = await GetTokenAsync(options).ConfigureAwait(false);

// add or replace authorization header
if (request.Headers.Contains(Constants.Authorization))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

using System;
using System.Net.Http;

using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Microsoft.Identity.Client;
using Microsoft.Identity.Client.AppConfig;

namespace Microsoft.Identity.Web
Expand Down Expand Up @@ -66,6 +67,13 @@ protected MicrosoftIdentityAuthenticationMessageHandlerOptions GetOptionsForRequ
return options;
}

/// <summary>
/// Method to abstract getting the auth token itself. Useful for subclasses to customize token acquisition behavior.
/// </summary>
/// <param name="options">Token acquisition options</param>
/// <returns>The <see cref="AuthenticationResult"/> token.</returns>
protected abstract Task<AuthenticationResult> GetTokenAsync(MicrosoftIdentityAuthenticationMessageHandlerOptions options);

private static void CreateProofOfPossessionConfiguration(MicrosoftIdentityAuthenticationMessageHandlerOptions options, Uri apiUri, HttpMethod method)
{
if (options.IsProofOfPossessionRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;

using Microsoft.Extensions.Options;
using Microsoft.Identity.Client;

namespace Microsoft.Identity.Web
{
Expand Down Expand Up @@ -34,6 +35,23 @@ public MicrosoftIdentityUserAuthenticationMessageHandler(
_microsoftIdentityOptions = microsoftIdentityOptions;
}

/// <inheritdoc/>
protected override async Task<AuthenticationResult> GetTokenAsync(MicrosoftIdentityAuthenticationMessageHandlerOptions options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

return await TokenAcquisition.GetAuthenticationResultForUserAsync(
options.GetScopes(),
authenticationScheme: options.AuthenticationScheme,
tenantId: options.Tenant,
userFlow: options.UserFlow,
tokenAcquisitionOptions: options.TokenAcquisitionOptions)
.ConfigureAwait(false);
}

/// <inheritdoc/>
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Expand All @@ -48,17 +66,12 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
var microsoftIdentityOptions = _microsoftIdentityOptions
.Get(TokenAcquisition.GetEffectiveAuthenticationScheme(options.AuthenticationScheme));

var userflow = microsoftIdentityOptions.IsB2C && string.IsNullOrEmpty(options.UserFlow)
? microsoftIdentityOptions.DefaultUserFlow
: options.UserFlow;
if (microsoftIdentityOptions.IsB2C && string.IsNullOrEmpty(options.UserFlow))
{
options.UserFlow = microsoftIdentityOptions.DefaultUserFlow;
}

var authResult = await TokenAcquisition.GetAuthenticationResultForUserAsync(
options.GetScopes(),
authenticationScheme: options.AuthenticationScheme,
tenantId: options.Tenant,
userFlow: userflow,
tokenAcquisitionOptions: options.TokenAcquisitionOptions)
.ConfigureAwait(false);
var authResult = await GetTokenAsync(options).ConfigureAwait(false);

// add or replace authorization header
if (request.Headers.Contains(Constants.Authorization))
Expand Down