2021-01-05 08:33:45 +00:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
2021-02-23 14:13:04 +00:00
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/v1/models"
|
2021-03-19 10:12:56 +00:00
|
|
|
"regexp"
|
2021-01-05 08:33:45 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-03-19 10:12:56 +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-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 {
|
2021-03-19 10:12:56 +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
|
|
|
|
}
|