package webui import ( "net/http" "strings" ) // cookieNames are the dashboard's cookie names. Browsers scope cookies by host // and ignore the port, so each name carries the instance: two instances on // one host would otherwise overwrite each other's sessions. type cookieNames struct { token, account, providerToken, loginState, loginVerifier, passkeyRegistration, passkeyLogin, org string } func newCookieNames(instance string) cookieNames { prefix := strings.ReplaceAll(instance, "-", "_") if prefix == "" { prefix = "archie" } return cookieNames{ token: prefix + "_ui", account: prefix + "_session", providerToken: prefix + "_provider_token", loginState: prefix + "_login_state", loginVerifier: prefix + "_login_verifier", passkeyRegistration: prefix + "_passkey_registration", passkeyLogin: prefix + "_passkey_login", org: prefix + "_org", } } func (s *Server) cookies() cookieNames { return newCookieNames(s.Instance) } // cookie is what varies between the dashboard's cookies. Every one is // HttpOnly, and Secure whenever the browser reached the dashboard over HTTPS. type cookie struct { name, value, path string // sameSite defaults to Strict; the sign-in flow needs Lax to survive the // provider's redirect back. sameSite http.SameSite // maxAge is in seconds; negative deletes the cookie. maxAge int } func (s *Server) setCookie(w http.ResponseWriter, r *http.Request, c cookie) { if c.path == "" { c.path = "/" } if c.sameSite == 0 { c.sameSite = http.SameSiteStrictMode } http.SetCookie(w, &http.Cookie{ //nolint:gosec // G124: a fixed Secure would stop plain-HTTP deployments signing in. Name: c.name, Value: c.value, Path: c.path, MaxAge: c.maxAge, HttpOnly: true, Secure: s.secureRequest(r), SameSite: c.sameSite, }) } func (s *Server) clearCookie(w http.ResponseWriter, r *http.Request, name, path string) { s.setCookie(w, r, cookie{name: name, path: path, maxAge: -1}) }