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

Fix AcquireTokenSilent for ADFS users #446

Merged
merged 4 commits into from
Aug 11, 2023
Merged

Fix AcquireTokenSilent for ADFS users #446

merged 4 commits into from
Aug 11, 2023

Conversation

chlowell
Copy link
Collaborator

@chlowell chlowell commented Aug 5, 2023

#426 broke silent auth for ADFS users by requiring a home account ID, which ADFS accounts don't have. This PR restores the v1.0.0 behavior for ADFS by relaxing that requirement, allowing any nonzero account for silent auth. However, I'm uncertain the prior behavior was correct. Because the home account ID for ADFS accounts and tokens is always "", the cache matches access tokens on realm, client ID and scope alone, effectively ignoring the user's identity. If the cache has tokens for more than one ADFS user, it will return the first token it considers regardless of the owner. @rayluo @bgavrilMS should the cache match something else in this case such as the local account ID?

@chlowell chlowell added the bug Something isn't working label Aug 5, 2023
@chlowell chlowell requested review from rayluo and bgavrilMS August 5, 2023 00:21
@bgavrilMS
Copy link
Member

bgavrilMS commented Aug 8, 2023

No, this is not the correct approach. The id of an ADFS account it based on the sub claim in the id token. THis is a mandatory OpenIdConnect claim, guaranteed to be there.

See https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/main/src/client/Microsoft.Identity.Client/TokenResponseHelper.cs#L48

 string homeAccountId = clientInfo?.ToAccountIdentifier() ?? idToken?.Subject;

Copy link
Member

@bgavrilMS bgavrilMS left a comment

Choose a reason for hiding this comment

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

Use sub claim as home account id

@chlowell chlowell added bug Something isn't working and removed bug Something isn't working labels Aug 9, 2023
@chlowell chlowell requested a review from bgavrilMS August 9, 2023 20:59
@sonarcloud
Copy link

sonarcloud bot commented Aug 11, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@chlowell chlowell merged commit 034e067 into dev Aug 11, 2023
@chlowell chlowell deleted the chlowell/silent-adfs branch August 11, 2023 17:10
bgavrilMS pushed a commit that referenced this pull request Aug 15, 2023
// a home account ID is required to find user tokens in the cache
if o.account.HomeAccountID == "" {
// an account is required to find user tokens in the cache
if reflect.ValueOf(o.account).IsZero() {
return AuthResult{}, errNoAccount

Choose a reason for hiding this comment

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

@chlowell would you guys consider exposing this errNoAccount as ErrNoAccount so us devs can use errors.Is()? I'm doing a string comparison currently and it feels messy.

https://github.com/bdwyertech/go-az/blob/529fe3c0feca36fc1cab1e74774222f589c9ecc3/pkg/az/auth.go#L117

Copy link
Member

@bgavrilMS bgavrilMS Aug 16, 2023

Choose a reason for hiding this comment

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

We should. Please open a bug.

But would like to understand your scenario better. In a public client (desktop app) scenario, if the silent auth fails for whatever reason, you should go to interactive auth. Why do you need this level of detail programatically?

Aside: sub error codes like AADSTS700082 can be useful for logging / metrics, but should not be part of app logic, as they are not guarateed to not change (or at least they can be expanded)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants