mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-08-01 13:52:46 +00:00
Add hcaptcha support besides Google ReCaptcha (#2834)
### Pull Request Checklist This PR add support for hcaptcha.com as an alternative to Google ReCaptcha. It also makes possible for user to customize ReCaptcha URL when needed. (Such as use recaptcha.net instead of www.google.com) This feature needs manual test cuz it involves 3rd party _captcha_. Signed-off-by: `Simon Ding <dxl@plotbridge.com>` Co-authored-by: dxl <dxl@plotbridge.com>
This commit is contained in:
parent
f6035822e7
commit
0782011f54
5 changed files with 45 additions and 11 deletions
|
@ -31,8 +31,7 @@ const recaptchaTemplate = `
|
|||
<title>Authentication</title>
|
||||
<meta name='viewport' content='width=device-width, initial-scale=1,
|
||||
user-scalable=no, minimum-scale=1.0, maximum-scale=1.0'>
|
||||
<script src="https://www.google.com/recaptcha/api.js"
|
||||
async defer></script>
|
||||
<script src="{{.apiJsUrl}}" async defer></script>
|
||||
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
|
||||
<script>
|
||||
function captchaDone() {
|
||||
|
@ -51,8 +50,8 @@ function captchaDone() {
|
|||
Please verify that you're not a robot.
|
||||
</p>
|
||||
<input type="hidden" name="session" value="{{.session}}" />
|
||||
<div class="g-recaptcha"
|
||||
data-sitekey="{{.siteKey}}"
|
||||
<div class="{{.sitekeyClass}}"
|
||||
data-sitekey="{{.sitekey}}"
|
||||
data-callback="captchaDone">
|
||||
</div>
|
||||
<noscript>
|
||||
|
@ -114,9 +113,12 @@ func AuthFallback(
|
|||
|
||||
serveRecaptcha := func() {
|
||||
data := map[string]string{
|
||||
"myUrl": req.URL.String(),
|
||||
"session": sessionID,
|
||||
"siteKey": cfg.RecaptchaPublicKey,
|
||||
"myUrl": req.URL.String(),
|
||||
"session": sessionID,
|
||||
"apiJsUrl": cfg.RecaptchaApiJsUrl,
|
||||
"sitekey": cfg.RecaptchaPublicKey,
|
||||
"sitekeyClass": cfg.RecaptchaSitekeyClass,
|
||||
"formField": cfg.RecaptchaFormField,
|
||||
}
|
||||
serveTemplate(w, recaptchaTemplate, data)
|
||||
}
|
||||
|
@ -155,7 +157,7 @@ func AuthFallback(
|
|||
return &res
|
||||
}
|
||||
|
||||
response := req.Form.Get("g-recaptcha-response")
|
||||
response := req.Form.Get(cfg.RecaptchaFormField)
|
||||
if err := validateRecaptcha(cfg, response, clientIP); err != nil {
|
||||
util.GetLogger(req.Context()).Error(err)
|
||||
return err
|
||||
|
|
|
@ -20,6 +20,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
|
@ -336,6 +337,7 @@ func validateRecaptcha(
|
|||
response string,
|
||||
clientip string,
|
||||
) *util.JSONResponse {
|
||||
ip, _, _ := net.SplitHostPort(clientip)
|
||||
if !cfg.RecaptchaEnabled {
|
||||
return &util.JSONResponse{
|
||||
Code: http.StatusConflict,
|
||||
|
@ -355,7 +357,7 @@ func validateRecaptcha(
|
|||
url.Values{
|
||||
"secret": {cfg.RecaptchaPrivateKey},
|
||||
"response": {response},
|
||||
"remoteip": {clientip},
|
||||
"remoteip": {ip},
|
||||
},
|
||||
)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue