| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | |
| 8 | "amadan.net/oficina/home/connect" |
| 9 | nativeauth "amadan.net/rastrillo/native/server" |
| 10 | "amadan.net/rastrillo/rastrillo/sessions" |
| 11 | |
| 12 | // The one import left on rastrillo's pre-move path, on purpose: |
| 13 | // rastrillo/native's server (2a5d07b, and its main) still takes a |
| 14 | // github.com/carlosframework/rastrillo *sessions.Sessions. Both |
| 15 | // sessions packages read and write the same frozen sessions table, so |
| 16 | // a session native sign-in mints is an ordinary session to the app. Move this to |
| 17 | // amadan.net/rastrillo/rastrillo/sessions when native/server moves. |
| 18 | presessions "github.com/carlosframework/rastrillo/sessions" |
| 19 | "github.com/go-chi/chi/v5" |
| 20 | ) |
| 21 | |
| 22 | // ConfigureNative reuses the cookie-backed auth and admission path. |
| 23 | func (a *App) ConfigureNative() error { |
| 24 | d, err := a.db.DB() |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | s, err := presessions.New(presessions.Config{DB: d, Origin: a.origin, SigninPath: "/signin"}) |
| 29 | if err != nil { |
| 30 | return err |
| 31 | } |
| 32 | a.native = nativeauth.New(s, a.origin, "Sheets", a.nativeApprover) |
| 33 | return nil |
| 34 | } |
| 35 | |
| 36 | // nativeApprover is the guard rastrillo/native's browser approval runs |
| 37 | // before approving a device (/native/authorize) and again on the approved |
| 38 | // cookie before minting its session (/native/exchange): the app's own |
| 39 | // guard, but never for a native or native-web session. The exchange mints a |
| 40 | // copy that keeps the method with a fresh creation time, and connect's |
| 41 | // seven-day limit counts from that, so a native session approving another |
| 42 | // would renew itself for ever (Fable, sheets b942939). A native session |
| 43 | // signs in with Oficina ID again instead. |
| 44 | func (a *App) nativeApprover(next http.Handler) http.Handler { |
| 45 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 46 | if a.homeSessions != nil { |
| 47 | if s, ok := a.homeSessions.From(r); ok && (s.Method == connect.NativeMethod || s.Method == connect.NativeWebMethod) { |
| 48 | w.Header().Set("Cache-Control", "no-store") |
| 49 | http.Error(w, "Sign in first.", http.StatusForbidden) |
| 50 | return |
| 51 | } |
| 52 | } |
| 53 | a.requireSession(next).ServeHTTP(w, r) |
| 54 | }) |
| 55 | } |
| 56 | func wantsNative(r *http.Request) bool { |
| 57 | return strings.Contains(r.Header.Get("Accept"), "application/json") |
| 58 | } |
| 59 | func nativeJSON(w http.ResponseWriter, status int, value any) { |
| 60 | w.Header().Set("Content-Type", "application/json") |
| 61 | w.Header().Set("Cache-Control", "no-store") |
| 62 | w.Header().Add("Vary", "Accept") |
| 63 | w.WriteHeader(status) |
| 64 | _ = json.NewEncoder(w).Encode(value) |
| 65 | } |
| 66 | |
| 67 | // Only explicit page payloads are serializable. No auth or shell data is exposed. |
| 68 | func nativePage(w http.ResponseWriter, r *http.Request, page string, status int, data any) bool { |
| 69 | if !wantsNative(r) { |
| 70 | return false |
| 71 | } |
| 72 | switch page { |
| 73 | case "sheet", "history", "comments", "share", "table", "import": |
| 74 | nativeJSON(w, status, data) |
| 75 | return true |
| 76 | } |
| 77 | return false |
| 78 | } |
| 79 | func nativeNotice(w http.ResponseWriter, r *http.Request, notice string) bool { |
| 80 | if !wantsNative(r) { |
| 81 | return false |
| 82 | } |
| 83 | message := noticeText(notice) |
| 84 | if message == "" { |
| 85 | message = historyNotice(notice) |
| 86 | } |
| 87 | if message == "" { |
| 88 | message = commentNotice(notice) |
| 89 | } |
| 90 | if message == "" { |
| 91 | message = "Saved." |
| 92 | } |
| 93 | status := http.StatusOK |
| 94 | switch notice { |
| 95 | case "bad-address", "bad-role", "last-owner", "bad-link-role", "empty", "no-cell", "gone": |
| 96 | status = http.StatusUnprocessableEntity |
| 97 | } |
| 98 | nativeJSON(w, status, map[string]string{"notice": notice, "message": message}) |
| 99 | return true |
| 100 | } |
| 101 | func (a *App) handleNativeJob(w http.ResponseWriter, r *http.Request) { |
| 102 | session, ok := sessions.Current(r) |
| 103 | if !ok { |
| 104 | a.notFound(w, r) |
| 105 | return |
| 106 | } |
| 107 | job, ok := a.jobs.Get(chi.URLParam(r, "id"), session.Subject) |
| 108 | if !ok { |
| 109 | a.notFound(w, r) |
| 110 | return |
| 111 | } |
| 112 | // An import job lands on its report, but the native client reads |
| 113 | // the sheet id from location's last segment: it keeps getting the |
| 114 | // sheet, and the report rides beside it. |
| 115 | location, report := job.Location, "" |
| 116 | if sheet, ok := strings.CutSuffix(location, "/import-report"); ok { |
| 117 | location, report = sheet, job.Location |
| 118 | } |
| 119 | nativeJSON(w, 200, map[string]any{"status": job.Status, "progress": job.Progress, |
| 120 | "error": job.Err, "location": location, "report": report}) |
| 121 | } |
| 122 | func (a *App) streamAuthorized(r *http.Request, sheetID uint64) bool { |
| 123 | if a.team != nil { |
| 124 | ok, _ := a.streamAccess(r, sheetID) |
| 125 | return ok |
| 126 | } |
| 127 | address, ok := a.identity(r) |
| 128 | if !ok || !a.Admits(address) { |
| 129 | return false |
| 130 | } |
| 131 | viewer, ok := a.actor(r) |
| 132 | return ok && a.mayRead(sheetID, viewer) |
| 133 | } |
| 134 | |
| 135 | // Local and Home sign-in finish on /sheets; the shared approval continuation |
| 136 | // recognises /. Translate only this middleware probe, never the app route. |
| 137 | func (a *App) nativeResume(next http.Handler) http.Handler { |
| 138 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 139 | probe := r |
| 140 | if r.Method == http.MethodGet && r.URL.Path == "/sheets" { |
| 141 | probe = r.Clone(r.Context()) |
| 142 | u := *r.URL |
| 143 | u.Path = "/" |
| 144 | probe.URL = &u |
| 145 | } |
| 146 | a.native.Resume(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { next.ServeHTTP(w, r) })).ServeHTTP(w, probe) |
| 147 | }) |
| 148 | } |
| 149 | |