| 1 | package oficinaid |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "strconv" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/rastrillo/rastrillo/sessions" |
| 12 | ) |
| 13 | |
| 14 | func TestIDLocalGesture(t *testing.T) { |
| 15 | for _, purpose := range []string{"link", "unlink"} { |
| 16 | for _, method := range []string{"magiclink", "keymail"} { |
| 17 | for _, gesture := range []string{"missing", "old", "fresh", "future"} { |
| 18 | t.Run(purpose+"/"+method+"/"+gesture, func(t *testing.T) { |
| 19 | s, _ := fixture(t) |
| 20 | ctx := context.Background() |
| 21 | now := time.Now().Truncate(time.Second) |
| 22 | old := seedLocal(t, s, "local@example.invalid", now) |
| 23 | proof, e := s.BeginReauth(ctx, old, purpose, now) |
| 24 | if e != nil { |
| 25 | t.Fatal(e) |
| 26 | } |
| 27 | fresh := seedLocal(t, s, "local@example.invalid", now.Add(time.Second)) |
| 28 | at := "" |
| 29 | switch gesture { |
| 30 | case "old": |
| 31 | at = now.Add(-time.Second).Format(time.RFC3339) |
| 32 | case "fresh": |
| 33 | at = now.Format(time.RFC3339) |
| 34 | case "future": |
| 35 | at = now.Add(time.Minute).Format(time.RFC3339) |
| 36 | } |
| 37 | if _, e = s.DB.Exec("UPDATE sessions SET method=?,auth_time=? WHERE token_hash=?", method, at, sessions.HashToken(fresh)); e != nil { |
| 38 | t.Fatal(e) |
| 39 | } |
| 40 | _, _, e = s.CompleteReauth(ctx, proof, fresh, now.Add(time.Second)) |
| 41 | want := gesture == "fresh" || (method == "magiclink" && gesture == "missing") |
| 42 | if (e == nil) != want { |
| 43 | t.Fatalf("gesture accepted=%v want=%v: %v", e == nil, want, e) |
| 44 | } |
| 45 | if e == nil && purpose == "unlink" { |
| 46 | if e = s.Unlink(ctx, proof, fresh, now.Add(time.Second)); e != nil { |
| 47 | t.Fatal(e) |
| 48 | } |
| 49 | } |
| 50 | }) |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestIDReauthCleanupScope(t *testing.T) { |
| 57 | for _, action := range []string{"unlink", "delete", "address"} { |
| 58 | t.Run(action, func(t *testing.T) { |
| 59 | s, k := fixture(t) |
| 60 | ctx := context.Background() |
| 61 | now := time.Now().Truncate(time.Second) |
| 62 | local, _ := establish(t, s, k, now) |
| 63 | migration, e := migrations.ReadFile("migrations/0001_lifecycle.sql") |
| 64 | if e != nil { |
| 65 | t.Fatal(e) |
| 66 | } |
| 67 | if _, e = s.DB.Exec(string(migration)); e != nil { |
| 68 | t.Fatal(e) |
| 69 | } |
| 70 | other := seedLocal(t, s, "other@example.invalid", now) |
| 71 | otherProof, e := s.BeginReauth(ctx, other, "link", now) |
| 72 | if e != nil { |
| 73 | t.Fatal(e) |
| 74 | } |
| 75 | proof, e := s.BeginReauth(ctx, local, "unlink", now) |
| 76 | if e != nil { |
| 77 | t.Fatal(e) |
| 78 | } |
| 79 | switch action { |
| 80 | case "unlink": |
| 81 | fresh := seedLocal(t, s, "local-canary@example.invalid", now) |
| 82 | if _, _, e = s.CompleteReauth(ctx, proof, fresh, now); e != nil { |
| 83 | t.Fatal(e) |
| 84 | } |
| 85 | e = s.Unlink(ctx, proof, fresh, now) |
| 86 | case "delete": |
| 87 | _, e = s.DB.Exec("DELETE FROM members WHERE address='local-canary@example.invalid'") |
| 88 | case "address": |
| 89 | _, e = s.DB.Exec("UPDATE members SET address='changed@example.invalid' WHERE address='local-canary@example.invalid'") |
| 90 | } |
| 91 | if e != nil { |
| 92 | t.Fatal(e) |
| 93 | } |
| 94 | var n int |
| 95 | if e = s.DB.QueryRow("SELECT count(*) FROM equipo_id_reauth WHERE id_hash=?", sessions.HashToken(proof)).Scan(&n); e != nil || n != 0 { |
| 96 | t.Fatal("affected proof survived", n, e) |
| 97 | } |
| 98 | fresh := seedLocal(t, s, "other@example.invalid", now) |
| 99 | if _, _, e = s.CompleteReauth(ctx, otherProof, fresh, now); e != nil { |
| 100 | t.Fatal("other account reauthentication lost", e) |
| 101 | } |
| 102 | }) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func TestIDStartIPLimit(t *testing.T) { |
| 107 | for _, platform := range []bool{false, true} { |
| 108 | t.Run(strconv.FormatBool(platform), func(t *testing.T) { |
| 109 | socket := "" |
| 110 | if platform { |
| 111 | socket = "/not-opened" |
| 112 | } |
| 113 | t.Setenv("CARLOS_CONTROL_SOCKET", socket) |
| 114 | testStartIPLimit(t, platform) |
| 115 | }) |
| 116 | } |
| 117 | } |
| 118 | func testStartIPLimit(t *testing.T, platform bool) { |
| 119 | t.Helper() |
| 120 | s, _ := fixture(t) |
| 121 | h := &HTTP{Store: s, Render: func(w http.ResponseWriter, _ *http.Request, _ View, status int) { w.WriteHeader(status) }} |
| 122 | start := func(ip, port string, i int) int { |
| 123 | r := httptest.NewRequest("GET", s.Origin+"/auth/home", nil) |
| 124 | r.RemoteAddr = ip + ":" + port |
| 125 | r.Header.Set("X-Forwarded-For", "198.51.100."+strconv.Itoa(i)) |
| 126 | if platform { |
| 127 | r.RemoteAddr = "127.0.0.1:" + port |
| 128 | r.Header.Add("X-Forwarded-For", ip) |
| 129 | } |
| 130 | w := httptest.NewRecorder() |
| 131 | h.Start(w, r) |
| 132 | return w.Code |
| 133 | } |
| 134 | for i := 0; i < 20; i++ { |
| 135 | if got := start("192.0.2.1", strconv.Itoa(1000+i), i); got != 303 { |
| 136 | t.Fatal("allowance", i, got) |
| 137 | } |
| 138 | } |
| 139 | for i := 0; i < 3; i++ { |
| 140 | if got := start("192.0.2.1", strconv.Itoa(2000+i), i+20); got != 429 { |
| 141 | t.Fatal("IP budget bypassed", got) |
| 142 | } |
| 143 | } |
| 144 | var n int |
| 145 | if e := s.DB.QueryRow("SELECT count(*) FROM oficina_handoff_pending").Scan(&n); e != nil || n != 20 { |
| 146 | t.Fatal("limited request inserted pending", n, e) |
| 147 | } |
| 148 | if got := start("192.0.2.2", "3000", 40); got != 303 { |
| 149 | t.Fatal("other IP blocked", got) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func TestIDUnlinkRechecksKeymailGesture(t *testing.T) { |
| 154 | s, k := fixture(t) |
| 155 | now := time.Now().Truncate(time.Second) |
| 156 | local, _ := establish(t, s, k, now) |
| 157 | ctx := context.Background() |
| 158 | proof, e := s.BeginReauth(ctx, local, "unlink", now) |
| 159 | if e != nil { |
| 160 | t.Fatal(e) |
| 161 | } |
| 162 | fresh := seedLocal(t, s, "local-canary@example.invalid", now) |
| 163 | if _, e = s.DB.Exec("UPDATE sessions SET method='keymail',auth_time=? WHERE token_hash=?", now.Format(time.RFC3339), sessions.HashToken(fresh)); e != nil { |
| 164 | t.Fatal(e) |
| 165 | } |
| 166 | if _, _, e = s.CompleteReauth(ctx, proof, fresh, now); e != nil { |
| 167 | t.Fatal(e) |
| 168 | } |
| 169 | if _, e = s.DB.Exec("UPDATE sessions SET auth_time='' WHERE token_hash=?", sessions.HashToken(fresh)); e != nil { |
| 170 | t.Fatal(e) |
| 171 | } |
| 172 | if e = s.Unlink(ctx, proof, fresh, now); e == nil { |
| 173 | t.Fatal("unlink accepted Keymail without gesture") |
| 174 | } |
| 175 | } |
| 176 | |