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