| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | |
| 6 | "amadan.net/oficina/home/internal/home/directorydata" |
| 7 | ) |
| 8 | |
| 9 | type directoryApp struct { |
| 10 | slug, name, foreignOrigin string |
| 11 | integrated, provisionable bool |
| 12 | } |
| 13 | |
| 14 | // The registry describes shipped integration, not team standing or readiness. |
| 15 | // Provisioning remains disabled until the audited operator path is available. |
| 16 | func directoryApps() []directoryApp { |
| 17 | return []directoryApp{ |
| 18 | {slug: "docs", name: "Docs", integrated: true}, |
| 19 | {slug: "sheets", name: "Sheets", integrated: true}, |
| 20 | {slug: "slides", name: "Slides", integrated: true}, |
| 21 | {slug: "calendar", name: "Calendar", integrated: true}, |
| 22 | {slug: "calls", name: "Calls", integrated: true}, |
| 23 | {slug: "memoria", name: "Memoria", integrated: true}, |
| 24 | {slug: "equipo", name: "Equipo", integrated: true}, |
| 25 | {slug: "base", name: "Base", integrated: true}, |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func directoryRouteAllowed(route directorydata.Route) bool { |
| 30 | if route.Label == "oficina" { |
| 31 | return directorydata.CanonicalBinding(route.Label, route.App, route.ConsumerID, route.Origin) |
| 32 | } |
| 33 | for _, app := range directoryApps() { |
| 34 | if app.slug == route.App && app.integrated { |
| 35 | return route.Origin == "https://"+route.Label+"."+app.slug+".oficina.works" |
| 36 | } |
| 37 | } |
| 38 | return false |
| 39 | } |
| 40 | |
| 41 | type directoryFeatures struct { |
| 42 | roster *rosterHandler |
| 43 | store *directorydata.Store |
| 44 | docs *docsChooser |
| 45 | choosers []*docsChooser |
| 46 | } |
| 47 | |
| 48 | func (a *App) newDirectoryFeatures() (*directoryFeatures, error) { |
| 49 | var apps []string |
| 50 | for _, app := range directoryApps() { |
| 51 | if app.integrated { |
| 52 | apps = append(apps, app.slug) |
| 53 | } |
| 54 | } |
| 55 | store, err := directorydata.New(a.db.Writer(), a.secrets.Sealer, directorydata.Config{Apps: apps, ValidateRoute: directoryRouteAllowed}) |
| 56 | if err != nil { |
| 57 | return nil, errExchangeConfig |
| 58 | } |
| 59 | chooser, err := a.newDocsChooser(store) |
| 60 | if err != nil { |
| 61 | return nil, errExchangeConfig |
| 62 | } |
| 63 | choosers := []*docsChooser{chooser} |
| 64 | for _, app := range directoryApps() { |
| 65 | if app.slug == "docs" || !app.integrated { |
| 66 | continue |
| 67 | } |
| 68 | c, err := a.newAppChooser(store, app.slug) |
| 69 | if err != nil { |
| 70 | return nil, errExchangeConfig |
| 71 | } |
| 72 | choosers = append(choosers, c) |
| 73 | } |
| 74 | var source rosterReadSource |
| 75 | if a.runtime.roster.mode == "serve" { |
| 76 | source, err = directorydata.NewRosterSource(a.db.Writer(), a.secrets.Sealer, directorydata.Config{Apps: apps, ValidateRoute: directoryRouteAllowed}, a.origins.Directory) |
| 77 | if err != nil { |
| 78 | return nil, errExchangeConfig |
| 79 | } |
| 80 | } |
| 81 | handler, err := newRosterHandler(*a.runtime, source) |
| 82 | if err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | return &directoryFeatures{store: store, docs: chooser, roster: handler, choosers: choosers}, nil |
| 86 | } |
| 87 | |
| 88 | // chooserPath covers the chooser routes and the Team page's: both answer |
| 89 | // per person, so both are no-store before routing and refuse a doubled |
| 90 | // session cookie. |
| 91 | func chooserPath(r *http.Request) bool { |
| 92 | if teamPaths[r.URL.Path] { |
| 93 | return true |
| 94 | } |
| 95 | for _, app := range directoryApps() { |
| 96 | path := "/apps/" + app.slug |
| 97 | if app.integrated && (r.URL.Path == path || r.URL.Path == path+"/team") { |
| 98 | return true |
| 99 | } |
| 100 | } |
| 101 | return false |
| 102 | } |
| 103 | |
| 104 | // Place this before host and method routing so their refusals are not cached. |
| 105 | func chooserNoStore(next http.Handler) http.Handler { |
| 106 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 107 | if chooserPath(r) { |
| 108 | w.Header().Set("Cache-Control", "no-store") |
| 109 | } |
| 110 | next.ServeHTTP(w, r) |
| 111 | }) |
| 112 | } |
| 113 | |
| 114 | // This narrow guard runs before session middleware can resolve the first cookie. |
| 115 | // The existing assertion callback and every other route retain their behavior. |
| 116 | func (a *App) chooserCookies(next http.Handler) http.Handler { |
| 117 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 118 | if chooserPath(r) { |
| 119 | count := 0 |
| 120 | for _, cookie := range r.Cookies() { |
| 121 | if cookie.Name == a.sessions.CookieName() { |
| 122 | count++ |
| 123 | } |
| 124 | } |
| 125 | if count > 1 { |
| 126 | w.WriteHeader(http.StatusBadRequest) |
| 127 | return |
| 128 | } |
| 129 | } |
| 130 | next.ServeHTTP(w, r) |
| 131 | }) |
| 132 | } |
| 133 | |