| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | "github.com/go-chi/chi/v5" |
| 12 | ) |
| 13 | |
| 14 | // The scriptless form saves the visit before opening the trusted Amadan URL. |
| 15 | func (a *application) openAccessApproval(w http.ResponseWriter, r *http.Request) { |
| 16 | w.Header().Set("Cache-Control", "no-store") |
| 17 | ctx, address, ok := sessionContext(r) |
| 18 | if !ok { |
| 19 | a.failure(w, r, ErrForbidden) |
| 20 | return |
| 21 | } |
| 22 | revision, _ := strconv.ParseInt(r.PostFormValue("revision"), 10, 64) |
| 23 | out, _, err := a.store.Chat().Answer(ctx, address, chi.URLParam(r, "id"), wire.AnswerCard{Choice: "open_access", Revision: revision}) |
| 24 | if err != nil { |
| 25 | if errors.Is(err, chat.ErrCardConflict) && strings.Contains(r.Header.Get("Accept"), "application/json") { |
| 26 | runnerJSON(w, http.StatusConflict, struct{}{}) |
| 27 | return |
| 28 | } |
| 29 | a.failure(w, r, chatError(err)) |
| 30 | return |
| 31 | } |
| 32 | if strings.Contains(r.Header.Get("Accept"), "application/json") { |
| 33 | runnerJSON(w, http.StatusOK, struct{}{}) |
| 34 | return |
| 35 | } |
| 36 | if out.Card.Access == nil || !wire.AccessURL(out.Card.Access.ApproveURL, out.Card.Access.RequestID) { |
| 37 | a.failure(w, r, ErrForbidden) |
| 38 | return |
| 39 | } |
| 40 | http.Redirect(w, r, out.Card.Access.ApproveURL, http.StatusSeeOther) |
| 41 | } |
| 42 | |