forked from mtusi2026/mtusi2026lab2
40 lines
1014 B
Go
40 lines
1014 B
Go
package service
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func encodeCursor(scope string, createdAt time.Time, id string) string {
|
|
raw := scope + "|" + createdAt.UTC().Format(time.RFC3339Nano) + "|" + id
|
|
return base64.RawURLEncoding.EncodeToString([]byte(raw))
|
|
}
|
|
|
|
func decodeCursor(cursor, expectedScope string) (time.Time, string, error) {
|
|
raw, err := base64.RawURLEncoding.DecodeString(cursor)
|
|
if err != nil {
|
|
return time.Time{}, "", ErrInvalidInput
|
|
}
|
|
parts := strings.SplitN(string(raw), "|", 3)
|
|
if len(parts) != 3 {
|
|
return time.Time{}, "", ErrInvalidInput
|
|
}
|
|
if _, err := uuid.Parse(parts[0]); err != nil {
|
|
return time.Time{}, "", ErrInvalidInput
|
|
}
|
|
if parts[0] != expectedScope {
|
|
return time.Time{}, "", ErrInvalidInput
|
|
}
|
|
t, err := time.Parse(time.RFC3339Nano, parts[1])
|
|
if err != nil {
|
|
return time.Time{}, "", ErrInvalidInput
|
|
}
|
|
if _, err := uuid.Parse(parts[2]); err != nil {
|
|
return time.Time{}, "", ErrInvalidInput
|
|
}
|
|
return t, parts[2], nil
|
|
}
|