From 90515527b661ce0739ec556de73b190ac36db617 Mon Sep 17 00:00:00 2001 From: Parminder Singh Date: Sun, 4 Mar 2018 21:28:08 +0530 Subject: [PATCH] Return JSON response for method and authtype errors --- .../clientapi/routing/auth_fallback.go | 46 +++++++++++++------ .../matrix-org/dendrite/common/httpapi.go | 2 +- 2 files changed, 34 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 9d087a20d..26d3504f7 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 @@ -18,7 +18,10 @@ import ( "html/template" "net/http" + "github.com/matrix-org/dendrite/clientapi/auth/authtypes" + "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/common/config" + "github.com/matrix-org/util" ) // RecaptchaTemplate is template for recaptcha auth @@ -88,25 +91,42 @@ if (window.onAuthDone) { ` +// ServeTemplate fills data in template and serves it in http.ResponseWriter +func ServeTemplate(w http.ResponseWriter, templateHTML string, data map[string]string) { + t := template.Must(template.New("response").Parse(templateHTML)) + if err := t.Execute(w, data); err != nil { + panic(err) + } +} + // AuthFallback implements GET on /auth/{authType}/fallback/web?session={sessionID} -// TODO: Handle POST requests too func AuthFallback( w http.ResponseWriter, req *http.Request, authType string, sessionID string, cfg config.Dendrite, -) { +) util.JSONRespose { if req.Method == "GET" { - t := template.Must(template.New("response").Parse(RECAPTCHA_TEMPLATE)) - - data := map[string]string{ - "MyUrl": req.URL.String(), - "Session": sessionID, - "SiteKey": cfg.Matrix.RecaptchaPublicKey, + // Handle Recaptcha + if authType == authtypes.LoginTypeRecaptcha { + data := map[string]string{ + "MyUrl": req.URL.String(), + "Session": sessionID, + "SiteKey": cfg.Matrix.RecaptchaPublicKey, + } + ServeTemplate(w, RecaptchaTemplate, data) + return nil } - - if err := t.Execute(w, data); err != nil { - panic(err) + return util.JSONResponse{ + Code: 404, + JSON: jsonerror.NotFound("Unknown auth stage type"), } - // TODO: Handle rest of flow + } else if req.Method == "POST" { + // TODO: Handle POST requests too + // Check Recaptcha + // Serve success + // Else serve Recaptcha again + } + return util.JSONResponse{ + Code: 405, + JSON: jsonerror.NotFound("Bad method"), } - // TODO: Handle invalid request } diff --git a/src/github.com/matrix-org/dendrite/common/httpapi.go b/src/github.com/matrix-org/dendrite/common/httpapi.go index fae17c736..1529ca90e 100644 --- a/src/github.com/matrix-org/dendrite/common/httpapi.go +++ b/src/github.com/matrix-org/dendrite/common/httpapi.go @@ -46,7 +46,7 @@ func MakeHTMLAPI(metricsName string, f func(http.ResponseWriter, *http.Request) span := opentracing.StartSpan(metricsName) defer span.Finish() req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span)) - if err := f(w, req); err { + if err := f(w, req); err != nil { h := util.MakeJSONAPI(util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse { return err }))