Add POST request handling

This commit is contained in:
Parminder Singh 2018-03-07 16:14:35 +05:30
parent e949cc3eda
commit 701555974e

View file

@ -51,7 +51,7 @@ function captchaDone() {
<p> <p>
Please verify that you're not a robot. Please verify that you're not a robot.
</p> </p>
<input type="hidden" name="session" value="{{.Session}}" /> <input type="hidden" name="session" value="{{.Session}}" />
<div class="g-recaptcha" <div class="g-recaptcha"
data-sitekey="{{.SiteKey}}" data-sitekey="{{.SiteKey}}"
data-callback="captchaDone"> data-callback="captchaDone">
@ -103,29 +103,43 @@ func ServeTemplate(w http.ResponseWriter, templateHTML string, data map[string]s
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 { ) *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" { if req.Method == "GET" {
// Handle Recaptcha // Handle Recaptcha
if authType == authtypes.LoginTypeRecaptcha { if authType == authtypes.LoginTypeRecaptcha {
data := map[string]string{ ServeRecaptcha()
"MyUrl": req.URL.String(),
"Session": sessionID,
"SiteKey": cfg.Matrix.RecaptchaPublicKey,
}
ServeTemplate(w, RecaptchaTemplate, data)
return nil return nil
} }
return util.JSONResponse{ return &util.JSONResponse{
Code: 404, Code: 404,
JSON: jsonerror.NotFound("Unknown auth stage type"), JSON: jsonerror.NotFound("Unknown auth stage type"),
} }
} else if req.Method == "POST" { } else if req.Method == "POST" {
// TODO: Handle POST requests too clientIP := req.RemoteAddr
// Check Recaptcha response := req.Form.Get("g-recaptcha-response")
// Serve success if err = validateRecaptcha(cfg, response, clientIP), if resErr != nil {
// Else serve Recaptcha again ServeRecaptcha()
return nil
}
ServeSuccess()
return nil
} }
return util.JSONResponse{ return &util.JSONResponse{
Code: 405, Code: 405,
JSON: jsonerror.NotFound("Bad method"), JSON: jsonerror.NotFound("Bad method"),
} }