call AppServices user ID regexes to check + get user IDs from AppServices instead of just making up user IDs in the homeserver

This commit is contained in:
Derek Meer 2018-08-02 11:58:03 -07:00
parent 916ab601a8
commit b49f014421

View file

@ -1,10 +1,12 @@
package routing package routing
import ( import (
"encoding/json"
"github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/common/config"
"github.com/matrix-org/util" "github.com/matrix-org/util"
"io/ioutil"
"net/http" "net/http"
"strings"
"unicode"
) )
// URIToUIDResponse represents response to an AppService URI to User Id // URIToUIDResponse represents response to an AppService URI to User Id
@ -17,10 +19,7 @@ type URIToUIDResponse struct {
// enables users to contact App Service users directly by taking an encoded // enables users to contact App Service users directly by taking an encoded
// URI and turning it into a Matrix ID on the homeserver. // URI and turning it into a Matrix ID on the homeserver.
// https://matrix.org/docs/spec/application_service/unstable.html#user-ids // https://matrix.org/docs/spec/application_service/unstable.html#user-ids
// tel://123.1234 -> @tel_//123.1234:matrix.org
// mailto:test@matrix.org -> @mailto_test_matrix.org:matrix.org
func URIToUID(req *http.Request, cfg config.Dendrite) util.JSONResponse { func URIToUID(req *http.Request, cfg config.Dendrite) util.JSONResponse {
homeserver := cfg.Matrix.ServerName
uri := req.URL.Query().Get("uri") uri := req.URL.Query().Get("uri")
if uri == "" { if uri == "" {
return util.JSONResponse{ return util.JSONResponse{
@ -28,25 +27,31 @@ func URIToUID(req *http.Request, cfg config.Dendrite) util.JSONResponse {
JSON: nil, JSON: nil,
} }
} }
// V1 just replaces the illegal characters (from baseReqURL := "http://" + string(cfg.Matrix.ServerName) + "/_matrix/app/unstable/thirdparty/user/"
// https://matrix.org/docs/spec/appendices.html#id12) with _ //appServices := cfg.Derived.ApplicationServices
// TODO: Come up with a better way to turn URIs into User IDs for _, appservice := range cfg.Derived.ApplicationServices {
w := strings.FieldsFunc(strings.ToLower(uri), func(r rune) bool { // Check all the fields associated with each application service
if unicode.IsDigit(r) || unicode.IsLetter(r) { if appservice.IsInterestedInUserID(uri) {
return false // call the application service
} reqURL := baseReqURL + appservice.ID + "?access_token=" + appservice.HSToken +
switch r { "&fields=" + uri
case '.', '_', '=', '-', '/': resp, err := http.Get(reqURL)
return false // take the first successful match and send that back to the user
} if err == nil {
return true body, _ := ioutil.ReadAll(resp.Body)
}) respMap := map[string]interface{}{}
json.Unmarshal(body, &respMap)
// compile user ID and return if userID, ok := respMap["userid"].(string); ok {
userID := "@" + strings.Join(w, "_") + ":" + homeserver
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusOK, Code: http.StatusOK,
JSON: URIToUIDResponse{UserID: userID}, JSON: URIToUIDResponse{UserID: userID},
} }
} }
}
}
}
return util.JSONResponse{
Code: http.StatusNotFound,
JSON: jsonerror.NotFound("URI not supported by app services"),
}
}