2022-02-14 17:22:30 +01:00
|
|
|
package login
|
2020-06-05 07:50:04 +02:00
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
l.renderRegister(w, r, authRequest, data, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if data.Password != data.Password2 {
|
2020-06-11 13:22:24 +02:00
|
|
|
err := caos_errs.ThrowInvalidArgument(nil, "VIEW-KaGue", "Errors.User.Password.ConfirmationWrong")
|
2020-06-05 07:50:04 +02:00
|
|
|
l.renderRegister(w, r, authRequest, data, err)
|
|
|
|
return
|
|
|
|
}
|
2020-06-15 14:57:19 +02:00
|
|
|
|
2022-06-03 14:30:39 +02:00
|
|
|
resourceOwner := authz.GetInstance(r.Context()).DefaultOrganisationID()
|
2021-02-08 11:30:30 +01:00
|
|
|
|
2022-06-03 14:30:39 +02:00
|
|
|
if authRequest != nil && authRequest.RequestedOrgID != "" && authRequest.RequestedOrgID != resourceOwner {
|
2020-12-14 10:54:29 +01:00
|
|
|
resourceOwner = authRequest.RequestedOrgID
|
2020-09-18 13:26:28 +02:00
|
|
|
}
|
2022-03-14 07:55:09 +01:00
|
|
|
initCodeGenerator, err := l.query.InitEncryptionGenerator(r.Context(), domain.SecretGeneratorTypeInitCode, l.userCodeAlg)
|
2022-02-16 16:49:17 +01:00
|
|
|
if err != nil {
|
|
|
|
l.renderRegister(w, r, authRequest, data, err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-18 16:48:26 +02:00
|
|
|
emailCodeGenerator, err := l.query.InitEncryptionGenerator(r.Context(), domain.SecretGeneratorTypeVerifyEmailCode, l.userCodeAlg)
|
|
|
|
if err != nil {
|
|
|
|
l.renderRegister(w, r, authRequest, data, err)
|
|
|
|
return
|
|
|
|
}
|
2022-03-14 07:55:09 +01:00
|
|
|
phoneCodeGenerator, err := l.query.InitEncryptionGenerator(r.Context(), domain.SecretGeneratorTypeVerifyPhoneCode, l.userCodeAlg)
|
2022-02-16 16:49:17 +01:00
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err = l.command.RegisterHuman(setContext(r.Context(), resourceOwner), resourceOwner, user, nil, nil, initCodeGenerator, emailCodeGenerator, phoneCodeGenerator)
|
2020-06-05 07:50:04 +02:00
|
|
|
if err != nil {
|
|
|
|
l.renderRegister(w, r, authRequest, data, err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-25 14:08:01 +01:00
|
|
|
|
|
|
|
if len(metadatas) > 0 {
|
|
|
|
_, err = l.command.BulkSetUserMetadata(r.Context(), user.AggregateID, resourceOwner, metadatas...)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: What if action is configured to be allowed to fail? Same question for external registration.
|
|
|
|
l.renderRegister(w, r, authRequest, data, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 11:40:49 +01:00
|
|
|
userGrants, err := l.runPostCreationActions(user.AggregateID, 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())
|
2022-04-05 07:58:09 +02:00
|
|
|
err = l.authRepo.SelectUser(r.Context(), authRequest.ID, user.AggregateID, 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
|
|
|
|
2021-10-05 08:33:20 +02:00
|
|
|
var resourceOwner string
|
|
|
|
if authRequest != nil {
|
|
|
|
resourceOwner = authRequest.RequestedOrgID
|
|
|
|
}
|
2021-04-19 12:43:17 +02:00
|
|
|
|
|
|
|
if resourceOwner == "" {
|
2022-06-03 14:30:39 +02:00
|
|
|
resourceOwner = authz.GetInstance(r.Context()).DefaultOrganisationID()
|
|
|
|
}
|
|
|
|
|
|
|
|
data := registerData{
|
2022-11-30 17:01:17 +01:00
|
|
|
baseData: l.getBaseData(r, authRequest, "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
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|