Added recaptcha check for POST requests

This commit is contained in:
Parminder Singh 2019-08-11 15:18:21 +05:30
parent f852c99eee
commit 170e8e4e5f

View file

@ -131,18 +131,8 @@ func AuthFallback(
if req.Method == http.MethodGet {
// Handle Recaptcha
if authType == authtypes.LoginTypeRecaptcha {
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,
)
if err := checkRecaptchaEnabled(&cfg, w, req); err != nil {
return err
}
serveRecaptcha()
@ -153,6 +143,12 @@ func AuthFallback(
JSON: jsonerror.NotFound("Unknown auth stage type"),
}
} 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
err := req.ParseForm()
if err != nil {
@ -172,13 +168,41 @@ func AuthFallback(
serveSuccess()
return nil
}
return &util.JSONResponse{
Code: http.StatusNotFound,
JSON: jsonerror.NotFound("Unknown auth stage type"),
}
}
return &util.JSONResponse{
Code: http.StatusMethodNotAllowed,
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(
w http.ResponseWriter, req *http.Request,
message string, header int,