From dc21c51a0c3a652dce475fe3c2b187b907b9f7e1 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Mon, 4 Sep 2023 16:23:11 +0800 Subject: [PATCH] fix --- fs_template/email_register.tpl | 2 +- fs_template/reset_password.tpl | 16 ++++++ server/auth/internal/logic/email_manager.go | 57 +++++++++++++++++-- .../internal/logic/useremailregisterlogic.go | 1 + .../auth/internal/logic/userregisterlogic.go | 1 + server/auth/internal/types/types.go | 3 +- server/info/internal/logic/infologic.go | 2 +- server_api/auth.api | 19 ++++--- 8 files changed, 84 insertions(+), 17 deletions(-) create mode 100644 fs_template/reset_password.tpl diff --git a/fs_template/email_register.tpl b/fs_template/email_register.tpl index 3565d19e..c5b8cb2a 100644 --- a/fs_template/email_register.tpl +++ b/fs_template/email_register.tpl @@ -6,7 +6,7 @@ Thank you for registering an account with {{ .CompanyName }}. We are thrilled to Please take a moment to confirm your email address by clicking the button below so we can activate your account: -Confirm Account: .UserName +Confirm Account: {{ .UserName }} Confirming your email ensures your account is properly secured. This also lets you access member-only tools and content. diff --git a/fs_template/reset_password.tpl b/fs_template/reset_password.tpl new file mode 100644 index 00000000..828226c6 --- /dev/null +++ b/fs_template/reset_password.tpl @@ -0,0 +1,16 @@ +Dear {{ .UserName }}, + +We have received your request to reset your {{ .CompanyName }} account password. + +Please click the button below to confirm your new password: + +

{{ .MaskedPassword }}

