oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1package dashboard
2
3import (
4 "context"
5 "database/sql"
6 "strings"
7 "sync"
8 "testing"
9 "time"
10
11 "amadan.net/rastrillo/rastrillo/assertion"
12 family "amadan.net/rastrillo/rastrillo/crypto"
13 _ "modernc.org/sqlite"
14)
15
16func fixture(t *testing.T) (*family.Keypair, Claims) {
17 t.Helper()
18 k, e := family.Generate()
19 if e != nil {
20 t.Fatal(e)
21 }
22 id, _ := ID()
23 nonce, _ := ID()
24 return k, Claims{Version: 1, Issuer: "https://id.example.com", Audience: "https://docs.example.com", Directory: "https://home.example.com", Subject: "pr_" + strings.Repeat("a", 26), Team: "pilot", Purpose: "dashboard-read", KeyID: "key", ID: id, Nonce: nonce, Issued: 1000, Expires: 1120}
25}
26func TestTokensAreBoundAndPurposeSeparated(t *testing.T) {
27 k, c := fixture(t)
28 token, e := Sign(k, c)
29 if e != nil {
30 t.Fatal(e)
31 }
32 verify := func(token string, c Claims, now int64) error {
33 _, e := Verify(token, c.Issuer, c.Audience, c.Directory, c.Team, map[string][]byte{"key": k.SignPub()}, time.Unix(now, 0))
34 return e
35 }
36 if e = verify(token, c, 1000); e != nil {
37 t.Fatal(e)
38 }
39 for _, field := range []string{"issuer", "audience", "directory", "team"} {
40 wrong := c
41 switch field {
42 case "issuer":
43 wrong.Issuer = "https://other.example.com"
44 case "audience":
45 wrong.Audience = "https://other.example.com"
46 case "directory":
47 wrong.Directory = "https://other.example.com"
48 case "team":
49 wrong.Team = "other"
50 }
51 if verify(token, wrong, 1000) == nil {
52 t.Fatal("accepted wrong", field)
53 }
54 }
55 for _, now := range []int64{969, 1120, 1200} {
56 if verify(token, c, now) == nil {
57 t.Fatal("accepted time", now)
58 }
59 }
60 login, e := assertion.Sign(k, assertion.Claims{Version: 1, Issuer: c.Issuer, Audience: c.Audience, Subject: c.Subject, KeyID: c.KeyID, ID: c.ID, Nonce: c.Nonce, IssuedAt: 1000, ExpiresAt: 1120, AuthTime: 1000})
61 if e != nil {
62 t.Fatal(e)
63 }
64 if verify(login, c, 1000) == nil {
65 t.Fatal("login accepted for dashboard")
66 }
67 v, _ := assertion.NewVerifier(c.Issuer, c.Audience, map[string][]byte{"key": k.SignPub()})
68 if _, e = v.Verify(token, time.Unix(1000, 0)); e == nil {
69 t.Fatal("dashboard accepted for login")
70 }
71}
72func TestReplaySurvivesReopenAndClockRollback(t *testing.T) {
73 path := t.TempDir() + "/replay.db"
74 db, e := sql.Open("sqlite", path)
75 if e != nil {
76 t.Fatal(e)
77 }
78 db.SetMaxOpenConns(1)
79 b, _ := Migrations.ReadFile("migrations/0001_dashboard.sql")
80 if _, e = db.Exec(string(b)); e != nil {
81 t.Fatal(e)
82 }
83 _, c := fixture(t)
84 var mu sync.Mutex
85 success := 0
86 var wg sync.WaitGroup
87 for range 8 {
88 wg.Add(1)
89 go func() {
90 defer wg.Done()
91 if Consume(context.Background(), db, c, time.Unix(1000, 0)) == nil {
92 mu.Lock()
93 success++
94 mu.Unlock()
95 }
96 }()
97 }
98 wg.Wait()
99 if success != 1 {
100 t.Fatalf("consumed %d times", success)
101 }
102 db.Close()
103 db, e = sql.Open("sqlite", path)
104 if e != nil {
105 t.Fatal(e)
106 }
107 defer db.Close()
108 if Consume(context.Background(), db, c, time.Unix(1000, 0)) == nil {
109 t.Fatal("replayed after reopen")
110 }
111 c.ID, _ = ID()
112 if Consume(context.Background(), db, c, time.Unix(969, 0)) == nil {
113 t.Fatal("clock rollback accepted")
114 }
115}
116