128 lines
2.7 KiB
Go
128 lines
2.7 KiB
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"encoding/json/v2"
|
|
"fmt"
|
|
"highload/internal/model"
|
|
"highload/internal/repository"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type SessionRepository struct {
|
|
client *redis.Client
|
|
}
|
|
|
|
func NewSessionRepository(client *redis.Client) *SessionRepository {
|
|
return &SessionRepository{client: client}
|
|
}
|
|
|
|
type sessionData struct {
|
|
UserID string `json:"user_id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
ExpiresAt time.Time `json:"expires_at"`
|
|
}
|
|
|
|
func (r *SessionRepository) Create(ctx context.Context, tokenHash []byte, userID string, sessionTTL time.Duration) (*model.Session, error) {
|
|
key := "session:" + hex.EncodeToString(tokenHash)
|
|
now := time.Now().UTC()
|
|
data := sessionData{
|
|
UserID: userID,
|
|
CreatedAt: now,
|
|
ExpiresAt: now.Add(sessionTTL),
|
|
}
|
|
value, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: encoding session %v", repository.ErrDatabase, err)
|
|
|
|
}
|
|
|
|
err = r.client.Set(ctx, key, value, sessionTTL).Err()
|
|
|
|
if err != nil {
|
|
return nil, convertDBError(err)
|
|
}
|
|
|
|
session := &model.Session{
|
|
TokenHash: tokenHash,
|
|
UserID: data.UserID,
|
|
CreatedAt: data.CreatedAt,
|
|
ExpiresAt: data.ExpiresAt,
|
|
RevokedAt: nil,
|
|
}
|
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
func (r *SessionRepository) GetSessionByTokenHash(ctx context.Context, tokenHash []byte) (*model.Session, error) {
|
|
key := "session:" + hex.EncodeToString(tokenHash)
|
|
|
|
value, err := r.client.Get(ctx, key).Result()
|
|
if err != nil {
|
|
return nil, convertDBError(err)
|
|
}
|
|
|
|
var data sessionData
|
|
|
|
err = json.Unmarshal([]byte(value), &data)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: decode session %v", repository.ErrDatabase, err)
|
|
|
|
}
|
|
|
|
session := &model.Session{
|
|
TokenHash: tokenHash,
|
|
UserID: data.UserID,
|
|
CreatedAt: data.CreatedAt,
|
|
ExpiresAt: data.ExpiresAt,
|
|
RevokedAt: nil,
|
|
}
|
|
|
|
return session, nil
|
|
}
|
|
func (r *SessionRepository) RevokeSession(
|
|
ctx context.Context,
|
|
tokenHash []byte,
|
|
) (int, error) {
|
|
key := "session:" + hex.EncodeToString(tokenHash)
|
|
|
|
conn := r.client.Conn()
|
|
defer conn.Close()
|
|
|
|
deleted, err := conn.Del(ctx, key).Result()
|
|
if err != nil {
|
|
return 0, convertDBError(err)
|
|
|
|
}
|
|
|
|
result, err := conn.Do(ctx, "WAITAOF", 1, 0, 2000).Slice()
|
|
if err != nil {
|
|
return 0, convertDBError(err)
|
|
|
|
}
|
|
|
|
if len(result) != 2 {
|
|
return 0, fmt.Errorf(
|
|
"%w: WAITAOF: expected 2 counters, got %d",
|
|
repository.ErrDatabase, len(result),
|
|
)
|
|
}
|
|
|
|
local, ok := result[0].(int64)
|
|
|
|
if !ok {
|
|
return 0, fmt.Errorf("%w: expected int64, got %T", repository.ErrDatabase, result[0])
|
|
}
|
|
if local != 1 {
|
|
return 0, fmt.Errorf("%w: WAITAOF expected local fsync = 1, got %d", repository.ErrServiceUnavailable, local)
|
|
}
|
|
|
|
return int(deleted), nil
|
|
}
|