Skip to content

Commit 245fa4d

Browse files
committed
chore: cancel lockdown in testing
1 parent 36c2004 commit 245fa4d

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

internal/service/auth_service.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package service
22

33
import (
4+
"context"
45
"database/sql"
56
"errors"
67
"fmt"
@@ -78,6 +79,8 @@ type AuthService struct {
7879
queries *repository.Queries
7980
oauthBroker *OAuthBrokerService
8081
lockdown *Lockdown
82+
lockdownCtx context.Context
83+
lockdownCancelFunc context.CancelFunc
8184
}
8285

8386
func NewAuthService(config AuthServiceConfig, docker *DockerService, ldap *LdapService, queries *repository.Queries, oauthBroker *OAuthBrokerService) *AuthService {
@@ -770,6 +773,11 @@ func (auth *AuthService) ensureOAuthSessionLimit() {
770773
}
771774

772775
func (auth *AuthService) lockdownMode() {
776+
ctx, cancel := context.WithCancel(context.Background())
777+
defer cancel()
778+
auth.lockdownCtx = ctx
779+
auth.lockdownCancelFunc = cancel
780+
773781
auth.loginMutex.Lock()
774782

775783
tlog.App.Warn().Msg("Multiple login attempts detected, possibly DDOS attack. Activating temporary lockdown.")
@@ -788,7 +796,12 @@ func (auth *AuthService) lockdownMode() {
788796

789797
auth.loginMutex.Unlock()
790798

791-
<-timer.C
799+
select {
800+
case <-timer.C:
801+
// Timer expired, end lockdown
802+
case <-ctx.Done():
803+
// Context cancelled, end lockdown
804+
}
792805

793806
auth.loginMutex.Lock()
794807

@@ -801,6 +814,8 @@ func (auth *AuthService) lockdownMode() {
801814
func (auth *AuthService) ClearRateLimitsTestingOnly() {
802815
auth.loginMutex.Lock()
803816
auth.loginAttempts = make(map[string]*LoginAttempt)
804-
auth.lockdown = nil
817+
if auth.lockdown != nil {
818+
auth.lockdownCancelFunc()
819+
}
805820
auth.loginMutex.Unlock()
806821
}

0 commit comments

Comments
 (0)