fix(auth): wiring Auth/Sessions, нормализация email, GetUserByID для /me, 409 на дубль, timing-guard логина

This commit is contained in:
2026-07-03 20:29:05 +07:00
parent aa0ef1c6a9
commit 35ffe73ae3
8 changed files with 265 additions and 10 deletions
+21
View File
@@ -7,6 +7,7 @@ import (
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgtype"
"github.com/vasyakrg/dns-autoresolver/internal/provider"
@@ -14,6 +15,11 @@ import (
"github.com/vasyakrg/dns-autoresolver/internal/store/dto"
)
// ErrEmailTaken is returned by RegisterUser when the email is already
// registered — a UNIQUE constraint violation (pgerrcode 23505) on
// users.email.
var ErrEmailTaken = errors.New("store: email already registered")
// Account/Template/Domain are provider-neutral domain structs returned by the
// thin wrappers below, so callers (internal/api) never need to import
// internal/store/db directly.
@@ -279,6 +285,17 @@ func (s *Store) GetUserByEmail(ctx context.Context, email string) (User, error)
return toUser(u), nil
}
// GetUserByID looks up a user by primary key — used by handleMe (Task 3
// hardening) to return the authenticated caller's real email instead of
// leaving it blank.
func (s *Store) GetUserByID(ctx context.Context, id uuid.UUID) (User, error) {
u, err := s.q.GetUserByID(ctx, id)
if err != nil {
return User{}, err
}
return toUser(u), nil
}
func (s *Store) CreateProjectForUser(ctx context.Context, userID uuid.UUID, name string) (Project, error) {
p, err := s.q.CreateProject(ctx, db.CreateProjectParams{ID: uuid.New(), UserID: userID, Name: name})
if err != nil {
@@ -337,6 +354,10 @@ func (s *Store) RegisterUser(ctx context.Context, email, passwordHash string) (U
uid := uuid.New()
dbu, err := q.CreateUser(ctx, db.CreateUserParams{ID: uid, Email: email, PasswordHash: ptr(passwordHash)})
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
return User{}, Project{}, ErrEmailTaken
}
return User{}, Project{}, err
}
dbp, err := q.CreateProject(ctx, db.CreateProjectParams{ID: uuid.New(), UserID: uid, Name: "default"})