Skip to content

fix(auth): stop logging tokens and fix the refresh, sign-out and provider-build paths - #3152

Merged
kazo0 merged 1 commit into
mainfrom
dev/sb/auth-core-fixes
Aug 24, 2026
Merged

kazo0 merged 1 commit into
mainfrom
dev/sb/auth-core-fixes

Conversation

@kazo0

@kazo0 kazo0 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

GitHub Issue (If applicable): split out of #3139

PR Type

  • Bugfix

What is the current behavior?

Five defects in Uno.Extensions.Authentication, all provider-independent, none covered by a test:

  • Tokens reach the logs. TokenCache prints every cached value at Trace when it enumerates keys, and HeaderHandler logs the bearer token at Debug on every outbound request. Both go to the consumer's configured logging pipeline (AGENTS.md §7 forbids exactly this).
  • A refresh that cannot renew the session leaves the user "authenticated". RefreshAsync saves whatever the provider returns; a provider that returns nothing (expired or revoked refresh token) leaves the cache with no tokens but never raises ITokenCache.Cleared, so LoggedOut is not raised and an app that navigates on it sits on a signed-in page with nothing to send.
  • A sign-out that throws leaves the user signed in. If the provider fails part-way through LogoutAsync (a locked keychain, a cache-file error), the exception escapes before _tokens.ClearAsync runs: the access token survives, IsAuthenticated stays true, and the HTTP handlers keep sending the bearer for a user who asked to sign out.
  • Providers can be built more than once. AuthenticationService populated its provider table behind a Count == 0 check and ProviderFactory configured its provider with a bare ??=; two concurrent callers - an HTTP handler refreshing while the UI asks IsAuthenticated, say - could both build. ConfigureProvider returns a record copy, so the losing instance stayed alive, subscribed to ITokenCache.Cleared, with nothing left to unsubscribe it.
  • IAuthenticationService.LoginAsync(CancellationToken)'s docs did not say that it passes no IDispatcher, which some providers require.

What is the new behavior?

  • TokenCache logs key + value length; HeaderHandler logs the scheme only.
  • RefreshAsync clears the token cache when the provider returns no tokens, so Cleared and LoggedOut fire - the same as an explicit sign-out. A provider that keeps its tokens on a transient failure is unaffected.
  • LogoutAsync clears the token cache (CancellationToken.None, so a cancelled token cannot skip it) and rethrows when the provider throws. A provider returning false is unchanged: that is "the sign-out did not happen", and the session stands.
  • The provider table and each configured provider are built exactly once (Lazy, ExecutionAndPublication). Providers still reports only what has been built - reading a property must not construct MSAL's client application.
  • The LoginAsync(CancellationToken) remarks say what the overload cannot do.

New Uno.Extensions.Authentication.Tests (plain net9.0 MSTest, in the package-CI VSTest filter): 12 tests - refresh returns nothing / empty / tokens, logout throws / throws with a cancelled token / declines, concurrent calls build once, Providers does not build, ProviderFactory configures once, and two log-leak guards. Every fix was verified red before it: the two leak guards fail against main's TokenCache and HeaderHandler, the refresh and logout cases against main's AuthenticationService.

PR Checklist

  • Tested code with current supported SDKs
  • Docs have been added/updated which fit documentation template - XML docs on the changed overload; behavior changes are listed in UpdatingExtensions.md on feat(msal): platform-correct sign-in, token cache and sign-out, tested on four heads #3139, to land with the MSAL layer that depends on them
  • Unit Tests and/or UI Tests for the changes have been added
  • Wasm UI Tests are not showing any unexpected differences
  • Contains NO breaking changes - binary-compatible; LoggedOut now fires in one more situation (an unrenewable session), which is the documented meaning of the event
  • Updated the Release Notes
  • Associated with an issue (GitHub or internal)

Other information

Verified locally: Uno.Extensions.Authentication.Tests 12/12 (2/12 red with main's TokenCache + HeaderHandler); Uno.Extensions-packageonly.slnf Release build 0 errors, which covers the MSAL, OIDC and Authentication.WinUI consumers of the changed internals.

Reviewer note: this is the auth-core layer of #3139's split. The MSAL provider changes that build on it (TokensOrNull, keeping tokens on transient refresh failures, sign-out removing every account) come in a later PR with their end-to-end tests; nothing here depends on them.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EqfQh5ovrPEvFGcERLiLhH

…ider-build paths

Provider-independent defects in Uno.Extensions.Authentication, none of them
covered by a test until now. Split out of #3139 as its auth-core layer.

- TokenCache logged every cached value at Trace when enumerating keys, and
  HeaderHandler logged the bearer at Debug on every outbound request - both to
  the consumer's configured logging pipeline (AGENTS.md section 7). They now
  log key + value length, and the scheme only
- AuthenticationService.RefreshAsync saved whatever the provider returned; a
  provider returning nothing (expired or revoked refresh token) left no tokens
  but never raised ITokenCache.Cleared, so LoggedOut did not fire and an app
  that navigates on it sat on a signed-in page with nothing to send. It now
  clears the cache, the same as an explicit sign-out
- LogoutAsync let a provider exception escape before _tokens.ClearAsync ran, so
  a sign-out that failed part-way (a locked keychain, a cache-file error) left
  the access token in place and IsAuthenticated true. It now clears with
  CancellationToken.None - a cancelled token must not skip it - and rethrows.
  A provider returning false is unchanged: that is "the sign-out did not
  happen", and the session stands
- The provider table was populated behind a Count == 0 check and each provider
  configured with a bare ??=, so two concurrent callers could both build.
  ConfigureProvider returns a record copy, so the losing MSAL instance stayed
  subscribed to ITokenCache.Cleared with nothing left to unsubscribe it. Both
  are Lazy (ExecutionAndPublication) now; Providers still reports only what has
  been built, since reading a property must not construct a client application
- The LoginAsync(CancellationToken) remarks now say the overload passes no
  IDispatcher

New Uno.Extensions.Authentication.Tests (net9.0 MSTest, in the package-CI
VSTest filter): 12 tests. Every fix was verified red first - the two log-leak
guards fail against main's TokenCache and HeaderHandler, the refresh and
sign-out cases against main's AuthenticationService.

Verified: Authentication.Tests 12/12; Uno.Extensions-packageonly.slnf Release
0 errors, which covers the MSAL, OIDC and Authentication.WinUI consumers.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01EqfQh5ovrPEvFGcERLiLhH
@kazo0
kazo0 enabled auto-merge August 24, 2026 16:41
@kazo0
kazo0 merged commit 85e75d7 into main Aug 24, 2026
17 of 18 checks passed
@kazo0
kazo0 deleted the dev/sb/auth-core-fixes branch August 24, 2026 17:06
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.

2 participants