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
Changes from 1 commit
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
Next Next commit
Simplification of the token cache serialization providers, which don'…
…t need a reference to the options (the ClientId, which was extracted from these options, can be provided by the TokenCacheArguments, or in the call ClearAsync
jmprieur committed Mar 30, 2020
commit ed1519ba85a8538afb16308ce1939da4b676781e
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
{
@@ -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;
Original file line number Diff line number Diff line change
@@ -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
{
@@ -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;
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;
@@ -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>
@@ -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;
}

@@ -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
{
@@ -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);
@@ -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))
{
@@ -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
Original file line number Diff line number Diff line change
@@ -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
{
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -96,7 +96,6 @@ private void InitializeTokenAcquisitionObjects()
IHttpContextAccessor httpContextAccessor = CreateMockHttpContextAccessor();

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