| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | "unicode/utf8" |
| 8 | |
| 9 | "amadan.net/rastrillo/rastrillo/sessions" |
| 10 | "github.com/go-chi/chi/v5" |
| 11 | ) |
| 12 | |
| 13 | // Guest sessions use the ordinary revocable session store, with a distinct |
| 14 | // identity method. They can never resolve to a verified address or team member. |
| 15 | func (a *app) requireParticipant(next http.Handler) http.Handler { |
| 16 | return a.requireSignedInPage(a.requirePerson(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 17 | p, _ := currentPerson(r) |
| 18 | path := r.URL.Path |
| 19 | if p.Anonymous && !strings.HasPrefix(path, "/m/") && path != "/native/authorize" && path != "/native/complete" && path != "/native/offer" { |
| 20 | a.notFound(w, r) |
| 21 | return |
| 22 | } |
| 23 | next.ServeHTTP(w, r) |
| 24 | }))) |
| 25 | } |
| 26 | func (a *app) guestPage(w http.ResponseWriter, r *http.Request, m Meeting, token, name, message string) { |
| 27 | nativeHeaders(w) |
| 28 | title := a.store.title(m) |
| 29 | f := onMeeting("", title) |
| 30 | f.public = true |
| 31 | 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)})) |
| 32 | } |
| 33 | func (a *app) joinGuest(w http.ResponseWriter, r *http.Request) { |
| 34 | nativeHeaders(w) |
| 35 | if _, signedIn := a.auth.SessionFrom(r); signedIn { |
| 36 | a.followLink(w, r) |
| 37 | return |
| 38 | } |
| 39 | token := chi.URLParam(r, "token") |
| 40 | link, m, live, err := a.store.liveLink(r.Context(), token) |
| 41 | if err != nil { |
| 42 | a.ErrorPage(w, r, 500, "") |
| 43 | return |
| 44 | } |
| 45 | if !live { |
| 46 | a.ErrorPage(w, r, 404, "") |
| 47 | return |
| 48 | } |
| 49 | if m.RequireSignin { |
| 50 | a.setJoinCookie(w, link.TokenHash) |
| 51 | http.Redirect(w, r, signinPath, 303) |
| 52 | return |
| 53 | } |
| 54 | name := strings.TrimSpace(r.PostFormValue("name")) |
| 55 | if name == "" || utf8.RuneCountInString(name) > 80 { |
| 56 | w.WriteHeader(422) |
| 57 | a.guestPage(w, r, m, token, name, "Enter your name (up to 80 characters).") |
| 58 | return |
| 59 | } |
| 60 | p, err := a.store.createGuest(r.Context(), name) |
| 61 | if err != nil { |
| 62 | a.ErrorPage(w, r, 500, "") |
| 63 | return |
| 64 | } |
| 65 | _, live, err = a.store.mintGrantViaLink(r.Context(), link.ID, p.ID) |
| 66 | if err != nil { |
| 67 | a.ErrorPage(w, r, 500, "") |
| 68 | return |
| 69 | } |
| 70 | if !live { |
| 71 | a.ErrorPage(w, r, 404, "") |
| 72 | return |
| 73 | } |
| 74 | value, err := a.nativeAuth.sessions.Mint(sessions.Session{Subject: strconv.FormatInt(p.ID, 10), Method: "guest", AuthTime: a.store.now(), At: a.store.now()}) |
| 75 | if err != nil { |
| 76 | a.ErrorPage(w, r, 500, "") |
| 77 | return |
| 78 | } |
| 79 | http.SetCookie(w, &http.Cookie{Name: a.auth.SessionCookie(), Value: value, Path: "/", HttpOnly: true, Secure: strings.HasPrefix(a.cfg.Origin, "https:"), SameSite: http.SameSiteLaxMode}) |
| 80 | http.Redirect(w, r, "/m/"+m.Sqid+"/join", 303) |
| 81 | } |
| 82 | |