auth_oauth_captcha_start.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package handler
  2. import (
  3. "net/http"
  4. "strings"
  5. "github.com/Wei-Shaw/sub2api/internal/pkg/ip"
  6. "github.com/Wei-Shaw/sub2api/internal/pkg/response"
  7. "github.com/Wei-Shaw/sub2api/internal/service"
  8. "github.com/gin-gonic/gin"
  9. )
  10. type oauthStartCaptchaRequest struct {
  11. // TurnstileToken 承载阿里云验证码的 captchaVerifyParam(复用既有请求字段名)
  12. TurnstileToken string `json:"turnstile_token"`
  13. TencentCaptchaTicket string `json:"tencent_captcha_ticket"`
  14. TencentCaptchaRandstr string `json:"tencent_captcha_randstr"`
  15. }
  16. type oauthStartResponse struct {
  17. AuthorizeURL string `json:"authorize_url"`
  18. }
  19. func (h *AuthHandler) requireActionCaptchaForOAuthLoginStart(c *gin.Context) bool {
  20. if strings.HasSuffix(strings.TrimRight(c.Request.URL.Path, "/"), "/bind/start") {
  21. return true
  22. }
  23. var req oauthStartCaptchaRequest
  24. if c.Request.Method == http.MethodPost {
  25. _ = c.ShouldBindJSON(&req)
  26. }
  27. if err := h.authService.VerifyActionCaptchaIfEnabled(c.Request.Context(), service.CaptchaProof{
  28. TurnstileToken: req.TurnstileToken,
  29. TencentTicket: req.TencentCaptchaTicket,
  30. TencentRandstr: req.TencentCaptchaRandstr,
  31. }, ip.GetClientIP(c)); err != nil {
  32. response.ErrorFrom(c, err)
  33. return false
  34. }
  35. return true
  36. }
  37. func respondOAuthStart(c *gin.Context, authorizeURL string) {
  38. if c.Request.Method == http.MethodPost {
  39. response.Success(c, oauthStartResponse{AuthorizeURL: authorizeURL})
  40. return
  41. }
  42. c.Redirect(http.StatusFound, authorizeURL)
  43. }