Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

fix(indieauth): synchronize pending client requests

Gabe Kangas5fb7371072fa
diff --git a/auth/indieauth/client.go b/auth/indieauth/client.go
index e151fe1e67..f21c630f50 100644
--- a/auth/indieauth/client.go
+++ b/auth/indieauth/client.go
@@ -17,11 +17,6 @@ import (
// StartAuthFlow will begin the IndieAuth flow by generating an auth request.
func (s *Service) StartAuthFlow(authHost, userID, accessToken, displayName string) (*url.URL, error) {
- // Limit the number of pending requests
- if len(s.pendingAuthRequests) >= maxPendingRequests {
- return nil, errors.New("Please try again later. Too many pending requests.")
- }
-
// Sanity check the server URL.
u, err := url.ParseRequestURI(authHost)
if err != nil {
@@ -48,7 +43,9 @@ func (s *Service) StartAuthFlow(authHost, userID, accessToken, displayName strin
return nil, errors.Wrap(err, "unable to generate IndieAuth request")
}
- s.pendingAuthRequests[r.State] = r
+ if err := s.storePendingAuthRequest(r); err != nil {
+ return nil, err
+ }
return r.Redirect, nil
}
@@ -56,7 +53,7 @@ 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.pendingAuthRequests[state]
+ request, exists := s.getPendingAuthRequest(state)
if !exists {
return nil, nil, errors.New("no auth requests pending")
}
@@ -134,3 +131,21 @@ func makeIndieAuthClientErrorText(err string) string {
return err
}
}
+
+func (s *Service) storePendingAuthRequest(request *Request) error {
+ s.pendingAuthRequestsLock.Lock()
+ defer s.pendingAuthRequestsLock.Unlock()
+
+ if len(s.pendingAuthRequests) >= maxPendingRequests {
+ return errors.New("Please try again later. Too many pending requests.")
+ }
+ s.pendingAuthRequests[request.State] = request
+ return nil
+}
+
+func (s *Service) getPendingAuthRequest(state string) (*Request, bool) {
+ s.pendingAuthRequestsLock.Lock()
+ defer s.pendingAuthRequestsLock.Unlock()
+ request, exists := s.pendingAuthRequests[state]
+ return request, exists
+}
diff --git a/auth/indieauth/indieauth_test.go b/auth/indieauth/indieauth_test.go
index 02b99424f6..328d0286d3 100644
--- a/auth/indieauth/indieauth_test.go
+++ b/auth/indieauth/indieauth_test.go
@@ -1,11 +1,35 @@
package indieauth
import (
+ "fmt"
"testing"
"github.com/owncast/owncast/utils"
)
+func TestPendingClientRequestsAreSynchronized(t *testing.T) {
+ svc := &Service{
+ pendingAuthRequests: make(map[string]*Request),
+ }
+
+ done := make(chan struct{})
+ go func() {
+ for i := range maxPendingRequests {
+ state := fmt.Sprintf("state-goroutine-%d", i)
+ _ = svc.storePendingAuthRequest(&Request{State: state})
+ _, _ = svc.getPendingAuthRequest(state)
+ }
+ close(done)
+ }()
+
+ for i := range maxPendingRequests {
+ state := fmt.Sprintf("state-main-%d", i)
+ _ = svc.storePendingAuthRequest(&Request{State: state})
+ _, _ = svc.getPendingAuthRequest(state)
+ }
+ <-done
+}
+
func TestLimitGlobalPendingRequests(t *testing.T) {
// Construct an isolated Service for this test. CompleteServerAuth is
// not called here, so the ConfigRepository can be nil. Each Service owns