Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Fix for #98 - cache cannot be used with ADFS #130

Merged
merged 3 commits into from
Feb 17, 2021
Merged

Conversation

bgavrilMS
Copy link
Member

@bgavrilMS bgavrilMS commented Feb 11, 2021

Before this fix, developers had to configure client_id.

This design does not take into account the following, which this PR fixes:

  • it's not clear that CacheChanged event is only relevant for the given client_id
  • since authority is not configured, the default authority is implied (i.e. l.m.o/common). This is the root cause of bug persistent cache resiliency #89
  • developers cannot use 1 instance of the CacheExtension for multiple client_ids (since a client_id is needed to construct it)

Fixes #98 #123 #121 #102 (yay!)

@@ -276,8 +276,7 @@ private static async Task<MsalCacheHelper> CreateCacheHelperAsync()
{
storageProperties = new StorageCreationPropertiesBuilder(
Copy link
Member Author

Choose a reason for hiding this comment

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

Example usage is here - you can see that Client_ID is no longer a mandatory param. If people need CacheChanged event, they need to configure both "client_id" and "authority". If they don't, they'll get a good error message when subscribing to CacheChanged

@@ -287,6 +286,9 @@ private static async Task<MsalCacheHelper> CreateCacheHelperAsync()
.WithMacKeyChain(
Config.KeyChainServiceName,
Config.KeyChainAccountName)
.WithCacheChangedEvent( // do NOT use unless really necessary, high perf penalty!
Copy link
Member Author

Choose a reason for hiding this comment

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

CacheChanged event is scoped to this (client_id, authority) pair. There isn't (currently) a way to get events about other pairs, except by creating different instances of the MsalCacheHelper object.

Copy link
Contributor

Choose a reason for hiding this comment

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

Does it mean that you won't have changes in the FOCI ?

@bgavrilMS bgavrilMS requested a review from henrik-me February 11, 2021 17:39
@bgavrilMS bgavrilMS changed the title Fix for #89 - cache cannot be used with ADFS Fix for #98 - cache cannot be used with ADFS Feb 11, 2021
@bgavrilMS bgavrilMS linked an issue Feb 11, 2021 that may be closed by this pull request
@@ -37,11 +38,25 @@ public class StorageCreationPropertiesBuilder
/// <param name="cacheFileName">The name of the cache file to use when creating or opening storage.</param>
/// <param name="cacheDirectory">The name of the directory containing the cache file.</param>
/// <param name="clientId">The client id for the calling application</param>
[Obsolete("Use StorageCreationPropertiesBuilder(string, string) instead. " +
"If you need to consume the CacheChanged event then also use WithCacheChangedEvent(string, string)", false)]
public StorageCreationPropertiesBuilder(string cacheFileName, string cacheDirectory, string clientId)
Copy link
Member Author

Choose a reason for hiding this comment

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

This constructor is now obsolete.

@bgavrilMS bgavrilMS requested a review from jmprieur February 17, 2021 13:44
@bgavrilMS
Copy link
Member Author

Also adding @jmprieur for the public API changes.
Note that I'll be merging this tomorrow.

Copy link
Contributor

@jmprieur jmprieur left a comment

Choose a reason for hiding this comment

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

LGTM.
I don't fully understand how this would be a shared cache from the point of view of the change events?

@@ -287,6 +286,9 @@ private static async Task<MsalCacheHelper> CreateCacheHelperAsync()
.WithMacKeyChain(
Config.KeyChainServiceName,
Config.KeyChainAccountName)
.WithCacheChangedEvent( // do NOT use unless really necessary, high perf penalty!
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it mean that you won't have changes in the FOCI ?

@bgavrilMS
Copy link
Member Author

@jmprieur - for FOCI apps, the client_id is not used in the internal logic of GetAccounts. Great question by the way. So no, there won't be any changes to VS or other FOCI apps.

Related to this being a shared cache. Think of this object as a singleton, as it represents a file on disk. I can then register a bunch of ITokenCache objects (from different PublicClientApplication objs) to synchronize with this file. If I'm interested in knowing which accounts were added / removed from the cache, I can register for an event:

StoregePropertiesBuilder().WithCacheChangedEvent(client_id, authority);

and later subscribe to the event

MsalCacheHelper helper = MsalCacheHelper.CreateAsync(storageProperties);
helper.CacheChangedEvent += <listner>

The CacheChangeEvent will only fire for the (client_id, authority) that you have configured. If you don't configure any, I'll throw.

You cannot currently listen to events for different pairs of (client_id, authority). VS doesn't care about it and nobody else expressed an interest in this event. As such, I think this is the simplest solution.

A better solution would be to have an API like

helper.RegisterCacheChangedCallback(client_id, authority, Action<accounts_added, accounts_removed>)

This is doable, but more work. Do you think it's worth doing now?

@jmprieur
Copy link
Contributor

Thanks for the explanations, @bgavrilMS
No, let's not do it until someone expresses their need.

Copy link

@henrik-me henrik-me left a comment

Choose a reason for hiding this comment

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

:shipit:

@bgavrilMS bgavrilMS merged commit f7ca3b3 into master Feb 17, 2021
@bgavrilMS bgavrilMS deleted the bogavril/event_wip branch February 17, 2021 19:59
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants