Skip to content

Include configurable token max lengths in ATM 4.3.x - #371

Merged
pgermishuys merged 5 commits into
releases/atm/4.3.xfrom
pg/atm-4.3-token-max-length
Oct 16, 2026
Merged

pgermishuys merged 5 commits into
releases/atm/4.3.xfrom
pg/atm-4.3-token-max-length

Conversation

@pgermishuys

@pgermishuys pgermishuys commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Brings #367 to 4.3.x. The fixed token length limits become options, so apps that receive larger tokens can raise them: MaxAccessTokenLength on ClientCredentialsTokenManagementOptions, and MaxAccessTokenLength, MaxIdentityTokenLength and MaxRefreshTokenLength on UserTokenManagementOptions. The defaults are the old limits. AccessToken, IdentityToken and RefreshToken get Parse and TryParse overloads that take a max length, so custom IUserTokenStore implementations can honour the settings. The public API only gains members.

Cherry-picked both commits of #367 with git cherry-pick -x, no conflicts. Without #369 in place, the XML doc comments in AccessToken.cs, IdentityToken.cs and RefreshToken.cs conflict, which is why this sits on top of it.

Also includes the fix from #377, cherry-picked with -x. The cache test from #367 raced with HybridCache's background write to the distributed cache and failed now and then on CI.

Merge order: third of five, after #370. Until #369 and #370 merge, this PR also shows their commits.

damianh and others added 4 commits October 16, 2026 14:01
Add XML docs for ATM public API

(cherry picked from commit 6eaad5d)
Downgrade 4 ATM user token log messages from Error to Warning

(cherry picked from commit c3b1d09)
Access, refresh and identity tokens were always checked against fixed
limits (32768, 4096 and 32768 characters). Token services that issue
longer tokens, such as ADFS and Entra refresh tokens or identity tokens
with many claims, broke login or refresh with no way to opt in.

- Add ClientCredentialsTokenManagementOptions.MaxAccessTokenLength for
  client credentials access tokens (token endpoint responses and cache
  reads)
- Add UserTokenManagementOptions.MaxAccessTokenLength,
  MaxRefreshTokenLength and MaxIdentityTokenLength for user tokens
  (refresh responses and tokens read back from the authentication
  session)
- Default every option to the existing limit, so apps that change
  nothing behave as before; values must be greater than zero
- Add Parse and TryParse overloads that take a max length to
  AccessToken, RefreshToken and IdentityToken, plus
  IdentityToken.ParseOrDefault(string?, int), so custom IUserTokenStore
  implementations can honour a configured limit
- Name the option to raise in the error when a token is too long; the
  message never includes the token value
- Apply the client credentials limit when reading from HybridCache
  through a runtime JSON converter on a source-generated context; the
  cached format is unchanged

(cherry picked from commit 7787219)
The cache read checked the access token against this instance's
MaxAccessTokenLength. When two instances share a distributed cache
with different limits, or after an operator lowers the limit, the
instance with the lower limit can't read the cached entry. Its catch
block then calls the token endpoint and never writes the new token
back, so every call goes to the token endpoint until the old entry
expires.

ATM only caches a token after the token endpoint response passed the
configured limit, so the cache read doesn't need to check it again.
The serializer now reads the cached value without validation, and the
limit applies to token endpoint responses only.

(cherry picked from commit 8d63ccc)
HybridCache returns the token to the caller and then writes it to the distributed cache in the background. The test started instance B straight after instance A returned, so on a busy CI runner B could read the shared cache before A's write landed, fetch its own token and fail. The test now waits for A's write before it starts B.

(cherry picked from commit 93e2a42)
@pgermishuys
pgermishuys merged commit 9ea48f9 into releases/atm/4.3.x Oct 16, 2026
2 checks passed
@pgermishuys
pgermishuys deleted the pg/atm-4.3-token-max-length branch October 16, 2026 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/foss/atm Issues related to Access Token Management

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants