oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package setup
2
3import (
4 "context"
5 "crypto/rand"
6 "errors"
7 "net/http"
8 "net/url"
9 "slices"
10 "strconv"
11 "time"
12
13 "amadan.net/oficina/equipo/internal/equipo/kernel"
14 "github.com/go-chi/chi/v5"
15)
16
17type Operations interface {
18 ValidateInvitationToken(context.Context, string, string) error
19 CreateInvitation(context.Context, kernel.Actor, string, kernel.Role, kernel.Mutation) (Invitation, error)
20 AcceptInvitation(context.Context, kernel.Actor, string, string, kernel.Mutation) (kernel.Member, error)
21 RevokeInvitation(context.Context, kernel.Actor, string, kernel.Mutation) error
22 Invitation(context.Context, kernel.Actor, string, string) (Invitation, error)
23}
24type InviteView struct {
25 Invitation Invitation
26 New, Issued, SignIn bool
27 SignInURL, RequestID, ErrorKey string
28}
29type Handler struct {
30 Key string
31 Now func() time.Time
32 Store Operations
33 Actor func(*http.Request) (kernel.Actor, error)
34 Owner func(*http.Request, kernel.Actor) error
35 Render func(http.ResponseWriter, *http.Request, int, InviteView)
36 Failure func(http.ResponseWriter, *http.Request, error)
37 Invalid, Forbidden, Expired error
38}
39
40func privacy(w http.ResponseWriter) {
41 w.Header().Set("Cache-Control", "no-store")
42 w.Header().Set("Referrer-Policy", "no-referrer")
43 w.Header().Set("X-Robots-Tag", "noindex, nofollow")
44}
45func (h *Handler) failure(w http.ResponseWriter, r *http.Request, err error) {
46 key := ""
47 if errors.Is(err, h.Expired) {
48 key = "invite.expired"
49 }
50 if errors.Is(err, h.Forbidden) {
51 key = "invite.wrong_person"
52 }
53 if key != "" {
54 status := http.StatusForbidden
55 if key == "invite.expired" {
56 status = http.StatusGone
57 }
58 h.Render(w, r, status, InviteView{ErrorKey: key})
59 return
60 }
61 h.Failure(w, r, err)
62}
63func (h *Handler) Page(w http.ResponseWriter, r *http.Request) {
64 privacy(w)
65 id := chi.URLParam(r, "id")
66 if !validInviteID(id) {
67 h.Failure(w, r, h.Invalid)
68 return
69 }
70 path := "/invites/" + id
71 if values, ok := r.URL.Query()["secret"]; ok {
72 if len(values) != 1 {
73 h.Failure(w, r, h.Invalid)
74 return
75 }
76 if err := h.Store.ValidateInvitationToken(r.Context(), id, values[0]); err != nil {
77 h.failure(w, r, err)
78 return
79 }
80 if err := h.issueCookie(w, id, values[0]); err != nil {
81 h.Failure(w, r, err)
82 return
83 }
84 http.Redirect(w, r, path, http.StatusSeeOther)
85 return
86 }
87 secret, err := h.cookieSecret(r, id)
88 if err != nil {
89 h.failure(w, r, err)
90 return
91 }
92 a, err := h.Actor(r)
93 if err != nil {
94 if !errors.Is(err, h.Forbidden) {
95 h.Failure(w, r, err)
96 return
97 }
98 h.Render(w, r, 200, InviteView{SignIn: true, SignInURL: "/signin?return_to=" + url.QueryEscape(path)})
99 return
100 }
101 in, err := h.Store.Invitation(r.Context(), a, id, secret)
102 if err != nil {
103 h.failure(w, r, err)
104 return
105 }
106 h.Render(w, r, 200, InviteView{Invitation: in, RequestID: rand.Text()})
107}
108
109func (h *Handler) New(w http.ResponseWriter, r *http.Request) {
110 privacy(w)
111 a, err := h.Actor(r)
112 if err == nil {
113 err = h.Owner(r, a)
114 }
115 if err != nil {
116 h.Failure(w, r, err)
117 return
118 }
119 h.Render(w, r, 200, InviteView{New: true, RequestID: rand.Text()})
120}
121func (h *Handler) form(w http.ResponseWriter, r *http.Request, fields ...string) (kernel.Actor, kernel.Mutation, error) {
122 a, err := h.Actor(r)
123 if err != nil {
124 return a, kernel.Mutation{}, err
125 }
126 r.Body = http.MaxBytesReader(w, r.Body, 64<<10)
127 if r.ParseForm() != nil {
128 return a, kernel.Mutation{}, h.Invalid
129 }
130 for k, v := range r.PostForm {
131 if !slices.Contains(fields, k) || len(v) != 1 {
132 return a, kernel.Mutation{}, h.Invalid
133 }
134 }
135 revision := int64(0)
136 if raw := r.PostForm.Get("revision"); raw != "" {
137 revision, err = strconv.ParseInt(raw, 10, 64)
138 if err != nil {
139 return a, kernel.Mutation{}, h.Invalid
140 }
141 }
142 m := kernel.Mutation{RequestID: r.PostForm.Get("request_id"), Revision: revision}
143 if !m.Valid() {
144 return a, m, h.Invalid
145 }
146 return a, m, nil
147}
148func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
149 privacy(w)
150 a, m, err := h.form(w, r, "address", "role", "request_id", "revision")
151 if err != nil {
152 h.Failure(w, r, err)
153 return
154 }
155 in, err := h.Store.CreateInvitation(r.Context(), a, r.PostForm.Get("address"), kernel.Role(r.PostForm.Get("role")), m)
156 if err != nil {
157 h.Failure(w, r, err)
158 return
159 }
160 h.Render(w, r, 200, InviteView{Invitation: in, Issued: true, RequestID: rand.Text()})
161}
162func (h *Handler) Accept(w http.ResponseWriter, r *http.Request) {
163 privacy(w)
164 a, m, err := h.form(w, r, "request_id", "revision")
165 if err == nil {
166 id := chi.URLParam(r, "id")
167 var secret string
168 secret, err = h.cookieSecret(r, id)
169 if err == nil {
170 _, err = h.Store.AcceptInvitation(r.Context(), a, id, secret, m)
171 }
172 }
173 if err != nil {
174 h.failure(w, r, err)
175 return
176 }
177 http.Redirect(w, r, "/onboarding", http.StatusSeeOther)
178}
179func (h *Handler) Revoke(w http.ResponseWriter, r *http.Request) {
180 privacy(w)
181 a, m, err := h.form(w, r, "request_id", "revision")
182 if err == nil {
183 err = h.Store.RevokeInvitation(r.Context(), a, chi.URLParam(r, "id"), m)
184 }
185 if err != nil {
186 h.Failure(w, r, err)
187 return
188 }
189 http.Redirect(w, r, "/people", http.StatusSeeOther)
190}
191