Skip to content

fix(auth): inject ITokenCacheService and add registration guard (#37)#52

Merged
jaybarden1 merged 2 commits into
mainfrom
bug/37-token-cache-never-registered
May 28, 2026
Merged

fix(auth): inject ITokenCacheService and add registration guard (#37)#52
jaybarden1 merged 2 commits into
mainfrom
bug/37-token-cache-never-registered

Conversation

@jaybarden1

Copy link
Copy Markdown
Contributor

Summary

  • ITokenCacheService was never injected into AuthService, so RegisterAsync was never called — MSAL used only an in-memory cache, losing all sessions on every restart
  • Add ITokenCacheService tokenCacheService as third constructor parameter
  • Add EnsureCacheRegisteredAsync guard — registers the file-backed cache exactly once per instance; shared across SignInInteractiveAsync and AcquireTokenSilentAsync
  • Add LogAuthFailed call in MsalUiRequiredException catch block (was missing, violating functional-usage.md rule 10)
  • 8 new tests: cache registration, cross-method guard, failure retry behaviour, and error logging

Test plan

  • dotnet build — 0 errors, 0 warnings
  • dotnet run --project test/AStar.Dev.CloudSyncFunctional.Tests.Unit — 207 passed, 0 failed
  • dotnet run --project test/AStar.Dev.CloudSyncFunctional.Tests.Integration — 31 passed, 0 failed
  • RegisterAsync called exactly once even when both SignInInteractiveAsync and AcquireTokenSilentAsync are called on the same instance
  • RegisterAsync failure leaves guard unset so next call retries

Closes #37

🤖 Generated with Claude Code

jaybarden1 and others added 2 commits May 28, 2026 13:44
AuthService currently takes only 2 constructor args (IPublicClientApplication,
ILogger<AuthService>). These tests require a 3rd arg (ITokenCacheService) and
assert that:
  - SignInInteractiveAsync calls ITokenCacheService.RegisterAsync exactly once
  - AcquireTokenSilentAsync calls ITokenCacheService.RegisterAsync exactly once
  - Calling SignInInteractiveAsync twice only triggers RegisterAsync once (guard)
  - MsalUiRequiredException in AcquireTokenSilentAsync logs at Error level

Build result: 4 errors (2 pre-existing in GivenAResult.cs; 2 new CS1729
'AuthService does not contain a constructor that takes 3 arguments').

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Add ITokenCacheService as third constructor parameter to AuthService
- Add EnsureCacheRegisteredAsync guard — calls RegisterAsync once per
  instance; subsequent calls on either SignIn or AcquireTokenSilent are
  no-ops so the file-backed cache is registered exactly once per session
- Call EnsureCacheRegisteredAsync at the top of SignInInteractiveAsync
  and AcquireTokenSilentAsync so every token operation activates the cache
- Add LogAuthFailed call in MsalUiRequiredException catch block — error
  paths must log per functional-usage.md rule 10
- Update GivenAnAuthService factory and add 8 new tests covering
  cache registration, cross-method guard, failure retry, and logging

Closes #37

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jaybarden1 jaybarden1 requested a review from a team May 28, 2026 13:14
@github-actions

Copy link
Copy Markdown

Test results

0 tests  ±0   0 ✅ ±0   0s ⏱️ ±0s
0 suites ±0   0 💤 ±0 
0 files   ±0   0 ❌ ±0 

Results for commit d6cddea. ± Comparison against base commit e5e503b.

@jaybarden1 jaybarden1 enabled auto-merge (squash) May 28, 2026 13:16

@jbarden jbarden left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

reviewed locally

@jaybarden1 jaybarden1 merged commit c834bf5 into main May 28, 2026
5 of 6 checks passed
@jaybarden1 jaybarden1 deleted the bug/37-token-cache-never-registered branch May 28, 2026 13:17
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(auth): Token cache never registered — sessions lost on every restart

2 participants