| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "log/slog" |
| 8 | "net/http" |
| 9 | "slices" |
| 10 | "strings" |
| 11 | |
| 12 | "amadan.net/oficina/estilo.git/chrome" |
| 13 | "amadan.net/rastrillo/rastrillo/auth" |
| 14 | "amadan.net/rastrillo/rastrillo/mail" |
| 15 | ) |
| 16 | |
| 17 | // signinPath is where every sign-in outcome lands. It must match the |
| 18 | // SigninPath given to auth.New, because beginGuard redirects here itself: |
| 19 | // if the two disagree the guard is told apart by its destination alone. |
| 20 | const signinPath = "/signin" |
| 21 | |
| 22 | // ParseOwners splits a comma-separated admission list, normalised. |
| 23 | func ParseOwners(raw string) []string { |
| 24 | var owners []string |
| 25 | for _, part := range strings.Split(raw, ",") { |
| 26 | if address := normaliseAddress(part); address != "" { |
| 27 | owners = append(owners, address) |
| 28 | } |
| 29 | } |
| 30 | return owners |
| 31 | } |
| 32 | |
| 33 | // normaliseAddress is the one spelling rule. A guard that spells an |
| 34 | // address differently from the thing it guards is not guarding it. |
| 35 | func normaliseAddress(address string) string { |
| 36 | return strings.ToLower(strings.TrimSpace(address)) |
| 37 | } |
| 38 | |
| 39 | // Admit is THE admission predicate: the one rastrillo runs at |
| 40 | // verification, the one beginGuard runs before the classifier dials, and |
| 41 | // the one requireSession runs on every request. Gate 9: taking a person |
| 42 | // off the list stops all three at once. |
| 43 | func (app *app) Admit(address string) bool { |
| 44 | normal := normaliseAddress(address) |
| 45 | return normal != "" && slices.Contains(app.config.Owners, normal) |
| 46 | } |
| 47 | |
| 48 | // beginGuard stops an anonymous visitor making this instance dial a |
| 49 | // domain of their choosing: rastrillo's classifier fetches the submitted |
| 50 | // domain's /.well-known/keymail before Authorize runs (Fichas, memoria |
| 51 | // §3.2). It answers a refusal exactly as it answers a send, or it would |
| 52 | // be an oracle for the admission list. |
| 53 | func (app *app) beginGuard(next http.HandlerFunc) http.HandlerFunc { |
| 54 | return func(writer http.ResponseWriter, request *http.Request) { |
| 55 | // FormValue, exactly as rastrillo reads it: memoria once |
| 56 | // parsed differently and `POST /auth/begin?a;b=1` dialled. |
| 57 | if !app.Admit(request.FormValue("address")) { |
| 58 | app.logger.Info("sign-in refused before classifier dial") |
| 59 | http.Redirect(writer, request, signinPath+"?sent=1", |
| 60 | http.StatusSeeOther) |
| 61 | return |
| 62 | } |
| 63 | next(writer, request) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // AuthEnv is the sign-in configuration read from platform config. |
| 68 | type AuthEnv struct { |
| 69 | Origin string |
| 70 | InstanceKey string |
| 71 | Mailer mail.Sender |
| 72 | } |
| 73 | |
| 74 | // NewAuth wires the sign-in flow. Exactly one per process: auth carries |
| 75 | // rate-limiter state, and one per request would rate-limit nothing. |
| 76 | func NewAuth(database *sql.DB, env AuthEnv, logger *slog.Logger, |
| 77 | authorize func(string) bool) (*auth.Auth, error) { |
| 78 | if authorize == nil { |
| 79 | return nil, errors.New( |
| 80 | "sign-in: refusing to wire auth with no admission gate") |
| 81 | } |
| 82 | return auth.New(auth.Config{ |
| 83 | DB: database, Origin: env.Origin, InstanceKey: env.InstanceKey, |
| 84 | Mailer: env.Mailer, Authorize: authorize, |
| 85 | SigninPath: signinPath, SignedInPath: "/", |
| 86 | Subject: chrome.EmailSubject("Mapa"), Logger: logger, |
| 87 | // No proof-of-work, as in Docs and Fichas: the abuse controls |
| 88 | // are rastrillo's rate limits and the admission list. |
| 89 | ProofOff: true, |
| 90 | // The emailed link opens the door with one Sign in button; |
| 91 | // only its POST spends the token. |
| 92 | RenderConfirm: confirmSignin, |
| 93 | }) |
| 94 | } |
| 95 | |
| 96 | // identity is the signed-in address, or false. |
| 97 | func (app *app) identity(request *http.Request) (string, bool) { |
| 98 | if app.allowUnauthenticated { |
| 99 | return app.testPerson(request), true |
| 100 | } |
| 101 | identity, ok := auth.From(request) |
| 102 | if !ok { |
| 103 | return "", false |
| 104 | } |
| 105 | return normaliseAddress(identity.Address), true |
| 106 | } |
| 107 | |
| 108 | // requireSession guards every route that is not sign-in itself, and it |
| 109 | // fails closed: memoria once fell through on a nil auth and served every |
| 110 | // route unauthenticated. A test that wants a signed-in app asks for one |
| 111 | // by name (AppForTest). |
| 112 | func (app *app) requireSession(next http.Handler) http.Handler { |
| 113 | return http.HandlerFunc(func(writer http.ResponseWriter, |
| 114 | request *http.Request) { |
| 115 | if app.allowUnauthenticated { |
| 116 | next.ServeHTTP(writer, request) |
| 117 | return |
| 118 | } |
| 119 | if app.auth == nil { |
| 120 | app.logger.Error("no sign-in configured; refusing") |
| 121 | http.Error(writer, "sign-in is not configured", |
| 122 | http.StatusInternalServerError) |
| 123 | return |
| 124 | } |
| 125 | app.auth.RequireSession(app.admitted(next)).ServeHTTP(writer, |
| 126 | request) |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | // admitted re-checks the admission list on every request, so a person |
| 131 | // taken off it is out at once, session or not (gate 9). A route you may |
| 132 | // not use is a route that is not there: 404, never 403. |
| 133 | func (app *app) admitted(next http.Handler) http.Handler { |
| 134 | return http.HandlerFunc(func(writer http.ResponseWriter, |
| 135 | request *http.Request) { |
| 136 | address, ok := app.identity(request) |
| 137 | if !ok || !app.Admit(address) { |
| 138 | notFound(writer, request) |
| 139 | return |
| 140 | } |
| 141 | next.ServeHTTP(writer, request) |
| 142 | }) |
| 143 | } |
| 144 | |
| 145 | // testPersonKey carries Built.As's address. Only read when the app was |
| 146 | // built by AppForTest. |
| 147 | type testPersonKey struct{} |
| 148 | |
| 149 | func (app *app) testPerson(request *http.Request) string { |
| 150 | if as, ok := request.Context().Value(testPersonKey{}).(string); ok { |
| 151 | return as |
| 152 | } |
| 153 | return app.testIdentity |
| 154 | } |
| 155 | |
| 156 | // As serves mux as address, for tests that need two people in one app |
| 157 | // (gate 1's Ana and Bo). It refuses an app AppForTest did not build. |
| 158 | func (built Built) As(address string) http.Handler { |
| 159 | if !built.app.allowUnauthenticated { |
| 160 | panic("mapa: As is for apps built by AppForTest") |
| 161 | } |
| 162 | return http.HandlerFunc(func(writer http.ResponseWriter, |
| 163 | request *http.Request) { |
| 164 | ctx := context.WithValue(request.Context(), testPersonKey{}, |
| 165 | normaliseAddress(address)) |
| 166 | built.Mux.ServeHTTP(writer, request.WithContext(ctx)) |
| 167 | }) |
| 168 | } |
| 169 | |