OXYGENHUB
◆ v0.9.24 oxygenhub / crates / hub / migrations-pg / 0104_second_factor.sql
1.3 KiB · SQL · last changed A phone as the second factor: Keystore keys approve sign-ins by the number the browser shows, and the sudo step asks a stale session again before sensitive changes (closes #356) 3 days ago source blame history raw
1
-- Phones as a second factor (oxygenhub#356): a phone's Keystore key, kept
2
-- with the sign-in it made (gone when that token is revoked); the
3
-- approvals it is asked for, each a number to match; and when each
4
-- browser session last proved a second factor, for the sudo step.
5
CREATE TABLE authenticators (
6
    token_id     BIGINT PRIMARY KEY REFERENCES tokens(id) ON DELETE CASCADE,
7
    account_id   BIGINT NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
8
    name         TEXT NOT NULL,
9
    public_key   TEXT NOT NULL,
10
    created_ms   BIGINT NOT NULL,
11
    last_used_ms BIGINT NOT NULL DEFAULT 0
12
);
13
CREATE INDEX authenticators_by_account ON authenticators (account_id);
14
15
CREATE TABLE approvals (
16
    id          TEXT PRIMARY KEY,
17
    account_id  BIGINT NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
18
    kind        TEXT NOT NULL,
19
    what        TEXT NOT NULL,
20
    number      BIGINT NOT NULL,
21
    choices     TEXT NOT NULL,
22
    session_id  BIGINT,
23
    created_ms  BIGINT NOT NULL,
24
    expires_ms  BIGINT NOT NULL,
25
    state       TEXT NOT NULL DEFAULT 'pending',
26
    decided_ms  BIGINT NOT NULL DEFAULT 0,
27
    decided_by  BIGINT
28
);
29
CREATE INDEX approvals_by_account ON approvals (account_id, state);
30
31
ALTER TABLE sessions ADD COLUMN second_factor_ms BIGINT NOT NULL DEFAULT 0;