Passport/pkg/internal/services/tokens.go

130 lines
3.8 KiB
Go
Raw Normal View History

2024-01-28 16:32:39 +00:00
package services
import (
"context"
2024-03-16 07:40:27 +00:00
"fmt"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
2024-03-16 07:40:27 +00:00
"strings"
"time"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/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`
2024-06-30 09:01:39 +00:00
const ResetPasswordTemplate = `Dear %s,
We received a request to reset the password for your account at %s. If you did not request a password reset, please ignore this email.
To confirm your password reset request and create a new password, please use the link below:
%s
This link will expire in 24 hours. If you do not reset your password within this time frame, you will need to submit a new password reset request.
If you have any questions or need further assistance, please do not hesitate to contact our support team.
Best regards,
%s`
2024-01-29 08:11:59 +00:00
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,
2024-06-26 07:52:58 +00:00
AccountID: &uid,
2024-01-28 16:32:39 +00:00
ExpiredAt: expiredAt,
}
if err := database.C.Save(&token).Error; err != nil {
return token, err
} else {
return token, nil
}
}
func NotifyMagicToken(token models.MagicToken) error {
2024-06-26 07:52:58 +00:00
if token.AccountID == nil {
2024-01-28 16:32:39 +00:00
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{
2024-06-26 07:52:58 +00:00
BaseModel: models.BaseModel{ID: *token.AccountID},
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-06-30 09:01:39 +00:00
link := fmt.Sprintf("https://%s/flow/confirm?code=%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-06-30 09:01:39 +00:00
case models.ResetPasswordMagicToken:
link := fmt.Sprintf("https://%s/flow/password-reset?code=%s", viper.GetString("domain"), token.Code)
subject = fmt.Sprintf("[%s] Reset your password", viper.GetString("name"))
content = fmt.Sprintf(
ResetPasswordTemplate,
user.Name,
viper.GetString("name"),
link,
viper.GetString("maintainer"),
)
2024-01-28 16:32:39 +00:00
default:
return fmt.Errorf("unsupported magic token type to notify")
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := proto.NewPostmanClient(gap.H.GetDealerGrpcConn()).DeliverEmail(ctx, &proto.DeliverEmailRequest{
To: user.GetPrimaryEmail().Content,
Email: &proto.EmailRequest{
Subject: subject,
TextBody: &content,
},
})
return err
2024-01-28 16:32:39 +00:00
}