2021-01-04 13:52:13 +00:00
|
|
|
package domain
|
2020-12-10 15:18:52 +00:00
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
import (
|
2023-04-11 15:56:51 +00:00
|
|
|
"strings"
|
2021-03-25 13:41:07 +00:00
|
|
|
"time"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
2023-03-14 19:20:38 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
2022-04-26 23:01:45 +00:00
|
|
|
caos_errors "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
2021-01-05 08:33:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Human struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
Username string
|
|
|
|
State UserState
|
2021-01-05 08:33:45 +00:00
|
|
|
*Password
|
2023-07-14 06:49:57 +00:00
|
|
|
HashedPassword string
|
2021-01-05 08:33:45 +00:00
|
|
|
*Profile
|
|
|
|
*Email
|
|
|
|
*Phone
|
|
|
|
*Address
|
|
|
|
}
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
func (h Human) GetUsername() string {
|
|
|
|
return h.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h Human) GetState() UserState {
|
|
|
|
return h.State
|
|
|
|
}
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
type InitUserCode struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
|
|
|
|
Code *crypto.CryptoValue
|
|
|
|
Expiry time.Duration
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:18:52 +00:00
|
|
|
type Gender int32
|
|
|
|
|
|
|
|
const (
|
|
|
|
GenderUnspecified Gender = iota
|
|
|
|
GenderFemale
|
|
|
|
GenderMale
|
|
|
|
GenderDiverse
|
|
|
|
|
|
|
|
genderCount
|
|
|
|
)
|
|
|
|
|
|
|
|
func (f Gender) Valid() bool {
|
|
|
|
return f >= 0 && f < genderCount
|
|
|
|
}
|
2021-01-05 08:33:45 +00:00
|
|
|
|
2021-11-30 07:57:51 +00:00
|
|
|
func (f Gender) Specified() bool {
|
|
|
|
return f > GenderUnspecified && f < genderCount
|
|
|
|
}
|
|
|
|
|
2023-03-14 19:20:38 +00:00
|
|
|
func (u *Human) Normalize() error {
|
|
|
|
if u.Username == "" {
|
|
|
|
return errors.ThrowInvalidArgument(nil, "COMMAND-00p2b", "Errors.User.Username.Empty")
|
|
|
|
}
|
|
|
|
if err := u.Profile.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := u.Email.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if u.Phone != nil && u.Phone.PhoneNumber != "" {
|
|
|
|
if err := u.Phone.Normalize(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-05 08:33:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (u *Human) CheckDomainPolicy(policy *DomainPolicy) error {
|
2021-01-05 08:33:45 +00:00
|
|
|
if policy == nil {
|
2022-03-24 16:21:34 +00:00
|
|
|
return caos_errors.ThrowPreconditionFailed(nil, "DOMAIN-zSH7j", "Errors.Users.DomainPolicyNil")
|
2021-01-05 08:33:45 +00:00
|
|
|
}
|
2021-02-08 10:30:30 +00:00
|
|
|
if !policy.UserLoginMustBeDomain && u.Profile != nil && u.Username == "" && u.Email != nil {
|
2023-03-14 19:20:38 +00:00
|
|
|
u.Username = string(u.EmailAddress)
|
2021-01-05 08:33:45 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-11 15:56:51 +00:00
|
|
|
func (u *Human) EnsureDisplayName() {
|
2023-04-17 06:26:40 +00:00
|
|
|
if u.Profile == nil {
|
|
|
|
u.Profile = new(Profile)
|
|
|
|
}
|
|
|
|
if u.DisplayName != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if u.FirstName != "" && u.LastName != "" {
|
2021-01-05 08:33:45 +00:00
|
|
|
u.DisplayName = u.FirstName + " " + u.LastName
|
2023-04-11 15:56:51 +00:00
|
|
|
return
|
2021-01-05 08:33:45 +00:00
|
|
|
}
|
2023-04-11 15:56:51 +00:00
|
|
|
if u.Email != nil && strings.TrimSpace(string(u.Email.EmailAddress)) != "" {
|
|
|
|
u.DisplayName = string(u.Email.EmailAddress)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
u.DisplayName = u.Username
|
2021-01-05 08:33:45 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 06:49:57 +00:00
|
|
|
func (u *Human) HashPasswordIfExisting(policy *PasswordComplexityPolicy, hasher *crypto.PasswordHasher, onetime bool) error {
|
2021-01-05 08:33:45 +00:00
|
|
|
if u.Password != nil {
|
2021-01-07 15:06:45 +00:00
|
|
|
u.Password.ChangeRequired = onetime
|
2023-07-14 06:49:57 +00:00
|
|
|
return u.Password.HashPasswordIfExisting(policy, hasher)
|
2021-01-05 08:33:45 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-06 10:12:56 +00:00
|
|
|
|
2021-08-11 11:50:03 +00:00
|
|
|
func (u *Human) IsInitialState(passwordless, externalIDPs bool) bool {
|
2022-10-18 14:48:26 +00:00
|
|
|
if externalIDPs {
|
|
|
|
return false
|
|
|
|
}
|
2023-07-14 06:49:57 +00:00
|
|
|
return u.Email == nil || !u.IsEmailVerified || !passwordless && (u.Password == nil || u.Password.SecretString == "") && u.HashedPassword == ""
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewInitUserCode(generator crypto.Generator) (*InitUserCode, error) {
|
|
|
|
initCodeCrypto, _, err := crypto.NewCode(generator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &InitUserCode{
|
|
|
|
Code: initCodeCrypto,
|
|
|
|
Expiry: generator.Expiry(),
|
|
|
|
}, nil
|
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
|
|
|
|
func GenerateLoginName(username, domain string, appendDomain bool) string {
|
|
|
|
if !appendDomain {
|
|
|
|
return username
|
|
|
|
}
|
|
|
|
return username + "@" + domain
|
|
|
|
}
|