2020-06-04 17:26:35 +02:00
|
|
|
package inthttp
|
2020-05-27 11:19:24 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-06-04 17:26:35 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2020-05-27 11:19:24 +02:00
|
|
|
"github.com/matrix-org/dendrite/serverkeyapi/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
2020-06-05 17:42:01 +02:00
|
|
|
func AddRoutes(s api.ServerKeyInternalAPI, internalAPIMux *mux.Router, cache caching.ServerKeyCache) {
|
2020-06-04 17:26:35 +02:00
|
|
|
internalAPIMux.Handle(ServerKeyQueryPublicKeyPath,
|
2020-05-27 11:19:24 +02:00
|
|
|
internal.MakeInternalAPI("queryPublicKeys", func(req *http.Request) util.JSONResponse {
|
|
|
|
request := api.QueryPublicKeysRequest{}
|
|
|
|
response := api.QueryPublicKeysResponse{}
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.MessageResponse(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
2020-06-04 16:40:23 +02:00
|
|
|
keys, err := s.FetchKeys(req.Context(), request.Requests)
|
2020-05-27 11:19:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2020-06-04 16:40:23 +02:00
|
|
|
response.Results = keys
|
2020-05-27 11:19:24 +02:00
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
2020-06-04 17:26:35 +02:00
|
|
|
internalAPIMux.Handle(ServerKeyInputPublicKeyPath,
|
2020-05-27 11:19:24 +02:00
|
|
|
internal.MakeInternalAPI("inputPublicKeys", func(req *http.Request) util.JSONResponse {
|
|
|
|
request := api.InputPublicKeysRequest{}
|
|
|
|
response := api.InputPublicKeysResponse{}
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.MessageResponse(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
store := make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult)
|
|
|
|
for req, res := range request.Keys {
|
|
|
|
store[req] = res
|
2020-06-04 17:26:35 +02:00
|
|
|
cache.StoreServerKey(req, res)
|
2020-05-27 11:19:24 +02:00
|
|
|
}
|
|
|
|
if err := s.StoreKeys(req.Context(), store); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|