2022-02-14 17:22:30 +01:00
|
|
|
package login
|
2020-06-05 07:50:04 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-07-08 13:56:37 +02:00
|
|
|
|
2024-01-30 15:36:34 +01:00
|
|
|
http_mw "github.com/zitadel/zitadel/internal/api/http/middleware"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2020-06-05 07:50:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tmplChangePassword = "changepassword"
|
|
|
|
tmplChangePasswordDone = "changepassworddone"
|
|
|
|
)
|
|
|
|
|
|
|
|
type changePasswordData struct {
|
2020-07-16 14:26:08 +02:00
|
|
|
OldPassword string `schema:"change-old-password"`
|
|
|
|
NewPassword string `schema:"change-new-password"`
|
|
|
|
NewPasswordConfirmation string `schema:"change-password-confirmation"`
|
2020-06-05 07:50:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Login) handleChangePassword(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data := new(changePasswordData)
|
2024-05-24 16:58:45 +02:00
|
|
|
authReq, err := l.ensureAuthRequestAndParseData(r, data)
|
2020-06-05 07:50:04 +02:00
|
|
|
if err != nil {
|
|
|
|
l.renderError(w, r, authReq, err)
|
|
|
|
return
|
|
|
|
}
|
2024-01-30 15:36:34 +01:00
|
|
|
userAgentID, _ := http_mw.UserAgentIDFromCtx(r.Context())
|
2024-05-02 11:50:13 +02:00
|
|
|
_, err = l.command.ChangePassword(setContext(r.Context(), authReq.UserOrgID), authReq.UserOrgID, authReq.UserID, data.OldPassword, data.NewPassword, userAgentID, false)
|
2020-06-05 07:50:04 +02:00
|
|
|
if err != nil {
|
|
|
|
l.renderChangePassword(w, r, authReq, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
l.renderChangePasswordDone(w, r, authReq)
|
|
|
|
}
|
|
|
|
|
2021-02-08 11:30:30 +01:00
|
|
|
func (l *Login) renderChangePassword(w http.ResponseWriter, r *http.Request, authReq *domain.AuthRequest, err error) {
|
2022-11-07 09:55:12 +01:00
|
|
|
translator := l.getTranslator(r.Context(), authReq)
|
2024-06-18 13:27:44 +02:00
|
|
|
if authReq == nil || len(authReq.PossibleSteps) < 1 {
|
|
|
|
l.renderError(w, r, authReq, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
step, ok := authReq.PossibleSteps[0].(*domain.ChangePasswordStep)
|
|
|
|
if !ok {
|
|
|
|
l.renderError(w, r, authReq, err)
|
|
|
|
return
|
|
|
|
}
|
2020-07-16 14:26:08 +02:00
|
|
|
data := passwordData{
|
2025-01-15 11:39:28 +01:00
|
|
|
baseData: l.getBaseData(r, authReq, translator, "PasswordChange.Title", "PasswordChange.Description", err),
|
2020-07-20 10:00:29 +02:00
|
|
|
profileData: l.getProfileData(authReq),
|
2024-06-18 13:27:44 +02:00
|
|
|
Expired: step.Expired,
|
2020-06-05 07:50:04 +02:00
|
|
|
}
|
2022-09-20 09:22:47 +02:00
|
|
|
policy := l.getPasswordComplexityPolicy(r, authReq.UserOrgID)
|
2020-07-16 14:26:08 +02:00
|
|
|
if policy != nil {
|
|
|
|
data.MinLength = policy.MinLength
|
|
|
|
if policy.HasUppercase {
|
|
|
|
data.HasUppercase = UpperCaseRegex
|
|
|
|
}
|
|
|
|
if policy.HasLowercase {
|
|
|
|
data.HasLowercase = LowerCaseRegex
|
|
|
|
}
|
|
|
|
if policy.HasSymbol {
|
|
|
|
data.HasSymbol = SymbolRegex
|
|
|
|
}
|
|
|
|
if policy.HasNumber {
|
|
|
|
data.HasNumber = NumberRegex
|
|
|
|
}
|
|
|
|
}
|
2021-07-05 15:10:49 +02:00
|
|
|
l.renderer.RenderTemplate(w, r, translator, l.renderer.Templates[tmplChangePassword], data, nil)
|
2020-06-05 07:50:04 +02:00
|
|
|
}
|
|
|
|
|
2021-02-08 11:30:30 +01:00
|
|
|
func (l *Login) renderChangePasswordDone(w http.ResponseWriter, r *http.Request, authReq *domain.AuthRequest) {
|
2022-11-07 09:55:12 +01:00
|
|
|
translator := l.getTranslator(r.Context(), authReq)
|
2025-01-15 11:39:28 +01:00
|
|
|
data := l.getUserData(r, authReq, translator, "PasswordChange.Title", "PasswordChange.Description", nil)
|
2022-11-07 09:55:12 +01:00
|
|
|
l.renderer.RenderTemplate(w, r, translator, l.renderer.Templates[tmplChangePasswordDone], data, nil)
|
2020-06-05 07:50:04 +02:00
|
|
|
}
|