2021-01-05 08:33:45 +00:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
2022-04-12 14:20:17 +00:00
|
|
|
"time"
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
"github.com/ttacon/libphonenumber"
|
2023-03-14 19:20:38 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
2021-01-05 08:33:45 +00:00
|
|
|
)
|
|
|
|
|
2023-03-14 19:20:38 +00:00
|
|
|
const defaultRegion = "CH"
|
|
|
|
|
|
|
|
type PhoneNumber string
|
|
|
|
|
|
|
|
func (p PhoneNumber) Normalize() (PhoneNumber, error) {
|
|
|
|
if p == "" {
|
|
|
|
return p, caos_errs.ThrowInvalidArgument(nil, "PHONE-Zt0NV", "Errors.User.Phone.Empty")
|
|
|
|
}
|
|
|
|
phoneNr, err := libphonenumber.Parse(string(p), defaultRegion)
|
|
|
|
if err != nil {
|
|
|
|
return p, caos_errs.ThrowInvalidArgument(err, "PHONE-so0wa", "Errors.User.Phone.Invalid")
|
|
|
|
}
|
|
|
|
return PhoneNumber(libphonenumber.Format(phoneNr, libphonenumber.E164)), nil
|
|
|
|
}
|
2021-01-05 08:33:45 +00:00
|
|
|
|
|
|
|
type Phone struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
|
2023-03-14 19:20:38 +00:00
|
|
|
PhoneNumber PhoneNumber
|
2021-01-05 08:33:45 +00:00
|
|
|
IsPhoneVerified bool
|
2023-08-03 04:42:59 +00:00
|
|
|
// PlainCode is set by the command and can be used to return it to the caller (API)
|
|
|
|
PlainCode *string
|
2021-01-05 08:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PhoneCode struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
|
|
|
|
Code *crypto.CryptoValue
|
|
|
|
Expiry time.Duration
|
|
|
|
}
|
|
|
|
|
2023-03-14 19:20:38 +00:00
|
|
|
func (p *Phone) Normalize() error {
|
|
|
|
if p == nil {
|
|
|
|
return caos_errs.ThrowInvalidArgument(nil, "PHONE-YlbwO", "Errors.User.Phone.Empty")
|
|
|
|
}
|
|
|
|
normalizedNumber, err := p.PhoneNumber.Normalize()
|
2021-01-05 08:33:45 +00:00
|
|
|
if err != nil {
|
2023-03-14 19:20:38 +00:00
|
|
|
return err
|
2021-01-05 08:33:45 +00:00
|
|
|
}
|
2023-03-14 19:20:38 +00:00
|
|
|
// Issue for avoiding mutating state: https://github.com/zitadel/zitadel/issues/5412
|
|
|
|
p.PhoneNumber = normalizedNumber
|
2021-01-05 08:33:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-06 10:12:56 +00:00
|
|
|
|
|
|
|
func NewPhoneCode(phoneGenerator crypto.Generator) (*PhoneCode, error) {
|
|
|
|
phoneCodeCrypto, _, err := crypto.NewCode(phoneGenerator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &PhoneCode{
|
|
|
|
Code: phoneCodeCrypto,
|
|
|
|
Expiry: phoneGenerator.Expiry(),
|
|
|
|
}, nil
|
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
|
|
|
|
type PhoneState int32
|
|
|
|
|
|
|
|
const (
|
|
|
|
PhoneStateUnspecified PhoneState = iota
|
|
|
|
PhoneStateActive
|
|
|
|
PhoneStateRemoved
|
|
|
|
|
|
|
|
phoneStateCount
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s PhoneState) Valid() bool {
|
|
|
|
return s >= 0 && s < phoneStateCount
|
|
|
|
}
|
2021-03-01 07:48:50 +00:00
|
|
|
|
|
|
|
func (s PhoneState) Exists() bool {
|
|
|
|
return s == PhoneStateActive
|
|
|
|
}
|