feat: add DB-backed token management to SQLiteAdapter#18
Merged
Conversation
Adds a `tokens` table (id, token_hash, entity_id, label, expires_at) with a hash index, and four new methods: createToken, lookupToken, listTokens, revokeToken. Tokens are stored as SHA-256 hashes of the raw token value; the plaintext is returned only at creation time. Also exports the TokenInfo type for use by the server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
tokenstable to the SQLite schema with(id, token_hash, entity_id, label, created_at, expires_at)columns andidx_tokens_hashindex for fast lookupTokenInfotype from@haverstack/adapter-sqliteSQLiteAdapter:createToken(entityId, opts?)— generates a random token, stores SHA-256 hash, returns{ id, token }(plaintext exposed only once)lookupToken(token)— hashes and looks up; returns{ entityId }ornull(respectsexpires_at)listTokens()— returns allTokenInfoobjects without plaintext valuesrevokeToken(id)— deletes by token IDMotivation
The server's
OWNER_TOKENenv var is the single bootstrap credential. All other tokens are issued and revoked at runtime via the/tokensAPI — no server restarts required. Storing hashes (SHA-256) means a DB breach doesn't expose usable tokens.Test plan
createTokenreturns a 16-char hexidand 64-char hex plaintexttokenlookupTokenreturnsentityIdfor a valid tokenlookupTokenreturnsnullfor an unknown tokenlookupTokenreturnsnullfor an expired tokenlookupTokenreturnsentityIdfor a non-expired tokenlistTokensreturns all tokens without plaintext valuesrevokeTokenremoves the token solookupTokenreturnsnulllabelandexpiresAtround-trip throughlistTokensGenerated by Claude Code