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