oficina / meet Public

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

Plain git — no account needed to clone.

Download

Download this file

1package calls
2
3import (
4 "amadan.net/oficina/home/teammode"
5 "net/http"
6 "strconv"
7 "strings"
8 "unicode/utf8"
9
10 "amadan.net/rastrillo/rastrillo/sessions"
11 "github.com/go-chi/chi/v5"
12)
13
14// Guest sessions use the ordinary revocable session store, with a distinct
15// identity method. They can never resolve to a verified address or team member.
16func (a *app) requireParticipant(next http.Handler) http.Handler {
17 return a.requireSignedInPage(a.requirePerson(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
18 p, _ := currentPerson(r)
19 path := r.URL.Path
20 if p.Anonymous && !strings.HasPrefix(path, "/m/") && path != "/native/authorize" && path != "/native/complete" && path != "/native/offer" {
21 a.notFound(w, r)
22 return
23 }
24 next.ServeHTTP(w, r)
25 })))
26}
27func (a *app) guestPage(w http.ResponseWriter, r *http.Request, m Meeting, token, name, message string) {
28 nativeHeaders(w)
29 title := a.store.title(m)
30 f := onMeeting("", title)
31 f.public = true
32 render(w, "guest", a.page(r, f, map[string]any{"Title": title, "Token": token, "Name": name, "NameError": message, "NativeURL": nativeJoinURL(a.cfg.Origin + "/j/" + token)}))
33}
34func (a *app) joinGuest(w http.ResponseWriter, r *http.Request) {
35 nativeHeaders(w)
36 if _, signedIn := a.auth.SessionFrom(r); signedIn {
37 a.followLink(w, r)
38 return
39 }
40 token := chi.URLParam(r, "token")
41 link, m, live, err := a.store.liveLink(r.Context(), token)
42 if err != nil {
43 a.ErrorPage(w, r, 500, "")
44 return
45 }
46 if !live {
47 a.ErrorPage(w, r, 404, "")
48 return
49 }
50 ctx := r.Context()
51 if a.team != nil {
52 // A link stands on its meeting's host, and so does the grant
53 // it gives (team_links.go).
54 host, ok := a.teamLinkHost(w, r, m)
55 if !ok {
56 return
57 }
58 ctx = teammode.WithAccess(ctx, host)
59 }
60 if m.RequireSignin {
61 a.setJoinCookie(w, link.TokenHash)
62 http.Redirect(w, r, signinPath, 303)
63 return
64 }
65 name := strings.TrimSpace(r.PostFormValue("name"))
66 if name == "" || utf8.RuneCountInString(name) > 80 {
67 w.WriteHeader(422)
68 a.guestPage(w, r, m, token, name, "Enter your name (up to 80 characters).")
69 return
70 }
71 var p Person
72 if a.team != nil {
73 // The guest and their grant in one transaction, on the host's
74 // standing: a refusal leaves neither (team_links.go).
75 p, live, err = a.store.guestWithGrant(ctx, name, link.ID)
76 if isTeamUnavailable(err) {
77 a.teamUnavailable(w, r)
78 return
79 }
80 } else {
81 p, err = a.store.createGuest(r.Context(), name)
82 if err != nil {
83 a.ErrorPage(w, r, 500, "")
84 return
85 }
86 _, live, err = a.store.mintGrantViaLink(ctx, link.ID, p.ID)
87 }
88 if err != nil {
89 a.ErrorPage(w, r, 500, "")
90 return
91 }
92 if !live {
93 a.ErrorPage(w, r, 404, "")
94 return
95 }
96 value, err := a.nativeAuth.sessions.Mint(sessions.Session{Subject: strconv.FormatInt(p.ID, 10), Method: "guest", AuthTime: a.store.now(), At: a.store.now()})
97 if err != nil {
98 a.ErrorPage(w, r, 500, "")
99 return
100 }
101 http.SetCookie(w, &http.Cookie{Name: a.auth.SessionCookie(), Value: value, Path: "/", HttpOnly: true, Secure: strings.HasPrefix(a.cfg.Origin, "https:"), SameSite: http.SameSiteLaxMode})
102 http.Redirect(w, r, "/m/"+m.Sqid+"/join", 303)
103}
104