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

Simplification of the token cache serialization providers #67

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 2 additions & 2 deletions src/Microsoft.Identity.Web/TokenAcquisition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public async Task RemoveAccountAsync(RedirectContext context)
await app.RemoveAsync(b2cAccount).ConfigureAwait(false);
}

_tokenCacheProvider?.ClearAsync().ConfigureAwait(false);
_tokenCacheProvider?.ClearAsync(_microsoftIdentityOptions.ClientId).ConfigureAwait(false);
}

else
Expand All @@ -304,7 +304,7 @@ public async Task RemoveAccountAsync(RedirectContext context)
if (account != null)
{
await app.RemoveAsync(account).ConfigureAwait(false);
_tokenCacheProvider?.ClearAsync().ConfigureAwait(false);
_tokenCacheProvider?.ClearAsync(_microsoftIdentityOptions.ClientId).ConfigureAwait(false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;
using System.Threading.Tasks;

namespace Microsoft.Identity.Web.TokenCacheProviders.Distributed
{
Expand All @@ -32,11 +31,10 @@ public class MsalDistributedTokenCacheAdapter : MsalAbstractTokenCacheProvider
/// <param name="httpContextAccessor"></param>
/// <param name="memoryCache"></param>
/// <param name="cacheOptions"></param>
public MsalDistributedTokenCacheAdapter(IOptions<MicrosoftIdentityOptions> microsoftIdentityOptions,
IHttpContextAccessor httpContextAccessor,
public MsalDistributedTokenCacheAdapter(IHttpContextAccessor httpContextAccessor,
IDistributedCache memoryCache,
IOptions<DistributedCacheEntryOptions> cacheOptions) :
base(microsoftIdentityOptions, httpContextAccessor)
base(httpContextAccessor)
{
_distributedCache = memoryCache;
_cacheOptions = cacheOptions.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public interface IMsalTokenCacheProvider
/// Clear the cache
/// </summary>
/// <returns></returns>
Task ClearAsync();
Task ClearAsync(string clientId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also update the comments or will there be a task to go over all of the comments in the project?

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using System.Threading.Tasks;

namespace Microsoft.Identity.Web.TokenCacheProviders.InMemory
{
Expand All @@ -32,11 +31,10 @@ public class MsalMemoryTokenCacheProvider : MsalAbstractTokenCacheProvider
/// <param name="httpContextAccessor"></param>
/// <param name="memoryCache"></param>
/// <param name="cacheOptions"></param>
public MsalMemoryTokenCacheProvider(IOptions<MicrosoftIdentityOptions> microsoftIdentityOptions,
IHttpContextAccessor httpContextAccessor,
public MsalMemoryTokenCacheProvider(IHttpContextAccessor httpContextAccessor,
IMemoryCache memoryCache,
IOptions<MsalMemoryTokenCacheOptions> cacheOptions) :
base(microsoftIdentityOptions, httpContextAccessor)
base(httpContextAccessor)
{
_memoryCache = memoryCache;
_cacheOptions = cacheOptions.Value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.Identity.Client;
using System.IdentityModel.Tokens.Jwt;
using System.Threading.Tasks;
Expand All @@ -14,11 +12,6 @@ namespace Microsoft.Identity.Web.TokenCacheProviders
/// <seealso cref="Microsoft.Identity.Web.TokenCacheProviders.IMsalTokenCacheProvider" />
public abstract class MsalAbstractTokenCacheProvider : IMsalTokenCacheProvider
{
/// <summary>
/// Azure AD options
/// </summary>
protected readonly IOptions<MicrosoftIdentityOptions> _microsoftIdentityOptions;

/// <summary>
/// Http accessor
/// </summary>
Expand All @@ -29,9 +22,8 @@ public abstract class MsalAbstractTokenCacheProvider : IMsalTokenCacheProvider
/// </summary>
/// <param name="azureAdOptions"></param>
/// <param name="httpContextAccessor"></param>
protected MsalAbstractTokenCacheProvider(IOptions<MicrosoftIdentityOptions> microsoftIdentityOptions, IHttpContextAccessor httpContextAccessor)
protected MsalAbstractTokenCacheProvider(IHttpContextAccessor httpContextAccessor)
{
_microsoftIdentityOptions = microsoftIdentityOptions;
_httpContextAccessor = httpContextAccessor;
}

Expand All @@ -52,11 +44,11 @@ public Task InitializeAsync(ITokenCache tokenCache)
/// <summary>
/// Cache key
/// </summary>
private string GetCacheKey(bool isAppTokenCache)
private string GetCacheKey(bool isAppTokenCache, string clientId)
{
if (isAppTokenCache)
{
return $"{_microsoftIdentityOptions.Value.ClientId}_AppTokenCache";
return $"{clientId}_AppTokenCache";
}
else
{
Expand All @@ -81,7 +73,7 @@ private async Task OnAfterAccessAsync(TokenCacheNotificationArgs args)
// if the access operation resulted in a cache update
if (args.HasStateChanged)
{
string cacheKey = GetCacheKey(args.IsApplicationCache);
string cacheKey = GetCacheKey(args.IsApplicationCache, args.ClientId);
if (!string.IsNullOrWhiteSpace(cacheKey))
{
await WriteCacheBytesAsync(cacheKey, args.TokenCache.SerializeMsalV3()).ConfigureAwait(false);
Expand All @@ -91,7 +83,7 @@ private async Task OnAfterAccessAsync(TokenCacheNotificationArgs args)

private async Task OnBeforeAccessAsync(TokenCacheNotificationArgs args)
{
string cacheKey = GetCacheKey(args.IsApplicationCache);
string cacheKey = GetCacheKey(args.IsApplicationCache, args.ClientId);

if (!string.IsNullOrEmpty(cacheKey))
{
Expand All @@ -106,10 +98,10 @@ protected virtual Task OnBeforeWriteAsync(TokenCacheNotificationArgs args)
return Task.CompletedTask;
}

public async Task ClearAsync()
public async Task ClearAsync(string clientId)
{
// This is a user token cache
await RemoveKeyAsync(GetCacheKey(false)).ConfigureAwait(false);
await RemoveKeyAsync(GetCacheKey(false, clientId)).ConfigureAwait(false);

// TODO: Clear the cookie session if any. Get inspiration from
// https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/issues/240
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// Licensed under the MIT License.

using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace Microsoft.Identity.Web.TokenCacheProviders.Session
{
Expand All @@ -32,10 +31,9 @@ public class MsalSessionTokenCacheProvider : MsalAbstractTokenCacheProvider, IMs
private ILogger _logger;

public MsalSessionTokenCacheProvider(
IOptions<MicrosoftIdentityOptions> microsoftIdentityOptions,
IHttpContextAccessor httpContextAccessor,
ILogger<MsalSessionTokenCacheProvider> logger) :
base(microsoftIdentityOptions, httpContextAccessor)
base(httpContextAccessor)
{
_logger = logger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ public class MsalTestTokenCacheProvider : MsalAbstractTokenCacheProvider
public int Count { get; internal set; }

public MsalTestTokenCacheProvider(
IOptions<MicrosoftIdentityOptions> microsoftIdentityOptions,
IHttpContextAccessor httpContextAccessor,
IMemoryCache memoryCache,
IOptions<MsalMemoryTokenCacheOptions> cacheOptions) :
base(microsoftIdentityOptions, httpContextAccessor)
base(httpContextAccessor)
{
MemoryCache = memoryCache;
_cacheOptions = cacheOptions.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ private void InitializeTokenAcquisitionObjects()
IHttpContextAccessor httpContextAccessor = CreateMockHttpContextAccessor();

_msalTestTokenCacheProvider = new MsalTestTokenCacheProvider(
microsoftIdentityOptions,
httpContextAccessor,
_provider.GetService<IMemoryCache>(),
tokenOptions);
Expand Down