oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package forge
2
3import (
4 "bytes"
5 "context"
6 "crypto/sha256"
7 "encoding/binary"
8 "encoding/hex"
9 "encoding/json"
10 "io"
11 "net/http"
12 "net/url"
13 "strings"
14 "time"
15 "unicode/utf8"
16
17 "amadan.net/oficina/equipo/internal/equipo/team"
18)
19
20type DescriptionChannels struct {
21 Description string `json:"description"`
22 Human string `json:"human"`
23 Summary string `json:"summary"`
24 Notes string `json:"notes"`
25}
26
27// ReadBranchDescription reads the four verbatim channels from Amadan.
28func ReadBranchDescription(ctx context.Context, client *http.Client, cred Credential, repo, ref string) (Description, error) {
29 if !validDescriptionTarget(cred, repo, ref) {
30 return Description{}, team.ErrInvalid
31 }
32 branch := ref
33 parts := strings.Split(repo, "/")
34 endpoint := "https://amadan.net/api/v1/repos/" + url.PathEscape(parts[1]) + "/" + url.PathEscape(parts[2]) + "/branches/" + url.PathEscape(branch) + "/description"
35 ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
36 defer cancel()
37 req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
38 if err != nil {
39 return Description{}, team.ErrInvalid
40 }
41 req.Header.Set("Authorization", "Bearer "+cred.Bearer)
42 req.Header.Set("Accept", "application/json")
43 if client == nil {
44 transport := &http.Transport{Proxy: nil}
45 defer transport.CloseIdleConnections()
46 client = &http.Client{Transport: transport}
47 }
48 local := *client
49 local.Jar = nil
50 local.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
51 response, err := local.Do(req)
52 if err != nil {
53 return Description{}, team.ErrUncertain
54 }
55 defer response.Body.Close()
56 if response.StatusCode != http.StatusOK {
57 return Description{}, team.ErrUncertain
58 }
59 data, err := io.ReadAll(io.LimitReader(response.Body, 1<<20+1))
60 if err != nil || len(data) > 1<<20 {
61 return Description{}, team.ErrUncertain
62 }
63 var result struct {
64 DescriptionChannels
65 By string `json:"by"`
66 At int64 `json:"at"`
67 Agent json.RawMessage `json:"agent"`
68 Empty bool `json:"empty"`
69 ContentHash string `json:"content_hash"`
70 }
71 if !utf8.Valid(data) || json.Unmarshal(data, &result) != nil || result.Empty || result.By != cred.Agent || result.Description == "" {
72 return Description{}, team.ErrEvidence
73 }
74 if result.ContentHash != DescriptionContentHash(result.DescriptionChannels) {
75 return Description{}, team.ErrEvidence
76 }
77 metadata := bytes.TrimSpace(result.Agent)
78 if len(metadata) > 0 && string(metadata) != "null" && metadata[0] != '{' {
79 return Description{}, team.ErrEvidence
80 }
81 return Description{Repository: repo, Ref: ref, Agent: result.By, Body: result.Description, Digest: team.ContentDigest([]byte(result.Description)), At: result.At, AgentMetadata: result.Agent, Channels: result.DescriptionChannels, ContentHash: result.ContentHash}, nil
82}
83
84func DescriptionContentHash(c DescriptionChannels) string {
85 hash := sha256.New()
86 var size [8]byte
87 for _, channel := range []string{c.Description, c.Human, c.Summary, c.Notes} {
88 binary.BigEndian.PutUint64(size[:], uint64(len(channel)))
89 _, _ = hash.Write(size[:])
90 _, _ = hash.Write([]byte(channel))
91 }
92 return "sha256:" + hex.EncodeToString(hash.Sum(nil))
93}
94
95func validDescriptionTarget(cred Credential, repo, ref string) bool {
96 return validRepository(repo) && validRef(ref) && cred.Remote == "https://"+repo && team.ValidPublicationAgent(cred.Agent) && cred.Bearer != "" && !strings.ContainsAny(cred.Bearer, "\r\n\x00")
97}
98