21_add_two_factor.up.sql 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. ALTER TABLE users ADD COLUMN is_two_factor_enabled boolean;
  2. UPDATE users SET is_two_factor_enabled = 'f';
  3. ALTER TABLE users ALTER COLUMN is_two_factor_enabled SET NOT NULL;
  4. ALTER TABLE users ALTER COLUMN is_two_factor_enabled SET DEFAULT FALSE;
  5. CREATE TABLE IF NOT EXISTS two_factor(
  6. user_id INTEGER NOT NULL UNIQUE,
  7. two_factor_secret_hash TEXT UNIQUE,
  8. encrypted_two_factor_secret BYTEA,
  9. two_factor_secret_decryption_nonce BYTEA,
  10. recovery_encrypted_two_factor_secret TEXT,
  11. recovery_two_factor_secret_decryption_nonce TEXT,
  12. CONSTRAINT fk_two_factor_user_id
  13. FOREIGN KEY(user_id)
  14. REFERENCES users(user_id)
  15. ON DELETE CASCADE
  16. );
  17. CREATE TABLE IF NOT EXISTS temp_two_factor(
  18. user_id INTEGER NOT NULL,
  19. two_factor_secret_hash TEXT UNIQUE,
  20. encrypted_two_factor_secret BYTEA,
  21. two_factor_secret_decryption_nonce BYTEA,
  22. creation_time BIGINT NOT NULL,
  23. expiration_time BIGINT NOT NULL,
  24. CONSTRAINT fk_two_factor_user_id
  25. FOREIGN KEY(user_id)
  26. REFERENCES users(user_id)
  27. ON DELETE CASCADE
  28. );
  29. CREATE TABLE IF NOT EXISTS two_factor_sessions(
  30. user_id INTEGER NOT NULL,
  31. session_id TEXT UNIQUE NOT NULL,
  32. creation_time BIGINT NOT NULL,
  33. expiration_time BIGINT NOT NULL,
  34. CONSTRAINT fk_sessions_user_id
  35. FOREIGN KEY(user_id)
  36. REFERENCES users(user_id)
  37. ON DELETE CASCADE
  38. );