oficina / home Public

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

Plain git — no account needed to clone.

Download

Download this file

1package home
2
3import (
4 "bytes"
5 "context"
6 "crypto/sha256"
7 "encoding/base64"
8 "encoding/hex"
9 "path/filepath"
10 "strings"
11 "sync"
12 "testing"
13
14 "amadan.net/rastrillo/rastrillo/assertion"
15 family "amadan.net/rastrillo/rastrillo/crypto"
16 "amadan.net/rastrillo/rastrillo/db"
17 "amadan.net/rastrillo/rastrillo/migrate"
18)
19
20func fixtureToken(b byte) string {
21 return base64.RawURLEncoding.EncodeToString(bytes.Repeat([]byte{b}, 32))
22}
23func hexID(k *family.Keypair) string {
24 sum := sha256.Sum256(k.SignPub())
25 return hex.EncodeToString(sum[:])
26}
27
28// newRegistryCredential boots a credential role through NewRuntime on a
29// serving-schema database, with rows written to the consumers table first.
30func newRegistryCredential(t *testing.T, rows ...[3]string) (*App, Runtime, *db.DB, error) {
31 t.Helper()
32 RegisterSecureDelete()
33 d, err := db.Open(filepath.Join(t.TempDir(), "credential.db"), quietLogger())
34 if err != nil {
35 t.Fatal(err)
36 }
37 t.Cleanup(func() { d.Close() })
38 if _, err = migrate.Apply(context.Background(), d, (Runtime{role: "credential"}).ServingSchema()); err != nil {
39 t.Fatal(err)
40 }
41 for _, row := range rows {
42 if _, err = d.Writer().Exec("INSERT INTO consumers VALUES(?,?,'docs',?,'2026-10-27',?)", row[0], row[1], row[2], fixtureToken(1)); err != nil {
43 t.Fatal(err)
44 }
45 }
46 secrets, _ := testSecrets(t)
47 key, _ := family.Generate()
48 kid := hexID(key)
49 cfg := Runtime{role: "credential", origins: Origins{Origin, directoryOrigin}, secrets: secrets, signer: key, keyID: kid, keys: map[string][]byte{kid: key.SignPub()}, consumers: map[string]string{"directory": directoryOrigin, "team_docs": "https://blue.docs.oficina.works"}}
50 a, err := NewRuntime(cfg, d, quietLogger())
51 return a, cfg, d, err
52}
53
54func TestConsumerRegistryUnionPastSixteen(t *testing.T) {
55 var rows [][3]string
56 for i := 0; i < 20; i++ {
57 id := "t" + strings.Repeat("x", i%3) + string(rune('a'+i))
58 rows = append(rows, [3]string{id, "https://" + id + ".docs.oficina.works", "active"})
59 }
60 rows = append(rows, [3]string{"gone", "https://gone.docs.oficina.works", "retired"})
61 a, _, d, err := newRegistryCredential(t, rows...)
62 if err != nil {
63 t.Fatal(err)
64 }
65 if n := len(a.consumers()); n != 22 {
66 t.Fatal("union size", n)
67 }
68 nonce, _ := assertion.RandomID()
69 // The twentieth table consumer, past HOME_CONSUMERS' 16-entry cap.
70 if r := exchangeRequest(a, "GET", Origin+"/authorize?consumer=txt&nonce="+nonce, "", nil); r.Code != 303 {
71 t.Fatal("table consumer past sixteen refused", r.Code)
72 }
73 // Retired: refused at /authorize and at sign-out.
74 if r := exchangeRequest(a, "GET", Origin+"/authorize?consumer=gone&nonce="+nonce, "", nil); r.Code != 400 {
75 t.Fatal("retired consumer admitted at /authorize", r.Code)
76 }
77 if r := exchangeRequest(a, "POST", Origin+signoutPath, "https://gone.docs.oficina.works", nil); r.Code != 403 {
78 t.Fatal("retired consumer admitted at sign-out", r.Code)
79 }
80 if r := exchangeRequest(a, "POST", Origin+signoutPath, "https://txt.docs.oficina.works", nil); r.Code != 303 {
81 t.Fatal("table consumer refused at sign-out", r.Code)
82 }
83 // The standing CSP names no consumer, however many there are.
84 if strings.Contains(a.SecurityPolicy(), "docs.oficina.works") {
85 t.Fatal("standing CSP lists consumers")
86 }
87 // Concurrent reads while the union is swapped (make test-race).
88 var wg sync.WaitGroup
89 stop := make(chan struct{})
90 for i := 0; i < 4; i++ {
91 wg.Add(1)
92 go func() {
93 defer wg.Done()
94 for {
95 select {
96 case <-stop:
97 return
98 default:
99 _ = a.consumers()["txt"]
100 _, _ = a.consumerOrigin("https://txt.docs.oficina.works")
101 }
102 }
103 }()
104 }
105 for i := 0; i < 20; i++ {
106 if err := a.runtime.registry.load(context.Background(), d.Writer(), Origin, a.runtime.keys); err != nil {
107 t.Fatal(err)
108 }
109 }
110 close(stop)
111 wg.Wait()
112}
113
114// An ID or origin in both HOME_CONSUMERS and the table — retired rows
115// included — refuses boot.
116func TestConsumerRegistryCollisionRefusesBoot(t *testing.T) {
117 for _, row := range [][3]string{{"team_docs", "https://other.docs.oficina.works", "active"}, {"other", "https://blue.docs.oficina.works", "retired"}} {
118 if _, _, _, err := newRegistryCredential(t, row); err != errExchangeConfig {
119 t.Fatal("collision booted", row, err)
120 }
121 }
122}
123