mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-14 18:33:09 -06:00
Return JSON response for method and authtype errors
This commit is contained in:
parent
62f9071217
commit
90515527b6
|
|
@ -18,7 +18,10 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"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/dendrite/common/config"
|
||||||
|
"github.com/matrix-org/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RecaptchaTemplate is template for recaptcha auth
|
// RecaptchaTemplate is template for recaptcha auth
|
||||||
|
|
@ -88,25 +91,42 @@ if (window.onAuthDone) {
|
||||||
</html>
|
</html>
|
||||||
`
|
`
|
||||||
|
|
||||||
|
// 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}
|
// AuthFallback implements GET on /auth/{authType}/fallback/web?session={sessionID}
|
||||||
// TODO: Handle POST requests too
|
|
||||||
func AuthFallback(
|
func AuthFallback(
|
||||||
w http.ResponseWriter, req *http.Request, authType string, sessionID string,
|
w http.ResponseWriter, req *http.Request, authType string, sessionID string,
|
||||||
cfg config.Dendrite,
|
cfg config.Dendrite,
|
||||||
) {
|
) util.JSONRespose {
|
||||||
if req.Method == "GET" {
|
if req.Method == "GET" {
|
||||||
t := template.Must(template.New("response").Parse(RECAPTCHA_TEMPLATE))
|
// Handle Recaptcha
|
||||||
|
if authType == authtypes.LoginTypeRecaptcha {
|
||||||
data := map[string]string{
|
data := map[string]string{
|
||||||
"MyUrl": req.URL.String(),
|
"MyUrl": req.URL.String(),
|
||||||
"Session": sessionID,
|
"Session": sessionID,
|
||||||
"SiteKey": cfg.Matrix.RecaptchaPublicKey,
|
"SiteKey": cfg.Matrix.RecaptchaPublicKey,
|
||||||
|
}
|
||||||
|
ServeTemplate(w, RecaptchaTemplate, data)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
return util.JSONResponse{
|
||||||
if err := t.Execute(w, data); err != nil {
|
Code: 404,
|
||||||
panic(err)
|
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
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ func MakeHTMLAPI(metricsName string, f func(http.ResponseWriter, *http.Request)
|
||||||
span := opentracing.StartSpan(metricsName)
|
span := opentracing.StartSpan(metricsName)
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span))
|
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 {
|
h := util.MakeJSONAPI(util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
|
||||||
return err
|
return err
|
||||||
}))
|
}))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue