forked from mtusi2026/mtusi2026lab5
122 lines
3.5 KiB
Go
122 lines
3.5 KiB
Go
package application
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"mtusi2026lab5/internal/domain"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const cursorVersion = 1
|
|
|
|
type CursorCodec struct {
|
|
key []byte
|
|
}
|
|
|
|
type cursorPayload struct {
|
|
Version int `json:"v"`
|
|
Kind string `json:"kind"`
|
|
CreatedAt string `json:"created_at"`
|
|
ID string `json:"id"`
|
|
ScopeID string `json:"scope_id"`
|
|
}
|
|
|
|
func NewCursorCodec(key string) (*CursorCodec, error) {
|
|
if len(key) < 32 {
|
|
return nil, fmt.Errorf("cursor signing key must contain at least 32 bytes")
|
|
}
|
|
return &CursorCodec{key: []byte(key)}, nil
|
|
}
|
|
|
|
func (c *CursorCodec) EncodeChat(cursor domain.ChatCursor) (string, error) {
|
|
return c.encode(cursorPayload{
|
|
Version: cursorVersion, Kind: "chat", CreatedAt: cursor.CreatedAt.UTC().Format(time.RFC3339Nano),
|
|
ID: cursor.ChatID, ScopeID: cursor.UserID,
|
|
})
|
|
}
|
|
|
|
func (c *CursorCodec) DecodeChat(token, expectedUserID string) (*domain.ChatCursor, error) {
|
|
payload, err := c.decode(token, "chat", expectedUserID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &domain.ChatCursor{CreatedAt: payload.createdAt, ChatID: payload.id, UserID: payload.scopeID}, nil
|
|
}
|
|
|
|
func (c *CursorCodec) EncodeMessage(cursor domain.MessageCursor) (string, error) {
|
|
return c.encode(cursorPayload{
|
|
Version: cursorVersion, Kind: "message", CreatedAt: cursor.CreatedAt.UTC().Format(time.RFC3339Nano),
|
|
ID: cursor.MessageID, ScopeID: cursor.ChatID,
|
|
})
|
|
}
|
|
|
|
func (c *CursorCodec) DecodeMessage(token, expectedChatID string) (*domain.MessageCursor, error) {
|
|
payload, err := c.decode(token, "message", expectedChatID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &domain.MessageCursor{CreatedAt: payload.createdAt, MessageID: payload.id, ChatID: payload.scopeID}, nil
|
|
}
|
|
|
|
func (c *CursorCodec) encode(payload cursorPayload) (string, error) {
|
|
encodedPayload, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return "", fmt.Errorf("encode cursor payload: %w", err)
|
|
}
|
|
mac := hmac.New(sha256.New, c.key)
|
|
_, _ = mac.Write(encodedPayload)
|
|
return base64.RawURLEncoding.EncodeToString(encodedPayload) + "." +
|
|
base64.RawURLEncoding.EncodeToString(mac.Sum(nil)), nil
|
|
}
|
|
|
|
type decodedCursor struct {
|
|
createdAt time.Time
|
|
id string
|
|
scopeID string
|
|
}
|
|
|
|
func (c *CursorCodec) decode(token, expectedKind, expectedScopeID string) (decodedCursor, error) {
|
|
parts := strings.Split(token, ".")
|
|
if len(parts) != 2 {
|
|
return decodedCursor{}, ErrInvalidCursor
|
|
}
|
|
payloadBytes, err := base64.RawURLEncoding.DecodeString(parts[0])
|
|
if err != nil {
|
|
return decodedCursor{}, ErrInvalidCursor
|
|
}
|
|
signature, err := base64.RawURLEncoding.DecodeString(parts[1])
|
|
if err != nil {
|
|
return decodedCursor{}, ErrInvalidCursor
|
|
}
|
|
mac := hmac.New(sha256.New, c.key)
|
|
_, _ = mac.Write(payloadBytes)
|
|
if !hmac.Equal(signature, mac.Sum(nil)) {
|
|
return decodedCursor{}, ErrInvalidCursor
|
|
}
|
|
|
|
var payload cursorPayload
|
|
if err = json.Unmarshal(payloadBytes, &payload); err != nil ||
|
|
payload.Version != cursorVersion || payload.Kind != expectedKind ||
|
|
payload.ScopeID != expectedScopeID {
|
|
return decodedCursor{}, ErrInvalidCursor
|
|
}
|
|
createdAt, err := time.Parse(time.RFC3339Nano, payload.CreatedAt)
|
|
if err != nil {
|
|
return decodedCursor{}, ErrInvalidCursor
|
|
}
|
|
if _, err = uuid.Parse(payload.ID); err != nil {
|
|
return decodedCursor{}, ErrInvalidCursor
|
|
}
|
|
if _, err = uuid.Parse(payload.ScopeID); err != nil {
|
|
return decodedCursor{}, ErrInvalidCursor
|
|
}
|
|
return decodedCursor{createdAt: createdAt.UTC(), id: payload.ID, scopeID: payload.ScopeID}, nil
|
|
}
|