mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 08:23:45 +00:00
feat: wire durable session authentication into API
This commit is contained in:
+13
-2
@@ -30,8 +30,13 @@ type QueueBackend interface {
|
||||
Get(context.Context, string, string, time.Time) (domain.QueueTicket, error)
|
||||
}
|
||||
|
||||
type SessionBackend interface {
|
||||
Authenticate(context.Context, string, string, time.Time) (domain.Session, error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
Sessions *domain.SessionStore
|
||||
SessionBackend SessionBackend
|
||||
Queue *domain.Queue
|
||||
Candidate CandidateProvider
|
||||
CandidateV2 CandidateProviderV2
|
||||
@@ -330,7 +335,7 @@ func (s *Service) probe(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (s *Service) authenticate(w http.ResponseWriter, r *http.Request) (string, bool) {
|
||||
if s.Sessions == nil {
|
||||
if s.Sessions == nil && s.SessionBackend == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "auth_unavailable")
|
||||
return "", false
|
||||
}
|
||||
@@ -344,7 +349,13 @@ func (s *Service) authenticate(w http.ResponseWriter, r *http.Request) (string,
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized")
|
||||
return "", false
|
||||
}
|
||||
session, err := s.Sessions.Authenticate(parts[1][:separator], parts[1][separator+1:], s.now())
|
||||
var session domain.Session
|
||||
var err error
|
||||
if s.SessionBackend != nil {
|
||||
session, err = s.SessionBackend.Authenticate(r.Context(), parts[1][:separator], parts[1][separator+1:], s.now())
|
||||
} else {
|
||||
session, err = s.Sessions.Authenticate(parts[1][:separator], parts[1][separator+1:], s.now())
|
||||
}
|
||||
if err != nil {
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized")
|
||||
return "", false
|
||||
|
||||
@@ -14,6 +14,13 @@ import (
|
||||
|
||||
type queueBackendSpy struct{ createCalls, heartbeatCalls, cancelCalls, getCalls int }
|
||||
|
||||
type sessionBackendSpy struct{ calls int }
|
||||
|
||||
func (s *sessionBackendSpy) Authenticate(_ context.Context, sessionID, _ string, _ time.Time) (domain.Session, error) {
|
||||
s.calls++
|
||||
return domain.Session{SessionID: sessionID, PlayerID: "player-1"}, nil
|
||||
}
|
||||
|
||||
func (b *queueBackendSpy) Create(_ context.Context, playerID, ticketID, _ string, spec domain.QueueSpec, now time.Time) (domain.QueueTicket, error) {
|
||||
b.createCalls++
|
||||
return domain.QueueTicket{TicketID: ticketID, PlayerID: playerID, Playlist: spec.Playlist, State: domain.Queued, EnqueuedAt: now, ExpiresAt: now.Add(domain.QueueExpiryWindow)}, nil
|
||||
@@ -269,6 +276,24 @@ func TestQueueAPIDelegatesAllMutationsAndRecoveryToBackend(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueueAPIUsesInjectedSessionBackend(t *testing.T) {
|
||||
backend := &sessionBackendSpy{}
|
||||
queue := &queueBackendSpy{}
|
||||
service := &Service{SessionBackend: backend, QueueBackend: queue, Now: func() time.Time { return time.Unix(1000, 0).UTC() }}
|
||||
server := httptest.NewServer(service.Handler())
|
||||
defer server.Close()
|
||||
req, _ := http.NewRequest(http.MethodGet, server.URL+"/v1/queue/ticket-1", nil)
|
||||
req.Header.Set("Authorization", "Bearer durable-session:durable-token")
|
||||
response, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode != http.StatusOK || backend.calls != 1 || queue.getCalls != 1 {
|
||||
t.Fatalf("status=%d session_calls=%d queue_calls=%d", response.StatusCode, backend.calls, queue.getCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueueRecoveryAPIIsAuthenticatedOwnerOnlyAndExpiresStaleTickets(t *testing.T) {
|
||||
now := time.Unix(1000, 0).UTC()
|
||||
sessions := domain.NewSessionStore()
|
||||
|
||||
Reference in New Issue
Block a user