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

There should be an option to have TokenAcquisition be a singleton #1

Closed
jennyf19 opened this issue Feb 18, 2020 · 6 comments
Closed
Assignees
Milestone

Comments

@jennyf19
Copy link
Collaborator

jennyf19 commented Feb 18, 2020

This issue is for a: (mark with an x)

- [ ] bug report -> please search issues before submitting
- [ x] feature request
- [ ] documentation issue or request
- [ ] regression (a behavior that used to work and stopped in a new release)

Why?

Today, TokenAcquisition is a scoped by request. See https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/blob/ab84785cf757c2d3244090a90d563528ee4be616/Microsoft.Identity.Web/ServiceCollectionExtensions.cs#L38

There are scenarios, like using the Graph SDK, which require it to be a singleton. This is a request from the Graph SDK team (Darrel Miller).
Given Bogdan's analysis (below), this is safe to have a singleton.

What: proposed developer experience?

Provide an option in the configuration, which sets the TokenAcquisition service to be a singleton.

{
  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
  
    // missing lines here ...

    "SingletonTokenAcquisition" :  false
  },

  // missing lines here ...
}

Proposed design:

  1. Add a boolean SingletonTokenAcquisition in the MicrosoftIdentityOptions, by default this would be false.
  2. Add a bool parameter in AddTokenAcquisition to specify if the token acquisition service should be a singleton or not (and use .AddScoped if it's not a singleton, and AddSignleton if this is a singleton)
  3. Pass this parameter in methods calling AddTokenAcquisition, the value coming from the MicrosoftIdentityOptions.SingletonTokenAcquisition. Probably:
    • AddProtectedWebApiCallsWebApis
    • AddWebAppCallsWebApis,

Issue copied from Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2#287, that will need to be updated

@bgavrilMS
Copy link
Member

bgavrilMS commented Feb 18, 2020

@jmprieur - I had a look at this scenario in more detail.

AquireTokenSilent does:

  1. Read the cache
  2. Internal logic, possibly call AAD for new tokens
  3. When new tokens arrive, re-read the cache and merge the new tokens

Steps 1 and 3 are guaranteed to be consistent because we use locks. The entire AcquireTokenSilent is not locked. So the whole thing might work from a consistency perspective, even

However, please note that using the same cache has performance implications:

a. Operations 1 and 3 above are probably executed in a critical section . This can result in high contention, especially under heavy load. This isn't a bug in MSAL per se, but we'll have to retest token cache web implementations. Some improvements can be made such as using separate read and write locks.
b. Token cache has not been perf tested for holding large number of entries. We've had bugs in the past around this, especially with around ADAL fallback mechanism.
c. Although not perf related, storing all tokens in the same cache is a security issue. If we ever introduce a bug in MSAL which would allow user A to fetch a token that does not belong to user A, today we are protected by the fact user A has its own cache. With a shared cache, this would be a major security incident.

All in all, this work item requires a lot of testing.

@jmprieur
Copy link
Collaborator

Thanks @bgavrilMS
about b and c, we don't store many accounts in a cache. We have one cache per account (the key is an account ID)

@bgavrilMS
Copy link
Member

Then concern a. above is also not relevant. The locks are at the Token Cache level, so if you have a token cache for each account, contention is not an issue.

@jmprieur
Copy link
Collaborator

Thanks for confirming @bgavrilMS

@jmprieur
Copy link
Collaborator

@jennyf19
Copy link
Collaborator Author

jennyf19 commented Jun 1, 2020

In 0.1.4-preview release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants