From 701555974e678865033e3d0e2e6b1bfece4882d8 Mon Sep 17 00:00:00 2001 From: Parminder Singh Date: Wed, 7 Mar 2018 16:14:35 +0530 Subject: [PATCH] Add POST request handling --- .../clientapi/routing/auth_fallback.go | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/auth_fallback.go b/src/github.com/matrix-org/dendrite/clientapi/routing/auth_fallback.go index 26d3504f7..13443e238 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/auth_fallback.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/auth_fallback.go @@ -51,7 +51,7 @@ function captchaDone() {

Please verify that you're not a robot.

- +
@@ -103,29 +103,43 @@ func ServeTemplate(w http.ResponseWriter, templateHTML string, data map[string]s func AuthFallback( w http.ResponseWriter, req *http.Request, authType string, sessionID string, cfg config.Dendrite, -) util.JSONRespose { +) *util.JSONResponse { + ServeRecaptcha = func(){ + data := map[string]string{ + "MyUrl": req.URL.String(), + "Session": sessionID, + "SiteKey": cfg.Matrix.RecaptchaPublicKey, + } + ServeTemplate(w, RecaptchaTemplate, data) + } + + ServeSuccess = func(){ + data := map[string]string{} + ServeTemplate(w, SuccessTemplate, data) + } + if req.Method == "GET" { // Handle Recaptcha if authType == authtypes.LoginTypeRecaptcha { - data := map[string]string{ - "MyUrl": req.URL.String(), - "Session": sessionID, - "SiteKey": cfg.Matrix.RecaptchaPublicKey, - } - ServeTemplate(w, RecaptchaTemplate, data) + ServeRecaptcha() return nil } - return util.JSONResponse{ + return &util.JSONResponse{ Code: 404, JSON: jsonerror.NotFound("Unknown auth stage type"), } } else if req.Method == "POST" { - // TODO: Handle POST requests too - // Check Recaptcha - // Serve success - // Else serve Recaptcha again + clientIP := req.RemoteAddr + response := req.Form.Get("g-recaptcha-response") + if err = validateRecaptcha(cfg, response, clientIP), if resErr != nil { + ServeRecaptcha() + return nil + } + + ServeSuccess() + return nil } - return util.JSONResponse{ + return &util.JSONResponse{ Code: 405, JSON: jsonerror.NotFound("Bad method"), }