From d25054843caafc63ffb4a82abb78fb31c1d9129f Mon Sep 17 00:00:00 2001 From: kk10 Date: Sat, 10 Oct 2026 10:38:55 +0300 Subject: [PATCH] fix(auth): cap session cache TTL by remaining session lifetime --- db/queries/sessions.sql | 4 ++-- internal/db/sessions.sql.go | 19 ++++++++++++------- internal/service/auth.go | 24 ++++++++++-------------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/db/queries/sessions.sql b/db/queries/sessions.sql index 4f1decde..065907f2 100644 --- a/db/queries/sessions.sql +++ b/db/queries/sessions.sql @@ -9,8 +9,8 @@ INSERT INTO sessions ( sqlc.arg(expires_at) ); --- name: GetSessionUserID :one -SELECT user_id FROM sessions +-- name: GetActiveSession :one +SELECT user_id, expires_at FROM sessions WHERE token_hash = sqlc.arg(token_hash) AND expires_at > now(); diff --git a/internal/db/sessions.sql.go b/internal/db/sessions.sql.go index 3d2e9de2..fa6abddb 100644 --- a/internal/db/sessions.sql.go +++ b/internal/db/sessions.sql.go @@ -62,15 +62,20 @@ func (q *Queries) DeleteSession(ctx context.Context, tokenHash string) (int64, e return result.RowsAffected(), nil } -const getSessionUserID = `-- name: GetSessionUserID :one -SELECT user_id FROM sessions +const getActiveSession = `-- name: GetActiveSession :one +SELECT user_id, expires_at FROM sessions WHERE token_hash = $1 AND expires_at > now() ` -func (q *Queries) GetSessionUserID(ctx context.Context, tokenHash string) (uuid.UUID, error) { - row := q.db.QueryRow(ctx, getSessionUserID, tokenHash) - var user_id uuid.UUID - err := row.Scan(&user_id) - return user_id, err +type GetActiveSessionRow struct { + UserID uuid.UUID + ExpiresAt time.Time +} + +func (q *Queries) GetActiveSession(ctx context.Context, tokenHash string) (GetActiveSessionRow, error) { + row := q.db.QueryRow(ctx, getActiveSession, tokenHash) + var i GetActiveSessionRow + err := row.Scan(&i.UserID, &i.ExpiresAt) + return i, err } diff --git a/internal/service/auth.go b/internal/service/auth.go index e15cc649..97eb5bef 100644 --- a/internal/service/auth.go +++ b/internal/service/auth.go @@ -26,7 +26,7 @@ var ( type authQueries interface { CreateSession(ctx context.Context, arg db.CreateSessionParams) error DeleteSession(ctx context.Context, tokenHash string) (int64, error) - GetSessionUserID(ctx context.Context, tokenHash string) (uuid.UUID, error) + GetActiveSession(ctx context.Context, tokenHash string) (db.GetActiveSessionRow, error) GetUserByUsername(ctx context.Context, username string) (db.User, error) } @@ -107,7 +107,7 @@ func (s *AuthService) Authenticate(ctx context.Context, token string) (uuid.UUID s.log.Warn("session cache get failed, falling back to database", "error", err) } - userID, err := s.q.GetSessionUserID(ctx, tokenHash) + row, err := s.q.GetActiveSession(ctx, tokenHash) if postgres.IsNotFound(err) { return uuid.Nil, ErrUnauthorized } @@ -115,13 +115,16 @@ func (s *AuthService) Authenticate(ctx context.Context, token string) (uuid.UUID return uuid.Nil, err } - cctx, cancel = context.WithTimeout(ctx, cacheOpTimeout) - if err := s.cache.Set(cctx, key, userID.String(), s.cacheTTL()).Err(); err != nil { - s.log.Warn("session cache set failed", "error", err) + ttl := min(time.Until(row.ExpiresAt), sessionCacheTTL) + if ttl > 0 { + cctx, cancel = context.WithTimeout(ctx, cacheOpTimeout) + if err := s.cache.Set(cctx, key, row.UserID.String(), ttl).Err(); err != nil { + s.log.Warn("session cache set failed", "error", err) + } + cancel() } - cancel() - return userID, nil + return row.UserID, nil } func (s *AuthService) Logout(ctx context.Context, token string) error { @@ -147,10 +150,3 @@ func (s *AuthService) Logout(ctx context.Context, token string) error { func cacheKey(tokenHash string) string { return "session:" + tokenHash } - -func (s *AuthService) cacheTTL() time.Duration { - if s.sessionTTL < sessionCacheTTL { - return s.sessionTTL - } - return sessionCacheTTL -}