2021-08-04 18:56:29 +02:00
|
|
|
// Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth"
|
2021-08-24 12:44:03 +02:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
2021-08-04 18:56:29 +02:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2023-02-20 14:58:03 +01:00
|
|
|
"github.com/matrix-org/dendrite/userapi/api"
|
2023-05-10 00:46:49 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2021-08-04 18:56:29 +02:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
2021-08-24 12:44:03 +02:00
|
|
|
type crossSigningRequest struct {
|
|
|
|
api.PerformUploadDeviceKeysRequest
|
|
|
|
Auth newPasswordAuth `json:"auth"`
|
|
|
|
}
|
|
|
|
|
2021-08-04 18:56:29 +02:00
|
|
|
func UploadCrossSigningDeviceKeys(
|
|
|
|
req *http.Request, userInteractiveAuth *auth.UserInteractive,
|
2023-02-20 14:58:03 +01:00
|
|
|
keyserverAPI api.ClientKeyAPI, device *api.Device,
|
|
|
|
accountAPI api.ClientUserAPI, cfg *config.ClientAPI,
|
2021-08-04 18:56:29 +02:00
|
|
|
) util.JSONResponse {
|
2021-08-24 12:44:03 +02:00
|
|
|
uploadReq := &crossSigningRequest{}
|
2021-08-04 18:56:29 +02:00
|
|
|
uploadRes := &api.PerformUploadDeviceKeysResponse{}
|
|
|
|
|
2021-08-24 12:44:03 +02:00
|
|
|
resErr := httputil.UnmarshalJSONRequest(req, &uploadReq)
|
|
|
|
if resErr != nil {
|
|
|
|
return *resErr
|
2021-08-04 18:56:29 +02:00
|
|
|
}
|
2021-08-24 12:44:03 +02:00
|
|
|
sessionID := uploadReq.Auth.Session
|
|
|
|
if sessionID == "" {
|
|
|
|
sessionID = util.RandomString(sessionIDLength)
|
2021-08-04 18:56:29 +02:00
|
|
|
}
|
2021-08-24 12:44:03 +02:00
|
|
|
if uploadReq.Auth.Type != authtypes.LoginTypePassword {
|
2021-08-04 18:56:29 +02:00
|
|
|
return util.JSONResponse{
|
2021-08-24 12:44:03 +02:00
|
|
|
Code: http.StatusUnauthorized,
|
|
|
|
JSON: newUserInteractiveResponse(
|
|
|
|
sessionID,
|
|
|
|
[]authtypes.Flow{
|
|
|
|
{
|
|
|
|
Stages: []authtypes.LoginType{authtypes.LoginTypePassword},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
),
|
2021-08-04 18:56:29 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-24 12:44:03 +02:00
|
|
|
typePassword := auth.LoginTypePassword{
|
2022-03-24 22:45:44 +01:00
|
|
|
GetAccountByPassword: accountAPI.QueryAccountByPassword,
|
2021-08-24 12:44:03 +02:00
|
|
|
Config: cfg,
|
|
|
|
}
|
|
|
|
if _, authErr := typePassword.Login(req.Context(), &uploadReq.Auth.PasswordRequest); authErr != nil {
|
|
|
|
return *authErr
|
|
|
|
}
|
2022-02-25 14:33:02 +01:00
|
|
|
sessions.addCompletedSessionStage(sessionID, authtypes.LoginTypePassword)
|
2021-08-04 18:56:29 +02:00
|
|
|
|
|
|
|
uploadReq.UserID = device.UserID
|
2023-05-10 00:46:49 +02:00
|
|
|
keyserverAPI.PerformUploadDeviceKeys(req.Context(), &uploadReq.PerformUploadDeviceKeysRequest, uploadRes)
|
2021-08-04 18:56:29 +02:00
|
|
|
|
|
|
|
if err := uploadRes.Error; err != nil {
|
|
|
|
switch {
|
|
|
|
case err.IsInvalidSignature:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.InvalidSignature(err.Error()),
|
2021-08-04 18:56:29 +02:00
|
|
|
}
|
|
|
|
case err.IsMissingParam:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.MissingParam(err.Error()),
|
2021-08-04 18:56:29 +02:00
|
|
|
}
|
2021-08-17 14:44:30 +02:00
|
|
|
case err.IsInvalidParam:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.InvalidParam(err.Error()),
|
2021-08-17 14:44:30 +02:00
|
|
|
}
|
2021-08-04 18:56:29 +02:00
|
|
|
default:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Unknown(err.Error()),
|
2021-08-04 18:56:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 14:58:03 +01:00
|
|
|
func UploadCrossSigningDeviceSignatures(req *http.Request, keyserverAPI api.ClientKeyAPI, device *api.Device) util.JSONResponse {
|
2021-08-04 18:56:29 +02:00
|
|
|
uploadReq := &api.PerformUploadDeviceSignaturesRequest{}
|
|
|
|
uploadRes := &api.PerformUploadDeviceSignaturesResponse{}
|
|
|
|
|
|
|
|
if err := httputil.UnmarshalJSONRequest(req, &uploadReq.Signatures); err != nil {
|
|
|
|
return *err
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadReq.UserID = device.UserID
|
2023-05-10 00:46:49 +02:00
|
|
|
keyserverAPI.PerformUploadDeviceSignatures(req.Context(), uploadReq, uploadRes)
|
2021-08-04 18:56:29 +02:00
|
|
|
|
|
|
|
if err := uploadRes.Error; err != nil {
|
|
|
|
switch {
|
|
|
|
case err.IsInvalidSignature:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.InvalidSignature(err.Error()),
|
2021-08-04 18:56:29 +02:00
|
|
|
}
|
|
|
|
case err.IsMissingParam:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.MissingParam(err.Error()),
|
2021-08-04 18:56:29 +02:00
|
|
|
}
|
2021-08-17 14:44:30 +02:00
|
|
|
case err.IsInvalidParam:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.InvalidParam(err.Error()),
|
2021-08-17 14:44:30 +02:00
|
|
|
}
|
2021-08-04 18:56:29 +02:00
|
|
|
default:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Unknown(err.Error()),
|
2021-08-04 18:56:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|