190 lines
5.3 KiB
Go
190 lines
5.3 KiB
Go
package services
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"math/rand"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"server/models"
|
|
"server/pkg/passwordutil"
|
|
)
|
|
|
|
var emailRegexp = regexp.MustCompile(`^[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}$`)
|
|
|
|
func IsEmail(email string) bool {
|
|
return emailRegexp.MatchString(strings.TrimSpace(email))
|
|
}
|
|
|
|
type identityEmailCodeItem struct {
|
|
Code string
|
|
ExpiredAt time.Time
|
|
IssuedAt time.Time
|
|
}
|
|
|
|
var identityEmailCodeStore sync.Map
|
|
|
|
func identityEmailCodeKey(purpose, email string) string {
|
|
return strings.TrimSpace(purpose) + "|" + strings.ToLower(strings.TrimSpace(email))
|
|
}
|
|
|
|
func findIdentityByEmail(email string) *models.AuthIdentity {
|
|
email = strings.TrimSpace(email)
|
|
if !IsEmail(email) {
|
|
return nil
|
|
}
|
|
var m models.AuthIdentity
|
|
if err := models.Orm.QueryTable(new(models.AuthIdentity)).
|
|
Filter("email", email).
|
|
Filter("delete_time__isnull", true).
|
|
One(&m); err != nil {
|
|
return nil
|
|
}
|
|
return &m
|
|
}
|
|
|
|
func defaultEmailSenderConfig() (SMTPConfig, error) {
|
|
emails, err := ListSystemEmails()
|
|
if err != nil || len(emails) == 0 {
|
|
return SMTPConfig{}, errors.New("邮件服务未配置,请联系平台管理员")
|
|
}
|
|
cfg := emails[0]
|
|
if strings.TrimSpace(cfg.Host) == "" || strings.TrimSpace(cfg.FromAddress) == "" {
|
|
return SMTPConfig{}, errors.New("邮件服务未配置,请联系平台管理员")
|
|
}
|
|
if cfg.Status != 1 {
|
|
return SMTPConfig{}, errors.New("邮件服务未启用,请联系平台管理员")
|
|
}
|
|
out := SMTPConfig{
|
|
FromAddress: cfg.FromAddress,
|
|
Host: cfg.Host,
|
|
Port: cfg.Port,
|
|
Password: cfg.Password,
|
|
Encryption: cfg.Encryption,
|
|
Timeout: cfg.Timeout,
|
|
}
|
|
if cfg.FromName != nil {
|
|
out.FromName = *cfg.FromName
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func SendIdentityEmailCode(purpose, email string) error {
|
|
email = strings.TrimSpace(email)
|
|
if !IsEmail(email) {
|
|
return errors.New("请输入正确的邮箱地址")
|
|
}
|
|
if purpose != "reset" {
|
|
return errors.New("参数错误")
|
|
}
|
|
if findIdentityByEmail(email) == nil {
|
|
return errors.New("该邮箱尚未绑定账号")
|
|
}
|
|
|
|
key := identityEmailCodeKey(purpose, email)
|
|
if v, ok := identityEmailCodeStore.Load(key); ok {
|
|
if item, ok := v.(identityEmailCodeItem); ok && time.Since(item.IssuedAt) < 60*time.Second {
|
|
return errors.New("验证码发送过于频繁,请稍后再试")
|
|
}
|
|
}
|
|
|
|
cfg, err := defaultEmailSenderConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
code := fmt.Sprintf("%06d", rand.Intn(1000000)) //nolint:gosec // 验证码场景可接受
|
|
html := fmt.Sprintf(`
|
|
<div style="font-family: Arial, sans-serif; padding: 22px; border: 1px solid #eee; border-radius: 6px; max-width: 520px;">
|
|
<h3 style="color: #2563eb; margin: 0 0 16px;">找回密码验证码</h3>
|
|
<p style="font-size: 15px; color: #333; margin: 0 0 6px;">您的验证码是:</p>
|
|
<p style="font-size: 30px; font-weight: 700; letter-spacing: 6px; color: #2563eb; margin: 0 0 18px;">%s</p>
|
|
<p style="font-size: 13px; color: #666; margin: 0;">验证码 5 分钟内有效。为保护账号安全,请勿转发给他人。</p>
|
|
</div>
|
|
`, code)
|
|
if err := SendHTMLEmailSMTP(cfg, email, "找回密码验证码", html); err != nil {
|
|
return errors.New("邮件发送失败,请稍后重试")
|
|
}
|
|
|
|
now := time.Now()
|
|
identityEmailCodeStore.Store(key, identityEmailCodeItem{
|
|
Code: code,
|
|
ExpiredAt: now.Add(5 * time.Minute),
|
|
IssuedAt: now,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func VerifyIdentityEmailCode(purpose, email, code string) error {
|
|
email = strings.TrimSpace(email)
|
|
code = strings.TrimSpace(code)
|
|
if code == "" {
|
|
return errors.New("请输入验证码")
|
|
}
|
|
key := identityEmailCodeKey(purpose, email)
|
|
v, ok := identityEmailCodeStore.Load(key)
|
|
if !ok {
|
|
return errors.New("请先获取验证码")
|
|
}
|
|
item, ok := v.(identityEmailCodeItem)
|
|
if !ok {
|
|
return errors.New("验证码状态异常,请重新获取")
|
|
}
|
|
if time.Now().After(item.ExpiredAt) {
|
|
identityEmailCodeStore.Delete(key)
|
|
return errors.New("验证码已过期,请重新获取")
|
|
}
|
|
if item.Code != code {
|
|
return errors.New("验证码错误")
|
|
}
|
|
identityEmailCodeStore.Delete(key)
|
|
return nil
|
|
}
|
|
|
|
func applyIdentityPassword(identity *models.AuthIdentity, newPassword string) error {
|
|
if identity == nil {
|
|
return errors.New("账号不存在")
|
|
}
|
|
hashed, err := passwordutil.Hash(strings.TrimSpace(newPassword))
|
|
if err != nil {
|
|
return errors.New("密码处理失败")
|
|
}
|
|
_, err = models.Orm.QueryTable(new(models.AuthIdentity)).
|
|
Filter("id", identity.ID).
|
|
Update(map[string]interface{}{
|
|
"password_hash": hashed,
|
|
"password_algo": passwordutil.AlgoOf(hashed),
|
|
"fail_count": 0,
|
|
})
|
|
if err != nil {
|
|
return errors.New("密码重置失败,请稍后重试")
|
|
}
|
|
mirrorPasswordToTenantRows(identity.ID, hashed)
|
|
return nil
|
|
}
|
|
|
|
func ResetIdentityPasswordByEmail(email, code, newPassword, confirm string) error {
|
|
email = strings.TrimSpace(email)
|
|
if !IsEmail(email) {
|
|
return errors.New("请输入正确的邮箱地址")
|
|
}
|
|
if err := VerifyIdentityEmailCode("reset", email, code); err != nil {
|
|
return err
|
|
}
|
|
if len(strings.TrimSpace(newPassword)) < 6 {
|
|
return errors.New("密码长度不能少于6个字符")
|
|
}
|
|
if newPassword != confirm {
|
|
return errors.New("两次输入的密码不一致")
|
|
}
|
|
|
|
identity := findIdentityByEmail(email)
|
|
if identity == nil {
|
|
return errors.New("该邮箱尚未绑定账号")
|
|
}
|
|
return applyIdentityPassword(identity, newPassword)
|
|
}
|