mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-15 04:18:01 +00:00
f680dd934d
* chore: rename package errors to zerrors * rename package errors to gerrors * fix error related linting issues * fix zitadel error assertion * fix gosimple linting issues * fix deprecated linting issues * resolve gci linting issues * fix import structure --------- Co-authored-by: Elio Bischof <elio@zitadel.com>
85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
package domain
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/ttacon/libphonenumber"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
const defaultRegion = "CH"
|
|
|
|
type PhoneNumber string
|
|
|
|
func (p PhoneNumber) Normalize() (PhoneNumber, error) {
|
|
if p == "" {
|
|
return p, zerrors.ThrowInvalidArgument(nil, "PHONE-Zt0NV", "Errors.User.Phone.Empty")
|
|
}
|
|
phoneNr, err := libphonenumber.Parse(string(p), defaultRegion)
|
|
if err != nil {
|
|
return p, zerrors.ThrowInvalidArgument(err, "PHONE-so0wa", "Errors.User.Phone.Invalid")
|
|
}
|
|
return PhoneNumber(libphonenumber.Format(phoneNr, libphonenumber.E164)), nil
|
|
}
|
|
|
|
type Phone struct {
|
|
es_models.ObjectRoot
|
|
|
|
PhoneNumber PhoneNumber
|
|
IsPhoneVerified bool
|
|
// PlainCode is set by the command and can be used to return it to the caller (API)
|
|
PlainCode *string
|
|
}
|
|
|
|
type PhoneCode struct {
|
|
es_models.ObjectRoot
|
|
|
|
Code *crypto.CryptoValue
|
|
Expiry time.Duration
|
|
}
|
|
|
|
func (p *Phone) Normalize() error {
|
|
if p == nil {
|
|
return zerrors.ThrowInvalidArgument(nil, "PHONE-YlbwO", "Errors.User.Phone.Empty")
|
|
}
|
|
normalizedNumber, err := p.PhoneNumber.Normalize()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Issue for avoiding mutating state: https://github.com/zitadel/zitadel/issues/5412
|
|
p.PhoneNumber = normalizedNumber
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
type PhoneState int32
|
|
|
|
const (
|
|
PhoneStateUnspecified PhoneState = iota
|
|
PhoneStateActive
|
|
PhoneStateRemoved
|
|
|
|
phoneStateCount
|
|
)
|
|
|
|
func (s PhoneState) Valid() bool {
|
|
return s >= 0 && s < phoneStateCount
|
|
}
|
|
|
|
func (s PhoneState) Exists() bool {
|
|
return s == PhoneStateActive
|
|
}
|