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