Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
Limit Fediverse OTP attempts (#5133)
Gabe Kangasdd57a40dfc8c
diff --git a/auth/fediverse/fediverse.go b/auth/fediverse/fediverse.goindex fe158568e2..145fd6b1d8 100644--- a/auth/fediverse/fediverse.go+++ b/auth/fediverse/fediverse.go@@ -18,11 +18,13 @@ type OTPRegistration struct {UserDisplayName stringCode stringAccount string+ FailedAttempts int}const (registrationTimeout = time.Minute * 10maxPendingRequests = 1000+ maxOTPAttempts = 5)// Service bundles the per-instance state for the Fediverse OTP flow.@@ -102,13 +104,24 @@ func (s *Service) RegisterFediverseOTP(accessToken, userID, userDisplayName, accreturn r, true, nil}-// ValidateFediverseOTP will verify a OTP code for a auth request.+// ValidateFediverseOTP will verify an OTP code for an auth request.func (s *Service) ValidateFediverseOTP(accessToken, code string) (bool, *OTPRegistration) {s.lock.Lock()defer s.lock.Unlock()request, ok := s.pendingAuthRequests[accessToken]- if !ok || request.Code != code || time.Since(request.Timestamp) > registrationTimeout {+ if !ok || time.Since(request.Timestamp) > registrationTimeout {+ delete(s.pendingAuthRequests, accessToken)+ return false, nil+ }++ if request.Code != code {+ request.FailedAttempts+++ if request.FailedAttempts >= maxOTPAttempts {+ delete(s.pendingAuthRequests, accessToken)+ } else {+ s.pendingAuthRequests[accessToken] = request+ }return false, nil}diff --git a/auth/fediverse/fediverse_test.go b/auth/fediverse/fediverse_test.goindex 215ea767d8..76b4d041e8 100644--- a/auth/fediverse/fediverse_test.go+++ b/auth/fediverse/fediverse_test.go@@ -55,6 +55,49 @@ func TestOTPFlowValidation(t *testing.T) {}}+func TestOTPFlowLimitsFailedAttempts(t *testing.T) {+ svc := New()+ token := "attempt-limit-token"+ registration, _, err := svc.RegisterFediverseOTP(token, userID, userDisplayName, account)+ if err != nil {+ t.Fatal(err)+ }++ for attempt := 1; attempt < maxOTPAttempts; attempt++ {+ if valid, _ := svc.ValidateFediverseOTP(token, "wrong-code"); valid {+ t.Fatalf("wrong code succeeded on attempt %d", attempt)+ }+ pending, ok := svc.pendingAuthRequests[token]+ if !ok {+ t.Fatalf("request was removed before attempt limit on attempt %d", attempt)+ }+ if pending.FailedAttempts != attempt {+ t.Fatalf("failed attempts = %d, want %d", pending.FailedAttempts, attempt)+ }+ }++ if valid, _ := svc.ValidateFediverseOTP(token, registration.Code); !valid {+ t.Fatal("correct code should succeed before the attempt limit")+ }++ exhaustedToken := "exhausted-attempt-token"+ exhausted, _, err := svc.RegisterFediverseOTP(exhaustedToken, userID, userDisplayName, account)+ if err != nil {+ t.Fatal(err)+ }+ for attempt := 1; attempt <= maxOTPAttempts; attempt++ {+ if valid, _ := svc.ValidateFediverseOTP(exhaustedToken, "wrong-code"); valid {+ t.Fatalf("wrong code succeeded on attempt %d", attempt)+ }+ }+ if _, ok := svc.pendingAuthRequests[exhaustedToken]; ok {+ t.Fatal("request remained after the attempt limit")+ }+ if valid, _ := svc.ValidateFediverseOTP(exhaustedToken, exhausted.Code); valid {+ t.Fatal("correct code succeeded after the attempt limit")+ }+}+func TestSingleOTPFlowRequest(t *testing.T) {svc := New()r1, _, _ := svc.RegisterFediverseOTP(accessToken, userID, userDisplayName, account)