2021-01-05 08:33:45 +00:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
2023-04-25 07:02:29 +00:00
|
|
|
"io"
|
2021-03-19 10:12:56 +00:00
|
|
|
"regexp"
|
2023-03-14 19:20:38 +00:00
|
|
|
"strings"
|
2021-01-05 08:33:45 +00:00
|
|
|
"time"
|
2022-03-28 08:05:09 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
2023-03-14 19:20:38 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
2022-04-26 23:01:45 +00:00
|
|
|
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
|
|
|
var (
|
2023-03-14 19:20:38 +00:00
|
|
|
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])?)*$")
|
2022-04-12 14:20:17 +00:00
|
|
|
)
|
2021-03-19 10:12:56 +00:00
|
|
|
|
2023-03-14 19:20:38 +00:00
|
|
|
type EmailAddress string
|
|
|
|
|
|
|
|
func (e EmailAddress) Validate() error {
|
|
|
|
if e == "" {
|
|
|
|
return errors.ThrowInvalidArgument(nil, "EMAIL-spblu", "Errors.User.Email.Empty")
|
|
|
|
}
|
|
|
|
if !emailRegex.MatchString(string(e)) {
|
|
|
|
return errors.ThrowInvalidArgument(nil, "EMAIL-599BI", "Errors.User.Email.Invalid")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e EmailAddress) Normalize() EmailAddress {
|
|
|
|
return EmailAddress(strings.TrimSpace(string(e)))
|
|
|
|
}
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
type Email struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
|
2023-03-14 19:20:38 +00:00
|
|
|
EmailAddress EmailAddress
|
2021-01-05 08:33:45 +00:00
|
|
|
IsEmailVerified bool
|
2023-04-25 07:02:29 +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 EmailCode struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
|
|
|
|
Code *crypto.CryptoValue
|
|
|
|
Expiry time.Duration
|
|
|
|
}
|
|
|
|
|
2023-03-14 19:20:38 +00:00
|
|
|
func (e *Email) Validate() error {
|
|
|
|
if e == nil {
|
|
|
|
return errors.ThrowInvalidArgument(nil, "EMAIL-spblu", "Errors.User.Email.Empty")
|
|
|
|
}
|
|
|
|
return e.EmailAddress.Validate()
|
2021-01-05 08:33:45 +00:00
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
|
2023-04-25 07:02:29 +00:00
|
|
|
func NewEmailCode(emailGenerator crypto.Generator) (*EmailCode, string, error) {
|
|
|
|
emailCodeCrypto, code, err := crypto.NewCode(emailGenerator)
|
2021-01-07 15:06:45 +00:00
|
|
|
if err != nil {
|
2023-04-25 07:02:29 +00:00
|
|
|
return nil, "", err
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
return &EmailCode{
|
|
|
|
Code: emailCodeCrypto,
|
|
|
|
Expiry: emailGenerator.Expiry(),
|
2023-04-25 07:02:29 +00:00
|
|
|
}, code, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConfirmURLData struct {
|
|
|
|
UserID string
|
|
|
|
Code string
|
|
|
|
OrgID string
|
|
|
|
}
|
|
|
|
|
2023-06-08 07:39:11 +00:00
|
|
|
// RenderConfirmURLTemplate parses and renders tmpl.
|
2023-04-25 07:02:29 +00:00
|
|
|
// userID, code and orgID are passed into the [ConfirmURLData].
|
2023-06-08 07:39:11 +00:00
|
|
|
func RenderConfirmURLTemplate(w io.Writer, tmpl, userID, code, orgID string) error {
|
|
|
|
return renderURLTemplate(w, tmpl, &ConfirmURLData{userID, code, orgID})
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|