fix: captcha preview panic when clientId or clientSecret is empty (#824)

* fix: captcha preview panic when clientId or clientSecret is empty

* return original errors from captcha
This commit is contained in:
Resulte Lee
2022-06-26 22:09:57 +08:00
committed by GitHub
parent 339c6c2dd0
commit 477d386f3c
4 changed files with 20 additions and 6 deletions

View File

@ -16,9 +16,11 @@ package captcha
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const HCaptchaVerifyUrl = "https://hcaptcha.com/siteverify"
@ -48,7 +50,8 @@ func (captcha *HCaptchaProvider) VerifyCaptcha(token, clientSecret string) (bool
}
type captchaResponse struct {
Success bool `json:"success"`
Success bool `json:"success"`
ErrorCodes []string `json:"error-codes"`
}
captchaResp := &captchaResponse{}
err = json.Unmarshal(body, captchaResp)
@ -56,5 +59,9 @@ func (captcha *HCaptchaProvider) VerifyCaptcha(token, clientSecret string) (bool
return false, err
}
if len(captchaResp.ErrorCodes) > 0 {
return false, errors.New(strings.Join(captchaResp.ErrorCodes, ","))
}
return captchaResp.Success, nil
}