Commit ad5cf81d authored by ale's avatar ale
Browse files

Compact list of login log entries on authentication

Keeps the number of login entries somewhat constant.
parent ada23165
Pipeline #434 passed with stages
in 1 minute and 21 seconds
......@@ -8,6 +8,7 @@ import (
"fmt"
mrand "math/rand"
"net"
"sort"
"time"
"github.com/amoghe/go-crypt"
......@@ -229,3 +230,29 @@ func (u *User) SendNewDeviceLoginEmail(mailer Mailer, publicURL string, dev *Dev
"Device": dev,
})
}
const (
loginLogCountLowWatermark = 10
loginLogCountHighWatermark = 30
)
// CompactLoginLog keeps the number of the UserLogTypeLogin log entries from
// growing too large, dropping old entries if necessary. It is run synchronously
// with the login process, but it should probably be asynchronous.
func (u *User) CompactLoginLog(txn Txn) error {
entries, err := txn.GetUserLogEntries(u.Name, UserLogTypeLogin, 0)
if err != nil {
return err
}
sort.Sort(UserLogEntryList(entries))
// Cut the list of login logs if it contains more than
// highWatermark entries.
if len(entries) > loginLogCountHighWatermark {
for i := 0; i < len(entries)-loginLogCountLowWatermark; i++ {
// Don't really care about errors here.
txn.DeleteUserLogEntry(entries[i])
}
}
return nil
}
......@@ -315,8 +315,14 @@ func (s *Service) successfulLogin(w http.ResponseWriter, r *http.Request, userna
if err != nil {
return err
}
if err := user.CompactLoginLog(txn); err != nil {
return err
}
s.AddUserLogEntryWithDevice(r, txn, user, dev, idp.UserLogTypeLogin, "successful login")
user.LastLoginAt = time.Now().UTC()
if err := txn.UpdateUser(user); err != nil {
return err
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment