Skip to content

PLAT-137 Pentest: Change /auth/with-token to use short-lived tokens #7750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refresh cookie on accessing /v1/users/current
  • Loading branch information
NamanMahor committed Aug 5, 2025
commit 2558489e788d2e1f5551b7461b55590ce220eee8
15 changes: 15 additions & 0 deletions admin/server/auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ func (a *Authenticator) HTTPMiddlewareLenient(next http.Handler) http.Handler {
return a.httpMiddleware(next, true)
}

// HTTPMiddlewareWithCookieRefresh is a middleware that refreshes the auth cookie
func (a *Authenticator) HTTPMiddlewareWithCookieRefresh(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sess := a.cookies.Get(r, cookieName)
if authToken, ok := sess.Values[cookieFieldAccessToken].(string); ok && authToken != "" {
// Re-save the cookie to refresh its expiration
if err := sess.Save(r, w); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
next.ServeHTTP(w, r)
})
}

// httpMiddleware is the actual implementation of HTTPMiddleware and HTTPMiddlewareLenient.
func (a *Authenticator) httpMiddleware(next http.Handler, lenient bool) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
6 changes: 5 additions & 1 deletion admin/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func New(logger *zap.Logger, adm *admin.Service, issuer *runtimeauth.Issuer, lim
cookieStore := cookies.New(logger, opts.SessionKeyPairs...)

// Auth tokens are validated against the DB on each request, so we can set a long MaxAge.
cookieStore.MaxAge(60 * 60 * 24 * 365 * 10) // 10 years
cookieStore.MaxAge(60 * 60 * 24 * 14) // 14 days

// Set Secure if the admin service is served over HTTPS (will resolve to true in production and false in local dev environments).
cookieStore.Options.Secure = adm.URLs.IsHTTPS()
Expand Down Expand Up @@ -185,6 +185,10 @@ func (s *Server) HTTPHandler(ctx context.Context) (http.Handler, error) {
if err != nil {
return nil, fmt.Errorf("failed to create transcoder: %w", err)
}

// Add specific route for GetCurrentUser with cookie refresh
mux.Handle("/v1/users/current", s.authenticator.HTTPMiddlewareWithCookieRefresh(transcoder))

mux.Handle("/v1/", transcoder)
mux.Handle("/rill.admin.v1.AdminService/", transcoder)
mux.Handle("/rill.admin.v1.AIService/", transcoder)
Expand Down
Loading