feat(identity): add server sessions and audited mail recovery

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 11:03:46 +03:00
1 parent 20c03288d6
commit 4ee53d9a74
58 files changed
+5359 -567

No files matched your search

@@ -61,6 +61,9 @@ func NewRouter(
// The client address is resolved once, through the trusted proxies only,
// for the per-client rate limits of the handlers.
router.Use(httpmiddleware.ClientIP(trustedProxies))
// Login and logout compare one Origin header value exactly with the
// configured public origin. Session metadata never trusts forwarded headers.
router.Use(httpmiddleware.SessionRequestMetadata())
router.Use(httpmiddleware.RequestLog(logger, resolveRoute, healthPath, readinessPath))
router.Use(httpmiddleware.CollectMetrics(collector, resolveRoute))
router.Group(func(operational chi.Router) {
@@ -73,7 +76,7 @@ func NewRouter(
// server aborts the response; the kit Recoverer would swallow that panic.
router.Group(func(public chi.Router) {
public.Use(problems.Recover)
public.Handle(apiWildcard, apiServer)
public.Handle(apiWildcard, clearUnauthorizedLogoutCookie(apiServer))
// Unknown /api paths outside the versioned prefix are also problems,
// so every error under /api has the contract's format.
public.Handle(apiRoot, http.HandlerFunc(problems.NotFound))
@@ -82,6 +85,30 @@ func NewRouter(
return router, nil
}
func clearUnauthorizedLogoutCookie(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
clearCookie := r.Method == http.MethodDelete && r.URL.Path == publicPrefix+"/identity/sessions/current"
next.ServeHTTP(&logoutCookieWriter{ResponseWriter: w, clearCookie: clearCookie}, r)
})
}
type logoutCookieWriter struct {
http.ResponseWriter
clearCookie bool
}
func (writer *logoutCookieWriter) WriteHeader(status int) {
if writer.clearCookie && status == http.StatusUnauthorized {
writer.Header().Add("Set-Cookie", v1.ExpiredSessionCookie())
}
writer.ResponseWriter.WriteHeader(status)
}
func (writer *logoutCookieWriter) Unwrap() http.ResponseWriter {
return writer.ResponseWriter
}
// apiWildcard is the chi pattern that mounts the generated server.
const apiWildcard = publicPrefix + "/*"