Skip to content

Update InitiateLongRunningObo method to retrieve tokens from the cache if request assertion matches.#4111

Closed
pmaytak wants to merge 17 commits into
mainfrom
pmaytak/lr-obo-assertion
Closed

Update InitiateLongRunningObo method to retrieve tokens from the cache if request assertion matches.#4111
pmaytak wants to merge 17 commits into
mainfrom
pmaytak/lr-obo-assertion

Conversation

@pmaytak

@pmaytak pmaytak commented May 1, 2023

Copy link
Copy Markdown
Contributor

Fixes #4100

Changes proposed in this request
Updates InitiateLongRunningObo method to return valid cached tokens if cached assertion matches the request assertion; otherwise, the assertion is used to retrieve new tokens.
Updates access token cache item .

Testing
Updated tests.

Performance impact
None.

Documentation

  • All relevant documentation is updated. - After release.

Comment thread src/client/Microsoft.Identity.Client/Internal/Requests/OnBehalfOfRequest.cs Outdated
@bgavrilMS

Copy link
Copy Markdown
Member

Fixes #4100

Changes proposed in this request Updates InitiateLongRunningObo method to return valid cached tokens if cached assertion matches the request assertion; otherwise, the assertion is used to retrieve new tokens. Updates access token cache item .

Testing Updated tests.

Performance impact

Documentation

  • All relevant documentation is updated.

Pls ensure that our OBO spec / api review is updated.

Comment thread src/client/Microsoft.Identity.Client/Cache/Items/MsalAccessTokenCacheItem.cs Outdated
pmaytak and others added 3 commits May 1, 2023 20:15
Co-authored-by: Gladwin Johnson <90415114+gladjohn@users.noreply.github.com>
@pmaytak pmaytak requested a review from gladjohn May 2, 2023 21:39
Base automatically changed from pmaytak/lr-obo-apiid to main May 2, 2023 22:17
Comment thread tests/Microsoft.Identity.Test.Unit/PublicApiTests/OBOTests.cs Outdated

@trwalke trwalke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The logic here makes sense overall, just one question about the test.

@pmaytak pmaytak requested review from bgavrilMS and trwalke May 10, 2023 06:10
cachedAccessToken = await CacheManager.FindAccessTokenAsync().ConfigureAwait(false);
}

if (AuthenticationRequestParameters.ApiId == ApiEvent.ApiIds.InitiateLongRunningObo && cachedAccessToken != null &&

@pmaytak pmaytak May 10, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Currently cached/serialized token items (aka legacy tokens) don't have this new assertion hash property. So there's a scenario if app is updated to this new MSAL version that checks the hash now.

An option here is to add additional check - && !string.IsNullOrEmpty(cachedAccessToken.OboAssertionHash).

  1. As-is, without the above check, InitiateObo will never match these cached legacy tokens, and will always use the passed-in assertion to get tokens from AAD, even if the cached tokens are unexpired. This actually mimics current behavior (4.51.0+).
  2. With the additional check, InitiateObo will return the valid matched legacy access tokens and, if expired, will use legacy refresh token to refresh. This mimics the 4.50.0- behavior but only for legacy tokens. But once the legacy RT is used to refresh, then new tokens will be saved with the assertion hash.

In the scenario when AAD revokes AT and RT, method 2 reintroduces the previous issue of returning revoked tokens, so devs would have to use the StopLongObo method.

AcquireLongObo is not affected since it doesn't have the assertion in the request.

A more general question, do we even want to have an assertion hash match for refresh tokens?

Thoughts @bgavrilMS, @trwalke? Maybe I missed something?

@trwalke trwalke May 11, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

do we even want to have an assertion hash match for refresh tokens?

The reason we discussed adding this was to prevent the scenario where someone is using initiate long running OBO over and over again from breaking after 4.51.0+. The initial implementation for this PR lead us to the realization that in order to actually do that while maintaining the logic that fixes the revocation issue is to add another check to the refresh OBO logic to check for a matching token hash. So, if we want to prevent breaking customers, we should keep the hash match for refresh tokens.

As-is, without the above check, InitiateObo will never match these cached legacy tokens, and will always use the passed-in assertion to get tokens from AAD, even if the cached tokens are unexpired. This actually mimics current behavior (4.51.0+).

I think this will only mimic the behavior of 4.51.0+ for the first iteration. So, once the user is required to sign in again, the legacy tokens should be overwritten and assertion will be cached with the new tokens. I think this is fine. The other option (#2) will require the developer to add the StopLongRunningApi method to remove the revoked tokens. That is a much more difficult state to recover from than simply asking the user to sign in again.

So, I dont think we need to add the null check. The end user experience is affected, but all they need to do is sign in again to recover and then everything will work fine.

Adding this null check will put the user in a state they cant recover from if the token are revoked without the app developer changing the app or deleting tokens manually.

Lets sync on this tomorrow @pmaytak incase I am missing something.

@pmaytak

pmaytak commented May 11, 2023

Copy link
Copy Markdown
Contributor Author

Per discussion, will not implement this at this time.

@pmaytak pmaytak closed this May 11, 2023
@bgavrilMS bgavrilMS deleted the pmaytak/lr-obo-assertion branch January 4, 2025 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] InitiateLongRunningProcessInWebApi should return cached token if request assertion hash matches cached one

4 participants