1// Copyright 2020 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package inthttp
16
17import (
18	"encoding/json"
19	"net/http"
20
21	"github.com/gorilla/mux"
22	"github.com/matrix-org/dendrite/internal/httputil"
23	"github.com/matrix-org/dendrite/keyserver/api"
24	"github.com/matrix-org/util"
25)
26
27func AddRoutes(internalAPIMux *mux.Router, s api.KeyInternalAPI) {
28	internalAPIMux.Handle(InputDeviceListUpdatePath,
29		httputil.MakeInternalAPI("inputDeviceListUpdate", func(req *http.Request) util.JSONResponse {
30			request := api.InputDeviceListUpdateRequest{}
31			response := api.InputDeviceListUpdateResponse{}
32			if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
33				return util.MessageResponse(http.StatusBadRequest, err.Error())
34			}
35			s.InputDeviceListUpdate(req.Context(), &request, &response)
36			return util.JSONResponse{Code: http.StatusOK, JSON: &response}
37		}),
38	)
39	internalAPIMux.Handle(PerformClaimKeysPath,
40		httputil.MakeInternalAPI("performClaimKeys", func(req *http.Request) util.JSONResponse {
41			request := api.PerformClaimKeysRequest{}
42			response := api.PerformClaimKeysResponse{}
43			if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
44				return util.MessageResponse(http.StatusBadRequest, err.Error())
45			}
46			s.PerformClaimKeys(req.Context(), &request, &response)
47			return util.JSONResponse{Code: http.StatusOK, JSON: &response}
48		}),
49	)
50	internalAPIMux.Handle(PerformDeleteKeysPath,
51		httputil.MakeInternalAPI("performDeleteKeys", func(req *http.Request) util.JSONResponse {
52			request := api.PerformDeleteKeysRequest{}
53			response := api.PerformDeleteKeysResponse{}
54			if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
55				return util.MessageResponse(http.StatusBadRequest, err.Error())
56			}
57			s.PerformDeleteKeys(req.Context(), &request, &response)
58			return util.JSONResponse{Code: http.StatusOK, JSON: &response}
59		}),
60	)
61	internalAPIMux.Handle(PerformUploadKeysPath,
62		httputil.MakeInternalAPI("performUploadKeys", func(req *http.Request) util.JSONResponse {
63			request := api.PerformUploadKeysRequest{}
64			response := api.PerformUploadKeysResponse{}
65			if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
66				return util.MessageResponse(http.StatusBadRequest, err.Error())
67			}
68			s.PerformUploadKeys(req.Context(), &request, &response)
69			return util.JSONResponse{Code: http.StatusOK, JSON: &response}
70		}),
71	)
72	internalAPIMux.Handle(PerformUploadDeviceKeysPath,
73		httputil.MakeInternalAPI("performUploadDeviceKeys", func(req *http.Request) util.JSONResponse {
74			request := api.PerformUploadDeviceKeysRequest{}
75			response := api.PerformUploadDeviceKeysResponse{}
76			if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
77				return util.MessageResponse(http.StatusBadRequest, err.Error())
78			}
79			s.PerformUploadDeviceKeys(req.Context(), &request, &response)
80			return util.JSONResponse{Code: http.StatusOK, JSON: &response}
81		}),
82	)
83	internalAPIMux.Handle(PerformUploadDeviceSignaturesPath,
84		httputil.MakeInternalAPI("performUploadDeviceSignatures", func(req *http.Request) util.JSONResponse {
85			request := api.PerformUploadDeviceSignaturesRequest{}
86			response := api.PerformUploadDeviceSignaturesResponse{}
87			if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
88				return util.MessageResponse(http.StatusBadRequest, err.Error())
89			}
90			s.PerformUploadDeviceSignatures(req.Context(), &request, &response)
91			return util.JSONResponse{Code: http.StatusOK, JSON: &response}
92		}),
93	)
94	internalAPIMux.Handle(QueryKeysPath,
95		httputil.MakeInternalAPI("queryKeys", func(req *http.Request) util.JSONResponse {
96			request := api.QueryKeysRequest{}
97			response := api.QueryKeysResponse{}
98			if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
99				return util.MessageResponse(http.StatusBadRequest, err.Error())
100			}
101			s.QueryKeys(req.Context(), &request, &response)
102			return util.JSONResponse{Code: http.StatusOK, JSON: &response}
103		}),
104	)
105	internalAPIMux.Handle(QueryOneTimeKeysPath,
106		httputil.MakeInternalAPI("queryOneTimeKeys", func(req *http.Request) util.JSONResponse {
107			request := api.QueryOneTimeKeysRequest{}
108			response := api.QueryOneTimeKeysResponse{}
109			if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
110				return util.MessageResponse(http.StatusBadRequest, err.Error())
111			}
112			s.QueryOneTimeKeys(req.Context(), &request, &response)
113			return util.JSONResponse{Code: http.StatusOK, JSON: &response}
114		}),
115	)
116	internalAPIMux.Handle(QueryDeviceMessagesPath,
117		httputil.MakeInternalAPI("queryDeviceMessages", func(req *http.Request) util.JSONResponse {
118			request := api.QueryDeviceMessagesRequest{}
119			response := api.QueryDeviceMessagesResponse{}
120			if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
121				return util.MessageResponse(http.StatusBadRequest, err.Error())
122			}
123			s.QueryDeviceMessages(req.Context(), &request, &response)
124			return util.JSONResponse{Code: http.StatusOK, JSON: &response}
125		}),
126	)
127	internalAPIMux.Handle(QueryKeyChangesPath,
128		httputil.MakeInternalAPI("queryKeyChanges", func(req *http.Request) util.JSONResponse {
129			request := api.QueryKeyChangesRequest{}
130			response := api.QueryKeyChangesResponse{}
131			if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
132				return util.MessageResponse(http.StatusBadRequest, err.Error())
133			}
134			s.QueryKeyChanges(req.Context(), &request, &response)
135			return util.JSONResponse{Code: http.StatusOK, JSON: &response}
136		}),
137	)
138	internalAPIMux.Handle(QuerySignaturesPath,
139		httputil.MakeInternalAPI("querySignatures", func(req *http.Request) util.JSONResponse {
140			request := api.QuerySignaturesRequest{}
141			response := api.QuerySignaturesResponse{}
142			if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
143				return util.MessageResponse(http.StatusBadRequest, err.Error())
144			}
145			s.QuerySignatures(req.Context(), &request, &response)
146			return util.JSONResponse{Code: http.StatusOK, JSON: &response}
147		}),
148	)
149}
150