Repository navigation
Verify a controller's access token once, not on every request (CHOO-3667) - #1263
abeldantas wants to merge 1 commit into
Conversation
…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.
There was a problem hiding this comment.
🟡 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
VerifiedTokensclass intokens.pythat caches verified access-token claims in a bounded, least-recently-used map. The existing verification logic moves into a shared helper. - Switches
ManagementAuthenticatorinauth.pyto 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 |
|
Combined into #1307 with the other scaling PRs. |

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
VerifiedTokenskeeps 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.