2022-02-14 17:22:30 +01:00
|
|
|
package login
|
2020-06-05 07:50:04 +02:00
|
|
|
|
|
|
|
import (
|
2024-10-16 15:09:32 +02:00
|
|
|
"context"
|
2020-09-07 14:52:49 +02:00
|
|
|
"net/http"
|
2020-07-08 13:56:37 +02:00
|
|
|
|
2020-12-14 10:54:29 +01:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2022-06-03 14:30:39 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2022-04-27 01:01:45 +02:00
|
|
|
http_mw "github.com/zitadel/zitadel/internal/api/http/middleware"
|
2024-04-24 17:50:58 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-06-05 07:50:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-02-08 11:30:30 +01:00
|
|
|
tmplRegister = "register"
|
2020-06-05 07:50:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type registerFormData struct {
|
2023-03-14 20:20:38 +01:00
|
|
|
Email domain.EmailAddress `schema:"email"`
|
|
|
|
Username string `schema:"username"`
|
|
|
|
Firstname string `schema:"firstname"`
|
|
|
|
Lastname string `schema:"lastname"`
|
|
|
|
Language string `schema:"language"`
|
|
|
|
Password string `schema:"register-password"`
|
|
|
|
Password2 string `schema:"register-password-confirmation"`
|
|
|
|
TermsConfirm bool `schema:"terms-confirm"`
|
2020-06-05 07:50:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type registerData struct {
|
|
|
|
baseData
|
|
|
|
registerFormData
|
2022-09-20 09:22:47 +02:00
|
|
|
MinLength uint64
|
|
|
|
HasUppercase string
|
|
|
|
HasLowercase string
|
|
|
|
HasNumber string
|
|
|
|
HasSymbol string
|
|
|
|
ShowUsername bool
|
|
|
|
ShowUsernameSuffix bool
|
|
|
|
OrgRegister bool
|
2020-06-05 07:50:04 +02:00
|
|
|
}
|
|
|
|
|
2024-10-16 15:09:32 +02:00
|
|
|
func determineResourceOwner(ctx context.Context, authRequest *domain.AuthRequest) string {
|
|
|
|
if authRequest != nil && authRequest.RequestedOrgID != "" {
|
|
|
|
return authRequest.RequestedOrgID
|
|
|
|
}
|
|
|
|
return authz.GetInstance(ctx).DefaultOrganisationID()
|
|
|
|
}
|
|
|
|
|
2020-06-05 07:50:04 +02:00
|
|
|
func (l *Login) handleRegister(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data := new(registerFormData)
|
|
|
|
authRequest, err := l.getAuthRequestAndParseData(r, data)
|
|
|
|
if err != nil {
|
|
|
|
l.renderError(w, r, authRequest, err)
|
|
|
|
return
|
|
|
|
}
|
2024-10-16 15:09:32 +02:00
|
|
|
if err := l.checkRegistrationAllowed(r, determineResourceOwner(r.Context(), authRequest), authRequest); err != nil {
|
|
|
|
l.renderError(w, r, authRequest, err)
|
|
|
|
return
|
|
|
|
}
|
2020-06-05 07:50:04 +02:00
|
|
|
l.renderRegister(w, r, authRequest, data, nil)
|
|
|
|
}
|
|
|
|
|
2024-10-16 15:09:32 +02:00
|
|
|
func (l *Login) checkRegistrationAllowed(r *http.Request, orgID string, authReq *domain.AuthRequest) error {
|
|
|
|
if authReq != nil {
|
|
|
|
if registrationAllowed(authReq) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return zerrors.ThrowPreconditionFailed(nil, "VIEW-RRGRXz4kGw", "Errors.Org.LoginPolicy.RegistrationNotAllowed")
|
|
|
|
}
|
|
|
|
loginPolicy, err := l.getLoginPolicy(r, orgID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if loginPolicy.AllowRegister && loginPolicy.AllowUsernamePassword {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return zerrors.ThrowPreconditionFailed(nil, "VIEW-Vq3bduAacD", "Errors.Org.LoginPolicy.RegistrationNotAllowed")
|
|
|
|
}
|
|
|
|
|
2020-06-05 07:50:04 +02:00
|
|
|
func (l *Login) handleRegisterCheck(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data := new(registerFormData)
|
|
|
|
authRequest, err := l.getAuthRequestAndParseData(r, data)
|
|
|
|
if err != nil {
|
|
|
|
l.renderError(w, r, authRequest, err)
|
|
|
|
return
|
|
|
|
}
|
2024-10-16 15:09:32 +02:00
|
|
|
resourceOwner := determineResourceOwner(r.Context(), authRequest)
|
|
|
|
if err := l.checkRegistrationAllowed(r, resourceOwner, authRequest); err != nil {
|
|
|
|
l.renderError(w, r, authRequest, err)
|
|
|
|
return
|
|
|
|
}
|
2020-06-05 07:50:04 +02:00
|
|
|
if data.Password != data.Password2 {
|
2023-12-08 16:30:55 +02:00
|
|
|
err := zerrors.ThrowInvalidArgument(nil, "VIEW-KaGue", "Errors.User.Password.ConfirmationWrong")
|
2020-06-05 07:50:04 +02:00
|
|
|
l.renderRegister(w, r, authRequest, data, err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-25 14:08:01 +01:00
|
|
|
// For consistency with the external authentication flow,
|
|
|
|
// the setMetadata() function is provided on the pre creation hook, for now,
|
|
|
|
// like for the ExternalAuthentication flow.
|
|
|
|
// If there is a need for additional context after registration,
|
|
|
|
// we could provide that method in the PostCreation trigger too,
|
|
|
|
// without breaking existing actions.
|
|
|
|
// Also, if that field is needed, we probably also should provide it
|
|
|
|
// for ExternalAuthentication.
|
2023-01-26 11:40:49 +01:00
|
|
|
user, metadatas, err := l.runPreCreationActions(authRequest, r, data.toHumanDomain(), make([]*domain.Metadata, 0), resourceOwner, domain.FlowTypeInternalAuthentication)
|
2023-01-25 14:08:01 +01:00
|
|
|
if err != nil {
|
|
|
|
l.renderRegister(w, r, authRequest, data, err)
|
|
|
|
return
|
|
|
|
}
|
2024-04-24 17:50:58 +02:00
|
|
|
|
|
|
|
human := command.AddHumanFromDomain(user, metadatas, authRequest, nil)
|
|
|
|
err = l.command.AddUserHuman(setContext(r.Context(), resourceOwner), resourceOwner, human, true, l.userCodeAlg)
|
2020-06-05 07:50:04 +02:00
|
|
|
if err != nil {
|
|
|
|
l.renderRegister(w, r, authRequest, data, err)
|
|
|
|
return
|
|
|
|
}
|
2024-04-24 17:50:58 +02:00
|
|
|
userGrants, err := l.runPostCreationActions(human.ID, authRequest, r, resourceOwner, domain.FlowTypeInternalAuthentication)
|
2023-01-25 14:08:01 +01:00
|
|
|
if err != nil {
|
|
|
|
l.renderError(w, r, authRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = l.appendUserGrants(r.Context(), userGrants, resourceOwner)
|
|
|
|
if err != nil {
|
|
|
|
l.renderError(w, r, authRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-05 07:50:04 +02:00
|
|
|
if authRequest == nil {
|
2022-05-16 15:39:09 +02:00
|
|
|
l.defaultRedirect(w, r)
|
2020-06-05 07:50:04 +02:00
|
|
|
return
|
|
|
|
}
|
2021-06-14 10:40:38 +02:00
|
|
|
userAgentID, _ := http_mw.UserAgentIDFromCtx(r.Context())
|
2024-04-24 17:50:58 +02:00
|
|
|
err = l.authRepo.SelectUser(r.Context(), authRequest.ID, human.ID, userAgentID)
|
2021-06-14 10:40:38 +02:00
|
|
|
if err != nil {
|
|
|
|
l.renderRegister(w, r, authRequest, data, err)
|
|
|
|
return
|
|
|
|
}
|
2020-06-05 07:50:04 +02:00
|
|
|
l.renderNextStep(w, r, authRequest)
|
|
|
|
}
|
|
|
|
|
2021-02-08 11:30:30 +01:00
|
|
|
func (l *Login) renderRegister(w http.ResponseWriter, r *http.Request, authRequest *domain.AuthRequest, formData *registerFormData, err error) {
|
2021-06-18 10:31:53 +02:00
|
|
|
var errID, errMessage string
|
2020-06-05 07:50:04 +02:00
|
|
|
if err != nil {
|
2021-06-18 10:31:53 +02:00
|
|
|
errID, errMessage = l.getErrorMessage(r, err)
|
2020-06-05 07:50:04 +02:00
|
|
|
}
|
2022-04-25 11:16:36 +02:00
|
|
|
translator := l.getTranslator(r.Context(), authRequest)
|
2020-06-05 07:50:04 +02:00
|
|
|
if formData == nil {
|
|
|
|
formData = new(registerFormData)
|
|
|
|
}
|
|
|
|
if formData.Language == "" {
|
2021-07-05 15:10:49 +02:00
|
|
|
formData.Language = l.renderer.ReqLang(translator, r).String()
|
2020-06-05 07:50:04 +02:00
|
|
|
}
|
2021-04-19 12:43:17 +02:00
|
|
|
|
2024-10-16 15:09:32 +02:00
|
|
|
resourceOwner := determineResourceOwner(r.Context(), authRequest)
|
2022-06-03 14:30:39 +02:00
|
|
|
data := registerData{
|
2023-12-05 12:12:01 +01:00
|
|
|
baseData: l.getBaseData(r, authRequest, translator, "RegistrationUser.Title", "RegistrationUser.Description", errID, errMessage),
|
2022-06-03 14:30:39 +02:00
|
|
|
registerFormData: *formData,
|
2021-04-19 12:43:17 +02:00
|
|
|
}
|
|
|
|
|
2022-09-20 09:22:47 +02:00
|
|
|
pwPolicy := l.getPasswordComplexityPolicy(r, resourceOwner)
|
2021-04-19 12:43:17 +02:00
|
|
|
if pwPolicy != nil {
|
|
|
|
data.MinLength = pwPolicy.MinLength
|
|
|
|
if pwPolicy.HasUppercase {
|
|
|
|
data.HasUppercase = UpperCaseRegex
|
|
|
|
}
|
|
|
|
if pwPolicy.HasLowercase {
|
|
|
|
data.HasLowercase = LowerCaseRegex
|
|
|
|
}
|
|
|
|
if pwPolicy.HasSymbol {
|
|
|
|
data.HasSymbol = SymbolRegex
|
|
|
|
}
|
|
|
|
if pwPolicy.HasNumber {
|
|
|
|
data.HasNumber = NumberRegex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
orgIAMPolicy, err := l.getOrgDomainPolicy(r, resourceOwner)
|
2021-04-19 12:43:17 +02:00
|
|
|
if err != nil {
|
|
|
|
l.renderRegister(w, r, authRequest, formData, err)
|
|
|
|
return
|
2020-07-16 14:26:08 +02:00
|
|
|
}
|
2021-04-19 12:43:17 +02:00
|
|
|
data.ShowUsername = orgIAMPolicy.UserLoginMustBeDomain
|
2021-07-05 15:10:49 +02:00
|
|
|
data.OrgRegister = orgIAMPolicy.UserLoginMustBeDomain
|
2020-07-16 14:26:08 +02:00
|
|
|
|
2022-08-02 16:31:35 +02:00
|
|
|
labelPolicy, err := l.getLabelPolicy(r, resourceOwner)
|
|
|
|
if err != nil {
|
|
|
|
l.renderRegister(w, r, authRequest, formData, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data.ShowUsernameSuffix = !labelPolicy.HideLoginNameSuffix
|
|
|
|
|
2020-06-05 07:50:04 +02:00
|
|
|
funcs := map[string]interface{}{
|
|
|
|
"selectedLanguage": func(l string) bool {
|
|
|
|
if formData == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return formData.Language == l
|
|
|
|
},
|
|
|
|
}
|
2022-09-20 09:22:47 +02:00
|
|
|
if authRequest == nil {
|
|
|
|
l.customTexts(r.Context(), translator, resourceOwner)
|
|
|
|
}
|
2021-07-05 15:10:49 +02:00
|
|
|
l.renderer.RenderTemplate(w, r, translator, l.renderer.Templates[tmplRegister], data, funcs)
|
2020-06-05 07:50:04 +02:00
|
|
|
}
|
|
|
|
|
2021-02-08 11:30:30 +01:00
|
|
|
func (d registerFormData) toHumanDomain() *domain.Human {
|
|
|
|
return &domain.Human{
|
2021-04-19 12:43:17 +02:00
|
|
|
Username: d.Username,
|
2021-02-08 11:30:30 +01:00
|
|
|
Profile: &domain.Profile{
|
|
|
|
FirstName: d.Firstname,
|
|
|
|
LastName: d.Lastname,
|
|
|
|
PreferredLanguage: language.Make(d.Language),
|
|
|
|
},
|
|
|
|
Password: &domain.Password{
|
|
|
|
SecretString: d.Password,
|
|
|
|
},
|
|
|
|
Email: &domain.Email{
|
|
|
|
EmailAddress: d.Email,
|
2020-06-05 07:50:04 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|