2025-12-26 10:42:08 +08:00
|
|
|
|
package routes
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-01-10 13:14:35 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
|
2025-12-26 10:42:08 +08:00
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/handler"
|
2026-01-10 13:14:35 +08:00
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/middleware"
|
|
|
|
|
|
servermiddleware "github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
2026-03-12 02:42:57 +03:00
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
2025-12-26 10:42:08 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2026-01-10 13:14:35 +08:00
|
|
|
|
"github.com/redis/go-redis/v9"
|
2025-12-26 10:42:08 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// RegisterAuthRoutes 注册认证相关路由
|
|
|
|
|
|
func RegisterAuthRoutes(
|
|
|
|
|
|
v1 *gin.RouterGroup,
|
|
|
|
|
|
h *handler.Handlers,
|
2026-01-10 13:14:35 +08:00
|
|
|
|
jwtAuth servermiddleware.JWTAuthMiddleware,
|
|
|
|
|
|
redisClient *redis.Client,
|
2026-03-12 02:42:57 +03:00
|
|
|
|
settingService *service.SettingService,
|
2025-12-26 10:42:08 +08:00
|
|
|
|
) {
|
2026-01-10 13:14:35 +08:00
|
|
|
|
// 创建速率限制器
|
|
|
|
|
|
rateLimiter := middleware.NewRateLimiter(redisClient)
|
|
|
|
|
|
|
2025-12-26 10:42:08 +08:00
|
|
|
|
// 公开接口
|
|
|
|
|
|
auth := v1.Group("/auth")
|
2026-03-12 02:42:57 +03:00
|
|
|
|
auth.Use(servermiddleware.BackendModeAuthGuard(settingService))
|
2025-12-26 10:42:08 +08:00
|
|
|
|
{
|
2026-02-14 11:23:10 +08:00
|
|
|
|
// 注册/登录/2FA/验证码发送均属于高风险入口,增加服务端兜底限流(Redis 故障时 fail-close)
|
|
|
|
|
|
auth.POST("/register", rateLimiter.LimitWithOptions("auth-register", 5, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}), h.Auth.Register)
|
|
|
|
|
|
auth.POST("/login", rateLimiter.LimitWithOptions("auth-login", 20, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}), h.Auth.Login)
|
|
|
|
|
|
auth.POST("/login/2fa", rateLimiter.LimitWithOptions("auth-login-2fa", 20, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}), h.Auth.Login2FA)
|
|
|
|
|
|
auth.POST("/send-verify-code", rateLimiter.LimitWithOptions("auth-send-verify-code", 5, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}), h.Auth.SendVerifyCode)
|
2026-02-05 12:38:48 +08:00
|
|
|
|
// Token刷新接口添加速率限制:每分钟最多 30 次(Redis 故障时 fail-close)
|
|
|
|
|
|
auth.POST("/refresh", rateLimiter.LimitWithOptions("refresh-token", 30, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}), h.Auth.RefreshToken)
|
|
|
|
|
|
// 登出接口(公开,允许未认证用户调用以撤销Refresh Token)
|
|
|
|
|
|
auth.POST("/logout", h.Auth.Logout)
|
2026-01-11 22:21:05 +08:00
|
|
|
|
// 优惠码验证接口添加速率限制:每分钟最多 10 次(Redis 故障时 fail-close)
|
|
|
|
|
|
auth.POST("/validate-promo-code", rateLimiter.LimitWithOptions("validate-promo", 10, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}), h.Auth.ValidatePromoCode)
|
2026-01-29 16:29:59 +08:00
|
|
|
|
// 邀请码验证接口添加速率限制:每分钟最多 10 次(Redis 故障时 fail-close)
|
|
|
|
|
|
auth.POST("/validate-invitation-code", rateLimiter.LimitWithOptions("validate-invitation", 10, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}), h.Auth.ValidateInvitationCode)
|
2026-01-24 22:33:45 +08:00
|
|
|
|
// 忘记密码接口添加速率限制:每分钟最多 5 次(Redis 故障时 fail-close)
|
|
|
|
|
|
auth.POST("/forgot-password", rateLimiter.LimitWithOptions("forgot-password", 5, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}), h.Auth.ForgotPassword)
|
|
|
|
|
|
// 重置密码接口添加速率限制:每分钟最多 10 次(Redis 故障时 fail-close)
|
|
|
|
|
|
auth.POST("/reset-password", rateLimiter.LimitWithOptions("reset-password", 10, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}), h.Auth.ResetPassword)
|
2026-01-09 12:05:25 +08:00
|
|
|
|
auth.GET("/oauth/linuxdo/start", h.Auth.LinuxDoOAuthStart)
|
2026-04-22 14:57:47 +08:00
|
|
|
|
auth.GET("/oauth/linuxdo/bind/start", func(c *gin.Context) {
|
|
|
|
|
|
query := c.Request.URL.Query()
|
|
|
|
|
|
query.Set("intent", "bind_current_user")
|
|
|
|
|
|
c.Request.URL.RawQuery = query.Encode()
|
|
|
|
|
|
h.Auth.LinuxDoOAuthStart(c)
|
|
|
|
|
|
})
|
2026-01-09 12:05:25 +08:00
|
|
|
|
auth.GET("/oauth/linuxdo/callback", h.Auth.LinuxDoOAuthCallback)
|
2026-04-20 17:39:57 +08:00
|
|
|
|
auth.GET("/oauth/wechat/start", h.Auth.WeChatOAuthStart)
|
2026-04-22 14:57:47 +08:00
|
|
|
|
auth.GET("/oauth/wechat/bind/start", func(c *gin.Context) {
|
|
|
|
|
|
query := c.Request.URL.Query()
|
|
|
|
|
|
query.Set("intent", "bind_current_user")
|
|
|
|
|
|
c.Request.URL.RawQuery = query.Encode()
|
|
|
|
|
|
h.Auth.WeChatOAuthStart(c)
|
|
|
|
|
|
})
|
2026-04-20 17:39:57 +08:00
|
|
|
|
auth.GET("/oauth/wechat/callback", h.Auth.WeChatOAuthCallback)
|
2026-04-20 23:34:57 +08:00
|
|
|
|
auth.GET("/oauth/wechat/payment/start", h.Auth.WeChatPaymentOAuthStart)
|
|
|
|
|
|
auth.GET("/oauth/wechat/payment/callback", h.Auth.WeChatPaymentOAuthCallback)
|
2026-04-20 17:39:57 +08:00
|
|
|
|
auth.POST("/oauth/pending/exchange",
|
|
|
|
|
|
rateLimiter.LimitWithOptions("oauth-pending-exchange", 20, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}),
|
|
|
|
|
|
h.Auth.ExchangePendingOAuthCompletion,
|
|
|
|
|
|
)
|
2026-04-21 10:00:06 +08:00
|
|
|
|
auth.POST("/oauth/pending/send-verify-code",
|
|
|
|
|
|
rateLimiter.LimitWithOptions("oauth-pending-send-verify-code", 5, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}),
|
|
|
|
|
|
h.Auth.SendPendingOAuthVerifyCode,
|
|
|
|
|
|
)
|
2026-04-20 19:30:09 +08:00
|
|
|
|
auth.POST("/oauth/pending/create-account",
|
|
|
|
|
|
rateLimiter.LimitWithOptions("oauth-pending-create-account", 10, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}),
|
|
|
|
|
|
h.Auth.CreatePendingOAuthAccount,
|
|
|
|
|
|
)
|
|
|
|
|
|
auth.POST("/oauth/pending/bind-login",
|
|
|
|
|
|
rateLimiter.LimitWithOptions("oauth-pending-bind-login", 10, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}),
|
|
|
|
|
|
h.Auth.BindPendingOAuthLogin,
|
|
|
|
|
|
)
|
2026-03-09 00:35:34 +08:00
|
|
|
|
auth.POST("/oauth/linuxdo/complete-registration",
|
|
|
|
|
|
rateLimiter.LimitWithOptions("oauth-linuxdo-complete", 10, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}),
|
|
|
|
|
|
h.Auth.CompleteLinuxDoOAuthRegistration,
|
|
|
|
|
|
)
|
2026-04-20 19:30:09 +08:00
|
|
|
|
auth.POST("/oauth/linuxdo/bind-login",
|
|
|
|
|
|
rateLimiter.LimitWithOptions("oauth-linuxdo-bind-login", 20, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}),
|
|
|
|
|
|
h.Auth.BindLinuxDoOAuthLogin,
|
|
|
|
|
|
)
|
|
|
|
|
|
auth.POST("/oauth/linuxdo/create-account",
|
|
|
|
|
|
rateLimiter.LimitWithOptions("oauth-linuxdo-create-account", 10, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}),
|
|
|
|
|
|
h.Auth.CreateLinuxDoOAuthAccount,
|
|
|
|
|
|
)
|
2026-04-20 17:39:57 +08:00
|
|
|
|
auth.POST("/oauth/wechat/complete-registration",
|
|
|
|
|
|
rateLimiter.LimitWithOptions("oauth-wechat-complete", 10, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}),
|
|
|
|
|
|
h.Auth.CompleteWeChatOAuthRegistration,
|
|
|
|
|
|
)
|
2026-04-20 19:30:09 +08:00
|
|
|
|
auth.POST("/oauth/wechat/bind-login",
|
|
|
|
|
|
rateLimiter.LimitWithOptions("oauth-wechat-bind-login", 20, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}),
|
|
|
|
|
|
h.Auth.BindWeChatOAuthLogin,
|
|
|
|
|
|
)
|
|
|
|
|
|
auth.POST("/oauth/wechat/create-account",
|
|
|
|
|
|
rateLimiter.LimitWithOptions("oauth-wechat-create-account", 10, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}),
|
|
|
|
|
|
h.Auth.CreateWeChatOAuthAccount,
|
|
|
|
|
|
)
|
2026-03-13 23:38:58 +08:00
|
|
|
|
auth.GET("/oauth/oidc/start", h.Auth.OIDCOAuthStart)
|
2026-04-22 14:57:47 +08:00
|
|
|
|
auth.GET("/oauth/oidc/bind/start", func(c *gin.Context) {
|
|
|
|
|
|
query := c.Request.URL.Query()
|
|
|
|
|
|
query.Set("intent", "bind_current_user")
|
|
|
|
|
|
c.Request.URL.RawQuery = query.Encode()
|
|
|
|
|
|
h.Auth.OIDCOAuthStart(c)
|
|
|
|
|
|
})
|
2026-03-13 23:38:58 +08:00
|
|
|
|
auth.GET("/oauth/oidc/callback", h.Auth.OIDCOAuthCallback)
|
|
|
|
|
|
auth.POST("/oauth/oidc/complete-registration",
|
|
|
|
|
|
rateLimiter.LimitWithOptions("oauth-oidc-complete", 10, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}),
|
|
|
|
|
|
h.Auth.CompleteOIDCOAuthRegistration,
|
|
|
|
|
|
)
|
2026-04-20 19:30:09 +08:00
|
|
|
|
auth.POST("/oauth/oidc/bind-login",
|
|
|
|
|
|
rateLimiter.LimitWithOptions("oauth-oidc-bind-login", 20, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}),
|
|
|
|
|
|
h.Auth.BindOIDCOAuthLogin,
|
|
|
|
|
|
)
|
|
|
|
|
|
auth.POST("/oauth/oidc/create-account",
|
|
|
|
|
|
rateLimiter.LimitWithOptions("oauth-oidc-create-account", 10, time.Minute, middleware.RateLimitOptions{
|
|
|
|
|
|
FailureMode: middleware.RateLimitFailClose,
|
|
|
|
|
|
}),
|
|
|
|
|
|
h.Auth.CreateOIDCOAuthAccount,
|
|
|
|
|
|
)
|
2025-12-26 10:42:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 公开设置(无需认证)
|
|
|
|
|
|
settings := v1.Group("/settings")
|
|
|
|
|
|
{
|
|
|
|
|
|
settings.GET("/public", h.Setting.GetPublicSettings)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 需要认证的当前用户信息
|
|
|
|
|
|
authenticated := v1.Group("")
|
|
|
|
|
|
authenticated.Use(gin.HandlerFunc(jwtAuth))
|
2026-03-12 02:42:57 +03:00
|
|
|
|
authenticated.Use(servermiddleware.BackendModeUserGuard(settingService))
|
2025-12-26 10:42:08 +08:00
|
|
|
|
{
|
|
|
|
|
|
authenticated.GET("/auth/me", h.Auth.GetCurrentUser)
|
2026-02-05 12:38:48 +08:00
|
|
|
|
// 撤销所有会话(需要认证)
|
|
|
|
|
|
authenticated.POST("/auth/revoke-all-sessions", h.Auth.RevokeAllSessions)
|
2026-04-22 10:26:22 +08:00
|
|
|
|
authenticated.POST("/auth/oauth/bind-token", h.Auth.PrepareOAuthBindAccessTokenCookie)
|
2025-12-26 10:42:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|