@@ -39,22 +39,7 @@ func (l *Lock) Init(ab *authboss.Authboss) error {
3939
4040// BeforeAuth ensures the account is not locked.
4141func (l * Lock ) BeforeAuth (w http.ResponseWriter , r * http.Request , handled bool ) (bool , error ) {
42- user , err := l .Authboss .CurrentUser (r )
43- if err != nil {
44- return false , err
45- }
46-
47- lu := authboss .MustBeLockable (user )
48- if ! IsLocked (lu ) {
49- return false , nil
50- }
51-
52- ro := authboss.RedirectOptions {
53- Code : http .StatusTemporaryRedirect ,
54- Failure : "Your account is locked. Please contact the administrator." ,
55- RedirectPath : l .Authboss .Config .Paths .LockNotOK ,
56- }
57- return true , l .Authboss .Config .Core .Redirector .Redirect (w , r , ro )
42+ return l .updateLockedState (w , r , true )
5843}
5944
6045// AfterAuthSuccess resets the attempt number field.
@@ -74,35 +59,41 @@ func (l *Lock) AfterAuthSuccess(w http.ResponseWriter, r *http.Request, handled
7459// AfterAuthFail adjusts the attempt number and time negatively
7560// and locks the user if they're beyond limits.
7661func (l * Lock ) AfterAuthFail (w http.ResponseWriter , r * http.Request , handled bool ) (bool , error ) {
62+ return l .updateLockedState (w , r , false )
63+ }
64+
65+ // updateLockedState exists to minimize any differences between a success and
66+ // a failure path in the case where a correct/incorrect password is entered
67+ func (l * Lock ) updateLockedState (w http.ResponseWriter , r * http.Request , wasCorrectPassword bool ) (bool , error ) {
7768 user , err := l .Authboss .CurrentUser (r )
7869 if err != nil {
7970 return false , err
8071 }
8172
73+ // Fetch things
8274 lu := authboss .MustBeLockable (user )
8375 last := lu .GetLastAttempt ()
8476 attempts := lu .GetAttemptCount ()
8577 attempts ++
8678
87- nowLocked := false
79+ if ! wasCorrectPassword {
80+ if time .Now ().UTC ().Sub (last ) <= l .Modules .LockWindow {
81+ if attempts >= l .Modules .LockAfter {
82+ lu .PutLocked (time .Now ().UTC ().Add (l .Modules .LockDuration ))
83+ }
8884
89- if time .Now ().UTC ().Sub (last ) <= l .Modules .LockWindow {
90- if attempts >= l .Modules .LockAfter {
91- lu .PutLocked (time .Now ().UTC ().Add (l .Modules .LockDuration ))
92- nowLocked = true
85+ lu .PutAttemptCount (attempts )
86+ } else {
87+ lu .PutAttemptCount (1 )
9388 }
94-
95- lu .PutAttemptCount (attempts )
96- } else {
97- lu .PutAttemptCount (1 )
9889 }
9990 lu .PutLastAttempt (time .Now ().UTC ())
10091
10192 if err := l .Authboss .Config .Storage .Server .Save (r .Context (), lu ); err != nil {
10293 return false , err
10394 }
10495
105- if ! nowLocked {
96+ if ! IsLocked ( lu ) {
10697 return false , nil
10798 }
10899
0 commit comments