2017-05-19 16:06:41 +01:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2017-08-03 15:10:39 +01:00
|
|
|
"net/http"
|
2017-09-06 12:38:22 +01:00
|
|
|
"time"
|
2017-08-03 15:10:39 +01:00
|
|
|
|
2017-05-19 16:06:41 +01:00
|
|
|
"github.com/gorilla/mux"
|
2017-09-13 13:31:46 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
2017-06-07 14:32:53 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/producers"
|
2017-09-04 13:14:01 +01:00
|
|
|
"github.com/matrix-org/dendrite/common"
|
2017-06-19 15:21:04 +01:00
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2017-06-07 14:32:53 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-05-19 16:06:41 +01:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-06-07 14:32:53 +01:00
|
|
|
pathPrefixV2Keys = "/_matrix/key/v2"
|
|
|
|
pathPrefixV1Federation = "/_matrix/federation/v1"
|
2017-05-19 16:06:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Setup registers HTTP handlers with the given ServeMux.
|
2017-06-07 14:32:53 +01:00
|
|
|
func Setup(
|
2017-08-03 15:10:39 +01:00
|
|
|
apiMux *mux.Router,
|
2017-06-19 15:21:04 +01:00
|
|
|
cfg config.Dendrite,
|
2017-06-07 14:32:53 +01:00
|
|
|
query api.RoomserverQueryAPI,
|
|
|
|
producer *producers.RoomserverProducer,
|
|
|
|
keys gomatrixserverlib.KeyRing,
|
|
|
|
federation *gomatrixserverlib.FederationClient,
|
2017-09-13 13:31:46 +01:00
|
|
|
accountDB *accounts.Database,
|
2017-06-07 14:32:53 +01:00
|
|
|
) {
|
2017-05-19 16:06:41 +01:00
|
|
|
v2keysmux := apiMux.PathPrefix(pathPrefixV2Keys).Subrouter()
|
2017-06-07 14:32:53 +01:00
|
|
|
v1fedmux := apiMux.PathPrefix(pathPrefixV1Federation).Subrouter()
|
2017-05-19 16:06:41 +01:00
|
|
|
|
2017-09-28 14:50:40 +01:00
|
|
|
localKeys := common.MakeExternalAPI("localkeys", func(req *http.Request) util.JSONResponse {
|
2017-10-25 11:27:44 +01:00
|
|
|
return LocalKeys(cfg)
|
2017-05-25 16:08:28 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
// Ignore the {keyID} argument as we only have a single server key so we always
|
|
|
|
// return that key.
|
|
|
|
// Even if we had more than one server key, we would probably still ignore the
|
|
|
|
// {keyID} argument and always return a response containing all of the keys.
|
2017-09-13 11:08:11 +01:00
|
|
|
v2keysmux.Handle("/server/{keyID}", localKeys).Methods("GET")
|
|
|
|
v2keysmux.Handle("/server/", localKeys).Methods("GET")
|
2017-05-19 16:06:41 +01:00
|
|
|
|
2017-09-04 13:14:01 +01:00
|
|
|
v1fedmux.Handle("/send/{txnID}/", common.MakeFedAPI(
|
|
|
|
"federation_send", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
|
|
|
vars := mux.Vars(httpReq)
|
2017-10-11 18:16:53 +01:00
|
|
|
return Send(
|
2017-09-04 13:14:01 +01:00
|
|
|
httpReq, request, gomatrixserverlib.TransactionID(vars["txnID"]),
|
2017-06-07 14:32:53 +01:00
|
|
|
cfg, query, producer, keys, federation,
|
|
|
|
)
|
|
|
|
},
|
2017-09-13 11:08:11 +01:00
|
|
|
)).Methods("PUT", "OPTIONS")
|
2017-08-23 15:13:47 +01:00
|
|
|
|
2017-09-04 13:14:01 +01:00
|
|
|
v1fedmux.Handle("/invite/{roomID}/{eventID}", common.MakeFedAPI(
|
|
|
|
"federation_invite", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
|
|
|
vars := mux.Vars(httpReq)
|
2017-10-11 18:16:53 +01:00
|
|
|
return Invite(
|
2017-09-04 13:14:01 +01:00
|
|
|
httpReq, request, vars["roomID"], vars["eventID"],
|
2017-08-23 15:13:47 +01:00
|
|
|
cfg, producer, keys,
|
|
|
|
)
|
|
|
|
},
|
2017-09-13 11:08:11 +01:00
|
|
|
)).Methods("PUT", "OPTIONS")
|
2017-09-06 12:38:22 +01:00
|
|
|
|
2017-09-28 14:50:40 +01:00
|
|
|
v1fedmux.Handle("/3pid/onbind", common.MakeExternalAPI("3pid_onbind",
|
2017-09-11 19:08:24 +01:00
|
|
|
func(req *http.Request) util.JSONResponse {
|
2017-10-11 18:16:53 +01:00
|
|
|
return CreateInvitesFrom3PIDInvites(req, query, cfg, producer, federation, accountDB)
|
2017-09-08 15:17:12 +01:00
|
|
|
},
|
2017-09-13 11:08:11 +01:00
|
|
|
)).Methods("POST", "OPTIONS")
|
2017-09-08 15:17:12 +01:00
|
|
|
|
2017-09-12 17:15:13 +01:00
|
|
|
v1fedmux.Handle("/exchange_third_party_invite/{roomID}", common.MakeFedAPI(
|
|
|
|
"exchange_third_party_invite", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
|
|
|
vars := mux.Vars(httpReq)
|
2017-10-11 18:16:53 +01:00
|
|
|
return ExchangeThirdPartyInvite(
|
2017-09-12 17:15:13 +01:00
|
|
|
httpReq, request, vars["roomID"], query, cfg, federation, producer,
|
|
|
|
)
|
|
|
|
},
|
2017-09-13 11:08:11 +01:00
|
|
|
)).Methods("PUT", "OPTIONS")
|
2017-09-12 17:15:13 +01:00
|
|
|
|
2017-09-06 12:38:22 +01:00
|
|
|
v1fedmux.Handle("/event/{eventID}", common.MakeFedAPI(
|
|
|
|
"federation_get_event", cfg.Matrix.ServerName, keys,
|
|
|
|
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
|
|
|
|
vars := mux.Vars(httpReq)
|
2017-10-25 11:27:44 +01:00
|
|
|
return GetEvent(
|
2017-09-13 13:37:50 +01:00
|
|
|
httpReq.Context(), request, cfg, query, time.Now(), keys, vars["eventID"],
|
2017-09-06 12:38:22 +01:00
|
|
|
)
|
|
|
|
},
|
2017-09-13 11:08:11 +01:00
|
|
|
)).Methods("GET")
|
2017-09-25 11:16:47 +01:00
|
|
|
|
2017-09-28 14:50:40 +01:00
|
|
|
v1fedmux.Handle("/version", common.MakeExternalAPI(
|
2017-09-25 11:16:47 +01:00
|
|
|
"federation_version",
|
|
|
|
func(httpReq *http.Request) util.JSONResponse {
|
2017-10-25 11:27:44 +01:00
|
|
|
return Version()
|
2017-09-25 11:16:47 +01:00
|
|
|
},
|
|
|
|
)).Methods("GET")
|
2017-05-19 16:06:41 +01:00
|
|
|
}
|