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/clientapi/jsonerror"
|
|
|
|
"github.com/matrix-org/dendrite/keyserver/api"
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
"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,
|
2022-05-05 14:17:38 +02:00
|
|
|
keyserverAPI api.ClientKeyAPI, device *userapi.Device,
|
|
|
|
accountAPI userapi.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
|
2022-08-11 16:29:33 +02:00
|
|
|
if err := keyserverAPI.PerformUploadDeviceKeys(req.Context(), &uploadReq.PerformUploadDeviceKeysRequest, uploadRes); err != nil {
|
|
|
|
return jsonerror.InternalAPIError(req.Context(), err)
|
|
|
|
}
|
2021-08-04 18:56:29 +02:00
|
|
|
|
|
|
|
if err := uploadRes.Error; err != nil {
|
|
|
|
switch {
|
|
|
|
case err.IsInvalidSignature:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.InvalidSignature(err.Error()),
|
|
|
|
}
|
|
|
|
case err.IsMissingParam:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.MissingParam(err.Error()),
|
|
|
|
}
|
2021-08-17 14:44:30 +02:00
|
|
|
case err.IsInvalidParam:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.InvalidParam(err.Error()),
|
|
|
|
}
|
2021-08-04 18:56:29 +02:00
|
|
|
default:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.Unknown(err.Error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 14:17:38 +02:00
|
|
|
func UploadCrossSigningDeviceSignatures(req *http.Request, keyserverAPI api.ClientKeyAPI, device *userapi.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
|
2022-08-11 16:29:33 +02:00
|
|
|
if err := keyserverAPI.PerformUploadDeviceSignatures(req.Context(), uploadReq, uploadRes); err != nil {
|
|
|
|
return jsonerror.InternalAPIError(req.Context(), err)
|
|
|
|
}
|
2021-08-04 18:56:29 +02:00
|
|
|
|
|
|
|
if err := uploadRes.Error; err != nil {
|
|
|
|
switch {
|
|
|
|
case err.IsInvalidSignature:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.InvalidSignature(err.Error()),
|
|
|
|
}
|
|
|
|
case err.IsMissingParam:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.MissingParam(err.Error()),
|
|
|
|
}
|
2021-08-17 14:44:30 +02:00
|
|
|
case err.IsInvalidParam:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.InvalidParam(err.Error()),
|
|
|
|
}
|
2021-08-04 18:56:29 +02:00
|
|
|
default:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.Unknown(err.Error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|