2022-03-03 11:40:53 +00:00
|
|
|
// Copyright 2021 Dan Peleg <dan@globekeeper.com>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-05-09 23:46:49 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-03-03 11:40:53 +00:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetPushers handles /_matrix/client/r0/pushers
|
|
|
|
func GetPushers(
|
|
|
|
req *http.Request, device *userapi.Device,
|
2022-05-05 13:17:38 +01:00
|
|
|
userAPI userapi.ClientUserAPI,
|
2022-03-03 11:40:53 +00:00
|
|
|
) util.JSONResponse {
|
|
|
|
var queryRes userapi.QueryPushersResponse
|
2022-11-11 16:41:37 +00:00
|
|
|
localpart, domain, err := gomatrixserverlib.SplitID('@', device.UserID)
|
2022-03-03 11:40:53 +00:00
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("SplitID failed")
|
2023-05-17 01:33:27 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2022-03-03 11:40:53 +00:00
|
|
|
}
|
|
|
|
err = userAPI.QueryPushers(req.Context(), &userapi.QueryPushersRequest{
|
2022-11-11 16:41:37 +00:00
|
|
|
Localpart: localpart,
|
|
|
|
ServerName: domain,
|
2022-03-03 11:40:53 +00:00
|
|
|
}, &queryRes)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("QueryPushers failed")
|
2023-05-17 01:33:27 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2022-03-03 11:40:53 +00:00
|
|
|
}
|
|
|
|
for i := range queryRes.Pushers {
|
|
|
|
queryRes.Pushers[i].SessionID = 0
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: queryRes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPusher handles /_matrix/client/r0/pushers/set
|
|
|
|
// This endpoint allows the creation, modification and deletion of pushers for this user ID.
|
|
|
|
// The behaviour of this endpoint varies depending on the values in the JSON body.
|
|
|
|
func SetPusher(
|
|
|
|
req *http.Request, device *userapi.Device,
|
2022-05-05 13:17:38 +01:00
|
|
|
userAPI userapi.ClientUserAPI,
|
2022-03-03 11:40:53 +00:00
|
|
|
) util.JSONResponse {
|
2022-11-11 16:41:37 +00:00
|
|
|
localpart, domain, err := gomatrixserverlib.SplitID('@', device.UserID)
|
2022-03-03 11:40:53 +00:00
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("SplitID failed")
|
2023-05-17 01:33:27 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2022-03-03 11:40:53 +00:00
|
|
|
}
|
|
|
|
body := userapi.PerformPusherSetRequest{}
|
|
|
|
if resErr := httputil.UnmarshalJSONRequest(req, &body); resErr != nil {
|
|
|
|
return *resErr
|
|
|
|
}
|
|
|
|
if len(body.AppID) > 64 {
|
|
|
|
return invalidParam("length of app_id must be no more than 64 characters")
|
|
|
|
}
|
|
|
|
if len(body.PushKey) > 512 {
|
|
|
|
return invalidParam("length of pushkey must be no more than 512 bytes")
|
|
|
|
}
|
|
|
|
uInt := body.Data["url"]
|
|
|
|
if uInt != nil {
|
|
|
|
u, ok := uInt.(string)
|
|
|
|
if !ok {
|
|
|
|
return invalidParam("url must be string")
|
|
|
|
}
|
|
|
|
if u != "" {
|
|
|
|
var pushUrl *url.URL
|
|
|
|
pushUrl, err = url.Parse(u)
|
|
|
|
if err != nil {
|
|
|
|
return invalidParam("malformed url passed")
|
|
|
|
}
|
|
|
|
if pushUrl.Scheme != "https" {
|
|
|
|
return invalidParam("only https scheme is allowed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
body.Localpart = localpart
|
2022-11-11 16:41:37 +00:00
|
|
|
body.ServerName = domain
|
2022-03-03 11:40:53 +00:00
|
|
|
body.SessionID = device.SessionID
|
|
|
|
err = userAPI.PerformPusherSet(req.Context(), &body, &struct{}{})
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("PerformPusherSet failed")
|
2023-05-17 01:33:27 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2022-03-03 11:40:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func invalidParam(msg string) util.JSONResponse {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.InvalidParam(msg),
|
2022-03-03 11:40:53 +00:00
|
|
|
}
|
|
|
|
}
|