Упростить ошибки сессий, логировать миграции и тестировать с рабочим клиентом Valkey

This commit is contained in:
2026-10-06 15:46:01 +03:00
parent 405fb07608
commit f9850a36c4
7 changed files with 39 additions and 34 deletions
+3 -6
View File
@@ -2,19 +2,16 @@ package auth
import (
"context"
"errors"
"github.com/google/uuid"
)
// ErrSessionNotFound means the token is unknown, expired or revoked; the
// store cannot tell these apart, and the client must not either.
var ErrSessionNotFound = errors.New("session not found")
type Repository interface {
// Create stores the session until its absolute ExpiresAt.
Create(ctx context.Context, s Session) error
// UserID returns the owner of a live session or ErrSessionNotFound.
// UserID returns the owner of a live session, or ErrUnauthenticated when
// the token is unknown, expired or revoked: the store cannot tell these
// apart, and the client must not either.
UserID(ctx context.Context, tokenHash string) (uuid.UUID, error)
// Delete revokes the session; deleting a missing one is not an error.
Delete(ctx context.Context, tokenHash string) error
+4 -9
View File
@@ -2,7 +2,6 @@ package auth
import (
"context"
"errors"
"time"
"github.com/google/uuid"
@@ -53,15 +52,11 @@ func (s *Service) Login(ctx context.Context, username, password string) (Issued,
return Issued{Token: token, ExpiresAt: expiresAt, User: u}, nil
}
// Authenticate returns the owner of the token's live session. A store
// failure is returned as is, never as ErrUnauthenticated: an outage must not
// look like a bad token to the client.
// Authenticate returns the owner of the token's live session or
// ErrUnauthenticated. A store failure stays an unavailability, never
// ErrUnauthenticated: an outage must not look like a bad token to the client.
func (s *Service) Authenticate(ctx context.Context, token string) (uuid.UUID, error) {
id, err := s.sessions.UserID(ctx, HashToken(token))
if errors.Is(err, ErrSessionNotFound) {
return uuid.Nil, ErrUnauthenticated
}
return id, err
return s.sessions.UserID(ctx, HashToken(token))
}
func (s *Service) Logout(ctx context.Context, token string) error {
+4 -4
View File
@@ -27,10 +27,10 @@ func NewSessions(client *redis.Client) *Sessions {
return &Sessions{client: client}
}
// Create sets an absolute expiry (PXAT): a restart of the store replays it
// from the append-only file and does not extend the session.
// Create sets an absolute expiry (EXAT/PXAT): a restart of the store replays
// it from the append-only file and does not extend the session.
func (r *Sessions) Create(ctx context.Context, s auth.Session) error {
err := r.client.Do(ctx, "SET", keyPrefix+s.TokenHash, s.UserID.String(), "PXAT", s.ExpiresAt.UnixMilli()).Err()
err := r.client.SetArgs(ctx, keyPrefix+s.TokenHash, s.UserID.String(), redis.SetArgs{ExpireAt: s.ExpiresAt}).Err()
if err != nil {
return unavailable(err)
}
@@ -40,7 +40,7 @@ func (r *Sessions) Create(ctx context.Context, s auth.Session) error {
func (r *Sessions) UserID(ctx context.Context, tokenHash string) (uuid.UUID, error) {
raw, err := r.client.Get(ctx, keyPrefix+tokenHash).Result()
if errors.Is(err, redis.Nil) {
return uuid.Nil, auth.ErrSessionNotFound
return uuid.Nil, auth.ErrUnauthenticated
}
if err != nil {
return uuid.Nil, unavailable(err)