forked from mtusi2026/mtusi2026lab2
102 lines
2.7 KiB
Go
102 lines
2.7 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/password"
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/session"
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/store"
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/token"
|
|
)
|
|
|
|
var (
|
|
_ AuthService = (*Auth)(nil)
|
|
)
|
|
|
|
type Auth struct {
|
|
users store.UserStore
|
|
sessions session.Store
|
|
hasher *password.Hasher
|
|
ttl time.Duration
|
|
now func() time.Time
|
|
|
|
dummyHash string
|
|
}
|
|
|
|
func NewAuth(users store.UserStore, sessions session.Store, hasher *password.Hasher, ttl time.Duration) (*Auth, error) {
|
|
dummy, err := hasher.Hash(context.Background(), "dummy-password-for-timing")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("auth: dummy hash: %w", err)
|
|
}
|
|
return &Auth{
|
|
users: users,
|
|
sessions: sessions,
|
|
hasher: hasher,
|
|
ttl: ttl,
|
|
now: time.Now,
|
|
dummyHash: dummy,
|
|
}, nil
|
|
}
|
|
|
|
func (a *Auth) Login(ctx context.Context, username, plainPassword string) (domain.Session, error) {
|
|
user, hash, err := a.users.CredentialsByUsername(ctx, username)
|
|
switch {
|
|
case errors.Is(err, domain.ErrNotFound):
|
|
_, _ = a.hasher.Verify(ctx, plainPassword, a.dummyHash)
|
|
return domain.Session{}, domain.ErrUnauthorized
|
|
case err != nil:
|
|
return domain.Session{}, err
|
|
}
|
|
|
|
ok, err := a.hasher.Verify(ctx, plainPassword, hash)
|
|
if err != nil {
|
|
if errors.Is(err, password.ErrMalformedHash) {
|
|
return domain.Session{}, domain.ErrUnauthorized
|
|
}
|
|
return domain.Session{}, err
|
|
}
|
|
if !ok {
|
|
return domain.Session{}, domain.ErrUnauthorized
|
|
}
|
|
|
|
plain, tokenHash, err := token.New()
|
|
if err != nil {
|
|
return domain.Session{}, err
|
|
}
|
|
expiresAt := a.now().Add(a.ttl).UTC().Truncate(time.Second)
|
|
if err := a.sessions.Save(ctx, tokenHash, session.Session{UserID: user.ID, ExpiresAt: expiresAt}); err != nil {
|
|
return domain.Session{}, err
|
|
}
|
|
return domain.Session{Token: plain, ExpiresAt: expiresAt, User: user}, nil
|
|
}
|
|
|
|
func (a *Auth) Authenticate(ctx context.Context, plainToken string) (string, error) {
|
|
tokenHash, err := token.Parse(plainToken)
|
|
if err != nil {
|
|
return "", domain.ErrUnauthorized
|
|
}
|
|
s, err := a.sessions.Get(ctx, tokenHash)
|
|
if errors.Is(err, session.ErrNotFound) {
|
|
return "", domain.ErrUnauthorized
|
|
}
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if !a.now().Before(s.ExpiresAt) {
|
|
return "", domain.ErrUnauthorized
|
|
}
|
|
return s.UserID, nil
|
|
}
|
|
|
|
func (a *Auth) Logout(ctx context.Context, plainToken string) error {
|
|
tokenHash, err := token.Parse(plainToken)
|
|
if err != nil {
|
|
return domain.ErrUnauthorized
|
|
}
|
|
return a.sessions.Delete(ctx, tokenHash)
|
|
}
|