What changes when you turn on Doorkeeper's hash_token_secrets in a provider that already has live access and refresh tokens?
answer
- a digest stored, not the value
- plaintext_token exists once, in memory
- fallback: :plain upgrades old rows on use
- reuse_access_token switched off with a warning
- BCrypt allowed for application secrets only
basics
~20 sDoorkeeper then stores a SHA-256 digest of each new access token, refresh token and authorization code; existing plaintext tokens stop matching unless you add fallback: :plain, which upgrades each on its next use, and reuse_access_token is disabled.
solid answer
~40 s`hash_token_secrets` switches the token strategy from `Doorkeeper::SecretStoring::Plain` to `Sha256Hash`, so `oauth_access_tokens.token`, `refresh_token` and the grants' `token` (the authorization code) hold hex digests. Lookups hash the presented value and `find_by` the digest, so the unique index still works. The raw value exists only on the freshly created record (`plaintext_token`), which the token response sends; afterwards nobody can show it. On a live provider the old rows still hold plaintext and stop matching, signing every client out, so deploy `hash_token_secrets fallback: :plain`: a miss on the digest retries with the plain value and rewrites that row as a digest. `reuse_access_token` needs plaintext back, so Doorkeeper logs a warning and turns it off. The `BCrypt` strategy is accepted only by `hash_application_secrets`.
code
ruby · 11 lines# config/initializers/doorkeeper.rb
Doorkeeper.configure do
# digests new tokens; finds and upgrades old plaintext rows on use
hash_token_secrets fallback: :plain
# client secrets: BCrypt is allowed here, never for tokens
hash_application_secrets using: "::Doorkeeper::SecretStoring::BCrypt",
fallback: :plain
use_refresh_token
endgo deeper
Recall that hash_token_secrets makes Doorkeeper store a digest of each token instead of the token, so a copied table no longer hands out working tokens.
Explain that lookups hash the presented value and find_by the digest, that plaintext_token exists only on the new record, and that client secrets have their own switch.
Show the rollout: deploy with fallback: :plain, let presented tokens upgrade themselves, expect reuse_access_token to switch off, and plan for refresh tokens that never expire on their own.
Weigh hashing at rest against the features it removes, such as showing or reusing tokens, and place it beside short lifetimes and revocation in the provider's token policy.
## What the option changes By default Doorkeeper stores secrets through `Doorkeeper::SecretStoring::Plain`, so a copy of `oauth_access_tokens` is a list of working tokens. `hash_token_secrets` in the initializer switches the **token strategy** to `::Doorkeeper::SecretStoring::Sha256Hash`, which stores `Digest::SHA256.hexdigest` of the value instead. (The generated initializer's comment calls the default `Doorkeeper::Hashing::SHA256`; the class that ships is `Sha256Hash`.) | Column | Before | After | |---|---|---| | `oauth_access_tokens.token` | raw access token | 64-character hex digest | | `oauth_access_tokens.refresh_token` | raw refresh token | hex digest | | `oauth_access_grants.token` | raw authorization code | hex digest | | `oauth_applications.secret` | unchanged | unchanged (separate switch) | ## How lookups still work SHA-256 is deterministic, so the same input always gives the same digest. `by_token` and `by_refresh_token` call `find_by_plaintext_token`, which transforms the presented value and runs one `find_by` on the digest. The unique index on `token` keeps working, and a lookup costs what it did before. The raw value exists in exactly one place: the in-memory record that was just created. `plaintext_token` and `plaintext_refresh_token` return it from instance variables, and Doorkeeper's token response reads them to answer the client. Load the same row again with `find` and both return `nil`, because `Sha256Hash.allows_restoring_secrets?` is false. ## Turning it on in a live provider Existing rows hold plaintext. After the switch, a presented token is hashed, the digest matches nothing, and every client of the running-club platform gets 401. The `fallback:` option handles the migration: 1. Deploy `hash_token_secrets fallback: :plain`. 2. On a miss, `find_by_fallback_token` looks the value up as plaintext. 3. On a hit, `upgrade_fallback_value` rewrites that row's column with the digest, so the plaintext leaves the table on first use. 4. Rows never presented again keep their plaintext until they are revoked or removed. The fallback's lifetime is the subtle part. Access tokens expire, but Doorkeeper accepts a refresh token for as long as it is not revoked, so an old plaintext refresh token can arrive months later. Remove `fallback: :plain` only after revoking the pre-switch refresh tokens, accepting that those clients must ask the member to approve again. ## What you give up - **Showing tokens later.** No screen, console or support tool can print a token after it was issued. - **`reuse_access_token`.** Reuse hands an existing token back to the client, which needs the plaintext. When both options are set, Doorkeeper's config validation logs `[DOORKEEPER] You have configured both reuse_access_token AND ...` and sets `reuse_access_token` to false. - **Rows read directly.** Any code that read `token` from the table for comparison must hash the input first, or use `by_token`. ## Application secrets are a separate switch `hash_application_secrets` does the same for `oauth_applications.secret`, again defaulting to `Sha256Hash`, and it also accepts `using: "::Doorkeeper::SecretStoring::BCrypt"` when the `bcrypt` gem is loaded. That works because a client is found by `uid` first and its secret then compared with `secret_matches?`. Tokens are found by their value, so a salted hash cannot be looked up, and Doorkeeper's config validation raises `ArgumentError` at `Doorkeeper.configure` if the BCrypt strategy is given to `hash_token_secrets`. With application hashing on, the admin screen shows a new secret once, from the flash, right after creation. ## A quick check in the console - `Doorkeeper::AccessToken.last.token.length` returns 64 for a new row. - `Doorkeeper::AccessToken.by_token(raw)` still finds a token from the value a client holds. - `Doorkeeper.config.reuse_access_token` returns `false` if both options were set. ## Common misreadings - **"Old tokens keep working."** Not without `fallback: :plain`; the digest of a token never equals its stored plaintext. - **"BCrypt is stronger, so use it for tokens."** Tokens are long random values, so a fast hash suffices, and BCrypt is refused for them because rows are found by value. - **"Hashing covers client secrets too."** That is `hash_application_secrets`, a separate line. - **"The fallback can go after the access-token lifetime."** Refresh tokens outlive it. Hashing at rest protects the copy of the table that leaks through a backup, a replica or a careless export; it does nothing for a token stolen from the client or from logs, which is why Doorkeeper also adds `access_token`, `refresh_token`, `client_secret` and, with the code flow, `code` to Rails' `filter_parameters`.
- When is it safe to remove fallback: :plain?Only when no pre-switch token can still be presented. Access tokens expire, but Doorkeeper honours a refresh token until it is revoked, so plaintext refresh tokens from before the switch can return indefinitely. Revoke those rows first, accepting that their clients must send the member through consent again, then drop the fallback.
- How does hash_application_secrets differ from hash_token_secrets?It covers `oauth_applications.secret` instead of tokens. It also defaults to `Sha256Hash` but accepts `::Doorkeeper::SecretStoring::BCrypt`, because a client is found by `uid` first and its secret compared afterwards, so a salted hash works. Doorkeeper's admin screen then shows a new secret only once, right after creation.
- Turning on hash_token_secrets keeps existing plaintext tokens working with no extra option.
- Support staff can still look up a member's token value after hashing.
- Tokens can be hashed with BCrypt, exactly like application secrets.
- reuse_access_token keeps working alongside hash_token_secrets.
- hash_token_secrets also hashes the client secrets in oauth_applications.