Skip to content

Commit 81311b2

Browse files
committed
Set login rate limiting before comparing credentials, added log entry for successful login #359
1 parent 72449d8 commit 81311b2

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

internal/logging/Logging.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ func LogInvalidLogin(username, ip string) {
270270
createLogEntry(categoryAuth, fmt.Sprintf("Invalid login for user %s by IP %s", username, ip), false)
271271
}
272272

273+
// LogValidLogin adds a log entry to indicate that a login was successful. Non-blocking
274+
func LogValidLogin(username string) {
275+
createLogEntry(categoryAuth, fmt.Sprintf("%s logged in sucessfully", username), false)
276+
}
277+
273278
// LogDownload adds a log entry when a download was requested. Non-Blocking
274279
func LogDownload(file models.File, r *http.Request, saveIp bool) {
275280
if saveIp {

internal/webserver/Webserver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,15 +511,16 @@ func showLogin(w http.ResponseWriter, r *http.Request) {
511511
pw := r.Form.Get("password")
512512
failedLogin := false
513513
if pw != "" && user != "" {
514+
ip := logging.GetIpAddress(r)
515+
ratelimiter.WaitOnLogin(ip)
514516
retrievedUser, validCredentials := authentication.IsCorrectUsernameAndPassword(user, pw)
515517
if validCredentials {
518+
logging.LogValidLogin(user)
516519
sessionmanager.CreateSession(w, false, 0, retrievedUser.Id)
517520
redirect(w, "admin")
518521
return
519522
}
520-
ip := logging.GetIpAddress(r)
521523
logging.LogInvalidLogin(user, ip)
522-
ratelimiter.WaitOnFailedLogin(ip)
523524
failedLogin = true
524525
}
525526
err = templateFolder.ExecuteTemplate(w, "login", LoginView{

internal/webserver/ratelimiter/RateLimiter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ func newLimiter() *store {
3131
}
3232
}
3333

34-
// WaitOnFailedLogin blocks the current goroutine until the rate limiter allows a request
35-
// Two failed attempts without limiting, thereafter one attempt every 3 seconds
36-
func WaitOnFailedLogin(ip string) {
37-
_ = failedLoginLimiter.Get(ip, 1, 6).WaitN(context.Background(), 3)
34+
// WaitOnLogin blocks the current goroutine until the rate limiter allows a request
35+
// Three attempts without limiting, thereafter one attempt every 3 seconds
36+
func WaitOnLogin(ip string) {
37+
_ = failedLoginLimiter.Get(ip, 1, 9).WaitN(context.Background(), 3)
3838
}
3939

4040
// WaitOnFailedId blocks the current goroutine until the rate limiter allows a request

0 commit comments

Comments
 (0)