mirror of
https://github.com/zitadel/zitadel.git
synced 2025-04-22 13:31:33 +00:00

* feat: return 404 or 409 if org reg disallowed * fix: system limit permissions * feat: add iam limits api * feat: disallow public org registrations on default instance * add integration test * test: integration * fix test * docs: describe public org registrations * avoid updating docs deps * fix system limits integration test * silence integration tests * fix linting * ignore strange linter complaints * review * improve reset properties naming * redefine the api * use restrictions aggregate * test query * simplify and test projection * test commands * fix unit tests * move integration test * support restrictions on default instance * also test GetRestrictions * self review * lint * abstract away resource owner * fix tests * configure supported languages * fix allowed languages * fix tests * default lang must not be restricted * preferred language must be allowed * change preferred languages * check languages everywhere * lint * test command side * lint * add integration test * add integration test * restrict supported ui locales * lint * lint * cleanup * lint * allow undefined preferred language * fix integration tests * update main * fix env var * ignore linter * ignore linter * improve integration test config * reduce cognitive complexity * compile * check for duplicates * remove useless restriction checks * review * revert restriction renaming * fix language restrictions * lint * generate * allow custom texts for supported langs for now * fix tests * cleanup * cleanup * cleanup * lint * unsupported preferred lang is allowed * fix integration test * finish reverting to old property name * finish reverting to old property name * load languages * refactor(i18n): centralize translators and fs * lint * amplify no validations on preferred languages * fix integration test * lint * fix resetting allowed languages * test unchanged restrictions
163 lines
5.1 KiB
Go
163 lines
5.1 KiB
Go
package login
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
http_mw "github.com/zitadel/zitadel/internal/api/http/middleware"
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
"github.com/zitadel/zitadel/internal/query"
|
|
)
|
|
|
|
const (
|
|
queryInitPWCode = "code"
|
|
queryInitPWUserID = "userID"
|
|
|
|
tmplInitPassword = "initpassword"
|
|
tmplInitPasswordDone = "initpassworddone"
|
|
)
|
|
|
|
type initPasswordFormData struct {
|
|
Code string `schema:"code"`
|
|
Password string `schema:"password"`
|
|
PasswordConfirm string `schema:"passwordconfirm"`
|
|
UserID string `schema:"userID"`
|
|
Resend bool `schema:"resend"`
|
|
}
|
|
|
|
type initPasswordData struct {
|
|
baseData
|
|
profileData
|
|
Code string
|
|
UserID string
|
|
MinLength uint64
|
|
HasUppercase string
|
|
HasLowercase string
|
|
HasNumber string
|
|
HasSymbol string
|
|
}
|
|
|
|
func InitPasswordLink(origin, userID, code, orgID string) string {
|
|
return fmt.Sprintf("%s%s?userID=%s&code=%s&orgID=%s", externalLink(origin), EndpointInitPassword, userID, code, orgID)
|
|
}
|
|
|
|
func (l *Login) handleInitPassword(w http.ResponseWriter, r *http.Request) {
|
|
userID := r.FormValue(queryInitPWUserID)
|
|
code := r.FormValue(queryInitPWCode)
|
|
l.renderInitPassword(w, r, nil, userID, code, nil)
|
|
}
|
|
|
|
func (l *Login) handleInitPasswordCheck(w http.ResponseWriter, r *http.Request) {
|
|
data := new(initPasswordFormData)
|
|
authReq, err := l.getAuthRequestAndParseData(r, data)
|
|
if err != nil {
|
|
l.renderError(w, r, authReq, err)
|
|
return
|
|
}
|
|
|
|
if data.Resend {
|
|
l.resendPasswordSet(w, r, authReq)
|
|
return
|
|
}
|
|
l.checkPWCode(w, r, authReq, data)
|
|
}
|
|
|
|
func (l *Login) checkPWCode(w http.ResponseWriter, r *http.Request, authReq *domain.AuthRequest, data *initPasswordFormData) {
|
|
if data.Password != data.PasswordConfirm {
|
|
err := errors.ThrowInvalidArgument(nil, "VIEW-KaGue", "Errors.User.Password.ConfirmationWrong")
|
|
l.renderInitPassword(w, r, authReq, data.UserID, data.Code, err)
|
|
return
|
|
}
|
|
userOrg := ""
|
|
if authReq != nil {
|
|
userOrg = authReq.UserOrgID
|
|
}
|
|
userAgentID, _ := http_mw.UserAgentIDFromCtx(r.Context())
|
|
_, err := l.command.SetPasswordWithVerifyCode(setContext(r.Context(), userOrg), userOrg, data.UserID, data.Code, data.Password, userAgentID)
|
|
if err != nil {
|
|
l.renderInitPassword(w, r, authReq, data.UserID, "", err)
|
|
return
|
|
}
|
|
l.renderInitPasswordDone(w, r, authReq, userOrg)
|
|
}
|
|
|
|
func (l *Login) resendPasswordSet(w http.ResponseWriter, r *http.Request, authReq *domain.AuthRequest) {
|
|
if authReq == nil {
|
|
l.renderError(w, r, nil, errors.ThrowInternal(nil, "LOGIN-8sn7s", "Errors.AuthRequest.NotFound"))
|
|
return
|
|
}
|
|
userOrg := login
|
|
if authReq != nil {
|
|
userOrg = authReq.UserOrgID
|
|
}
|
|
loginName, err := query.NewUserLoginNamesSearchQuery(authReq.LoginName)
|
|
if err != nil {
|
|
l.renderInitPassword(w, r, authReq, authReq.UserID, "", err)
|
|
return
|
|
}
|
|
passwordCodeGenerator, err := l.query.InitEncryptionGenerator(r.Context(), domain.SecretGeneratorTypePasswordResetCode, l.userCodeAlg)
|
|
if err != nil {
|
|
l.renderInitPassword(w, r, authReq, authReq.UserID, "", err)
|
|
return
|
|
}
|
|
user, err := l.query.GetUser(setContext(r.Context(), userOrg), false, loginName)
|
|
if err != nil {
|
|
l.renderInitPassword(w, r, authReq, authReq.UserID, "", err)
|
|
return
|
|
}
|
|
_, err = l.command.RequestSetPassword(setContext(r.Context(), userOrg), user.ID, user.ResourceOwner, domain.NotificationTypeEmail, passwordCodeGenerator)
|
|
l.renderInitPassword(w, r, authReq, authReq.UserID, "", err)
|
|
}
|
|
|
|
func (l *Login) renderInitPassword(w http.ResponseWriter, r *http.Request, authReq *domain.AuthRequest, userID, code string, err error) {
|
|
var errID, errMessage string
|
|
if err != nil {
|
|
errID, errMessage = l.getErrorMessage(r, err)
|
|
}
|
|
if userID == "" && authReq != nil {
|
|
userID = authReq.UserID
|
|
}
|
|
|
|
translator := l.getTranslator(r.Context(), authReq)
|
|
|
|
data := initPasswordData{
|
|
baseData: l.getBaseData(r, authReq, translator, "InitPassword.Title", "InitPassword.Description", errID, errMessage),
|
|
profileData: l.getProfileData(authReq),
|
|
UserID: userID,
|
|
Code: code,
|
|
}
|
|
policy := l.getPasswordComplexityPolicyByUserID(r, userID)
|
|
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
|
|
}
|
|
}
|
|
if authReq == nil {
|
|
user, err := l.query.GetUserByID(r.Context(), false, userID)
|
|
if err == nil {
|
|
l.customTexts(r.Context(), translator, user.ResourceOwner)
|
|
}
|
|
}
|
|
l.renderer.RenderTemplate(w, r, translator, l.renderer.Templates[tmplInitPassword], data, nil)
|
|
}
|
|
|
|
func (l *Login) renderInitPasswordDone(w http.ResponseWriter, r *http.Request, authReq *domain.AuthRequest, orgID string) {
|
|
translator := l.getTranslator(r.Context(), authReq)
|
|
data := l.getUserData(r, authReq, translator, "InitPasswordDone.Title", "InitPasswordDone.Description", "", "")
|
|
if authReq == nil {
|
|
l.customTexts(r.Context(), translator, orgID)
|
|
}
|
|
l.renderer.RenderTemplate(w, r, translator, l.renderer.Templates[tmplInitPasswordDone], data, nil)
|
|
}
|