feat: 添加 Gitee OAuth 认证支持并更新相关配置、路由和服务。
This commit is contained in:
@@ -15,15 +15,16 @@ import (
|
||||
type AuthHandler struct {
|
||||
authService *service.AuthService
|
||||
gitHubOAuthService *service.GitHubOAuthService
|
||||
giteeOAuthService *service.GiteeOAuthService
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
func NewAuthHandler(authService *service.AuthService, gitHubOAuthService *service.GitHubOAuthService) *AuthHandler {
|
||||
return &AuthHandler{authService: authService, gitHubOAuthService: gitHubOAuthService}
|
||||
func NewAuthHandler(authService *service.AuthService, gitHubOAuthService *service.GitHubOAuthService, giteeOAuthService *service.GiteeOAuthService) *AuthHandler {
|
||||
return &AuthHandler{authService: authService, gitHubOAuthService: gitHubOAuthService, giteeOAuthService: giteeOAuthService}
|
||||
}
|
||||
|
||||
func NewAuthHandlerWithConfig(authService *service.AuthService, gitHubOAuthService *service.GitHubOAuthService, cfg *config.Config) *AuthHandler {
|
||||
return &AuthHandler{authService: authService, gitHubOAuthService: gitHubOAuthService, cfg: cfg}
|
||||
func NewAuthHandlerWithConfig(authService *service.AuthService, gitHubOAuthService *service.GitHubOAuthService, giteeOAuthService *service.GiteeOAuthService, cfg *config.Config) *AuthHandler {
|
||||
return &AuthHandler{authService: authService, gitHubOAuthService: gitHubOAuthService, giteeOAuthService: giteeOAuthService, cfg: cfg}
|
||||
}
|
||||
|
||||
type RegisterRequest struct {
|
||||
@@ -154,6 +155,8 @@ func (h *AuthHandler) RegisterRoutes(rg *gin.RouterGroup) {
|
||||
auth.POST("/refresh", h.RefreshToken)
|
||||
auth.GET("/github", h.GitHubLogin)
|
||||
auth.GET("/github/callback", h.GitHubCallback)
|
||||
auth.GET("/gitee", h.GiteeLogin)
|
||||
auth.GET("/gitee/callback", h.GiteeCallback)
|
||||
}
|
||||
|
||||
func (h *AuthHandler) RegisterProtectedRoutes(rg *gin.RouterGroup) {
|
||||
@@ -206,3 +209,48 @@ func (h *AuthHandler) GitHubCallback(c *gin.Context) {
|
||||
frontendURL, url.QueryEscape(tokens.AccessToken), url.QueryEscape(tokens.RefreshToken), user.ID)
|
||||
c.Redirect(302, redirectURL)
|
||||
}
|
||||
|
||||
func (h *AuthHandler) GiteeLogin(c *gin.Context) {
|
||||
if h.giteeOAuthService == nil {
|
||||
api.BadRequest(c, "Gitee OAuth is not configured")
|
||||
return
|
||||
}
|
||||
state := c.Query("state")
|
||||
if state == "" {
|
||||
state = "default"
|
||||
}
|
||||
authURL := h.giteeOAuthService.GetAuthorizationURL(state)
|
||||
c.Redirect(302, authURL)
|
||||
}
|
||||
|
||||
func (h *AuthHandler) GiteeCallback(c *gin.Context) {
|
||||
if h.giteeOAuthService == nil {
|
||||
api.BadRequest(c, "Gitee OAuth is not configured")
|
||||
return
|
||||
}
|
||||
|
||||
frontendURL := "http://localhost:2613"
|
||||
if h.cfg != nil && h.cfg.FrontendURL != "" {
|
||||
frontendURL = h.cfg.FrontendURL
|
||||
}
|
||||
|
||||
code := c.Query("code")
|
||||
if code == "" {
|
||||
redirectURL := fmt.Sprintf("%s/login?error=missing_code", frontendURL)
|
||||
c.Redirect(302, redirectURL)
|
||||
return
|
||||
}
|
||||
|
||||
user, tokens, err := h.giteeOAuthService.HandleCallback(code)
|
||||
if err != nil {
|
||||
fmt.Printf("[Auth] Gitee callback failed: %v\n", err)
|
||||
redirectURL := fmt.Sprintf("%s/login?error=%s", frontendURL, url.QueryEscape(err.Error()))
|
||||
c.Redirect(302, redirectURL)
|
||||
return
|
||||
}
|
||||
|
||||
// 重定向到前端回调页面,带上token信息
|
||||
redirectURL := fmt.Sprintf("%s/auth/gitee/callback?access_token=%s&refresh_token=%s&user_id=%d",
|
||||
frontendURL, url.QueryEscape(tokens.AccessToken), url.QueryEscape(tokens.RefreshToken), user.ID)
|
||||
c.Redirect(302, redirectURL)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user