2019-04-24 21:52:08 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/middlewares"
|
|
|
|
"github.com/authelia/authelia/internal/session"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func identityRetrieverFromStorage(ctx *middlewares.AutheliaCtx) (*session.Identity, error) {
|
|
|
|
var requestBody resetPasswordStep1RequestBody
|
|
|
|
err := json.Unmarshal(ctx.PostBody(), &requestBody)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
details, err := ctx.Providers.UserProvider.GetDetails(requestBody.Username)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(details.Emails) == 0 {
|
|
|
|
return nil, fmt.Errorf("User %s has no email address configured", requestBody.Username)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &session.Identity{
|
|
|
|
Username: requestBody.Username,
|
|
|
|
Email: details.Emails[0],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetPasswordIdentityStart the handler for initiating the identity validation for resetting a password.
|
2020-01-21 00:10:00 +00:00
|
|
|
// We need to ensure the attacker cannot perform user enumeration by always replying with 200 whatever what happens in backend.
|
2019-04-24 21:52:08 +00:00
|
|
|
var ResetPasswordIdentityStart = middlewares.IdentityVerificationStart(middlewares.IdentityVerificationStartArgs{
|
|
|
|
MailSubject: "[Authelia] Reset your password",
|
|
|
|
MailTitle: "Reset your password",
|
|
|
|
MailButtonContent: "Reset",
|
2019-11-18 23:37:36 +00:00
|
|
|
TargetEndpoint: "/reset-password/step2",
|
2019-04-24 21:52:08 +00:00
|
|
|
ActionClaim: ResetPasswordAction,
|
|
|
|
IdentityRetrieverFunc: identityRetrieverFromStorage,
|
|
|
|
})
|
|
|
|
|
|
|
|
func resetPasswordIdentityFinish(ctx *middlewares.AutheliaCtx, username string) {
|
|
|
|
userSession := ctx.GetSession()
|
|
|
|
// TODO(c.michaud): use JWT tokens to expire the request in only few seconds for better security.
|
|
|
|
userSession.PasswordResetUsername = &username
|
|
|
|
ctx.SaveSession(userSession)
|
|
|
|
|
|
|
|
ctx.ReplyOK()
|
|
|
|
}
|
|
|
|
|
2020-01-21 00:10:00 +00:00
|
|
|
// ResetPasswordIdentityFinish the handler for finishing the identity validation.
|
2019-04-24 21:52:08 +00:00
|
|
|
var ResetPasswordIdentityFinish = middlewares.IdentityVerificationFinish(
|
|
|
|
middlewares.IdentityVerificationFinishArgs{ActionClaim: ResetPasswordAction}, resetPasswordIdentityFinish)
|