Skip to content

Adding fix for OBO cache key error in long running OBO with DefaultAuthorizationHeaderProvider #3381

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

trwalke
Copy link
Member

@trwalke trwalke commented Jun 4, 2025

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Description

Updates the DefaultAuthorizationHeaderProvider to update the AcquireTokenOptions.LongRunningWebApiSessionKey after the token is acquired so that the key can be used in the next OBO call.

Fixes #3382

@trwalke trwalke marked this pull request as ready for review June 4, 2025 00:50
@trwalke trwalke requested a review from a team as a code owner June 4, 2025 00:50
@@ -27,13 +27,16 @@ public async Task<string> CreateAuthorizationHeaderForUserAsync(
ClaimsPrincipal? claimsPrincipal = null,
CancellationToken cancellationToken = default)
{
var newTokenAcquisitionOptions = CreateTokenAcquisitionOptionsFromApiOptions(downstreamApiOptions, cancellationToken);
Copy link
Contributor

Choose a reason for hiding this comment

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

can you help me understand this a bit more?

Does this option get captured in the result object as part of GetAuthenticationResultForUserAsync/GetAuthenticationResultForAppAsync?

Can you point to where the modification is to AcquireTokenOptions?

Would it be a better fix to make it so that the code that is changing LongRunningWebApiSessionKey doesn't mutate input?

Copy link
Member Author

@trwalke trwalke Jun 4, 2025

Choose a reason for hiding this comment

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

Yeah, it is an interesting design, I would also prefer that it did not mutate the AcquireTokenOptions after the authentication but this is how it works at the moment. This PR is to fix this bug in particular, but a follow up PR can be used to refactor how the Long running OBO works all together. It would take some discussion on the API and user experience.

When the options are provided with a LongRunningWebApiSessionKey that is equal to LongRunningWebApiSessionKeyAuto ("AllocateForMe") , the code will set this value to the cache key that MSAL calculates after initiating the long running OBO session. It is then expected for the developer to save the updated key from the modified AcquireTokenOptions and provide it in subsequent authentication calls.

See https://github.com/AzureAD/microsoft-identity-web/wiki/get-token-in-event-handler#principle
and

tokenAcquisitionOptions.LongRunningWebApiSessionKey = sessionKey;

Copy link
Member Author

@trwalke trwalke Jun 4, 2025

Choose a reason for hiding this comment

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

Also, it does not happen for GetAuthenticationResultForAppAsync since this code path does not use msal's OBO flow

Copy link
Member Author

Choose a reason for hiding this comment

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

claimsPrincipal);

Assert.NotNull(result);
Assert.NotEqual(options.AcquireTokenOptions.LongRunningWebApiSessionKey, TokenAcquisitionOptions.LongRunningWebApiSessionKeyAuto);
Copy link
Member

Choose a reason for hiding this comment

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

What is it equal to then?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is set to the hash of the AT, which is acquired from MSAL


using (mockHttpClient)
{
mockHttpClient!.AddMockHandler(MockHttpCreator.CreateClientCredentialTokenHandler());
Copy link
Member

Choose a reason for hiding this comment

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

You should add a mock response that has AT, RT, IdT


using (mockHttpClient)
{
mockHttpClient!.AddMockHandler(MockHttpCreator.CreateClientCredentialTokenHandler());
Copy link
Member

Choose a reason for hiding this comment

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

You should add a mock response that has AT, RT, IdT, client_info


using (mockHttpClient)
{
mockHttpClient!.AddMockHandler(MockHttpCreator.CreateClientCredentialTokenHandler());
Copy link
Member

Choose a reason for hiding this comment

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

You should add a mock response that has AT, RT, IdT, client_info

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.

Microsoft.IdentityWeb - Impossible to keep LR OBO session with auto generated key
3 participants