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)
);
-- 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();
+12 -7
View File
@@ -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
}
+10 -14
View File
@@ -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
}