Skip to content

Commit

Permalink
Changed the DI pattern for Token Acquisition interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaytak committed Jun 25, 2020
1 parent 35d83f6 commit 5efc6b2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.Identity.Web/ITokenAcquisitionInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.Identity.Web
/// <summary>
/// Interface for the internal operations of token acquisition service (encapsulating MSAL.NET).
/// </summary>
internal interface ITokenAcquisitionInternal
internal interface ITokenAcquisitionInternal : ITokenAcquisition
{
/// <summary>
/// In a Web App, adds, to the MSAL.NET cache, the account of the user authenticating to the Web App, when the authorization code is received (after the user
Expand Down
8 changes: 5 additions & 3 deletions src/Microsoft.Identity.Web/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ public static IServiceCollection AddTokenAcquisition(
{
// Token acquisition service
services.AddHttpContextAccessor();
if (!isTokenAcquisitionSingleton)
if (isTokenAcquisitionSingleton)
{
services.AddScoped<ITokenAcquisition, TokenAcquisition>();
services.AddSingleton<ITokenAcquisition, TokenAcquisition>();
services.AddSingleton<ITokenAcquisitionInternal>(s => (ITokenAcquisitionInternal)s.GetService<ITokenAcquisition>());
}
else
{
services.AddSingleton<ITokenAcquisition, TokenAcquisition>();
services.AddScoped<ITokenAcquisition, TokenAcquisition>();
services.AddScoped<ITokenAcquisitionInternal>(s => (ITokenAcquisitionInternal)s.GetService<ITokenAcquisition>());
}

return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static AuthenticationBuilder AddMicrosoftWebAppCallsWebApi(
var codeReceivedHandler = options.Events.OnAuthorizationCodeReceived;
options.Events.OnAuthorizationCodeReceived = async context =>
{
var tokenAcquisition = context.HttpContext.RequestServices.GetRequiredService<ITokenAcquisition>() as ITokenAcquisitionInternal;
var tokenAcquisition = context.HttpContext.RequestServices.GetRequiredService<ITokenAcquisitionInternal>();
await tokenAcquisition.AddAccountToCacheFromAuthorizationCodeAsync(context, options.Scope).ConfigureAwait(false);
await codeReceivedHandler(context).ConfigureAwait(false);
};
Expand Down Expand Up @@ -160,7 +160,7 @@ public static AuthenticationBuilder AddMicrosoftWebAppCallsWebApi(
options.Events.OnRedirectToIdentityProviderForSignOut = async context =>
{
// Remove the account from MSAL.NET token cache
var tokenAcquisition = context.HttpContext.RequestServices.GetRequiredService<ITokenAcquisition>() as ITokenAcquisitionInternal;
var tokenAcquisition = context.HttpContext.RequestServices.GetRequiredService<ITokenAcquisitionInternal>();
await tokenAcquisition.RemoveAccountAsync(context).ConfigureAwait(false);
await signOutHandler(context).ConfigureAwait(false);
};
Expand Down
12 changes: 6 additions & 6 deletions tests/Microsoft.Identity.Web.Test/WebAppExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public async Task AddWebAppCallsProtectedWebApi_WithConfigNameParameters()
{
var configMock = Substitute.For<IConfiguration>();
var initialScopes = new List<string>() { "custom_scope" };
var tokenAcquisitionMock = Substitute.For<ITokenAcquisition, ITokenAcquisitionInternal>();
var tokenAcquisitionMock = Substitute.For<ITokenAcquisitionInternal>();
var authCodeReceivedFuncMock = Substitute.For<Func<AuthorizationCodeReceivedContext, Task>>();
var tokenValidatedFuncMock = Substitute.For<Func<TokenValidatedContext, Task>>();
var redirectFuncMock = Substitute.For<Func<RedirectContext, Task>>();
Expand Down Expand Up @@ -237,7 +237,7 @@ public async Task AddWebAppCallsProtectedWebApi_WithConfigNameParameters()
public async Task AddWebAppCallsProtectedWebApi_WithConfigActionParameters()
{
var initialScopes = new List<string>() { "custom_scope" };
var tokenAcquisitionMock = Substitute.For<ITokenAcquisition, ITokenAcquisitionInternal>();
var tokenAcquisitionMock = Substitute.For<ITokenAcquisitionInternal>();
var authCodeReceivedFuncMock = Substitute.For<Func<AuthorizationCodeReceivedContext, Task>>();
var tokenValidatedFuncMock = Substitute.For<Func<TokenValidatedContext, Task>>();
var redirectFuncMock = Substitute.For<Func<RedirectContext, Task>>();
Expand Down Expand Up @@ -445,15 +445,15 @@ private async Task AddWebAppCallsProtectedWebApi_TestAuthorizationCodeReceivedEv
IServiceProvider provider,
OpenIdConnectOptions oidcOptions,
Func<AuthorizationCodeReceivedContext, Task> authCodeReceivedFuncMock,
ITokenAcquisition tokenAcquisitionMock)
ITokenAcquisitionInternal tokenAcquisitionMock)
{
var (httpContext, authScheme, authProperties) = CreateContextParameters(provider);

await oidcOptions.Events.AuthorizationCodeReceived(new AuthorizationCodeReceivedContext(httpContext, authScheme, oidcOptions, authProperties)).ConfigureAwait(false);

// Assert original AuthorizationCodeReceived event and TokenAcquisition method were called
await authCodeReceivedFuncMock.ReceivedWithAnyArgs().Invoke(Arg.Any<AuthorizationCodeReceivedContext>()).ConfigureAwait(false);
await ((ITokenAcquisitionInternal)tokenAcquisitionMock).ReceivedWithAnyArgs().AddAccountToCacheFromAuthorizationCodeAsync(Arg.Any<AuthorizationCodeReceivedContext>(), Arg.Any<IEnumerable<string>>()).ConfigureAwait(false);
await tokenAcquisitionMock.ReceivedWithAnyArgs().AddAccountToCacheFromAuthorizationCodeAsync(Arg.Any<AuthorizationCodeReceivedContext>(), Arg.Any<IEnumerable<string>>()).ConfigureAwait(false);
}

private async Task AddWebAppCallsProtectedWebApi_TestTokenValidatedEvent(IServiceProvider provider, OpenIdConnectOptions oidcOptions, Func<TokenValidatedContext, Task> tokenValidatedFuncMock)
Expand All @@ -473,15 +473,15 @@ private async Task AddWebAppCallsProtectedWebApi_TestRedirectToIdentityProviderF
IServiceProvider provider,
OpenIdConnectOptions oidcOptions,
Func<RedirectContext, Task> redirectFuncMock,
ITokenAcquisition tokenAcquisitionMock)
ITokenAcquisitionInternal tokenAcquisitionMock)
{
var (httpContext, authScheme, authProperties) = CreateContextParameters(provider);

await oidcOptions.Events.RedirectToIdentityProviderForSignOut(new RedirectContext(httpContext, authScheme, oidcOptions, authProperties)).ConfigureAwait(false);

// Assert original RedirectToIdentityProviderForSignOut event and TokenAcquisition method were called
await redirectFuncMock.ReceivedWithAnyArgs().Invoke(Arg.Any<RedirectContext>()).ConfigureAwait(false);
await ((ITokenAcquisitionInternal)tokenAcquisitionMock).ReceivedWithAnyArgs().RemoveAccountAsync(Arg.Any<RedirectContext>()).ConfigureAwait(false);
await tokenAcquisitionMock.ReceivedWithAnyArgs().RemoveAccountAsync(Arg.Any<RedirectContext>()).ConfigureAwait(false);
}

private (HttpContext, AuthenticationScheme, AuthenticationProperties) CreateContextParameters(IServiceProvider provider)
Expand Down

0 comments on commit 5efc6b2

Please sign in to comment.