(This can be replaced by the masked new password) + +Confirm New Password + +This password reset confirmation link will expire in 60 minutes. Please let us know if you have any other questions! + +Regards, +{{ .SenderName }} +{{ .SenderTitle }} +{{ .CompanyName }} \ No newline at end of file diff --git a/server/auth/internal/logic/email_manager.go b/server/auth/internal/logic/email_manager.go index 24a1c16a..8931ef6a 100644 --- a/server/auth/internal/logic/email_manager.go +++ b/server/auth/internal/logic/email_manager.go @@ -2,6 +2,7 @@ package logic import ( "bytes" + "fmt" "fusenapi/utils/check" "fusenapi/utils/fstpl" "log" @@ -52,6 +53,7 @@ func init() { } type EmailFormat struct { + TemplateName string // 模板名字 UniqueKey string // 用于处理唯一的任务,重发都会被利用到 TargetEmail string // 发送的目标email CompanyName string // fs公司名 @@ -61,6 +63,48 @@ type EmailFormat struct { Extend map[string]string } +func (eformat *EmailFormat) Vaild() error { + + // 1. 检查模板名称 + if tpls.Lookup(eformat.TemplateName) == nil { + return fmt.Errorf("%s template name is not found", eformat.TemplateName) + } + + // 2. 检查公司名称 + if eformat.CompanyName == "" { + return fmt.Errorf("company name cannot be empty") + } + + // 3. 检查确认链接 + if eformat.ConfirmationLink == "" { + return fmt.Errorf("confirmation link cannot be empty") + } + + // 4. 检查发送人名称 + if eformat.SenderName == "" { + return fmt.Errorf("sender name cannot be empty") + } + + // 5. 检查发送人头衔 + if eformat.SenderTitle == "" { + return fmt.Errorf("sender title cannot be empty") + } + + // 6. 检查目标邮箱 + if eformat.TargetEmail == "" { + return fmt.Errorf("target email cannot be empty") + } + + // 7. 检查唯一键 + if eformat.UniqueKey == "" { + return fmt.Errorf("unique key cannot be empty") + } + + // 所有校验通过 + return nil + +} + // EmailSender type EmailSender struct { lock sync.Mutex @@ -87,8 +131,9 @@ func (m *EmailSender) ProcessEmailTasks() { break } - if emailformat.UniqueKey == "" { - logx.Error("email UniqueKey must be exists") + err := emailformat.Vaild() + if err != nil { + logx.Error(err) continue } @@ -111,7 +156,9 @@ func (m *EmailSender) ProcessEmailTasks() { go func() { defer func() { <-m.semaphore }() // Release a token - content := RenderEmailRegisterTemplate(emailformat.CompanyName, + content := RenderEmailTemplate( + emailformat.TemplateName, + emailformat.CompanyName, emailformat.ConfirmationLink, emailformat.SenderName, emailformat.SenderTitle, @@ -162,7 +209,7 @@ func (m *EmailSender) ClearExpiredTasks() { } } -func RenderEmailRegisterTemplate(companyName, confirmationLink, senderName, senderTitle string, extend map[string]string) []byte { +func RenderEmailTemplate(templateName, companyName, confirmationLink, senderName, senderTitle string, extend map[string]string) []byte { data := map[string]string{ "CompanyName": companyName, @@ -176,7 +223,7 @@ func RenderEmailRegisterTemplate(companyName, confirmationLink, senderName, send } var result bytes.Buffer - err := tpls.ExecuteTemplate(&result, "email_register.tpl", data) + err := tpls.ExecuteTemplate(&result, templateName, data) if err != nil { log.Fatal(err) } diff --git a/server/auth/internal/logic/useremailregisterlogic.go b/server/auth/internal/logic/useremailregisterlogic.go index be88c9b4..9511b9ca 100644 --- a/server/auth/internal/logic/useremailregisterlogic.go +++ b/server/auth/internal/logic/useremailregisterlogic.go @@ -75,6 +75,7 @@ func (l *UserEmailRegisterLogic) UserEmailRegister(req *types.RequestEmailRegist userName := token.Extend["first_name"].(string) + " " + token.Extend["last_name"].(string) // 进入发送邮箱的系统 EmailManager.EmailTasks <- &EmailFormat{ + TemplateName: "email_register.tpl", UniqueKey: "register-" + req.Email, TargetEmail: req.Email, CompanyName: "fusen", diff --git a/server/auth/internal/logic/userregisterlogic.go b/server/auth/internal/logic/userregisterlogic.go index 6d621e48..83e0c5bb 100644 --- a/server/auth/internal/logic/userregisterlogic.go +++ b/server/auth/internal/logic/userregisterlogic.go @@ -74,6 +74,7 @@ func (l *UserRegisterLogic) UserRegister(req *types.RequestUserRegister, userinf // 进入发送邮箱的系统 EmailManager.EmailTasks <- &EmailFormat{ + TemplateName: "email_register.tpl", UniqueKey: "register-" + req.Email, TargetEmail: req.Email, CompanyName: "fusen", diff --git a/server/auth/internal/types/types.go b/server/auth/internal/types/types.go index 4fda1d3d..44ab1232 100644 --- a/server/auth/internal/types/types.go +++ b/server/auth/internal/types/types.go @@ -28,7 +28,8 @@ type RequestUserRegister struct { } type RequestUserResetToken struct { - Wid string `json:"wid"` + Wid string `json:"wid"` + Email string `json:"email"` // email } type DataResetToken struct { diff --git a/server/info/internal/logic/infologic.go b/server/info/internal/logic/infologic.go index 582fc116..689e52de 100644 --- a/server/info/internal/logic/infologic.go +++ b/server/info/internal/logic/infologic.go @@ -76,7 +76,7 @@ func (mquery *ModuleQuery) EncodeEmpty() map[string]any { query = "." + query } k := fmt.Sprintf("%s%s", mquery.ModuleName, query) - qstr[k] = map[string]any{} + qstr[k] = nil } return qstr diff --git a/server_api/auth.api b/server_api/auth.api index 3e0279a9..5c8c7f90 100644 --- a/server_api/auth.api +++ b/server_api/auth.api @@ -12,28 +12,28 @@ import "basic.api" service auth { @handler UserLoginHandler post /api/auth/login(RequestUserLogin) returns (response); - + @handler UserRegisterHandler post /api/auth/register(RequestUserRegister) returns (response); - + @handler AcceptCookieHandler post /api/auth/accept-cookie(request) returns (response); - + @handler UserGoogleLoginHandler get /api/auth/oauth2/login/google(RequestGoogleLogin) returns (response); - + @handler UserEmailConfirmationHandler get /api/auth/email/confirmation(RequestEmailConfirmation) returns (response); - + @handler UserEmailRegisterHandler post /api/auth/oauth2/register(RequestEmailRegister) returns (response); - + @handler UserResetTokenHandler post /api/auth/reset/token(RequestUserResetToken) returns (response); - + @handler UserResetPasswordHandler post /api/auth/reset/password(RequestUserResetPassword) returns (response); - + @handler DebugAuthDeleteHandler post /api/auth/debug/delete(RequestAuthDelete) returns (response); } @@ -79,7 +79,8 @@ type ( // RequestUserResetToken 请求重置token, 一定不为null RequestUserResetToken { - Wid string `json:"wid"` + Wid string `json:"wid"` + Email string `json:"email"` // email } // UserResetTokenHandler 返回重置token