Minor refactoring

This commit is contained in:
Parminder Singh 2019-08-04 03:37:51 +05:30
parent e32c609bf2
commit d9b80305d4

View file

@ -35,7 +35,6 @@ const recaptchaTemplate = `
<script src="https://www.google.com/recaptcha/api.js" <script src="https://www.google.com/recaptcha/api.js"
async defer></script> async defer></script>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script> <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<link rel="stylesheet" href="/_matrix/static/client/register/style.css">
<script> <script>
function captchaDone() { function captchaDone() {
$('#registrationForm').submit(); $('#registrationForm').submit();
@ -109,13 +108,10 @@ func AuthFallback(
sessionID := req.URL.Query().Get("session") sessionID := req.URL.Query().Get("session")
if sessionID == "" { if sessionID == "" {
w.WriteHeader(http.StatusBadRequest) return writeErrorMessage(w, req,
_, err := w.Write([]byte("Session ID not provided")) "Session ID not provided",
if err != nil { http.StatusBadRequest,
res := httputil.LogThenError(req, err) )
return &res
}
return nil
} }
serveRecaptcha := func() { serveRecaptcha := func() {
@ -135,21 +131,25 @@ func AuthFallback(
if req.Method == http.MethodGet { if req.Method == http.MethodGet {
// Handle Recaptcha // Handle Recaptcha
if authType == authtypes.LoginTypeRecaptcha { if authType == authtypes.LoginTypeRecaptcha {
if cfg.Matrix.RecaptchaEnabled {
if cfg.Matrix.RecaptchaPublicKey == "" { if cfg.Matrix.RecaptchaPublicKey == "" {
w.WriteHeader(http.StatusInternalServerError) return writeErrorMessage(w, req,
_, err := w.Write([]byte("This Homeserver doesn't have a recaptcha public key")) "This Homeserver doesn't have a recaptcha public key",
if err != nil { http.StatusInternalServerError,
res := httputil.LogThenError(req, err) )
return &res
} }
return nil } else {
return writeErrorMessage(w, req,
"Recaptcha login is disabled on this Homeserver",
http.StatusBadRequest,
)
} }
serveRecaptcha() serveRecaptcha()
return nil return nil
} }
return &util.JSONResponse{ return &util.JSONResponse{
Code: 404, Code: http.StatusNotFound,
JSON: jsonerror.NotFound("Unknown auth stage type"), JSON: jsonerror.NotFound("Unknown auth stage type"),
} }
} else if req.Method == http.MethodPost { } else if req.Method == http.MethodPost {
@ -173,7 +173,21 @@ func AuthFallback(
return nil return nil
} }
return &util.JSONResponse{ return &util.JSONResponse{
Code: 405, Code: http.StatusMethodNotAllowed,
JSON: jsonerror.NotFound("Bad method"), JSON: jsonerror.NotFound("Bad method"),
} }
} }
// WriteErrorMessage writes an error response with the given header and message
func writeErrorMessage(
w http.ResponseWriter, req *http.Request,
message string, header int,
) *util.JSONResponse {
w.WriteHeader(header)
_, err := w.Write([]byte(message))
if err != nil {
res := httputil.LogThenError(req, err)
return &res
}
return nil
}