forked from mtusi2026/mtusi2026lab2
ЛР 2: PostgreSQL, сессии в Valkey, кэш истории (трек 1)
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/store"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/validate"
|
||||
)
|
||||
|
||||
var (
|
||||
_ ChatsService = (*Chats)(nil)
|
||||
)
|
||||
|
||||
type Chats struct {
|
||||
store store.ChatStore
|
||||
}
|
||||
|
||||
func NewChats(s store.ChatStore) *Chats {
|
||||
return &Chats{store: s}
|
||||
}
|
||||
|
||||
func (c *Chats) Create(ctx context.Context, callerID, title string, memberIDs []string) (domain.Chat, error) {
|
||||
if err := validate.ChatTitle(title); err != nil {
|
||||
return domain.Chat{}, err
|
||||
}
|
||||
memberIDs, err := validate.MemberIDs(memberIDs, callerID)
|
||||
if err != nil {
|
||||
return domain.Chat{}, err
|
||||
}
|
||||
return c.store.CreateChat(ctx, callerID, title, memberIDs)
|
||||
}
|
||||
|
||||
func (c *Chats) Get(ctx context.Context, callerID, chatID string) (domain.Chat, error) {
|
||||
chatID, err := validate.CanonicalUUID("chat_id", chatID)
|
||||
if err != nil {
|
||||
return domain.Chat{}, err
|
||||
}
|
||||
return c.store.ChatByID(ctx, callerID, chatID)
|
||||
}
|
||||
|
||||
func (c *Chats) List(ctx context.Context, callerID, cursor string, limit int) (domain.Page[domain.Chat], error) {
|
||||
if err := validate.Limit(limit); err != nil {
|
||||
return domain.Page[domain.Chat]{}, err
|
||||
}
|
||||
return c.store.ListChats(ctx, callerID, cursor, limit)
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/store"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/validate"
|
||||
)
|
||||
|
||||
var (
|
||||
_ MessagesService = (*Messages)(nil)
|
||||
)
|
||||
|
||||
type Messages struct {
|
||||
store store.MessageStore
|
||||
}
|
||||
|
||||
func NewMessages(s store.MessageStore) *Messages {
|
||||
return &Messages{store: s}
|
||||
}
|
||||
|
||||
func (m *Messages) Send(ctx context.Context, callerID, chatID, text string) (domain.Message, error) {
|
||||
chatID, err := validate.CanonicalUUID("chat_id", chatID)
|
||||
if err != nil {
|
||||
return domain.Message{}, err
|
||||
}
|
||||
if err := validate.MessageText(text); err != nil {
|
||||
return domain.Message{}, err
|
||||
}
|
||||
return m.store.AddMessage(ctx, callerID, chatID, text)
|
||||
}
|
||||
|
||||
func (m *Messages) List(ctx context.Context, callerID, chatID, cursor string, limit int) (domain.Page[domain.Message], error) {
|
||||
chatID, err := validate.CanonicalUUID("chat_id", chatID)
|
||||
if err != nil {
|
||||
return domain.Page[domain.Message]{}, err
|
||||
}
|
||||
if err := validate.Limit(limit); err != nil {
|
||||
return domain.Page[domain.Message]{}, err
|
||||
}
|
||||
return m.store.ListMessages(ctx, callerID, chatID, cursor, limit)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
||||
)
|
||||
|
||||
type UsersService interface {
|
||||
Register(ctx context.Context, username, displayName, password string) (domain.User, error)
|
||||
Me(ctx context.Context, callerID string) (domain.User, error)
|
||||
}
|
||||
|
||||
type ChatsService interface {
|
||||
Create(ctx context.Context, callerID, title string, memberIDs []string) (domain.Chat, error)
|
||||
Get(ctx context.Context, callerID, chatID string) (domain.Chat, error)
|
||||
List(ctx context.Context, callerID, cursor string, limit int) (domain.Page[domain.Chat], error)
|
||||
}
|
||||
|
||||
type MessagesService interface {
|
||||
Send(ctx context.Context, callerID, chatID, text string) (domain.Message, error)
|
||||
List(ctx context.Context, callerID, chatID, cursor string, limit int) (domain.Page[domain.Message], error)
|
||||
}
|
||||
|
||||
type AuthService interface {
|
||||
Login(ctx context.Context, username, password string) (domain.Session, error)
|
||||
Authenticate(ctx context.Context, token string) (userID string, err error)
|
||||
Logout(ctx context.Context, token string) error
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/password"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/store"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/validate"
|
||||
)
|
||||
|
||||
var (
|
||||
_ UsersService = (*Users)(nil)
|
||||
)
|
||||
|
||||
type Users struct {
|
||||
store store.UserStore
|
||||
hasher *password.Hasher
|
||||
}
|
||||
|
||||
func NewUsers(s store.UserStore, h *password.Hasher) *Users {
|
||||
return &Users{store: s, hasher: h}
|
||||
}
|
||||
|
||||
func (u *Users) Register(ctx context.Context, username, displayName, plainPassword string) (domain.User, error) {
|
||||
if err := validate.Username(username); err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
if err := validate.DisplayName(displayName); err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
if err := validate.Password(plainPassword); err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
hash, err := u.hasher.Hash(ctx, plainPassword)
|
||||
if err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
return u.store.CreateUser(ctx, username, displayName, hash)
|
||||
}
|
||||
|
||||
func (u *Users) Me(ctx context.Context, callerID string) (domain.User, error) {
|
||||
return u.store.UserByID(ctx, callerID)
|
||||
}
|
||||
Reference in New Issue
Block a user