forked from mtusi2026/mtusi2026lab2
lab2
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
"mtusi-messenger/internal/platform/cache"
|
||||
)
|
||||
|
||||
type SessionSource interface {
|
||||
Credentials(ctx context.Context, username string) (domain.UserCredentials, error)
|
||||
Create(ctx context.Context, session domain.NewSession) (domain.Session, error)
|
||||
Resolve(ctx context.Context, tokenHash []byte) (domain.Session, error)
|
||||
Revoke(ctx context.Context, userID uuid.UUID, tokenHash []byte) error
|
||||
}
|
||||
|
||||
type SessionRepository struct {
|
||||
source SessionSource
|
||||
cache cache.Cache
|
||||
ttlCap time.Duration
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func NewSessionRepository(
|
||||
source SessionSource,
|
||||
cache cache.Cache,
|
||||
ttlCap time.Duration,
|
||||
log *slog.Logger,
|
||||
) *SessionRepository {
|
||||
return &SessionRepository{source: source, cache: cache, ttlCap: ttlCap, log: log}
|
||||
}
|
||||
|
||||
func (r *SessionRepository) Credentials(ctx context.Context, username string) (domain.UserCredentials, error) {
|
||||
return r.source.Credentials(ctx, username)
|
||||
}
|
||||
|
||||
func (r *SessionRepository) Create(ctx context.Context, session domain.NewSession) (domain.Session, error) {
|
||||
return r.source.Create(ctx, session)
|
||||
}
|
||||
|
||||
type cachedSession struct {
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
User cachedUser `json:"user"`
|
||||
}
|
||||
|
||||
type cachedUser struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func newCachedSession(session domain.Session) cachedSession {
|
||||
return cachedSession{
|
||||
ExpiresAt: session.ExpiresAt,
|
||||
User: cachedUser{
|
||||
ID: session.User.ID,
|
||||
Username: session.User.Username,
|
||||
DisplayName: session.User.DisplayName,
|
||||
CreatedAt: session.User.CreatedAt,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c cachedSession) toDomain() domain.Session {
|
||||
return domain.Session{
|
||||
ExpiresAt: c.ExpiresAt,
|
||||
User: domain.User{
|
||||
ID: c.User.ID,
|
||||
Username: c.User.Username,
|
||||
DisplayName: c.User.DisplayName,
|
||||
CreatedAt: c.User.CreatedAt,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *SessionRepository) Resolve(ctx context.Context, tokenHash []byte) (domain.Session, error) {
|
||||
key := sessionKey(tokenHash)
|
||||
|
||||
if cached, err := r.cache.Get(ctx, key); err == nil {
|
||||
var session cachedSession
|
||||
if unmarshalErr := json.Unmarshal(cached, &session); unmarshalErr == nil {
|
||||
if session.User.ID != uuid.Nil && time.Now().Before(session.ExpiresAt) {
|
||||
return session.toDomain(), nil
|
||||
}
|
||||
_ = r.cache.Delete(ctx, key)
|
||||
}
|
||||
}
|
||||
|
||||
session, err := r.source.Resolve(ctx, tokenHash)
|
||||
if err != nil {
|
||||
return domain.Session{}, err
|
||||
}
|
||||
|
||||
if ttl := min(time.Until(session.ExpiresAt), r.ttlCap); ttl > 0 {
|
||||
if encoded, marshalErr := json.Marshal(newCachedSession(session)); marshalErr == nil {
|
||||
if err := r.cache.Set(ctx, key, encoded, ttl); err != nil && r.log != nil {
|
||||
r.log.WarnContext(ctx, "session cache write failed", slog.Any("error", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (r *SessionRepository) Revoke(ctx context.Context, userID uuid.UUID, tokenHash []byte) error {
|
||||
key := sessionKey(tokenHash)
|
||||
|
||||
if err := r.cache.Delete(ctx, key); err != nil && r.log != nil {
|
||||
r.log.WarnContext(ctx, "session cache eviction failed", slog.Any("error", err))
|
||||
}
|
||||
|
||||
if err := r.source.Revoke(ctx, userID, tokenHash); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func sessionKey(tokenHash []byte) string {
|
||||
return "session:" + hex.EncodeToString(tokenHash)
|
||||
}
|
||||
Reference in New Issue
Block a user