2021-01-05 08:33:45 +00:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
2021-03-19 10:12:56 +00:00
|
|
|
"regexp"
|
2021-01-05 08:33:45 +00:00
|
|
|
"time"
|
2022-03-28 08:05:09 +00:00
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/v1/models"
|
2021-01-05 08:33:45 +00:00
|
|
|
)
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
var (
|
|
|
|
EmailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
|
|
|
|
)
|
2021-03-19 10:12:56 +00:00
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
type Email struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
|
|
|
|
EmailAddress string
|
|
|
|
IsEmailVerified bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type EmailCode struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
|
|
|
|
Code *crypto.CryptoValue
|
|
|
|
Expiry time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Email) IsValid() bool {
|
2022-03-28 08:05:09 +00:00
|
|
|
return e.EmailAddress != "" && EmailRegex.MatchString(e.EmailAddress)
|
2021-01-05 08:33:45 +00:00
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
|
|
|
|
func NewEmailCode(emailGenerator crypto.Generator) (*EmailCode, error) {
|
|
|
|
emailCodeCrypto, _, err := crypto.NewCode(emailGenerator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &EmailCode{
|
|
|
|
Code: emailCodeCrypto,
|
|
|
|
Expiry: emailGenerator.Expiry(),
|
|
|
|
}, nil
|
|
|
|
}
|