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 "net/http"
6
7 "amadan.net/oficina/home/dashboard"
8 "github.com/go-json-experiment/json"
9)
10
11const dashboardTeamCookie = "__Host-oficina-dashboard-team"
12
13type dashboardPreference struct{ Session, Team string }
14
15func (a *App) selectedDashboardTeam(r *http.Request, session string) (string, bool) {
16 count := 0
17 value := ""
18 for _, c := range r.Cookies() {
19 if c.Name == dashboardTeamCookie {
20 count++
21 value = c.Value
22 }
23 }
24 if count == 0 {
25 return "", true
26 }
27 if count != 1 || len(value) > 2048 {
28 return "", false
29 }
30 b, e := base64.RawURLEncoding.Strict().DecodeString(value)
31 if e != nil {
32 return "", false
33 }
34 plain, e := a.secrets.Sealer.Open("dashboard.team", b)
35 if e != nil {
36 return "", false
37 }
38 var p dashboardPreference
39 if json.Unmarshal(plain, &p, json.RejectUnknownMembers(true)) != nil || !dashboard.ValidName(p.Team) {
40 return "", false
41 }
42 if p.Session != base64.RawURLEncoding.EncodeToString([]byte(session)) {
43 return "", true
44 }
45 return p.Team, true
46}
47func (a *App) rememberDashboardTeam(w http.ResponseWriter, session, team string) {
48 b, e := json.Marshal(dashboardPreference{base64.RawURLEncoding.EncodeToString([]byte(session)), team})
49 if e != nil {
50 return
51 }
52 sealed, e := a.secrets.Sealer.Seal("dashboard.team", b)
53 if e != nil {
54 return
55 }
56 http.SetCookie(w, &http.Cookie{Name: dashboardTeamCookie, Value: base64.RawURLEncoding.EncodeToString(sealed), Path: "/", MaxAge: 30 * 24 * 3600, Secure: true, HttpOnly: true, SameSite: http.SameSiteLaxMode})
57}
58