mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-16 07:33:06 +08:00
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/ip"
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type oauthStartCaptchaRequest struct {
|
|
TencentCaptchaTicket string `json:"tencent_captcha_ticket"`
|
|
TencentCaptchaRandstr string `json:"tencent_captcha_randstr"`
|
|
}
|
|
|
|
type oauthStartResponse struct {
|
|
AuthorizeURL string `json:"authorize_url"`
|
|
}
|
|
|
|
func (h *AuthHandler) requireTencentCaptchaForOAuthLoginStart(c *gin.Context) bool {
|
|
if strings.HasSuffix(strings.TrimRight(c.Request.URL.Path, "/"), "/bind/start") {
|
|
return true
|
|
}
|
|
|
|
var req oauthStartCaptchaRequest
|
|
if c.Request.Method == http.MethodPost {
|
|
_ = c.ShouldBindJSON(&req)
|
|
}
|
|
if err := h.authService.VerifyTencentCaptchaIfEnabled(c.Request.Context(), service.CaptchaProof{
|
|
TencentTicket: req.TencentCaptchaTicket,
|
|
TencentRandstr: req.TencentCaptchaRandstr,
|
|
}, ip.GetClientIP(c)); err != nil {
|
|
response.ErrorFrom(c, err)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func respondOAuthStart(c *gin.Context, authorizeURL string) {
|
|
if c.Request.Method == http.MethodPost {
|
|
response.Success(c, oauthStartResponse{AuthorizeURL: authorizeURL})
|
|
return
|
|
}
|
|
c.Redirect(http.StatusFound, authorizeURL)
|
|
}
|