feat(auth): add refresh tokens, admin role and user management
- feat(auth): short-lived access tokens plus rotating refresh tokens stored as
SHA-256 hashes; replaying a spent token revokes every session of that user
- feat(auth): is_admin flag; the first registered user is the admin and the
last admin can be neither demoted nor deleted
- feat(api): POST /auth/refresh, /auth/logout, /auth/password and GET /auth/me
- feat(api): admin-only GET/POST /users, PATCH /users/{id}, PUT
/users/{id}/password and DELETE /users/{id}; the admin check reads the role
from the database on every call
- feat(api): starting a library scan is now admin-only
- feat(storage): users and refresh token repositories for SQLite and
PostgreSQL (migration 0004; the oldest existing account becomes admin)
- feat(config): auth.access_ttl_minutes and auth.refresh_ttl_hours replace
auth.token_ttl_hours
- test: rotation, reuse detection, concurrent rotation with a single winner,
admin rules, role changes taking effect immediately
This commit is contained in:
parent
b9c9328fc2
commit
dbe9338af3
1 changed files with 1883 additions and 187 deletions
18
migrations/sqlite/0004_admin_refresh.sql
Normal file
18
migrations/sqlite/0004_admin_refresh.sql
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
-- Admin flag and rotating refresh tokens.
|
||||
|
||||
ALTER TABLE users ADD COLUMN is_admin INTEGER NOT NULL DEFAULT 0;
|
||||
|
||||
-- Accounts that already exist: the oldest one becomes the admin.
|
||||
UPDATE users SET is_admin = 1 WHERE id = (SELECT MIN(id) FROM users);
|
||||
|
||||
-- Only a hash of the token is stored, never the token itself.
|
||||
CREATE TABLE refresh_tokens (
|
||||
id INTEGER PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
token_hash TEXT NOT NULL UNIQUE, -- SHA-256 of the token, hex
|
||||
created_at INTEGER NOT NULL, -- unix seconds
|
||||
expires_at INTEGER NOT NULL, -- unix seconds
|
||||
revoked_at INTEGER -- unix seconds, NULL while active
|
||||
);
|
||||
|
||||
CREATE INDEX idx_refresh_tokens_user ON refresh_tokens(user_id);
|
||||
Loading…
Reference in a new issue