2020-06-04 15:26:35 +00:00
|
|
|
package inthttp
|
2020-05-27 09:19:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2020-06-04 15:26:35 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2020-06-12 13:55:57 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
2020-05-27 09:19:24 +00:00
|
|
|
"github.com/matrix-org/dendrite/serverkeyapi/api"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
2020-06-05 15:42:01 +00:00
|
|
|
func AddRoutes(s api.ServerKeyInternalAPI, internalAPIMux *mux.Router, cache caching.ServerKeyCache) {
|
2020-06-04 15:26:35 +00:00
|
|
|
internalAPIMux.Handle(ServerKeyQueryPublicKeyPath,
|
2020-06-12 13:55:57 +00:00
|
|
|
httputil.MakeInternalAPI("queryPublicKeys", func(req *http.Request) util.JSONResponse {
|
2020-05-27 09:19:24 +00:00
|
|
|
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 14:40:23 +00:00
|
|
|
keys, err := s.FetchKeys(req.Context(), request.Requests)
|
2020-05-27 09:19:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2020-06-04 14:40:23 +00:00
|
|
|
response.Results = keys
|
2020-05-27 09:19:24 +00:00
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
2020-06-04 15:26:35 +00:00
|
|
|
internalAPIMux.Handle(ServerKeyInputPublicKeyPath,
|
2020-06-12 13:55:57 +00:00
|
|
|
httputil.MakeInternalAPI("inputPublicKeys", func(req *http.Request) util.JSONResponse {
|
2020-05-27 09:19:24 +00:00
|
|
|
request := api.InputPublicKeysRequest{}
|
|
|
|
response := api.InputPublicKeysResponse{}
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.MessageResponse(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
2020-06-12 10:07:26 +00:00
|
|
|
if err := s.StoreKeys(req.Context(), request.Keys); err != nil {
|
2020-05-27 09:19:24 +00:00
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|