移动端和 PC 端登录页都加了“图形验证码
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
package captcha
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
"math/big"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrDependencyUnavailable = errors.New("captcha dependency unavailable")
|
||||
ErrInvalid = errors.New("captcha invalid")
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
CaptchaID string `json:"captcha_id"`
|
||||
Image string `json:"image"`
|
||||
ExpiresIn int64 `json:"expires_in"`
|
||||
}
|
||||
|
||||
func Generate(ctx context.Context, redisClient *redis.Client, namespace string, ttl time.Duration) (*Item, error) {
|
||||
if redisClient == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
captchaID, err := randomToken(16)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
code, err := randomCaptchaCode(4)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := redisClient.Set(ctx, key(namespace, captchaID), strings.ToUpper(code), ttl).Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Item{
|
||||
CaptchaID: captchaID,
|
||||
Image: imageDataURL(code),
|
||||
ExpiresIn: int64(ttl.Seconds()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func Verify(ctx context.Context, redisClient *redis.Client, namespace string, captchaID string, captchaCode string) error {
|
||||
if redisClient == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
captchaID = strings.TrimSpace(captchaID)
|
||||
captchaCode = strings.TrimSpace(captchaCode)
|
||||
if captchaID == "" || captchaCode == "" {
|
||||
return ErrInvalid
|
||||
}
|
||||
|
||||
redisKey := key(namespace, captchaID)
|
||||
stored, err := redisClient.Get(ctx, redisKey).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return ErrInvalid
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = redisClient.Del(ctx, redisKey).Err()
|
||||
if strings.ToUpper(captchaCode) != stored {
|
||||
return ErrInvalid
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func key(namespace string, id string) string {
|
||||
namespace = strings.Trim(strings.ToLower(strings.TrimSpace(namespace)), ":")
|
||||
if namespace == "" {
|
||||
namespace = "default"
|
||||
}
|
||||
return "captcha:" + namespace + ":" + id
|
||||
}
|
||||
|
||||
func randomToken(length int) (string, error) {
|
||||
buf := make([]byte, length)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(buf), nil
|
||||
}
|
||||
|
||||
func randomCaptchaCode(length int) (string, error) {
|
||||
const alphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"
|
||||
result := make([]byte, length)
|
||||
for i := range result {
|
||||
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(alphabet))))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
result[i] = alphabet[n.Int64()]
|
||||
}
|
||||
return string(result), nil
|
||||
}
|
||||
|
||||
func imageDataURL(code string) string {
|
||||
safeCode := html.EscapeString(strings.ToUpper(code))
|
||||
svg := fmt.Sprintf(`<svg xmlns="http://www.w3.org/2000/svg" width="132" height="44" viewBox="0 0 132 44">
|
||||
<rect width="132" height="44" rx="8" fill="#eef5f7"/>
|
||||
<path d="M8 32 C32 2, 62 52, 124 12" stroke="#0f766e" stroke-width="2" fill="none" opacity=".28"/>
|
||||
<path d="M10 13 C42 42, 86 0, 122 31" stroke="#2563eb" stroke-width="2" fill="none" opacity=".22"/>
|
||||
<text x="66" y="29" text-anchor="middle" font-family="Menlo,Consolas,monospace" font-size="24" font-weight="700" letter-spacing="4" fill="#111827">%s</text>
|
||||
</svg>`, safeCode)
|
||||
return "data:image/svg+xml;base64," + base64.StdEncoding.EncodeToString([]byte(svg))
|
||||
}
|
||||
@@ -2,16 +2,13 @@ package adminauth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
"math/big"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/captcha"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/auth"
|
||||
|
||||
@@ -38,24 +35,17 @@ func NewRepository(db *gorm.DB, redis *redis.Client, jwt *auth.JWTManager) *Repo
|
||||
}
|
||||
|
||||
func (r *Repository) Captcha(ctx context.Context) (*CaptchaDTO, error) {
|
||||
if r.redis == nil {
|
||||
item, err := captcha.Generate(ctx, r.redis, "admin", captchaTTL)
|
||||
if errors.Is(err, captcha.ErrDependencyUnavailable) {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
captchaID, err := randomToken(16)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
code, err := randomCaptchaCode(4)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := r.redis.Set(ctx, captchaKey(captchaID), strings.ToUpper(code), captchaTTL).Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &CaptchaDTO{
|
||||
CaptchaID: captchaID,
|
||||
Image: captchaImageDataURL(code),
|
||||
ExpiresIn: int64(captchaTTL.Seconds()),
|
||||
CaptchaID: item.CaptchaID,
|
||||
Image: item.Image,
|
||||
ExpiresIn: item.ExpiresIn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -120,22 +110,14 @@ func (r *Repository) RevokeTokens(ctx context.Context, adminID uint64) error {
|
||||
}
|
||||
|
||||
func (r *Repository) verifyCaptcha(ctx context.Context, captchaID string, captchaCode string) error {
|
||||
if r.redis == nil {
|
||||
err := captcha.Verify(ctx, r.redis, "admin", captchaID, captchaCode)
|
||||
if errors.Is(err, captcha.ErrDependencyUnavailable) {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
key := captchaKey(captchaID)
|
||||
stored, err := r.redis.Get(ctx, key).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
if errors.Is(err, captcha.ErrInvalid) {
|
||||
return ErrCaptchaInvalid
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = r.redis.Del(ctx, key).Err()
|
||||
if strings.ToUpper(strings.TrimSpace(captchaCode)) != stored {
|
||||
return ErrCaptchaInvalid
|
||||
}
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repository) FindByID(ctx context.Context, id uint64) (*AdminDTO, error) {
|
||||
@@ -273,39 +255,3 @@ func loginKeyPart(value string) string {
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func captchaKey(id string) string {
|
||||
return "admin:captcha:" + id
|
||||
}
|
||||
|
||||
func randomToken(length int) (string, error) {
|
||||
buf := make([]byte, length)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(buf), nil
|
||||
}
|
||||
|
||||
func randomCaptchaCode(length int) (string, error) {
|
||||
const alphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"
|
||||
result := make([]byte, length)
|
||||
for i := range result {
|
||||
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(alphabet))))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
result[i] = alphabet[n.Int64()]
|
||||
}
|
||||
return string(result), nil
|
||||
}
|
||||
|
||||
func captchaImageDataURL(code string) string {
|
||||
safeCode := html.EscapeString(strings.ToUpper(code))
|
||||
svg := fmt.Sprintf(`<svg xmlns="http://www.w3.org/2000/svg" width="132" height="44" viewBox="0 0 132 44">
|
||||
<rect width="132" height="44" rx="8" fill="#eef5f7"/>
|
||||
<path d="M8 32 C32 2, 62 52, 124 12" stroke="#0f766e" stroke-width="2" fill="none" opacity=".28"/>
|
||||
<path d="M10 13 C42 42, 86 0, 122 31" stroke="#2563eb" stroke-width="2" fill="none" opacity=".22"/>
|
||||
<text x="66" y="29" text-anchor="middle" font-family="Menlo,Consolas,monospace" font-size="24" font-weight="700" letter-spacing="4" fill="#111827">%s</text>
|
||||
</svg>`, safeCode)
|
||||
return "data:image/svg+xml;base64," + base64.StdEncoding.EncodeToString([]byte(svg))
|
||||
}
|
||||
|
||||
@@ -15,7 +15,9 @@ type Handler struct {
|
||||
}
|
||||
|
||||
type SendSMSRequest struct {
|
||||
Phone string `json:"phone" binding:"required"`
|
||||
Phone string `json:"phone" binding:"required"`
|
||||
CaptchaID string `json:"captcha_id" binding:"required"`
|
||||
CaptchaCode string `json:"captcha_code" binding:"required"`
|
||||
}
|
||||
|
||||
type LoginRequest struct {
|
||||
@@ -31,6 +33,15 @@ func NewHandler(service *Service) *Handler {
|
||||
return &Handler{service: service}
|
||||
}
|
||||
|
||||
func (h *Handler) Captcha(c *gin.Context) {
|
||||
item, err := h.service.Captcha(c.Request.Context())
|
||||
if err != nil {
|
||||
writeAuthError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, item)
|
||||
}
|
||||
|
||||
// SendSMS 发送短信验证码
|
||||
// @Summary 发送短信验证码
|
||||
// @Description 发送登录验证码到指定手机号
|
||||
@@ -45,10 +56,10 @@ func NewHandler(service *Service) *Handler {
|
||||
func (h *Handler) SendSMS(c *gin.Context) {
|
||||
var req SendSMSRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "手机号不能为空")
|
||||
response.BadRequest(c, "手机号和图形验证码不能为空")
|
||||
return
|
||||
}
|
||||
if err := h.service.SendSMSCode(c.Request.Context(), strings.TrimSpace(req.Phone)); err != nil {
|
||||
if err := h.service.SendSMSCode(c.Request.Context(), strings.TrimSpace(req.Phone), strings.TrimSpace(req.CaptchaID), strings.TrimSpace(req.CaptchaCode)); err != nil {
|
||||
writeAuthError(c, err)
|
||||
return
|
||||
}
|
||||
@@ -125,6 +136,8 @@ func writeAuthError(c *gin.Context, err error) {
|
||||
response.ServiceUnavailable(c, "数据库或 Redis 未连接")
|
||||
case errors.Is(err, ErrInvalidPhone):
|
||||
response.BadRequest(c, "手机号格式不正确")
|
||||
case errors.Is(err, ErrCaptchaInvalid):
|
||||
response.BadRequest(c, "图形验证码错误或已过期")
|
||||
case errors.Is(err, ErrCodeRateLimited):
|
||||
response.Error(c, http.StatusTooManyRequests, "rate_limited", "验证码发送过于频繁")
|
||||
case errors.Is(err, ErrSMSSendFailed):
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/captcha"
|
||||
smsprovider "hfb_sys/backend/internal/integrations/sms"
|
||||
"hfb_sys/backend/internal/model"
|
||||
|
||||
@@ -19,6 +20,7 @@ import (
|
||||
var (
|
||||
ErrDependencyUnavailable = errors.New("dependency unavailable")
|
||||
ErrInvalidPhone = errors.New("invalid phone")
|
||||
ErrCaptchaInvalid = errors.New("captcha invalid")
|
||||
ErrCodeRateLimited = errors.New("sms code rate limited")
|
||||
ErrCodeInvalid = errors.New("sms code invalid")
|
||||
ErrSMSSendFailed = errors.New("sms send failed")
|
||||
@@ -29,6 +31,7 @@ const (
|
||||
smsLoginCooldown = 60 * time.Second
|
||||
smsLoginHourlyLimit = 5
|
||||
smsLoginHourlyWindow = time.Hour
|
||||
smsCaptchaTTL = 3 * time.Minute
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
@@ -48,7 +51,18 @@ func NewService(users *UserRepository, redis *redis.Client, jwt *JWTManager, sms
|
||||
return &Service{users: users, redis: redis, jwt: jwt, sms: sms, log: log}
|
||||
}
|
||||
|
||||
func (s *Service) SendSMSCode(ctx context.Context, phone string) error {
|
||||
func (s *Service) Captcha(ctx context.Context) (*captcha.Item, error) {
|
||||
if s.redis == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
item, err := captcha.Generate(ctx, s.redis, "auth:sms", smsCaptchaTTL)
|
||||
if errors.Is(err, captcha.ErrDependencyUnavailable) {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return item, err
|
||||
}
|
||||
|
||||
func (s *Service) SendSMSCode(ctx context.Context, phone string, captchaID string, captchaCode string) error {
|
||||
if s.redis == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
@@ -74,6 +88,16 @@ func (s *Service) SendSMSCode(ctx context.Context, phone string) error {
|
||||
return ErrCodeRateLimited
|
||||
}
|
||||
|
||||
if err := captcha.Verify(ctx, s.redis, "auth:sms", captchaID, captchaCode); err != nil {
|
||||
if errors.Is(err, captcha.ErrDependencyUnavailable) {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
if errors.Is(err, captcha.ErrInvalid) {
|
||||
return ErrCaptchaInvalid
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
code, err := randomDigits(6)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -299,6 +299,7 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
|
||||
|
||||
authRoutes := api.Group("/auth")
|
||||
{
|
||||
authRoutes.GET("/captcha", authHandler.Captcha)
|
||||
authRoutes.POST("/sms/send", authHandler.SendSMS)
|
||||
authRoutes.POST("/sms/login", authHandler.Login)
|
||||
authRoutes.POST("/refresh", authHandler.Refresh)
|
||||
|
||||
Reference in New Issue
Block a user