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 "encoding/base64"
5 "io"
6 "mime"
7 "net/http"
8 "net/url"
9 "time"
10
11 "amadan.net/oficina/home/dashboard"
12 "amadan.net/rastrillo/rastrillo/sessions"
13 "github.com/go-json-experiment/json"
14)
15
16const dashboardCallback = "/dashboard/callback"
17
18const dashboardIntentCookie = "__Host-oficina-dashboard-intent"
19
20type dashboardIntent struct {
21 Team, Nonce string
22 Expires int64
23}
24
25func (a *App) beginDashboard(w http.ResponseWriter, r *http.Request) {
26 w.Header().Set("Cache-Control", "private, no-store")
27 q, e := url.ParseQuery(r.URL.RawQuery)
28 if e != nil || len(q) != 2 || len(q["team"]) != 1 || len(q["nonce"]) != 1 || !dashboard.ValidID(q.Get("nonce")) {
29 http.NotFound(w, r)
30 return
31 }
32 team, ok := a.dashboardTeam(q.Get("team"))
33 if !ok {
34 http.NotFound(w, r)
35 return
36 }
37 // A valid credential session already proves identity on this GET.
38 // Only an absent session needs the sealed intent and sign-in resume.
39 if session, ok := a.sessions.From(r); ok && ValidPersonRef(session.Subject) {
40 a.mintDashboard(w, r, team, q.Get("nonce"), session.Subject)
41 return
42 }
43 b, e := json.Marshal(dashboardIntent{q.Get("team"), q.Get("nonce"), time.Now().Unix() + 300})
44 if e != nil {
45 http.Error(w, "Dashboard unavailable", 503)
46 return
47 }
48 sealed, e := a.secrets.Sealer.Seal("dashboard.intent", b)
49 if e != nil {
50 http.Error(w, "Dashboard unavailable", 503)
51 return
52 }
53 http.SetCookie(w, &http.Cookie{Name: dashboardIntentCookie, Value: base64.RawURLEncoding.EncodeToString(sealed), Path: "/", MaxAge: 300, Secure: true, HttpOnly: true, SameSite: http.SameSiteLaxMode})
54 http.Redirect(w, r, "/dashboard/resume", http.StatusSeeOther)
55}
56func (a *App) authorizeDashboard(w http.ResponseWriter, r *http.Request) {
57 w.Header().Set("Cache-Control", "private, no-store")
58 value := ""
59 count := 0
60 for _, c := range r.Cookies() {
61 if c.Name == dashboardIntentCookie {
62 value = c.Value
63 count++
64 }
65 }
66 if count != 1 || len(value) > 2048 || r.URL.RawQuery != "" {
67 http.NotFound(w, r)
68 return
69 }
70 sealed, e := base64.RawURLEncoding.Strict().DecodeString(value)
71 if e != nil {
72 http.NotFound(w, r)
73 return
74 }
75 b, e := a.secrets.Sealer.Open("dashboard.intent", sealed)
76 if e != nil {
77 http.NotFound(w, r)
78 return
79 }
80 var intent dashboardIntent
81 if json.Unmarshal(b, &intent, json.RejectUnknownMembers(true)) != nil || !dashboard.ValidID(intent.Nonce) || intent.Expires <= time.Now().Unix() || intent.Expires > time.Now().Unix()+300 {
82 http.NotFound(w, r)
83 return
84 }
85 team, ok := a.dashboardTeam(intent.Team)
86 if !ok {
87 http.NotFound(w, r)
88 return
89 }
90
91 session, ok := sessions.Current(r)
92 if !ok || !ValidPersonRef(session.Subject) {
93 http.Error(w, "Sign in to continue", 401)
94 return
95 }
96 a.mintDashboard(w, r, team, intent.Nonce, session.Subject)
97}
98
99func (a *App) mintDashboard(w http.ResponseWriter, r *http.Request, team dashboardTeam, nonce, subject string) {
100 tokens := map[string]string{}
101 now := time.Now()
102 for _, s := range team.Sources {
103 id, e := dashboard.ID()
104 if e != nil {
105 http.Error(w, "Dashboard unavailable", 503)
106 return
107 }
108 token, e := dashboard.Sign(a.runtime.signer, dashboard.Claims{Version: 1, Issuer: a.origins.Credential, Audience: s.Origin, Directory: a.origins.Directory, Subject: subject, Team: team.ID, Purpose: "dashboard-read", KeyID: a.runtime.keyID, ID: id, Nonce: nonce, Issued: now.Unix(), Expires: now.Unix() + 120})
109 if e != nil {
110 http.Error(w, "Dashboard unavailable", 503)
111 return
112 }
113 tokens[s.Origin] = token
114 }
115 b, e := json.Marshal(tokens)
116 if e != nil {
117 http.Error(w, "Dashboard unavailable", 503)
118 return
119 }
120 w.Header().Set("Referrer-Policy", "strict-origin")
121 a.render(w, r, 200, "assertion.html", map[string]any{"Action": a.origins.Directory + dashboardCallback, "Token": string(b)})
122}
123func (a *App) receiveDashboard(w http.ResponseWriter, r *http.Request) {
124 w.Header().Set("Cache-Control", "private, no-store")
125 fail := func() { http.Error(w, "Unable to load your dashboard. Return to Oficina and try again.", 400) }
126 if a.dash == nil || len(r.Header.Values("Origin")) != 1 || r.Header.Get("Origin") != a.origins.Credential {
127 fail()
128 return
129 }
130 media, _, e := mime.ParseMediaType(r.Header.Get("Content-Type"))
131 if e != nil || media != "application/x-www-form-urlencoded" {
132 fail()
133 return
134 }
135 b, e := io.ReadAll(http.MaxBytesReader(w, r.Body, 24576))
136 if e != nil {
137 fail()
138 return
139 }
140 form, e := url.ParseQuery(string(b))
141 if e != nil || len(form) != 1 || len(form["assertion"]) != 1 {
142 fail()
143 return
144 }
145 var tokens map[string]string
146 if json.Unmarshal([]byte(form.Get("assertion")), &tokens) != nil {
147 fail()
148 return
149 }
150 nonce := dashboardNonce(r)
151 if nonce == "" {
152 fail()
153 return
154 }
155 now := time.Now()
156 a.dash.mu.Lock()
157 defer a.dash.mu.Unlock()
158 a.dash.sweep(now)
159 pending, ok := a.dash.pending[nonce]
160 if !ok || pending.Tokens != nil {
161 fail()
162 return
163 }
164 team, ok := a.dashboardTeam(pending.Team)
165 if !ok || len(tokens) != len(team.Sources) {
166 fail()
167 return
168 }
169 for _, s := range team.Sources {
170 c, e := dashboard.Verify(tokens[s.Origin], a.origins.Credential, s.Origin, a.origins.Directory, team.ID, a.runtime.keys, now)
171 if e != nil || c.Subject != pending.Subject || c.Nonce != nonce {
172 fail()
173 return
174 }
175 }
176 pending.Tokens = tokens
177 pending.Expires = now.Add(120 * time.Second)
178 a.dash.pending[nonce] = pending
179 http.Redirect(w, r, "/?"+url.Values{"team": {team.ID}}.Encode(), http.StatusSeeOther)
180}
181