Skip to content

Commit

Permalink
邮箱正则判断
Browse files Browse the repository at this point in the history
  • Loading branch information
xmdhs committed Nov 25, 2023
1 parent 2e4b8cb commit e1517d4
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 16 deletions.
8 changes: 7 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ type EmailConfig struct {
Enable bool `toml:"enable" comment:"注册验证邮件,且允许使用邮箱找回账号"`
Smtp []SmtpUser `toml:"smtp"`
AllowDomain []string `toml:"allow_domain" comment:"允许用于注册的邮箱域名,留空则允许全部"`
EmailReg string `toml:"email_reg" comment:"邮箱正则,留空则不处理,如 ^[0-9][email protected]$|^[^+\\.A-Z][email protected]$"`
EmailRegMsg string `toml:"email_reg_msg" comment:"不满足要求时的提示信息"`
}

type SmtpUser struct {
Host string `toml:"host"`
Port int `toml:"port"`
SSL bool `toml:"SSL"`
SSL bool `toml:"SSL" comment:"启用 ssl"`
Name string `toml:"name"`
Pass string `toml:"password"`
}
Expand Down Expand Up @@ -82,5 +84,9 @@ func Default() Config {
WebBaseUrl: "",
ServerName: "没有设置名字",
Captcha: Captcha{},
Email: EmailConfig{
Smtp: []SmtpUser{},
AllowDomain: []string{},
},
}
}
2 changes: 2 additions & 0 deletions frontend/src/apis/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export interface ApiConfig {
serverName: string
NeedEmail: boolean
AllowDomain: string[]
EmailReg: string
EmailRegMsg: string
}

export interface UserInfo {
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/views/SendEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ export default function SendEmail({ title, anyEmail = false, sendService }: { ti
setHelperText("邮箱格式错误")
return
}
if (!anyEmail && server.data?.EmailReg && server.data?.EmailReg != ""
&& !new RegExp(server.data?.EmailReg).test(sendEmail)) {
setHelperText(server.data?.EmailRegMsg ?? "邮箱不满足正则要求")
return
}

if (server.data?.captcha.type != "" && captchaToken == "") {
return
Expand Down
39 changes: 39 additions & 0 deletions handle/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package handle
import (
"bytes"
"context"
"errors"
"fmt"
"image/png"
"io"
"log/slog"
"net/http"
"regexp"
"strings"
"sync"

"github.com/go-chi/chi/v5"
"github.com/go-playground/validator/v10"
Expand All @@ -25,17 +29,25 @@ type UserHandel struct {
logger *slog.Logger
textureService *service.TextureService
config config.Config

emailReg func() (*regexp.Regexp, error)
}

func NewUserHandel(handleError *handelerror.HandleError, validate *validator.Validate,
userService *service.UserService, logger *slog.Logger, textureService *service.TextureService, config config.Config) *UserHandel {
emailReg := sync.OnceValues[*regexp.Regexp, error](func() (*regexp.Regexp, error) {
return regexp.Compile(config.Email.EmailReg)
})

return &UserHandel{
handleError: handleError,
validate: validate,
userService: userService,
logger: logger,
textureService: textureService,
config: config,

emailReg: emailReg,
}
}

Expand Down Expand Up @@ -227,6 +239,8 @@ func (h *UserHandel) NeedEnableEmail(handle http.Handler) http.Handler {
})
}

var ErrNotAllowDomain = errors.New("不在允许域名列表内")

func (h *UserHandel) SendRegEmail() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
Expand All @@ -235,6 +249,31 @@ func (h *UserHandel) SendRegEmail() http.HandlerFunc {
return
}

if len(h.config.Email.AllowDomain) != 0 {
allow := false
for _, v := range h.config.Email.AllowDomain {
if strings.HasSuffix(c.Email, v) {
allow = true
break
}
}
if !allow {
h.handleError.Error(ctx, w, "不在允许邮箱域名内", model.ErrInput, 400, slog.LevelDebug)
return
}
}
if h.config.Email.EmailReg != "" {
r, err := h.emailReg()
if err != nil {
h.handleError.Error(ctx, w, "正则错误", model.ErrUnknown, 500, slog.LevelError)
return
}
if !r.MatchString(c.Email) {
h.handleError.Error(ctx, w, "邮箱不符合正则要求", model.ErrInput, 400, slog.LevelDebug)
return
}
}

err := h.userService.SendRegEmail(ctx, c.Email, c.CaptchaToken, r.Host, ip)
if err != nil {
h.handleError.Service(ctx, w, err)
Expand Down
2 changes: 2 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ type Config struct {
ServerName string `json:"serverName"`
NeedEmail bool
AllowDomain []string
EmailReg string
EmailRegMsg string
}

type EditUser struct {
Expand Down
15 changes: 0 additions & 15 deletions service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,22 +238,7 @@ func (w *UserService) ChangeName(ctx context.Context, newName string, t *model.T
return nil
}

var ErrNotAllowDomain = errors.New("不在允许域名列表内")

func (w *UserService) SendRegEmail(ctx context.Context, email, CaptchaToken, host, ip string) error {
if len(w.config.Email.AllowDomain) != 0 {
allow := false
for _, v := range w.config.Email.AllowDomain {
if strings.HasSuffix(email, v) {
allow = true
break
}
}
if !allow {
return fmt.Errorf("SendRegEmail: %w", ErrNotAllowDomain)
}
}

err := w.captchaService.VerifyCaptcha(ctx, CaptchaToken, ip)
if err != nil {
return fmt.Errorf("SendRegEmail: %w", err)
Expand Down
2 changes: 2 additions & 0 deletions service/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ func (w *WebService) GetConfig(ctx context.Context) model.Config {
AllowChangeName: !w.config.OfflineUUID,
NeedEmail: w.config.Email.Enable,
AllowDomain: w.config.Email.AllowDomain,
EmailReg: w.config.Email.EmailReg,
EmailRegMsg: w.config.Email.EmailRegMsg,
}
}

0 comments on commit e1517d4

Please sign in to comment.