Delete duplicated /register/email/requestToken endpoint

This commit is contained in:
Piotr Kozimor 2021-04-20 14:34:02 +02:00
parent 7d9304ea63
commit 01ce9abc5b
2 changed files with 0 additions and 103 deletions

View file

@ -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()
}

View file

@ -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))