/
util.go
59 lines (45 loc) · 1.98 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package handlers
import (
"fmt"
"github.com/authelia/authelia/v4/internal/authentication"
"github.com/authelia/authelia/v4/internal/middlewares"
"github.com/authelia/authelia/v4/internal/templates"
)
const (
eventLogKeyAction = "Action"
eventLogKeyCategory = "Category"
eventLogKeyDescription = "Description"
eventLogAction2FAAdded = "Second Factor Method Added"
eventLogAction2FARemoved = "Second Factor Method Removed"
eventLogCategoryOneTimePassword = "One-Time Password"
eventLogCategoryWebAuthnCredential = "WebAuthn Credential" //nolint:gosec
)
func ctxLogEvent(ctx *middlewares.AutheliaCtx, username, description string, eventDetails map[string]any) {
var (
details *authentication.UserDetails
err error
)
ctx.Logger.Debugf("Getting user details for notification")
// Send Notification.
if details, err = ctx.Providers.UserProvider.GetDetails(username); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred looking up user details for user '%s' while attempting to alert them of an important event", username)
return
}
if len(details.Emails) == 0 {
ctx.Logger.WithError(fmt.Errorf("no email address was found for user")).Errorf("Error occurred looking up user details for user '%s' while attempting to alert them of an important event", username)
return
}
data := templates.EmailEventValues{
Title: description,
DisplayName: details.DisplayName,
RemoteIP: ctx.RemoteIP().String(),
Details: eventDetails,
}
ctx.Logger.Debugf("Getting user addresses for notification")
addresses := details.Addresses()
ctx.Logger.Debugf("Sending an email to user %s (%s) to inform them of an important event.", username, addresses[0].String())
if err = ctx.Providers.Notifier.Send(ctx, addresses[0], description, ctx.Providers.Templates.GetEventEmailTemplate(), data); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred sending notification to user '%s' while attempting to alert them of an important event", username)
return
}
}