| 1 | // Package dashboard defines read-only, person-bound dashboard requests. Tokens |
| 2 | // have a distinct signing context and cannot be used for login or account linking. |
| 3 | package dashboard |
| 4 | |
| 5 | import ( |
| 6 | "encoding/base64" |
| 7 | "errors" |
| 8 | "net/url" |
| 9 | "strings" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/rastrillo/rastrillo/assertion" |
| 13 | family "amadan.net/rastrillo/rastrillo/crypto" |
| 14 | "github.com/go-json-experiment/json" |
| 15 | ) |
| 16 | |
| 17 | const Path = "/api/oficina/dashboard" |
| 18 | const signingContext = "oficina/dashboard/read/v1" |
| 19 | const MaxItems = 20 |
| 20 | const MaxBody = 65536 |
| 21 | |
| 22 | var ErrRefused = errors.New("dashboard: refused") |
| 23 | var ErrUnavailable = errors.New("dashboard: unavailable") |
| 24 | |
| 25 | type Claims struct { |
| 26 | Version int `json:"v"` |
| 27 | Issuer string `json:"iss"` |
| 28 | Audience string `json:"aud"` |
| 29 | Directory string `json:"directory"` |
| 30 | Subject string `json:"sub"` |
| 31 | Team string `json:"team"` |
| 32 | Purpose string `json:"purpose"` |
| 33 | KeyID string `json:"kid"` |
| 34 | ID string `json:"jti"` |
| 35 | Nonce string `json:"nonce"` |
| 36 | Issued int64 `json:"iat"` |
| 37 | Expires int64 `json:"exp"` |
| 38 | } |
| 39 | |
| 40 | func ID() (string, error) { return assertion.RandomID() } |
| 41 | func ValidID(s string) bool { |
| 42 | b, e := base64.RawURLEncoding.Strict().DecodeString(s) |
| 43 | return e == nil && len(b) == 16 && base64.RawURLEncoding.EncodeToString(b) == s |
| 44 | } |
| 45 | func ValidName(s string) bool { |
| 46 | if len(s) == 0 || len(s) > 64 { |
| 47 | return false |
| 48 | } |
| 49 | for _, c := range s { |
| 50 | if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-' || c == '_') { |
| 51 | return false |
| 52 | } |
| 53 | } |
| 54 | return true |
| 55 | } |
| 56 | func ValidOrigin(s string) bool { |
| 57 | u, e := url.Parse(s) |
| 58 | return e == nil && u.Scheme == "https" && u.Host != "" && u.Host == strings.ToLower(u.Host) && u.User == nil && u.Path == "" && u.RawQuery == "" && !u.ForceQuery && u.Fragment == "" && u.Opaque == "" && u.Port() == "" |
| 59 | } |
| 60 | func valid(c Claims) bool { |
| 61 | return c.Version == 1 && c.Purpose == "dashboard-read" && ValidOrigin(c.Issuer) && ValidOrigin(c.Audience) && ValidOrigin(c.Directory) && c.Issuer != c.Audience && c.Issuer != c.Directory && strings.HasPrefix(c.Subject, "pr_") && len(c.Subject) == 29 && ValidName(c.Subject) && ValidName(c.Team) && ValidName(c.KeyID) && ValidID(c.ID) && ValidID(c.Nonce) && c.ID != c.Nonce && c.Issued > 0 && c.Expires > c.Issued && c.Expires-c.Issued <= 120 |
| 62 | } |
| 63 | func Sign(key *family.Keypair, c Claims) (string, error) { |
| 64 | if !valid(c) || key == nil || key.SignPriv == nil { |
| 65 | return "", ErrRefused |
| 66 | } |
| 67 | b, e := json.Marshal(c) |
| 68 | if e != nil { |
| 69 | return "", ErrUnavailable |
| 70 | } |
| 71 | sig, e := family.Sign(key, signingContext, b) |
| 72 | if e != nil { |
| 73 | return "", ErrUnavailable |
| 74 | } |
| 75 | return base64.RawURLEncoding.EncodeToString(b) + "." + base64.RawURLEncoding.EncodeToString(sig), nil |
| 76 | } |
| 77 | func Verify(token, issuer, audience, directory, team string, keys map[string][]byte, now time.Time) (Claims, error) { |
| 78 | if len(token) > 4096 { |
| 79 | return Claims{}, ErrRefused |
| 80 | } |
| 81 | p, s, ok := strings.Cut(token, ".") |
| 82 | if !ok { |
| 83 | return Claims{}, ErrRefused |
| 84 | } |
| 85 | b, e := base64.RawURLEncoding.Strict().DecodeString(p) |
| 86 | if e != nil || base64.RawURLEncoding.EncodeToString(b) != p { |
| 87 | return Claims{}, ErrRefused |
| 88 | } |
| 89 | sig, e := base64.RawURLEncoding.Strict().DecodeString(s) |
| 90 | if e != nil || base64.RawURLEncoding.EncodeToString(sig) != s { |
| 91 | return Claims{}, ErrRefused |
| 92 | } |
| 93 | var c Claims |
| 94 | if json.Unmarshal(b, &c, json.RejectUnknownMembers(true)) != nil || !valid(c) || c.Issuer != issuer || c.Audience != audience || c.Directory != directory || c.Team != team || c.Issued > now.Unix()+30 || c.Expires <= now.Unix() { |
| 95 | return Claims{}, ErrRefused |
| 96 | } |
| 97 | key := keys[c.KeyID] |
| 98 | if len(key) == 0 || !family.Verify(key, signingContext, b, sig) { |
| 99 | return Claims{}, ErrRefused |
| 100 | } |
| 101 | return c, nil |
| 102 | } |
| 103 | |