mirror of
https://github.com/mjl-/mox.git
synced 2025-06-28 01:48:15 +03:00

and show them in the account and admin interfaces. this should help with debugging, to find misconfigured clients, and potentially find attackers trying to login. we include details like login name, account name, protocol, authentication mechanism, ip addresses, tls connection properties, user-agent. and of course the result. we group entries by their details. repeat connections don't cause new records in the database, they just increase the count on the existing record. we keep data for at most 30 days. and we keep at most 10k entries per account. to prevent unbounded growth. for successful login attempts, we store them all for 30d. if a bad user causes so many entries this becomes a problem, it will be time to talk to the user... there is no pagination/searching yet in the admin/account interfaces. so the list may be long. we only show the 10 most recent login attempts by default. the rest is only shown on a separate page. there is no way yet to disable this. may come later, either as global setting or per account.
48 lines
1.7 KiB
Go
48 lines
1.7 KiB
Go
package webauth
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/mjl-/mox/mlog"
|
|
"github.com/mjl-/mox/store"
|
|
)
|
|
|
|
// AccountAuth is for user accounts, with username/password, and sessions stored in
|
|
// memory and in the database with lifetimes that are automatically extended.
|
|
var Accounts SessionAuth = accountSessionAuth{}
|
|
|
|
type accountSessionAuth struct{}
|
|
|
|
func (accountSessionAuth) login(ctx context.Context, log mlog.Log, username, password string) (valid, disabled bool, accName string, rerr error) {
|
|
acc, accName, err := store.OpenEmailAuth(log, username, password, true)
|
|
if err != nil && errors.Is(err, store.ErrUnknownCredentials) {
|
|
return false, false, accName, nil
|
|
} else if err != nil && errors.Is(err, store.ErrLoginDisabled) {
|
|
return false, true, accName, err // Returning error, for its message.
|
|
} else if err != nil {
|
|
return false, false, accName, err
|
|
}
|
|
defer func() {
|
|
err := acc.Close()
|
|
log.Check(err, "closing account")
|
|
}()
|
|
return true, false, accName, nil
|
|
}
|
|
|
|
func (accountSessionAuth) add(ctx context.Context, log mlog.Log, accountName string, loginAddress string) (sessionToken store.SessionToken, csrfToken store.CSRFToken, rerr error) {
|
|
return store.SessionAdd(ctx, log, accountName, loginAddress)
|
|
}
|
|
|
|
func (accountSessionAuth) use(ctx context.Context, log mlog.Log, accountName string, sessionToken store.SessionToken, csrfToken store.CSRFToken) (loginAddress string, rerr error) {
|
|
ls, err := store.SessionUse(ctx, log, accountName, sessionToken, csrfToken)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return ls.LoginAddress, nil
|
|
}
|
|
|
|
func (accountSessionAuth) remove(ctx context.Context, log mlog.Log, accountName string, sessionToken store.SessionToken) error {
|
|
return store.SessionRemove(ctx, log, accountName, sessionToken)
|
|
}
|