Skip to content

Verify a controller's access token once, not on every request (CHOO-3667) - #1263

Closed
abeldantas wants to merge 1 commit into
mainfrom
verified-token-memo
Closed

abeldantas wants to merge 1 commit into
mainfrom
verified-token-memo

Conversation

@abeldantas

@abeldantas abeldantas commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

A controller sends the same access token with every request for an hour, and each request verified it again. PyJWT checks every character of the token in Python, which cost more than the rest of authenticating it. In this PR VerifiedTokens keeps each token that verified, by its sha256, with its claims until it expires. Its expiry is checked on every use, and revocation is still checked against the controller as before.

Locally (400 agents, 20 messages a second), this and #1262 take CPU per message from 13.2 to 12.7 ms.

…667)

A controller sends the same access token with every request for an
hour, and each request verified it again: PyJWT checks every character
of the token in Python, which cost more than the rest of authenticating
it. VerifiedTokens keeps each token that verified, by its sha256, with
its claims until it expires; its expiry is checked on every use, and an
expired one is verified again and refused as expired. Revocation is
still checked against the controller as before.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

One active finding remains: a module docstring in tokens.py still says tokens are never stored, which is now inaccurate because verified tokens are cached until they expire, though the issue is documentation-only.

1 open issue
What changed in this PR

This PR speeds up controller authentication. A controller sends the same access token with every request for up to an hour, and each request used to decode and verify it again. PyJWT's character-by-character checks made that the main cost. The change keeps each token that verified, keyed by its sha256, until it expires. Expiry is still checked on every use, and revocation is still checked against the controller on every request.

Changes:

  • Adds a VerifiedTokens class in tokens.py that caches verified access-token claims in a bounded, least-recently-used map. The existing verification logic moves into a shared helper.
  • Switches ManagementAuthenticator in auth.py to use the cache instead of verifying each token on every request.
  • Adds tests for single decoding, expiry, altered tokens, and the cache size bound. Adds a CHANGELOG entry.
File Description
core/​tests/​switch_core/​management/​test_verified_tokens.py New tests: a token is decoded once, an expired token is refused, an altered token misses the cache, and the cache size bound holds.
core/​switch_core/​management/​tokens.py Adds the VerifiedTokens cache and moves the decode logic into a shared _verify helper that also returns exp.
core/​switch_core/​management/​auth.py Makes the authenticator use VerifiedTokens in place of the per-request verification.
CHANGELOG.md Adds an entry noting that access tokens are now verified once.

Review effort: Lite



class VerifiedTokens:
"""`verify_access_token`, remembering the tokens that verified until
@abeldantas

Copy link
Copy Markdown
Collaborator Author

Combined into #1307 with the other scaling PRs.

@abeldantas abeldantas closed this Nov 2, 2026
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