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 (
|
2021-03-25 13:41:07 +00:00
|
|
|
"time"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
type HumanDetails struct {
|
|
|
|
ID string
|
|
|
|
ObjectDetails
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*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
|
|
|
|
}
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
func (u *Human) IsValid() bool {
|
2021-04-19 10:43:17 +00:00
|
|
|
return u.Username != "" && u.Profile != nil && u.Profile.IsValid() && u.Email != nil && u.Email.IsValid() && u.Phone == nil || (u.Phone != nil && u.Phone.PhoneNumber != "" && u.Phone.IsValid())
|
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 {
|
|
|
|
u.Username = u.EmailAddress
|
2021-01-05 08:33:45 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Human) SetNamesAsDisplayname() {
|
|
|
|
if u.Profile != nil && u.DisplayName == "" && u.FirstName != "" && u.LastName != "" {
|
|
|
|
u.DisplayName = u.FirstName + " " + u.LastName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Human) HashPasswordIfExisting(policy *PasswordComplexityPolicy, passwordAlg crypto.HashAlgorithm, onetime bool) error {
|
|
|
|
if u.Password != nil {
|
2021-01-07 15:06:45 +00:00
|
|
|
u.Password.ChangeRequired = onetime
|
|
|
|
return u.Password.HashPasswordIfExisting(policy, passwordAlg)
|
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 {
|
|
|
|
return u.Email == nil || !u.IsEmailVerified || !externalIDPs && !passwordless && (u.Password == nil || u.SecretString == "")
|
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
|
|
|
|
}
|