31 lines
885 B
Go
31 lines
885 B
Go
package service
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// encodeCursor кодирует (scope, created_at, id) в base64.
|
|
func encodeCursor(scope string, createdAt time.Time, id string) string {
|
|
raw := scope + "|" + createdAt.UTC().Format(time.RFC3339Nano) + "|" + id
|
|
return base64.RawURLEncoding.EncodeToString([]byte(raw))
|
|
}
|
|
|
|
// decodeCursor декодирует и проверяет scope.
|
|
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 || parts[0] != expectedScope {
|
|
return time.Time{}, "", ErrInvalidInput
|
|
}
|
|
t, err := time.Parse(time.RFC3339Nano, parts[1])
|
|
if err != nil {
|
|
return time.Time{}, "", ErrInvalidInput
|
|
}
|
|
return t, parts[2], nil
|
|
}
|