8881214213
Mechanical, behaviour-preserving removal of Stage N / TODO-N / phase (RN) references from comments, doc-comments, service READMEs, the current-state docs (ARCHITECTURE, FUNCTIONAL+_ru, TESTING, UI_DESIGN), config-file comments, and the .fbs/.proto schema comments. PLAN.md / PRERELEASE.md / CLAUDE.md keep the stage history. - Rename the only stage-named identifiers: registerStage8 -> registerSocialOps, registerStage11 -> registerLinkOps (gateway transcode). - Split stage6_test.go: TestEmailLoginFlow -> email_test.go, TestGuestAutoMatchLeavesNoStats (+ provisionGuest) -> account_test.go. - Regenerated proto bindings (push.pb.go, telegram_grpc.pb.go) from the de-staged .proto comments; FB Go/TS bindings unchanged (flatc strips schema comments). go build/vet/gofmt clean across modules; integration typecheck and pnpm check green.
85 lines
2.5 KiB
Go
85 lines
2.5 KiB
Go
package session
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// Service mints, resolves, and revokes sessions over the store and the
|
|
// write-through cache. The gateway is its only caller (from a later stage); the
|
|
// HTTP surface is wired then.
|
|
type Service struct {
|
|
store *Store
|
|
cache *Cache
|
|
}
|
|
|
|
// NewService constructs a Service over store and cache.
|
|
func NewService(store *Store, cache *Cache) *Service {
|
|
return &Service{store: store, cache: cache}
|
|
}
|
|
|
|
// Warm hydrates the cache from the store. Call once before serving traffic.
|
|
func (svc *Service) Warm(ctx context.Context) error {
|
|
return svc.cache.Warm(ctx, svc.store)
|
|
}
|
|
|
|
// Ready reports whether the session cache has been warmed.
|
|
func (svc *Service) Ready() bool {
|
|
return svc.cache.Ready()
|
|
}
|
|
|
|
// Create mints a new active session for accountID and returns the plaintext
|
|
// token (shown to the caller once) together with the persisted session.
|
|
func (svc *Service) Create(ctx context.Context, accountID uuid.UUID) (string, Session, error) {
|
|
token, tokenHash, err := GenerateToken()
|
|
if err != nil {
|
|
return "", Session{}, err
|
|
}
|
|
sess, err := svc.store.Insert(ctx, accountID, tokenHash)
|
|
if err != nil {
|
|
return "", Session{}, err
|
|
}
|
|
svc.cache.Add(sess)
|
|
return token, sess, nil
|
|
}
|
|
|
|
// Resolve maps a presented token to its active session, consulting the cache
|
|
// first and falling back to the store (repopulating the cache on a hit).
|
|
// Returns ErrNotFound when no active session matches.
|
|
func (svc *Service) Resolve(ctx context.Context, token string) (Session, error) {
|
|
hash := HashToken(token)
|
|
if sess, ok := svc.cache.Get(hash); ok {
|
|
return sess, nil
|
|
}
|
|
sess, err := svc.store.FindActiveByTokenHash(ctx, hash)
|
|
if err != nil {
|
|
return Session{}, err
|
|
}
|
|
svc.cache.Add(sess)
|
|
return sess, nil
|
|
}
|
|
|
|
// Revoke revokes the session for the presented token. It is idempotent:
|
|
// revoking an unknown or already-revoked token returns nil.
|
|
func (svc *Service) Revoke(ctx context.Context, token string) error {
|
|
hash := HashToken(token)
|
|
if _, _, err := svc.store.RevokeByTokenHash(ctx, hash, time.Now().UTC()); err != nil {
|
|
return err
|
|
}
|
|
svc.cache.Remove(hash)
|
|
return nil
|
|
}
|
|
|
|
// RevokeAllForAccount revokes every active session of accountID and evicts them
|
|
// from the cache. The account-merge flow calls it to retire a secondary account.
|
|
// It is idempotent.
|
|
func (svc *Service) RevokeAllForAccount(ctx context.Context, accountID uuid.UUID) error {
|
|
if _, err := svc.store.RevokeAllForAccount(ctx, accountID, time.Now().UTC()); err != nil {
|
|
return err
|
|
}
|
|
svc.cache.RemoveByAccount(accountID)
|
|
return nil
|
|
}
|