2024-01-28 16:32:39 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-03-16 07:40:27 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2024-02-18 03:20:22 +00:00
|
|
|
"code.smartsheep.studio/hydrogen/identity/pkg/database"
|
|
|
|
"code.smartsheep.studio/hydrogen/identity/pkg/models"
|
2024-01-28 16:32:39 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2024-01-29 08:11:59 +00:00
|
|
|
const ConfirmRegistrationTemplate = `Dear %s,
|
|
|
|
|
|
|
|
Thank you for choosing to register with %s. We are excited to welcome you to our community and appreciate your trust in us.
|
|
|
|
|
|
|
|
Your registration details have been successfully received, and you are now a valued member of %s. Here are the confirm link of your registration:
|
|
|
|
|
|
|
|
%s
|
|
|
|
|
|
|
|
As a confirmed registered member, you will have access to all our services.
|
|
|
|
We encourage you to explore our services and take full advantage of the resources available to you.
|
|
|
|
|
|
|
|
Once again, thank you for choosing us. We look forward to serving you and hope you have a positive experience with us.
|
|
|
|
|
|
|
|
Best regards,
|
|
|
|
%s`
|
|
|
|
|
|
|
|
func ValidateMagicToken(code string, mode models.MagicTokenType) (models.MagicToken, error) {
|
|
|
|
var tk models.MagicToken
|
|
|
|
if err := database.C.Where(models.MagicToken{Code: code, Type: mode}).First(&tk).Error; err != nil {
|
|
|
|
return tk, err
|
|
|
|
} else if tk.ExpiredAt != nil && time.Now().Unix() >= tk.ExpiredAt.Unix() {
|
|
|
|
return tk, fmt.Errorf("token has been expired")
|
|
|
|
}
|
|
|
|
|
|
|
|
return tk, nil
|
|
|
|
}
|
|
|
|
|
2024-01-28 16:32:39 +00:00
|
|
|
func NewMagicToken(mode models.MagicTokenType, assignTo *models.Account, expiredAt *time.Time) (models.MagicToken, error) {
|
|
|
|
var uid uint
|
|
|
|
if assignTo != nil {
|
|
|
|
uid = assignTo.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
token := models.MagicToken{
|
|
|
|
Code: strings.Replace(uuid.NewString(), "-", "", -1),
|
|
|
|
Type: mode,
|
|
|
|
AssignTo: &uid,
|
|
|
|
ExpiredAt: expiredAt,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := database.C.Save(&token).Error; err != nil {
|
|
|
|
return token, err
|
|
|
|
} else {
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NotifyMagicToken(token models.MagicToken) error {
|
|
|
|
if token.AssignTo == nil {
|
|
|
|
return fmt.Errorf("could notify a non-assign magic token")
|
|
|
|
}
|
|
|
|
|
|
|
|
var user models.Account
|
2024-01-29 08:11:59 +00:00
|
|
|
if err := database.C.Where(&models.Account{
|
|
|
|
BaseModel: models.BaseModel{ID: *token.AssignTo},
|
2024-01-28 16:32:39 +00:00
|
|
|
}).Preload("Contacts").First(&user).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var subject string
|
|
|
|
var content string
|
|
|
|
switch token.Type {
|
|
|
|
case models.ConfirmMagicToken:
|
2024-03-16 07:40:27 +00:00
|
|
|
link := fmt.Sprintf("https://%s/me/confirm?tk=%s", viper.GetString("domain"), token.Code)
|
2024-01-28 16:32:39 +00:00
|
|
|
subject = fmt.Sprintf("[%s] Confirm your registration", viper.GetString("name"))
|
2024-01-29 08:11:59 +00:00
|
|
|
content = fmt.Sprintf(
|
|
|
|
ConfirmRegistrationTemplate,
|
|
|
|
user.Name,
|
|
|
|
viper.GetString("name"),
|
|
|
|
viper.GetString("maintainer"),
|
|
|
|
link,
|
|
|
|
viper.GetString("maintainer"),
|
|
|
|
)
|
2024-01-28 16:32:39 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported magic token type to notify")
|
|
|
|
}
|
|
|
|
|
|
|
|
return SendMail(user.GetPrimaryEmail().Content, subject, content)
|
|
|
|
}
|