| 1 | package dashboard |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/go-json-experiment/json" |
| 11 | ) |
| 12 | |
| 13 | type ProviderConfig struct { |
| 14 | DB *sql.DB |
| 15 | Issuer, Origin, Directory, Team, Kind string |
| 16 | Keys map[string][]byte |
| 17 | // Resolve reads an existing explicit link AND current admission. Never creates accounts. |
| 18 | Resolve func(context.Context, string) (int64, error) |
| 19 | Read func(context.Context, int64, string, time.Time) (Response, error) |
| 20 | // Write optionally applies the app's ordinary response-time authorization guard. |
| 21 | // It owns writing success or refusal and must never log the body. |
| 22 | Write func(http.ResponseWriter, *http.Request, []byte) |
| 23 | } |
| 24 | |
| 25 | func NewProvider(c ProviderConfig) (http.Handler, error) { |
| 26 | if c.DB == nil || !ValidOrigin(c.Issuer) || !ValidOrigin(c.Origin) || !ValidOrigin(c.Directory) || !ValidName(c.Team) || len(c.Keys) == 0 || c.Resolve == nil || c.Read == nil || (c.Kind != "documents" && c.Kind != "events") { |
| 27 | return nil, ErrRefused |
| 28 | } |
| 29 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 30 | w.Header().Set("Cache-Control", "private, no-store") |
| 31 | w.Header().Set("Vary", "Authorization") |
| 32 | refuse := func(status int) { http.Error(w, "Dashboard unavailable", status) } |
| 33 | if r.Method != http.MethodGet || r.URL.Path != Path || r.URL.RawQuery != "" || r.Host != strings.TrimPrefix(c.Origin, "https://") { |
| 34 | refuse(404) |
| 35 | return |
| 36 | } |
| 37 | headers := r.Header.Values("Authorization") |
| 38 | if len(headers) != 1 || !strings.HasPrefix(headers[0], "Bearer ") { |
| 39 | refuse(401) |
| 40 | return |
| 41 | } |
| 42 | now := time.Now() |
| 43 | claims, e := Verify(strings.TrimPrefix(headers[0], "Bearer "), c.Issuer, c.Origin, c.Directory, c.Team, c.Keys, now) |
| 44 | if e != nil { |
| 45 | refuse(401) |
| 46 | return |
| 47 | } |
| 48 | if e = Consume(r.Context(), c.DB, claims, now); e != nil { |
| 49 | if e == ErrUnavailable { |
| 50 | refuse(503) |
| 51 | } else { |
| 52 | refuse(401) |
| 53 | } |
| 54 | return |
| 55 | } |
| 56 | id, e := c.Resolve(r.Context(), claims.Subject) |
| 57 | if e != nil || id <= 0 { |
| 58 | if e == ErrUnavailable { |
| 59 | refuse(503) |
| 60 | } else { |
| 61 | refuse(403) |
| 62 | } |
| 63 | return |
| 64 | } |
| 65 | result, e := c.Read(r.Context(), id, claims.Subject, now) |
| 66 | if e != nil { |
| 67 | refuse(503) |
| 68 | return |
| 69 | } |
| 70 | result.Version = 1 |
| 71 | result.Team = c.Team |
| 72 | result.Kind = c.Kind |
| 73 | if result.Items == nil { |
| 74 | result.Items = []Item{} |
| 75 | } |
| 76 | if !result.Valid(c.Origin, c.Team, c.Kind) { |
| 77 | refuse(503) |
| 78 | return |
| 79 | } |
| 80 | b, e := json.Marshal(result) |
| 81 | if e != nil || len(b) > MaxBody { |
| 82 | refuse(503) |
| 83 | return |
| 84 | } |
| 85 | w.Header().Set("Content-Type", "application/json") |
| 86 | if c.Write != nil { |
| 87 | c.Write(w, r, b) |
| 88 | } else { |
| 89 | _, _ = w.Write(b) |
| 90 | } |
| 91 | }), nil |
| 92 | } |
| 93 | |