From 01ce9abc5b87efe11f735b83d844dee5402d20ba Mon Sep 17 00:00:00 2001 From: Piotr Kozimor Date: Tue, 20 Apr 2021 14:34:02 +0200 Subject: [PATCH] Delete duplicated /register/email/requestToken endpoint --- clientapi/routing/register.go | 96 ----------------------------------- clientapi/routing/routing.go | 7 --- 2 files changed, 103 deletions(-) diff --git a/clientapi/routing/register.go b/clientapi/routing/register.go index 85f51ad71..feea3b049 100644 --- a/clientapi/routing/register.go +++ b/clientapi/routing/register.go @@ -16,7 +16,6 @@ package routing import ( - "bytes" "context" "crypto/hmac" "crypto/sha1" @@ -1104,98 +1103,3 @@ func RegisterAvailable( }, } } - -func RegisterEmailRequestToken( - req *http.Request, - cfg *config.ClientAPI, -) util.JSONResponse { - var r registerEmailRequestTokenRequest - resErr := httputil.UnmarshalJSONRequest(req, &r) - if resErr != nil { - return *resErr - } - idServer := r.IdServer - idAccessToken := r.IdAccessToken - // if !isServerTrusted(idServer, cfg.Matrix.TrustedIDServers) { - // return util.JSONResponse{ - // Code: http.StatusForbidden, - // JSON: jsonerror.NotTrusted(fmt.Sprintf("identity server %s is not trusted.", idServer)), - // } - // } - r.IdServer = "" - r.IdAccessToken = "" - return requestEmailTokenFromIdServer(req.Context(), idServer, idAccessToken, &r) -} - -func isServerTrusted(s string, trusted []string) bool { - for _, t := range trusted { - if s == t { - return true - } - } - return false -} - -func requestEmailTokenFromIdServer( - ctx context.Context, - idServer, idAccessToken string, - r *registerEmailRequestTokenRequest, -) util.JSONResponse { - tr := &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } - client := &http.Client{Transport: tr} - b := bytes.Buffer{} - enc := json.NewEncoder(&b) - err := enc.Encode(r) - if err != nil { - util.GetLogger(ctx).WithError(err).Error("enc.Encode failed") - return jsonerror.InternalServerError() - } - idReq, err := http.NewRequestWithContext( - ctx, - "POST", - fmt.Sprintf( - "https://%s/_matrix/identity/api/v1/validate/email/requestToken", - idServer), - &b) - if err != nil { - util.GetLogger(ctx).WithError(err).Error("http.NewRequestWithContext failed") - return jsonerror.InternalServerError() - } - idReq.Header.Add("Content-Type", "application/json") - idResp, err := client.Do(idReq) - if err != nil { - util.GetLogger(ctx).WithError(err).Errorf("failed to connect to identity server %s", idServer) - return jsonerror.InternalServerError() - } - defer idResp.Body.Close() - decoder := json.NewDecoder(idResp.Body) - if idResp.StatusCode < 300 { - var resp registerEmailRequestTokenResponse - decoder.Decode(&resp) - if err != nil { - util.GetLogger(ctx).WithError(err).Error("failed to decode identity server response body") - return jsonerror.InternalServerError() - } - return util.JSONResponse{ - Code: idResp.StatusCode, - JSON: resp, - } - } - if idResp.StatusCode < 500 { - var resp jsonerror.MatrixError - decoder.Decode(&resp) - if err != nil { - util.GetLogger(ctx).WithError(err).Error("failed to decode identity server response body") - return jsonerror.InternalServerError() - } - return util.JSONResponse{ - Code: idResp.StatusCode, - JSON: resp, - } - } - // We got 500 status code from indentity server - util.GetLogger(ctx).WithError(err).Error("identity server internal server error") - return jsonerror.InternalServerError() -} diff --git a/clientapi/routing/routing.go b/clientapi/routing/routing.go index 0022f5161..9f980e0a9 100644 --- a/clientapi/routing/routing.go +++ b/clientapi/routing/routing.go @@ -312,13 +312,6 @@ func Setup( return RegisterAvailable(req, cfg, accountDB) })).Methods(http.MethodGet, http.MethodOptions) - r0mux.Handle("/register/email/requestToken", httputil.MakeExternalAPI("registerEmailRequestToken", func(req *http.Request) util.JSONResponse { - if r := rateLimits.rateLimit(req); r != nil { - return *r - } - return RegisterEmailRequestToken(req, cfg) - })).Methods(http.MethodPost, http.MethodOptions) - r0mux.Handle("/directory/room/{roomAlias}", httputil.MakeExternalAPI("directory_room", func(req *http.Request) util.JSONResponse { vars, err := httputil.URLDecodeMapValues(mux.Vars(req))