Secrets
For: an operator who keeps the secrets of an API with authtempo, or who must change one.
What it does: this guide lists what the app secret protects, and gives the steps of a change that locks nobody out.
Before you start
Section titled “Before you start”- Access to the secret store of the API. On Workers that is
pnpm --filter my-app exec wrangler secret put. On Node and Bun it is the process environment. Deploy sets the first value. - Core at schema version 9 on every instance. An instance at version 8 writes each value under version 1. Upgrades gives the steps.
- A way to run code in the API’s process, for the in-process API. Or the operator routes on, for the commands of the CLI.
- For the operator routes: run
authtempo secret --operator --out operator-secret.txt. It writes the operator secret to the file and prints itssecretHash. Store the operator secret in your secret store, then deleteoperator-secret.txt. authtempo secret gives the flags. Setoperator: { secretHash }in the config, and deploy before step 5. Give the command the secret inAUTHTEMPO_OPERATOR_SECRET. Without the routes, steps 5 and 9 stop with exit 2 and “the operator route is off”. A Better Auth compat app has nooperatoroption, so the CLI forms of steps 5 and 9 do not apply. It calls the in-process API of the line before asauth.tempo.api.secrets.
What the secret protects
Section titled “What the secret protects”| What | How | After a change that follows this guide |
|---|---|---|
| A second factor secret | Sealed with AES-256-GCM under a key from the secret | Opens under its version. It moves to the current version at its next right code, or by rewrap. |
| A private signing key | Sealed under a key from the secret | Opens under its version. It moves at its next signature, or by rewrap. |
| A backup code | HMAC-SHA256 under the secret | Matches under its version until it is used or the list is replaced. |
| A live email code | HMAC-SHA256 under the secret | Passes under its version for its life, at most 60 minutes. |
A social app payload of social.stateData |
Sealed with AES-256-GCM under a key from the secret, in a state row or a challenge row | Opens under its version for its life, at most 10 minutes. rewrap does not move it. |
| A session, an API token, a magic link, a reset link | SHA-256 with no key | No change. |
Each such row names its version in the column secret_version. A read opens a value only under that version. A row from before schema version 9 reads as version 1.
secret is version 1. secrets lists the versions, the current one first, as { version, value } entries. The current version seals and keys each new value. Give secret, secrets, or both. With both, secrets must not give version 1 another value.
The provider secrets, such as social.google.clientSecret and social.apple.privateKey, are separate settings. Key rotation tells how to change them.
The @authtempo packages are on npm. Add the CLI to your app with pnpm add -D @authtempo/cli, and run each command as pnpm exec authtempo ….
-
Make a new secret of 32 random bytes with
authtempo secret --version 2 --out secret-v2.txt. The command writes a new file that only its owner can read, and replaces no file.On a machine with no CLI, use
openssl rand -base64 32. -
Store the line of the file as a second secret, for example
AUTH_SECRET_V2. KeepAUTH_SECRETas it is. Then delete the file. -
Replace
secretwithsecrets, and read the old value as version 1. Put version 1 first, then version 2. Deploy to every instance at 100 percent.On Workers the key reads
secrets: [{ version: 1, value: env.AUTH_SECRET }, { version: 2, value: env.AUTH_SECRET_V2 }]. On Node and Bun, readprocess.envin the same way.Remove the key
secret. While it stays, core adds it back as version 1, and step 10 cannot remove that version.Do not keep
secretbeside asecretslist that holds only version 2. Core addssecretafter it as version 1, so version 2 is current in one deploy, and this step does not happen.A gradual deployment keeps an old version of the API live. That version cannot open a value of version 2, so wait until the new version serves all traffic.
Every instance now knows version 2, and version 1 stays current. Nothing is written under version 2 yet. If you skip this step, an instance on the old config cannot open a value of version 2. Its users then fail the second factor, and its signing key retires at each signature.
-
Put version 2 first in
secrets, then version 1. Deploy to every instance at 100 percent. Each new value is now written under version 2. -
Run
authtempo secret rewrap <base url> --yes. The command ends when no row of an old version remains, when a pass ends, or at--max-calls. It exits 1 when a row did not open or rows remain. authtempo secret rewrap gives the flags.The operator routes allow 120 calls an hour from one IP address. Each call stays inside 50 queries, the limit of D1 Free. The route’s limiter query counts in the 50, so a call moves at most 42 rows. A call may stop early with
more: true, and the command calls again. A run makes at most 100 calls, so it moves about 4 200 rows. Run it again while it exits 1.In the API’s process, the same pass is
auth.api.secrets.rewrap({ limit: 100 }). The answer is a result: readvaluewhenokis true. On Workers Paid or Postgres, passcallBudgetup to 1000 to move more rows in one call. -
In the API’s process, pass the
nextof each answer asafterto the next call. Stop whennextisnull. That is one pass. The command does this step itself. -
Run a new pass from step 5, with no
after, while the last answer hasmore: true. Wait 15 minutes first whenskippedorfailedwas above 0. With the command, run it again while it exits 1.A second run in the same hour can reach the limit of 120 calls. It then stops with exit 2 and a line that names the limit. Wait one hour, then run it again.
After 3 passes with the same
failedcount, stop and read the second warning below. Such a row fails on every pass. -
Wait for the life of the live email codes:
emailOtp.ttlMinutes, 10 minutes by default. Wait at least 10 minutes, also with a shorter code life. A social app payload lives 10 minutes, andrewrapdoes not move it. -
Run
authtempo secret status <base url>. Remove a version only when the command lists each of its counts as 0. authtempo secret status gives the flags.In the API’s process, run
auth.api.secrets.status(). Read the entry of version 1. WhenemailCodesis above 0, wait foremailOtp.ttlMinutesagain, then repeat step 9. WhenstateDatais above 0, wait 10 minutes, then repeat step 9. -
Remove version 1 from
secretswhentotpSecrets,signingKeys,emailCodes,stateData,backupCodes, andssoClientSecretsare 0. A count ofssoClientSecretsabove 0 means a sealed client secret of an SSO connection. Without the version, its sign-in fails. A SCIM token has no count. Core stores its SHA-256, not a sealed value, so a new app secret keeps it working. Make sure that the config has no keysecret. Deploy.
On Node and Bun, restart each process: it reads the environment only at start.
Then delete AUTH_SECRET from the secret store, on every runtime. On Workers that is pnpm --filter my-app exec wrangler secret delete AUTH_SECRET.
When backupCodes stays above 0, read the first warning below before you remove the version.
packages/core/README.md, section “Secrets”, holds the rewrap loop as code. The table prefix ta_ and the names in the seal keep the old product name tempo-auth for compatibility. Schema and migrations states the rule.
How it behaves
Section titled “How it behaves”createAuthrefuses any secret shorter than 32 characters. It also refuses a current secret with an estimate under 100 bits.- The estimate is the length times log2 of the distinct characters. An estimate under 120 bits writes one warning line, also for an old version.
- The estimate does not see a pattern:
0123456789abcdeftwice gives 128 bits. Use random bytes. - One pass of
rewrapreads each row of a configured old version once, in the order of the ids. A row that fails stays behind the cursor, so it never stops a later row. moreis true while a row of a configured old version remains, also a row that failed or was skipped. Alimitoutside 1 to 1000, or a cursor thatrewrapdid not answer, answersinvalid_body.skippedcounts a TOTP setup that is not confirmed, holds backup codes, and is less than 15 minutes old. A later pass reseals the expired setup and clears its codes.failedcounts a row that did not open under its version, or that a concurrent write changed first. The log names the table, the row, and the version. A later pass reads it again.statusreads every row of five tables, one query per 1000 rows. It reads the live codes and the live social state rows the same way. Run it from a script, not from a request.stateDatacounts the live sealed app payloads ofsocial.stateData: state rows, and challenge rows of a social sign-in. A removed version fails each such sign-in withinvalid_token, and the person starts again.rewrapleaves these rows, because each lives at most 10 minutes.- A backup code cannot move to a new version: core never sees the code again.
statuscounts the unused codes under each version. rewrapnever reads a row of a version that the config does not hold.statuslists that version withconfigured: false.- A signing key of a removed version does not open. It retires at the next signature, and a new key signs.
- A new secret changes no password hash. PBKDF2 needs no key.
If it fails
Section titled “If it fails”invalid_codeat the second factor: the instance does not know the version of the user’s row. Do step 3 on every instance.invalid_codefor an email code: its version left the config before the code expired. Ask for a new code.invalid_bodyfromrewrap: thelimitis outside 1 to 1000, orafteris not anextthatrewrapanswered.createAuththrows a config error that names an estimate of bits: the current secret is weak. Make a new one with step 1.- A log line names a secret version that is not configured: a row holds a version that the config lacks. Add the version back, or let that row fail.