| 1 | package export |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/ed25519" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "sort" |
| 9 | "strings" |
| 10 | "time" |
| 11 | |
| 12 | "the-valley/note" |
| 13 | ) |
| 14 | |
| 15 | const ( |
| 16 | // AuthorizationType is the first line of every authorization's text. |
| 17 | AuthorizationType = "the-valley/web-authorization/v1" |
| 18 | |
| 19 | // MaxAuthorization is the most bytes of a signed authorization a |
| 20 | // receiver reads, and a server reads of the stored one. |
| 21 | MaxAuthorization = 256 << 10 |
| 22 | |
| 23 | // Anyone is the reader every visitor is, signed in or not. |
| 24 | Anyone = "anyone" |
| 25 | ) |
| 26 | |
| 27 | // Authorization is the signed statement of who may read which project at |
| 28 | // one destination. The publisher's policy path alone writes it, with the |
| 29 | // same key as content manifests, and the receiver stores the newest one it |
| 30 | // admitted. A server serves a project only while the stored authorization |
| 31 | // names it, by name and publication identity, with a reader the request |
| 32 | // is. Content manifests carry nothing of it, so no content upload can |
| 33 | // grant or restore access. |
| 34 | // |
| 35 | // Its text is the type line and then one record per line, each a keyword |
| 36 | // and its fields separated by single spaces: |
| 37 | // |
| 38 | // the-valley/web-authorization/v1 |
| 39 | // instance valley-host |
| 40 | // destination example.org |
| 41 | // sequence 42 |
| 42 | // checked 2026-10-29T01:03:00Z |
| 43 | // project app 9da6809f038df92138c131cd16bed76d |
| 44 | // reader app anyone |
| 45 | // |
| 46 | // The sequence is the publisher's own counter, independent of any clock: |
| 47 | // each authorization it signs has a sequence greater than every one before. |
| 48 | // A receiver admits an authorization by its sequence alone, never by its |
| 49 | // age. Checked is when the publisher last finished a policy check in full; |
| 50 | // pages show it, and nothing decides access by it. A project has one or |
| 51 | // more reader records. |
| 52 | type Authorization struct { |
| 53 | Instance string |
| 54 | Destination string |
| 55 | Sequence int64 |
| 56 | Checked time.Time |
| 57 | Projects []AuthorizedProject |
| 58 | } |
| 59 | |
| 60 | // AuthorizedProject is one project an authorization names: its name, its |
| 61 | // publication identity, and its effective readers, sorted. |
| 62 | type AuthorizedProject struct { |
| 63 | Name string |
| 64 | Identity string |
| 65 | Readers []string |
| 66 | } |
| 67 | |
| 68 | // Text is the authorization's text: the type line, the instance, |
| 69 | // destination, sequence and checked records, then each project's records |
| 70 | // in name order, its readers sorted. |
| 71 | func (a *Authorization) Text() []byte { |
| 72 | var b bytes.Buffer |
| 73 | fmt.Fprintf(&b, "%s\ninstance %s\ndestination %s\nsequence %d\nchecked %s\n", |
| 74 | AuthorizationType, a.Instance, a.Destination, a.Sequence, a.Checked.UTC().Format(time.RFC3339)) |
| 75 | projects := append([]AuthorizedProject{}, a.Projects...) |
| 76 | sort.Slice(projects, func(i, j int) bool { return projects[i].Name < projects[j].Name }) |
| 77 | for _, p := range projects { |
| 78 | fmt.Fprintf(&b, "project %s %s\n", p.Name, p.Identity) |
| 79 | readers := append([]string{}, p.Readers...) |
| 80 | sort.Strings(readers) |
| 81 | for _, r := range readers { |
| 82 | fmt.Fprintf(&b, "reader %s %s\n", p.Name, r) |
| 83 | } |
| 84 | } |
| 85 | return b.Bytes() |
| 86 | } |
| 87 | |
| 88 | // Sign is the authorization signed as a note by the publisher's export |
| 89 | // key. It is checked against ParseAuthorization first, so the publisher |
| 90 | // never signs what a receiver would refuse to read. |
| 91 | func (a *Authorization) Sign(name string, key ed25519.PrivateKey) ([]byte, error) { |
| 92 | text := a.Text() |
| 93 | if _, err := ParseAuthorization(text); err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | return note.Sign(text, name, key) |
| 97 | } |
| 98 | |
| 99 | // Allows reports whether the authorization lets reader read the project |
| 100 | // called name with the publication identity given. |
| 101 | func (a *Authorization) Allows(reader, name, identity string) bool { |
| 102 | if a == nil { |
| 103 | return false |
| 104 | } |
| 105 | for _, p := range a.Projects { |
| 106 | if p.Name == name && p.Identity == identity { |
| 107 | for _, r := range p.Readers { |
| 108 | if r == reader { |
| 109 | return true |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | return false |
| 115 | } |
| 116 | |
| 117 | // Names reports whether the authorization names the project called name |
| 118 | // with the publication identity given, for any reader. |
| 119 | func (a *Authorization) Names(name, identity string) bool { |
| 120 | p, ok := a.Project(name) |
| 121 | return ok && p.Identity == identity |
| 122 | } |
| 123 | |
| 124 | // Project is the authorization's project called name. |
| 125 | func (a *Authorization) Project(name string) (AuthorizedProject, bool) { |
| 126 | if a == nil { |
| 127 | return AuthorizedProject{}, false |
| 128 | } |
| 129 | for _, p := range a.Projects { |
| 130 | if p.Name == name { |
| 131 | return p, true |
| 132 | } |
| 133 | } |
| 134 | return AuthorizedProject{}, false |
| 135 | } |
| 136 | |
| 137 | // AuthorizationAdmission is what a receiver admits an authorization |
| 138 | // against: the pinned verifier keys, the instance and destination it is |
| 139 | // configured with, and the sequence of the authorization it stores. It |
| 140 | // holds no clock: an authorization is never refused for its age. |
| 141 | type AuthorizationAdmission struct { |
| 142 | Verifiers []note.VerifierKey |
| 143 | Instance string |
| 144 | Destination string |
| 145 | HighWater int64 |
| 146 | } |
| 147 | |
| 148 | // VerifyAuthorization admits a signed authorization, in this order, each |
| 149 | // step its own: |
| 150 | // |
| 151 | // 1. read at most MaxAuthorization bytes; |
| 152 | // 2. open the note with the pinned verifier keys; |
| 153 | // 3. require the type line, then read the records; |
| 154 | // 4. require the configured instance and destination; |
| 155 | // 5. require a sequence greater than the stored authorization's. |
| 156 | // |
| 157 | // There is no step for its age or its checked time: a valid narrowing |
| 158 | // above the stored sequence is admitted however old it is, and however far |
| 159 | // the clocks disagree. Every refusal is an *Error. |
| 160 | func VerifyAuthorization(r io.Reader, a AuthorizationAdmission) (*Authorization, error) { |
| 161 | signed, err := io.ReadAll(io.LimitReader(r, MaxAuthorization+1)) |
| 162 | if err != nil { |
| 163 | return nil, refuse(CodeUnreadable, "the authorization could not be read: %v", err) |
| 164 | } |
| 165 | if len(signed) > MaxAuthorization { |
| 166 | return nil, refuse(CodeTooLarge, "an authorization is at most %d bytes", MaxAuthorization) |
| 167 | } |
| 168 | text, _, err := note.Open(signed, a.Verifiers) |
| 169 | if err != nil { |
| 170 | return nil, refuse(CodeBadSignature, "the authorization does not open under the pinned key: %v", err) |
| 171 | } |
| 172 | auth, err := ParseAuthorization(text) |
| 173 | if err != nil { |
| 174 | return nil, err |
| 175 | } |
| 176 | if auth.Instance != a.Instance { |
| 177 | return nil, refuse(CodeWrongInstance, "the authorization is from instance %s, and this receiver takes %s", auth.Instance, a.Instance) |
| 178 | } |
| 179 | if auth.Destination != a.Destination { |
| 180 | return nil, refuse(CodeWrongDestination, "the authorization is for %s, and this receiver is %s", auth.Destination, a.Destination) |
| 181 | } |
| 182 | if auth.Sequence <= a.HighWater { |
| 183 | e := refuse(CodeReplayed, "the authorization's sequence %d is not greater than %d, the stored authorization's", auth.Sequence, a.HighWater) |
| 184 | e.HighWater = a.HighWater |
| 185 | return nil, e |
| 186 | } |
| 187 | return auth, nil |
| 188 | } |
| 189 | |
| 190 | // ReadStoredAuthorization reads an authorization a receiver stored: the |
| 191 | // signed note it admitted. The receiver verified the signature before it |
| 192 | // stored it, so this reads the text without verifying it again, and holds |
| 193 | // it to ParseAuthorization. |
| 194 | func ReadStoredAuthorization(signed []byte) (*Authorization, error) { |
| 195 | if len(signed) > MaxAuthorization { |
| 196 | return nil, fmt.Errorf("the stored authorization is larger than %d bytes", MaxAuthorization) |
| 197 | } |
| 198 | text, _, err := note.Split(signed) |
| 199 | if err != nil { |
| 200 | return nil, fmt.Errorf("the stored authorization is not a signed note: %w", err) |
| 201 | } |
| 202 | return ParseAuthorization(text) |
| 203 | } |
| 204 | |
| 205 | // ParseAuthorization reads an authorization's text, step 3 of |
| 206 | // VerifyAuthorization. The first line is exactly AuthorizationType. Each |
| 207 | // record after it is a keyword and exactly its fields. The instance, |
| 208 | // destination, sequence and checked records appear once each, a project at |
| 209 | // most once, and every project has one or more readers, each named once. |
| 210 | // A reader record names a project that has a project record. Records may |
| 211 | // come in any order; Text writes them in one. |
| 212 | func ParseAuthorization(text []byte) (*Authorization, error) { |
| 213 | bad := func(format string, args ...any) error { return refuse(CodeBadManifest, format, args...) } |
| 214 | if len(text) == 0 || text[len(text)-1] != '\n' { |
| 215 | return nil, bad("an authorization's text ends with a newline") |
| 216 | } |
| 217 | lines := strings.Split(strings.TrimSuffix(string(text), "\n"), "\n") |
| 218 | if lines[0] != AuthorizationType { |
| 219 | return nil, bad("the authorization opens with %q, not %q", lines[0], AuthorizationType) |
| 220 | } |
| 221 | arity := map[string]int{"instance": 1, "destination": 1, "sequence": 1, "checked": 1, "project": 2, "reader": 2} |
| 222 | a := &Authorization{} |
| 223 | seen := map[string]bool{} |
| 224 | projects := map[string]*AuthorizedProject{} |
| 225 | readers := map[string]map[string]bool{} |
| 226 | for i, line := range lines[1:] { |
| 227 | n := i + 2 |
| 228 | fields := strings.Split(line, " ") |
| 229 | key, args := fields[0], fields[1:] |
| 230 | want, known := arity[key] |
| 231 | switch { |
| 232 | case !known: |
| 233 | return nil, bad("line %d is %q; no record opens with %q", n, line, key) |
| 234 | case len(args) != want: |
| 235 | return nil, bad("line %d is %q; a %s record has %d fields", n, line, key, want) |
| 236 | } |
| 237 | for _, f := range args { |
| 238 | if f == "" { |
| 239 | return nil, bad("line %d is %q; fields are separated by one space", n, line) |
| 240 | } |
| 241 | } |
| 242 | switch key { |
| 243 | case "instance", "destination", "sequence", "checked": |
| 244 | if seen[key] { |
| 245 | return nil, bad("line %d is a second %s record", n, key) |
| 246 | } |
| 247 | seen[key] = true |
| 248 | } |
| 249 | switch key { |
| 250 | case "instance", "destination": |
| 251 | if !ValidName(args[0]) { |
| 252 | return nil, bad("line %d names %s %q, which is not a name", n, key, args[0]) |
| 253 | } |
| 254 | if key == "instance" { |
| 255 | a.Instance = args[0] |
| 256 | } else { |
| 257 | a.Destination = args[0] |
| 258 | } |
| 259 | case "sequence": |
| 260 | seq, ok := decimal(args[0]) |
| 261 | if !ok || seq == 0 { |
| 262 | return nil, bad("line %d gives sequence %q; a sequence is a positive decimal", n, args[0]) |
| 263 | } |
| 264 | a.Sequence = seq |
| 265 | case "checked": |
| 266 | at, err := time.Parse(time.RFC3339, args[0]) |
| 267 | if err != nil || at.UTC().Format(time.RFC3339) != args[0] { |
| 268 | return nil, bad("line %d gives checked %q; it is a time in RFC 3339, in UTC, to the second", n, args[0]) |
| 269 | } |
| 270 | a.Checked = at.UTC() |
| 271 | case "project": |
| 272 | name, id := args[0], args[1] |
| 273 | if !ValidName(name) { |
| 274 | return nil, bad("line %d names project %q, which is not a project name", n, name) |
| 275 | } |
| 276 | if projects[name] != nil { |
| 277 | return nil, bad("line %d is a second project record for %s", n, name) |
| 278 | } |
| 279 | if !ValidIdentity(id) { |
| 280 | return nil, bad("line %d gives %s identity %q, which is not 32 lowercase hex digits", n, name, id) |
| 281 | } |
| 282 | projects[name] = &AuthorizedProject{Name: name, Identity: id} |
| 283 | case "reader": |
| 284 | name, reader := args[0], args[1] |
| 285 | if !ValidName(name) || !ValidName(reader) { |
| 286 | return nil, bad("line %d is %q; a reader record names a project and a principal", n, line) |
| 287 | } |
| 288 | if readers[name][reader] { |
| 289 | return nil, bad("line %d names reader %s of %s a second time", n, reader, name) |
| 290 | } |
| 291 | if readers[name] == nil { |
| 292 | readers[name] = map[string]bool{} |
| 293 | } |
| 294 | readers[name][reader] = true |
| 295 | } |
| 296 | } |
| 297 | for _, key := range []string{"instance", "destination", "sequence", "checked"} { |
| 298 | if !seen[key] { |
| 299 | return nil, bad("the authorization has no %s record", key) |
| 300 | } |
| 301 | } |
| 302 | for name, rs := range readers { |
| 303 | p := projects[name] |
| 304 | if p == nil { |
| 305 | return nil, bad("the authorization has a reader of %s and no project record for it", name) |
| 306 | } |
| 307 | for r := range rs { |
| 308 | p.Readers = append(p.Readers, r) |
| 309 | } |
| 310 | sort.Strings(p.Readers) |
| 311 | } |
| 312 | var names []string |
| 313 | for name, p := range projects { |
| 314 | if len(p.Readers) == 0 { |
| 315 | return nil, bad("project %s has no reader record", name) |
| 316 | } |
| 317 | names = append(names, name) |
| 318 | } |
| 319 | sort.Strings(names) |
| 320 | for _, name := range names { |
| 321 | a.Projects = append(a.Projects, *projects[name]) |
| 322 | } |
| 323 | return a, nil |
| 324 | } |