fix(auth): cap session cache TTL by remaining session lifetime

This commit is contained in:
kk10
2026-10-10 10:38:55 +03:00
parent af88065a1c
commit d25054843c
3 changed files with 24 additions and 23 deletions
+2 -2
View File
@@ -9,8 +9,8 @@ INSERT INTO sessions (
sqlc.arg(expires_at) sqlc.arg(expires_at)
); );
-- name: GetSessionUserID :one -- name: GetActiveSession :one
SELECT user_id FROM sessions SELECT user_id, expires_at FROM sessions
WHERE token_hash = sqlc.arg(token_hash) WHERE token_hash = sqlc.arg(token_hash)
AND expires_at > now(); AND expires_at > now();
+12 -7
View File
@@ -62,15 +62,20 @@ func (q *Queries) DeleteSession(ctx context.Context, tokenHash string) (int64, e
return result.RowsAffected(), nil return result.RowsAffected(), nil
} }
const getSessionUserID = `-- name: GetSessionUserID :one const getActiveSession = `-- name: GetActiveSession :one
SELECT user_id FROM sessions SELECT user_id, expires_at FROM sessions
WHERE token_hash = $1 WHERE token_hash = $1
AND expires_at > now() AND expires_at > now()
` `
func (q *Queries) GetSessionUserID(ctx context.Context, tokenHash string) (uuid.UUID, error) { type GetActiveSessionRow struct {
row := q.db.QueryRow(ctx, getSessionUserID, tokenHash) UserID uuid.UUID
var user_id uuid.UUID ExpiresAt time.Time
err := row.Scan(&user_id) }
return user_id, err
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
} }
+10 -14
View File
@@ -26,7 +26,7 @@ var (
type authQueries interface { type authQueries interface {
CreateSession(ctx context.Context, arg db.CreateSessionParams) error CreateSession(ctx context.Context, arg db.CreateSessionParams) error
DeleteSession(ctx context.Context, tokenHash string) (int64, 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) 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) 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) { if postgres.IsNotFound(err) {
return uuid.Nil, ErrUnauthorized return uuid.Nil, ErrUnauthorized
} }
@@ -115,13 +115,16 @@ func (s *AuthService) Authenticate(ctx context.Context, token string) (uuid.UUID
return uuid.Nil, err return uuid.Nil, err
} }
cctx, cancel = context.WithTimeout(ctx, cacheOpTimeout) ttl := min(time.Until(row.ExpiresAt), sessionCacheTTL)
if err := s.cache.Set(cctx, key, userID.String(), s.cacheTTL()).Err(); err != nil { if ttl > 0 {
s.log.Warn("session cache set failed", "error", err) 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 { 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 { func cacheKey(tokenHash string) string {
return "session:" + tokenHash return "session:" + tokenHash
} }
func (s *AuthService) cacheTTL() time.Duration {
if s.sessionTTL < sessionCacheTTL {
return s.sessionTTL
}
return sessionCacheTTL
}