mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-16 11:23:11 -06:00
Added recaptcha check for POST requests
This commit is contained in:
parent
f852c99eee
commit
170e8e4e5f
|
|
@ -131,18 +131,8 @@ func AuthFallback(
|
||||||
if req.Method == http.MethodGet {
|
if req.Method == http.MethodGet {
|
||||||
// Handle Recaptcha
|
// Handle Recaptcha
|
||||||
if authType == authtypes.LoginTypeRecaptcha {
|
if authType == authtypes.LoginTypeRecaptcha {
|
||||||
if cfg.Matrix.RecaptchaEnabled {
|
if err := checkRecaptchaEnabled(&cfg, w, req); err != nil {
|
||||||
if cfg.Matrix.RecaptchaPublicKey == "" {
|
return err
|
||||||
return writeErrorMessage(w, req,
|
|
||||||
"This Homeserver doesn't have a recaptcha public key",
|
|
||||||
http.StatusInternalServerError,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return writeErrorMessage(w, req,
|
|
||||||
"Recaptcha login is disabled on this Homeserver",
|
|
||||||
http.StatusBadRequest,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
serveRecaptcha()
|
serveRecaptcha()
|
||||||
|
|
@ -153,6 +143,12 @@ func AuthFallback(
|
||||||
JSON: jsonerror.NotFound("Unknown auth stage type"),
|
JSON: jsonerror.NotFound("Unknown auth stage type"),
|
||||||
}
|
}
|
||||||
} else if req.Method == http.MethodPost {
|
} else if req.Method == http.MethodPost {
|
||||||
|
// Handle Recaptcha
|
||||||
|
if authType == authtypes.LoginTypeRecaptcha {
|
||||||
|
if err := checkRecaptchaEnabled(&cfg, w, req); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
clientIP := req.RemoteAddr
|
clientIP := req.RemoteAddr
|
||||||
err := req.ParseForm()
|
err := req.ParseForm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -172,13 +168,41 @@ func AuthFallback(
|
||||||
serveSuccess()
|
serveSuccess()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return &util.JSONResponse{
|
||||||
|
Code: http.StatusNotFound,
|
||||||
|
JSON: jsonerror.NotFound("Unknown auth stage type"),
|
||||||
|
}
|
||||||
|
}
|
||||||
return &util.JSONResponse{
|
return &util.JSONResponse{
|
||||||
Code: http.StatusMethodNotAllowed,
|
Code: http.StatusMethodNotAllowed,
|
||||||
JSON: jsonerror.NotFound("Bad method"),
|
JSON: jsonerror.NotFound("Bad method"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteErrorMessage writes an error response with the given header and message
|
// checkRecaptchaEnabled creates an error response if recaptcha is not usable on homeserver.
|
||||||
|
func checkRecaptchaEnabled(
|
||||||
|
cfg *config.Dendrite,
|
||||||
|
w http.ResponseWriter,
|
||||||
|
req *http.Request,
|
||||||
|
) *util.JSONResponse {
|
||||||
|
if cfg.Matrix.RecaptchaEnabled {
|
||||||
|
if cfg.Matrix.RecaptchaPublicKey == "" {
|
||||||
|
return writeErrorMessage(w, req,
|
||||||
|
"This Homeserver doesn't have a recaptcha public key",
|
||||||
|
http.StatusInternalServerError,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return writeErrorMessage(w, req,
|
||||||
|
"Recaptcha login is disabled on this Homeserver",
|
||||||
|
http.StatusBadRequest,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeErrorMessage writes an error response with the given header and message
|
||||||
func writeErrorMessage(
|
func writeErrorMessage(
|
||||||
w http.ResponseWriter, req *http.Request,
|
w http.ResponseWriter, req *http.Request,
|
||||||
message string, header int,
|
message string, header int,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue