Hash OAuth client secrets with one round, not a million #138

Merged
CrowderSoup merged 1 commit from claude/fast-client-secret-hash into main 2026-11-25 14:40:43 +00:00
Owner

Token refreshes from clients that have a secret (the claude.ai connector, Google's clients) take 1.0-2.6 s. The token endpoint's wall-clock profile in Pyroscope shows 80% of that time in PBKDF2PasswordHasher.verify.

django-oauth-toolkit hashes client secrets with Django's password hasher, which runs over a million PBKDF2 rounds to slow down guessing passwords people chose. Our client secrets are 128 random characters we generate, so those rounds add no security.

What changes

  • apps/api/hashers.py: ClientSecretHasher is PBKDF2-SHA256 with one round, salted. It's set as CLIENT_SECRET_HASHER and appended to PASSWORD_HASHERS. Django's own hasher stays first, so user passwords are untouched.
  • FastClientSecrets (validator mixin): after a client authenticates with its secret, a hash made by an older hasher is replaced with the fast one. That's the only moment we have the plaintext. The update is conditional on the stored value, so a concurrent change wins.
  • New clients from dynamic registration get the fast hash immediately.

Locally a secret check goes from about 140 ms to under 0.1 ms; on the droplet it's 1-3.5 s down to roughly nothing. The first refresh per existing client still pays the old cost once.

Tests: test_client_secrets.py covers new secrets, rehashing via basic and body auth, wrong secrets, and current hashes. The full suite passes.

🤖 Generated with Claude Code

Token refreshes from clients that have a secret (the claude.ai connector, Google's clients) take 1.0-2.6 s. The token endpoint's wall-clock profile in Pyroscope shows 80% of that time in `PBKDF2PasswordHasher.verify`. django-oauth-toolkit hashes client secrets with Django's password hasher, which runs over a million PBKDF2 rounds to slow down guessing passwords people chose. Our client secrets are 128 random characters we generate, so those rounds add no security. **What changes** - `apps/api/hashers.py`: `ClientSecretHasher` is PBKDF2-SHA256 with one round, salted. It's set as `CLIENT_SECRET_HASHER` and appended to `PASSWORD_HASHERS`. Django's own hasher stays first, so user passwords are untouched. - `FastClientSecrets` (validator mixin): after a client authenticates with its secret, a hash made by an older hasher is replaced with the fast one. That's the only moment we have the plaintext. The update is conditional on the stored value, so a concurrent change wins. - New clients from dynamic registration get the fast hash immediately. Locally a secret check goes from about 140 ms to under 0.1 ms; on the droplet it's 1-3.5 s down to roughly nothing. The first refresh per existing client still pays the old cost once. **Tests:** `test_client_secrets.py` covers new secrets, rehashing via basic and body auth, wrong secrets, and current hashes. The full suite passes. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Hash OAuth client secrets with one round, not a million
All checks were successful
tests / pytest (push) Successful in 2m14s
00c8f4697a
Token requests from clients with a secret (claude.ai refreshing) spent
1-2.6 s in PBKDF2 checking it: 80% of the token endpoint's wall time in
Pyroscope. Client secrets are 128 random characters we generated, so
key stretching adds nothing. Old hashes are replaced the next time the
client signs in with its secret.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No description provided.