| 1 | package idtest |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/rastrillo/rastrillo/sessions" |
| 5 | "net/url" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | func TestIDDeletedAccountCannotBeRecreatedWithOldLink(t *testing.T) { |
| 11 | f := newFixture(t, true) |
| 12 | f.link(t) |
| 13 | done, _ := f.exchange(t, nil, nil, person) |
| 14 | session := cookie(t, done, "__Host-rastrillo_session") |
| 15 | if _, e := f.db.Writer().Exec("DELETE FROM members WHERE address=?", address); e != nil { |
| 16 | t.Fatal(e) |
| 17 | } |
| 18 | if _, e := f.db.Writer().Exec("INSERT INTO members(address,role,active,session_epoch) VALUES(?,'owner',1,?)", address, time.Now().Unix()); e != nil { |
| 19 | t.Fatal(e) |
| 20 | } |
| 21 | if r := f.request("GET", "/work", "", nil, session); r.Code == 200 { |
| 22 | t.Fatal("deleted account's Home session revived") |
| 23 | } |
| 24 | if r, _ := f.exchange(t, nil, nil, person); r.Code != 403 { |
| 25 | t.Fatal("old Home link transferred to recreated account", r.Code) |
| 26 | } |
| 27 | } |
| 28 | func TestIDAddressChangeNeedsFreshLink(t *testing.T) { |
| 29 | f := newFixture(t, true) |
| 30 | f.link(t) |
| 31 | if _, e := f.db.Writer().Exec("UPDATE members SET address='changed@example.invalid' WHERE address=?", address); e != nil { |
| 32 | t.Fatal(e) |
| 33 | } |
| 34 | if r, _ := f.exchange(t, nil, nil, person); r.Code != 403 { |
| 35 | t.Fatal("address change preserved link", r.Code) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestIDLocalReauthenticationUsesRealEmail(t *testing.T) { |
| 40 | f := newFixture(t, true) |
| 41 | old := f.local(t, address) |
| 42 | start := f.request("POST", "/account/home/reauth", f.origin, url.Values{"purpose": {"link"}}, old) |
| 43 | proof := cookie(t, start, "__Host-equipo-id-reauth") |
| 44 | fresh := f.emailWith(t, start.Header().Get("Location"), old, proof) |
| 45 | if fresh.Value == old.Value { |
| 46 | t.Fatal("local reauthentication reused session") |
| 47 | } |
| 48 | var oldRows int |
| 49 | if e := f.db.Writer().QueryRow("SELECT count(*) FROM sessions WHERE token_hash=?", sessions.HashToken(old.Value)).Scan(&oldRows); e != nil || oldRows != 1 { |
| 50 | t.Fatal("original session lost before proof completion", oldRows, e) |
| 51 | } |
| 52 | end := f.request("GET", "/account/home/reauth-complete", "", nil, proof, fresh) |
| 53 | if end.Code != 303 || end.Header().Get("Location") != "/auth/home" { |
| 54 | t.Fatal("local proof did not start Home leg", end.Code, end.Body.String()) |
| 55 | } |
| 56 | if e := f.db.Writer().QueryRow("SELECT count(*) FROM sessions WHERE token_hash=?", sessions.HashToken(old.Value)).Scan(&oldRows); e != nil || oldRows != 0 { |
| 57 | t.Fatal("original session was not rotated", oldRows, e) |
| 58 | } |
| 59 | done, _ := f.exchange(t, cookie(t, end, "__Host-equipo-id-init"), fresh, person) |
| 60 | if done.Code != 303 { |
| 61 | t.Fatal("fresh email then Home", done.Code) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestIDReauthHasNoAddressBridge(t *testing.T) { |
| 66 | f := newFixture(t, true) |
| 67 | local := f.local(t, address) |
| 68 | out := f.request("POST", "/account/home/reauth", f.origin, url.Values{"purpose": {"link"}}, local) |
| 69 | if out.Code != 303 { |
| 70 | t.Fatal("reauth start", out.Code) |
| 71 | } |
| 72 | var n int |
| 73 | if e := f.db.Writer().QueryRow("SELECT count(*) FROM pragma_table_info('equipo_id_accounts') WHERE name LIKE '%address%'").Scan(&n); e != nil || n != 0 { |
| 74 | t.Fatal("redundant address bridge remains", n, e) |
| 75 | } |
| 76 | var key, memberKey string |
| 77 | if e := f.db.Writer().QueryRow("SELECT member_key FROM equipo_id_accounts").Scan(&key); e != nil { |
| 78 | t.Fatal(e) |
| 79 | } |
| 80 | if e := f.db.Writer().QueryRow("SELECT identity_key FROM members WHERE address=?", address).Scan(&memberKey); e != nil || key != memberKey { |
| 81 | t.Fatal("immutable member binding missing", e) |
| 82 | } |
| 83 | } |
| 84 | |