auth.schema 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. CREATE TABLE IF NOT EXISTS user (
  2. name TEXT NOT NULL UNIQUE,
  3. display_name TEXT NOT NULL UNIQUE,
  4. salt TEXT NOT NULL,
  5. hash TEXT NOT NULL,
  6. totp_secret TEXT DEFAULT NULL,
  7. contact_email TEXT DEFAULT NULL
  8. );
  9. CREATE TABLE IF NOT EXISTS session (
  10. id TEXT PRIMARY KEY UNIQUE,
  11. username TEXT NOT NULL UNIQUE REFERENCES user(name) ON DELETE CASCADE
  12. );
  13. CREATE INDEX IF NOT EXISTS username_idx ON user(name);
  14. CREATE VIEW IF NOT EXISTS sess_user AS SELECT user.name AS name, session.id AS session FROM user JOIN session ON session.username=user.name;
  15. CREATE TABLE IF NOT EXISTS user_acl (
  16. username TEXT NOT NULL UNIQUE REFERENCES user(name) ON DELETE CASCADE,
  17. acl TEXT NOT NULL
  18. );
  19. CREATE TABLE IF NOT EXISTS change_request (
  20. username TEXT NOT NULL REFERENCES user(name) ON DELETE CASCADE,
  21. type TEXT NOT NULL,
  22. token TEXT PRIMARY KEY UNIQUE,
  23. secret TEXT,
  24. processed NUMERIC DEFAULT 0
  25. );
  26. CREATE VIEW IF NOT EXISTS change_request_full AS SELECT cr.username, u.display_name, cr.type, cr.token, cr.secret, cr.processed, u.contact_email from change_request AS cr JOIN user AS u ON u.name=cr.username;
  27. CREATE TABLE IF NOT EXISTS audit_log (
  28. id INTEGER PRIMARY KEY AUTOINCREMENT,
  29. event_time INTEGER NOT NULL,
  30. username TEXT NOT NULL,
  31. session_id TEXT DEFAULT NULL,
  32. ip_addr TEXT NOT NULL DEFAULT '',
  33. event_type TEXT NOT NULL
  34. );
  35. CREATE INDEX IF NOT EXISTS audit_log_time_idx ON audit_log(event_time);
  36. CREATE INDEX IF NOT EXISTS audit_log_user_idx ON audit_log(username);
  37. -- What a user asked us to remember for them, sealed with the installation's
  38. -- master key. See Trog::Vault: the key is not in this file and not in this
  39. -- database, so these rows are ciphertext to anybody who only has the db.
  40. CREATE TABLE IF NOT EXISTS user_secret (
  41. username TEXT NOT NULL REFERENCES user(name) ON DELETE CASCADE,
  42. name TEXT NOT NULL,
  43. key_id TEXT NOT NULL,
  44. salt BLOB NOT NULL,
  45. ciphertext BLOB NOT NULL,
  46. tag BLOB NOT NULL,
  47. created INTEGER NOT NULL,
  48. last_used INTEGER DEFAULT NULL,
  49. PRIMARY KEY (username, name)
  50. );
  51. -- The last TOTP timestep this user spent. A code authorizes things now, so it
  52. -- is worth exactly one thing rather than everything you can do inside its
  53. -- window.
  54. CREATE TABLE IF NOT EXISTS totp_spent (
  55. username TEXT NOT NULL UNIQUE REFERENCES user(name) ON DELETE CASCADE,
  56. step INTEGER NOT NULL
  57. );