SessionAuth middleware resolves session cookies into authenticated users. If a user was already resolved by TokenAuth, this is a no-op.
(next http.Handler)
| 290 | // SessionAuth middleware resolves session cookies into authenticated users. |
| 291 | // If a user was already resolved by TokenAuth, this is a no-op. |
| 292 | func (s *Server) SessionAuth(next http.Handler) http.Handler { |
| 293 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 294 | // Already authenticated by TokenAuth — user-bound API tokens set |
| 295 | // currentUser, legacy workspace-scoped tokens set tokenWorkspaceID |
| 296 | // with no user. In either case we must short-circuit: otherwise a |
| 297 | // stale session cookie on the same request could trigger the |
| 298 | // IP-change strict-mode path and 401 the request even though the |
| 299 | // API token itself is valid. |
| 300 | if currentUser(r) != nil || tokenWorkspaceID(r) != "" { |
| 301 | next.ServeHTTP(w, r) |
| 302 | return |
| 303 | } |
| 304 | |
| 305 | // Try session cookie (with fallback to unprefixed name for upgrade path) |
| 306 | cookie, err := r.Cookie(sessionCookieName(s.secureCookies)) |
| 307 | if err != nil { |
| 308 | cookie, err = r.Cookie("pad_session") |
| 309 | if err != nil { |
| 310 | next.ServeHTTP(w, r) |
| 311 | return |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | session, err := s.store.ValidateSessionContext(r.Context(), cookie.Value) |
| 316 | if err != nil && r.Context().Err() != nil { |
| 317 | return // client gone: quiet (TASK-4278) |
| 318 | } |
| 319 | // A CLI session is a bearer credential, never a cookie (BUG-3350). |
| 320 | if err != nil || session == nil || !cookieKindAccepted(session) { |
| 321 | next.ServeHTTP(w, r) |
| 322 | return |
| 323 | } |
| 324 | |
| 325 | // Session binding: detect a User-Agent change. Logged in all modes; |
| 326 | // the session is revoked only when PAD_IP_CHANGE_ENFORCE=strict. See |
| 327 | // the matching note in TokenAuth above — UA is client-supplied, weak |
| 328 | // as a binding, and false-positives on routine client churn |
| 329 | // (browser/WebView updates, DevTools device emulation, mobile-app |
| 330 | // rebuilds), so it stays log-only by default. |
| 331 | switch s.handleSessionUAChange(w, r, session, cookie.Value) { |
| 332 | case sessionIPChangeTerminated: |
| 333 | return |
| 334 | case sessionIPChangeRevoked: |
| 335 | // Browser path: let the request through unauthenticated so |
| 336 | // the SPA renders its login flow instead of a JSON error. |
| 337 | next.ServeHTTP(w, r) |
| 338 | return |
| 339 | } |
| 340 | |
| 341 | // Session binding: detect IP changes. Log to audit log in all modes; |
| 342 | // reject only when PAD_IP_CHANGE_ENFORCE=strict. |
| 343 | switch s.handleSessionIPChange(w, r, session, cookie.Value) { |
| 344 | case sessionIPChangeTerminated: |
| 345 | return |
| 346 | case sessionIPChangeRevoked: |
| 347 | // Browser path: let the request through unauthenticated so |
| 348 | // the SPA renders its login flow instead of a JSON error. |
| 349 | next.ServeHTTP(w, r) |
nothing calls this directly
no test coverage detected