Ejemplo n.º 1
0
func (h *MatrixLoginHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	defer req.Body.Close()
	if req.Method != "POST" {
		h.proxy.ProxyHTTP(w, req.Method, req.URL, req.Body, req.ContentLength, req.Header)
		return
	}

	body, err := ioutil.ReadAll(req.Body)
	if err != nil {
		proxy.LogAndReplyError(w, &proxy.HTTPError{err, 500, "M_UKNOWN", "Error reading request"})
	}

	var login matrixLoginRequest
	if err := json.Unmarshal(body, &login); err != nil {
		proxy.LogAndReplyError(w, &proxy.HTTPError{err, 400, "M_BAD_JSON", "Error decoding JSON"})
		return
	}

	switch login.Type {
	case "m.login.password":
		if login.Medium != "" && login.Address != "" {
			login.UserID, err = h.db.matrixIDFor3PID(login.Medium, login.Address)
			if err != nil {
				proxy.LogAndReplyError(w, &proxy.HTTPError{err, 403, "M_FORBIDDEN", "Forbidden"})
				return
			}
		}

		response, err := h.loginPassword(login.UserID, login.Password)
		if err != nil {
			proxy.LogAndReplyError(w, err)
			return
		}

		proxy.SetHeaders(w)

		json.NewEncoder(w).Encode(response)
	default:
		h.proxy.ProxyHTTP(w, req.Method, req.URL, bytes.NewBuffer(body), req.ContentLength, req.Header)
	}
}
Ejemplo n.º 2
0
func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	proxy.SetHeaders(w)
	w.Write(h.resp.Load().([]byte))
}