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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
package handlers
import (
"errors"
"net/http"
"github.com/authelia/authelia/v4/internal/authentication"
"github.com/authelia/authelia/v4/internal/middlewares"
"github.com/authelia/authelia/v4/internal/session"
"github.com/authelia/authelia/v4/internal/templates"
)
func ChangePasswordPOST(ctx *middlewares.AutheliaCtx) {
var (
userSession session.UserSession
provider *session.Session
err error
)
if provider, err = ctx.GetSessionProvider(); err != nil {
ctx.Logger.WithError(err).
Error("Unable to change password for user: error occurred retrieving session provider")
ctx.SetJSONError(messageUnableToChangePassword)
ctx.SetStatusCode(http.StatusInternalServerError)
return
}
if userSession, err = provider.GetSession(ctx.RequestCtx); err != nil {
ctx.Logger.WithError(err).
Error("Unable to change password for user: error occurred retrieving session for user")
ctx.SetJSONError(messageUnableToChangePassword)
ctx.SetStatusCode(http.StatusInternalServerError)
return
}
username := userSession.Username
var requestBody changePasswordRequestBody
if err = ctx.ParseBody(&requestBody); err != nil {
ctx.Logger.WithError(err).
WithFields(map[string]any{"username": username}).
Error("Unable to change password for user: unable to parse request body")
ctx.SetJSONError(messageUnableToChangePassword)
ctx.SetStatusCode(http.StatusBadRequest)
return
}
if err = ctx.Providers.PasswordPolicy.Check(requestBody.NewPassword); err != nil {
ctx.Logger.WithError(err).
WithFields(map[string]any{"username": username}).
Debug("Unable to change password for user as their new password was weak or empty")
ctx.SetJSONError(messagePasswordWeak)
ctx.SetStatusCode(http.StatusBadRequest)
return
}
if err = ctx.Providers.UserProvider.ChangePassword(username, requestBody.OldPassword, requestBody.NewPassword); err != nil {
switch {
case errors.Is(err, authentication.ErrIncorrectPassword):
ctx.Logger.WithError(err).
WithFields(map[string]any{"username": username}).
Debug("Unable to change password for user as their old password was incorrect")
ctx.SetJSONError(messageIncorrectPassword)
ctx.SetStatusCode(http.StatusUnauthorized)
case errors.Is(err, authentication.ErrPasswordWeak):
ctx.Logger.WithError(err).
WithFields(map[string]any{"username": username}).
Debug("Unable to change password for user as their new password was weak or empty")
ctx.SetJSONError(messagePasswordWeak)
ctx.SetStatusCode(http.StatusBadRequest)
case errors.Is(err, authentication.ErrAuthenticationFailed):
ctx.Logger.WithError(err).
WithFields(map[string]any{"username": username}).
Error("Unable to change password for user as authentication failed for the user")
ctx.SetJSONError(messageOperationFailed)
ctx.SetStatusCode(http.StatusUnauthorized)
default:
ctx.Logger.WithError(err).
WithFields(map[string]any{"username": username}).
Error("Unable to change password for user for an unknown reason")
ctx.SetJSONError(messageOperationFailed)
ctx.SetStatusCode(http.StatusInternalServerError)
}
return
}
ctx.Logger.
WithFields(map[string]any{"username": username}).
Debug("User has changed their password")
if err = provider.SaveSession(ctx.RequestCtx, userSession); err != nil {
ctx.Logger.WithError(err).
WithFields(map[string]any{"username": username}).
Error("Unable to update password change state")
ctx.SetJSONError(messageOperationFailed)
return
}
userInfo, err := ctx.Providers.UserProvider.GetDetails(username)
if err != nil {
ctx.Logger.Error(err)
ctx.ReplyOK()
return
}
if len(userInfo.Emails) == 0 {
ctx.Logger.WithFields(map[string]any{"username": username}).
Debug("user has no email address configured")
ctx.ReplyOK()
return
}
data := templates.EmailEventValues{
Title: "Password changed successfully",
DisplayName: userInfo.DisplayName,
RemoteIP: ctx.RemoteIP().String(),
Details: map[string]any{
"Action": "Password Change",
},
BodyPrefix: eventEmailActionPasswordModifyPrefix,
BodyEvent: eventEmailActionPasswordChange,
BodySuffix: eventEmailActionPasswordModifySuffix,
}
addresses := userInfo.Addresses()
ctx.Logger.WithFields(map[string]any{
"username": username,
"email": addresses[0].String(),
}).
Debug("Sending an email to inform user that their password has changed.")
if err = ctx.Providers.Notifier.Send(ctx, addresses[0], "Password changed successfully", ctx.Providers.Templates.GetEventEmailTemplate(), data); err != nil {
ctx.Logger.WithError(err).
WithFields(map[string]any{
"username": username,
"email": addresses[0].String(),
}).
Debug("Unable to notify user of password change")
ctx.ReplyOK()
return
}
}
|