Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
fix(indieauth): consume auth requests once
Gabe Kangasa8a29589bc0a
diff --git a/auth/indieauth/client.go b/auth/indieauth/client.goindex f21c630f50..c77efba44c 100644--- a/auth/indieauth/client.go+++ b/auth/indieauth/client.go@@ -8,6 +8,7 @@ import ("net/url""strconv""strings"+ "time""github.com/pkg/errors"log "github.com/sirupsen/logrus"@@ -53,10 +54,13 @@ func (s *Service) StartAuthFlow(authHost, userID, accessToken, displayName strin// HandleCallbackCode will handle the callback from the IndieAuth server// to continue the next step of the auth flow.func (s *Service) HandleCallbackCode(code, state string) (*Request, *Response, error) {- request, exists := s.getPendingAuthRequest(state)+ request, exists := s.takePendingAuthRequest(state)if !exists {return nil, nil, errors.New("no auth requests pending")}+ if time.Since(request.Timestamp) > registrationTimeout {+ return nil, nil, errors.New("auth request has expired")+ }data := url.Values{}data.Set("grant_type", "authorization_code")@@ -143,9 +147,12 @@ func (s *Service) storePendingAuthRequest(request *Request) error {return nil}-func (s *Service) getPendingAuthRequest(state string) (*Request, bool) {+func (s *Service) takePendingAuthRequest(state string) (*Request, bool) {s.pendingAuthRequestsLock.Lock()defer s.pendingAuthRequestsLock.Unlock()request, exists := s.pendingAuthRequests[state]+ if exists {+ delete(s.pendingAuthRequests, state)+ }return request, exists}diff --git a/auth/indieauth/indieauth_test.go b/auth/indieauth/indieauth_test.goindex 328d0286d3..f74dd91b4a 100644--- a/auth/indieauth/indieauth_test.go+++ b/auth/indieauth/indieauth_test.go@@ -17,7 +17,7 @@ func TestPendingClientRequestsAreSynchronized(t *testing.T) {for i := range maxPendingRequests {state := fmt.Sprintf("state-goroutine-%d", i)_ = svc.storePendingAuthRequest(&Request{State: state})- _, _ = svc.getPendingAuthRequest(state)+ _, _ = svc.takePendingAuthRequest(state)}close(done)}()@@ -25,7 +25,7 @@ func TestPendingClientRequestsAreSynchronized(t *testing.T) {for i := range maxPendingRequests {state := fmt.Sprintf("state-main-%d", i)_ = svc.storePendingAuthRequest(&Request{State: state})- _, _ = svc.getPendingAuthRequest(state)+ _, _ = svc.takePendingAuthRequest(state)}<-done}diff --git a/auth/indieauth/lifecycle_test.go b/auth/indieauth/lifecycle_test.gonew file mode 100644index 0000000000..e3d2ab727d--- /dev/null+++ b/auth/indieauth/lifecycle_test.go@@ -0,0 +1,58 @@+package indieauth++import (+ "testing"+ "time"++ "github.com/owncast/owncast/persistence/configrepository"+ "github.com/owncast/owncast/services/datastore"+)++func newLifecycleTestService(t *testing.T) *Service {+ t.Helper()+ ds, err := datastore.SetupPersistence(":memory:", t.TempDir())+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = ds.DB.Close() })+ config := configrepository.New(ds)+ if err := config.SetServerURL("https://owncast.example"); err != nil {+ t.Fatal(err)+ }+ return New(Deps{ConfigRepository: config})+}++func TestServerAuthCodeIsSingleUse(t *testing.T) {+ svc := newLifecycleTestService(t)+ request, err := svc.StartServerAuth("https://client.example", "https://client.example/callback", createCodeChallenge("verifier"), "state", "me")+ if err != nil {+ t.Fatal(err)+ }+ if _, err := svc.CompleteServerAuth(request.Code, request.RedirectURI, request.ClientID, "verifier"); err != nil {+ t.Fatalf("first completion: %v", err)+ }+ if _, err := svc.CompleteServerAuth(request.Code, request.RedirectURI, request.ClientID, "verifier"); err == nil {+ t.Fatal("replayed authorization code was accepted")+ }+}++func TestExpiredAuthRequestsAreRejectedAndPruned(t *testing.T) {+ svc := newLifecycleTestService(t)+ expired := time.Now().Add(-registrationTimeout - time.Second)+ svc.pendingAuthRequests["client"] = &Request{State: "client", Timestamp: expired}+ svc.pendingServerAuthRequests["server"] = ServerAuthRequest{Code: "server", Timestamp: expired}++ if _, _, err := svc.HandleCallbackCode("code", "client"); err == nil {+ t.Fatal("expired client request was accepted")+ }+ if _, err := svc.CompleteServerAuth("server", "https://client.example/callback", "https://client.example", "verifier"); err == nil {+ t.Fatal("expired server request was accepted")+ }++ svc.pendingAuthRequests["client"] = &Request{State: "client", Timestamp: expired}+ svc.pendingServerAuthRequests["server"] = ServerAuthRequest{Code: "server", Timestamp: expired}+ svc.pruneExpiredRequests(time.Now())+ if len(svc.pendingAuthRequests) != 0 || len(svc.pendingServerAuthRequests) != 0 {+ t.Fatal("expired auth requests were not pruned")+ }+}diff --git a/auth/indieauth/server.go b/auth/indieauth/server.goindex a9f2226f25..6ffa59793a 100644--- a/auth/indieauth/server.go+++ b/auth/indieauth/server.go@@ -129,10 +129,16 @@ func isWebURL(u *url.URL) bool {func (s *Service) CompleteServerAuth(code, redirectURI, clientID string, codeVerifier string) (*ServerProfileResponse, error) {s.pendingServerAuthRequestsLock.Lock()request, pending := s.pendingServerAuthRequests[code]+ if pending {+ delete(s.pendingServerAuthRequests, code)+ }s.pendingServerAuthRequestsLock.Unlock()if !pending {return nil, errors.New("no pending authentication request")}+ if time.Since(request.Timestamp) > registrationTimeout {+ return nil, errors.New("authentication request has expired")+ }if request.RedirectURI != redirectURI {return nil, errors.New("redirect URI does not match")diff --git a/auth/indieauth/service.go b/auth/indieauth/service.goindex 34468dd5f8..20b465ae26 100644--- a/auth/indieauth/service.go+++ b/auth/indieauth/service.go@@ -32,7 +32,7 @@ type Deps struct {}// New constructs a Service. It also starts a background goroutine that-// periodically prunes expired client-side auth requests.+// periodically prunes expired client- and server-side auth requests.func New(deps Deps) *Service {s := &Service{configRepository: deps.ConfigRepository,@@ -43,18 +43,29 @@ func New(deps Deps) *Service {return s}-// runExpiredRequestPruner clears out any pending client-side requests-// that have been waiting longer than registrationTimeout.func (s *Service) runExpiredRequestPruner() {ticker := time.NewTicker(registrationTimeout)- for range ticker.C {- s.pendingAuthRequestsLock.Lock()+ defer ticker.Stop()+ for now := range ticker.C {log.Debugln("Pruning expired IndieAuth requests.")- for k, v := range s.pendingAuthRequests {- if time.Since(v.Timestamp) > registrationTimeout {- delete(s.pendingAuthRequests, k)- }+ s.pruneExpiredRequests(now)+ }+}++func (s *Service) pruneExpiredRequests(now time.Time) {+ s.pendingAuthRequestsLock.Lock()+ for k, v := range s.pendingAuthRequests {+ if now.Sub(v.Timestamp) > registrationTimeout {+ delete(s.pendingAuthRequests, k)+ }+ }+ s.pendingAuthRequestsLock.Unlock()++ s.pendingServerAuthRequestsLock.Lock()+ for k, v := range s.pendingServerAuthRequests {+ if now.Sub(v.Timestamp) > registrationTimeout {+ delete(s.pendingServerAuthRequests, k)}- s.pendingAuthRequestsLock.Unlock()}+ s.pendingServerAuthRequestsLock.Unlock()}