2020-06-15 10:54:11 +02:00
|
|
|
// Copyright 2020 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 internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2020-06-18 19:36:03 +02:00
|
|
|
"encoding/json"
|
2020-06-16 18:39:56 +02:00
|
|
|
"errors"
|
2020-06-15 10:54:11 +02:00
|
|
|
"fmt"
|
2022-03-03 12:40:53 +01:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2020-06-15 10:54:11 +02:00
|
|
|
|
2023-04-03 20:19:26 +02:00
|
|
|
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
2023-02-20 14:58:03 +01:00
|
|
|
fedsenderapi "github.com/matrix-org/dendrite/federationapi/api"
|
2023-04-14 13:35:27 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/pushrules"
|
2022-02-16 18:55:38 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 16:50:33 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-02-16 18:55:38 +01:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
"github.com/sirupsen/logrus"
|
2022-03-24 22:45:44 +01:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2022-02-16 18:55:38 +01:00
|
|
|
|
2023-06-22 18:37:21 +02:00
|
|
|
clientapi "github.com/matrix-org/dendrite/clientapi/api"
|
2020-06-16 15:10:55 +02:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/userutil"
|
2022-07-25 18:30:07 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/eventutil"
|
2022-09-27 15:01:34 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/pushgateway"
|
2020-06-16 18:39:56 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2022-06-29 16:29:39 +02:00
|
|
|
rsapi "github.com/matrix-org/dendrite/roomserver/api"
|
2020-12-02 18:41:00 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-07-25 18:30:07 +02:00
|
|
|
synctypes "github.com/matrix-org/dendrite/syncapi/types"
|
2020-06-15 10:54:11 +02:00
|
|
|
"github.com/matrix-org/dendrite/userapi/api"
|
2022-03-03 12:40:53 +01:00
|
|
|
"github.com/matrix-org/dendrite/userapi/producers"
|
2022-02-18 12:31:05 +01:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage"
|
2022-03-03 12:40:53 +01:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/tables"
|
2022-09-27 15:01:34 +02:00
|
|
|
userapiUtil "github.com/matrix-org/dendrite/userapi/util"
|
2020-06-15 10:54:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserInternalAPI struct {
|
2023-02-20 14:58:03 +01:00
|
|
|
DB storage.UserDatabase
|
|
|
|
KeyDatabase storage.KeyDatabase
|
|
|
|
SyncProducer *producers.SyncAPI
|
|
|
|
KeyChangeProducer *producers.KeyChange
|
|
|
|
Config *config.UserAPI
|
2022-03-03 12:40:53 +01:00
|
|
|
|
|
|
|
DisableTLSValidation bool
|
2020-06-16 15:10:55 +02:00
|
|
|
// AppServices is the list of all registered AS
|
|
|
|
AppServices []config.ApplicationService
|
2022-06-29 16:29:39 +02:00
|
|
|
RSAPI rsapi.UserRoomserverAPI
|
2022-09-27 15:01:34 +02:00
|
|
|
PgClient pushgateway.Client
|
2023-02-20 14:58:03 +01:00
|
|
|
FedClient fedsenderapi.KeyserverFederationAPI
|
|
|
|
Updater *DeviceListUpdater
|
2020-06-15 10:54:11 +02:00
|
|
|
}
|
|
|
|
|
2023-06-22 18:37:21 +02:00
|
|
|
func (a *UserInternalAPI) PerformAdminCreateRegistrationToken(ctx context.Context, registrationToken *clientapi.RegistrationToken) (bool, error) {
|
|
|
|
exists, err := a.DB.RegistrationTokenExists(ctx, *registrationToken.Token)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
return false, fmt.Errorf("token: %s already exists", *registrationToken.Token)
|
|
|
|
}
|
|
|
|
_, err = a.DB.InsertRegistrationToken(ctx, registrationToken)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("Error creating token: %s"+err.Error(), *registrationToken.Token)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) PerformAdminListRegistrationTokens(ctx context.Context, returnAll bool, valid bool) ([]clientapi.RegistrationToken, error) {
|
|
|
|
return a.DB.ListRegistrationTokens(ctx, returnAll, valid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) PerformAdminGetRegistrationToken(ctx context.Context, tokenString string) (*clientapi.RegistrationToken, error) {
|
|
|
|
return a.DB.GetRegistrationToken(ctx, tokenString)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) PerformAdminDeleteRegistrationToken(ctx context.Context, tokenString string) error {
|
|
|
|
return a.DB.DeleteRegistrationToken(ctx, tokenString)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) PerformAdminUpdateRegistrationToken(ctx context.Context, tokenString string, newAttributes map[string]interface{}) (*clientapi.RegistrationToken, error) {
|
|
|
|
return a.DB.UpdateRegistrationToken(ctx, tokenString, newAttributes)
|
|
|
|
}
|
|
|
|
|
2020-06-18 19:36:03 +02:00
|
|
|
func (a *UserInternalAPI) InputAccountData(ctx context.Context, req *api.InputAccountDataRequest, res *api.InputAccountDataResponse) error {
|
|
|
|
local, domain, err := gomatrixserverlib.SplitID('@', req.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-26 13:59:19 +02:00
|
|
|
if !a.Config.Matrix.IsLocalServerName(domain) {
|
|
|
|
return fmt.Errorf("cannot update account data of remote users (server name %s)", domain)
|
2020-06-18 19:36:03 +02:00
|
|
|
}
|
|
|
|
if req.DataType == "" {
|
|
|
|
return fmt.Errorf("data type must not be empty")
|
|
|
|
}
|
2022-11-11 17:41:37 +01:00
|
|
|
if err := a.DB.SaveAccountData(ctx, local, domain, req.RoomID, req.DataType, req.AccountData); err != nil {
|
2022-07-25 18:30:07 +02:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("a.DB.SaveAccountData failed")
|
|
|
|
return fmt.Errorf("failed to save account data: %w", err)
|
|
|
|
}
|
|
|
|
var ignoredUsers *synctypes.IgnoredUsers
|
|
|
|
if req.DataType == "m.ignored_user_list" {
|
|
|
|
ignoredUsers = &synctypes.IgnoredUsers{}
|
|
|
|
_ = json.Unmarshal(req.AccountData, ignoredUsers)
|
|
|
|
}
|
2022-09-27 15:01:34 +02:00
|
|
|
if req.DataType == "m.fully_read" {
|
|
|
|
if err := a.setFullyRead(ctx, req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 18:30:07 +02:00
|
|
|
if err := a.SyncProducer.SendAccountData(req.UserID, eventutil.AccountData{
|
|
|
|
RoomID: req.RoomID,
|
|
|
|
Type: req.DataType,
|
|
|
|
IgnoredUsers: ignoredUsers,
|
|
|
|
}); err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("a.SyncProducer.SendAccountData failed")
|
|
|
|
return fmt.Errorf("failed to send account data to output: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-06-18 19:36:03 +02:00
|
|
|
}
|
|
|
|
|
2022-09-27 15:01:34 +02:00
|
|
|
func (a *UserInternalAPI) setFullyRead(ctx context.Context, req *api.InputAccountDataRequest) error {
|
|
|
|
var output eventutil.ReadMarkerJSON
|
|
|
|
|
|
|
|
if err := json.Unmarshal(req.AccountData, &output); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
localpart, domain, err := gomatrixserverlib.SplitID('@', req.UserID)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("UserInternalAPI.setFullyRead: SplitID failure")
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-26 13:59:19 +02:00
|
|
|
if !a.Config.Matrix.IsLocalServerName(domain) {
|
2022-09-27 15:01:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-19 16:50:33 +02:00
|
|
|
deleted, err := a.DB.DeleteNotificationsUpTo(ctx, localpart, domain, req.RoomID, uint64(spec.AsTimestamp(time.Now())))
|
2022-09-27 15:01:34 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Errorf("UserInternalAPI.setFullyRead: DeleteNotificationsUpTo failed")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = a.SyncProducer.GetAndSendNotificationData(ctx, req.UserID, req.RoomID); err != nil {
|
|
|
|
logrus.WithError(err).Error("UserInternalAPI.setFullyRead: GetAndSendNotificationData failed")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// nothing changed, no need to notify the push gateway
|
|
|
|
if !deleted {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-11 17:41:37 +01:00
|
|
|
if err = userapiUtil.NotifyUserCountsAsync(ctx, a.PgClient, localpart, domain, a.DB); err != nil {
|
2022-09-27 15:01:34 +02:00
|
|
|
logrus.WithError(err).Error("UserInternalAPI.setFullyRead: NotifyUserCounts failed")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:04:53 +02:00
|
|
|
func postRegisterJoinRooms(cfg *config.UserAPI, acc *api.Account, rsAPI rsapi.UserRoomserverAPI) {
|
|
|
|
// POST register behaviour: check if the user is a normal user.
|
|
|
|
// If the user is a normal user, add user to room specified in the configuration "auto_join_rooms".
|
|
|
|
if acc.AccountType != api.AccountTypeAppService && acc.AppServiceID == "" {
|
|
|
|
for room := range cfg.AutoJoinRooms {
|
|
|
|
userID := userutil.MakeUserID(acc.Localpart, cfg.Matrix.ServerName)
|
|
|
|
err := addUserToRoom(context.Background(), rsAPI, cfg.AutoJoinRooms[room], acc.Localpart, userID)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"user_id": userID,
|
|
|
|
"room": cfg.AutoJoinRooms[room],
|
|
|
|
}).WithError(err).Errorf("user failed to auto-join room")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add user to a room. This function currently working for auto_join_rooms config,
|
|
|
|
// which can add a newly registered user to a specified room.
|
|
|
|
func addUserToRoom(
|
|
|
|
ctx context.Context,
|
|
|
|
rsAPI rsapi.UserRoomserverAPI,
|
|
|
|
roomID string,
|
|
|
|
username string,
|
|
|
|
userID string,
|
|
|
|
) error {
|
|
|
|
addGroupContent := make(map[string]interface{})
|
|
|
|
// This make sure the user's username can be displayed correctly.
|
|
|
|
// Because the newly-registered user doesn't have an avatar, the avatar_url is not needed.
|
|
|
|
addGroupContent["displayname"] = username
|
|
|
|
joinReq := rsapi.PerformJoinRequest{
|
|
|
|
RoomIDOrAlias: roomID,
|
|
|
|
UserID: userID,
|
|
|
|
Content: addGroupContent,
|
|
|
|
}
|
2023-04-28 17:46:01 +02:00
|
|
|
_, _, err := rsAPI.PerformJoin(ctx, &joinReq)
|
|
|
|
return err
|
2022-10-26 11:04:53 +02:00
|
|
|
}
|
|
|
|
|
2020-06-16 18:39:56 +02:00
|
|
|
func (a *UserInternalAPI) PerformAccountCreation(ctx context.Context, req *api.PerformAccountCreationRequest, res *api.PerformAccountCreationResponse) error {
|
2022-10-26 13:59:19 +02:00
|
|
|
serverName := req.ServerName
|
|
|
|
if serverName == "" {
|
|
|
|
serverName = a.Config.Matrix.ServerName
|
|
|
|
}
|
2022-11-11 17:41:37 +01:00
|
|
|
if !a.Config.Matrix.IsLocalServerName(serverName) {
|
|
|
|
return fmt.Errorf("server name %s is not local", serverName)
|
|
|
|
}
|
|
|
|
acc, err := a.DB.CreateAccount(ctx, req.Localpart, serverName, req.Password, req.AppServiceID, req.AccountType)
|
2020-06-16 18:39:56 +02:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sqlutil.ErrUserExists) { // This account already exists
|
|
|
|
switch req.OnConflict {
|
|
|
|
case api.ConflictUpdate:
|
|
|
|
break
|
|
|
|
case api.ConflictAbort:
|
|
|
|
return &api.ErrorConflict{
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-17 12:22:26 +02:00
|
|
|
// account already exists
|
2020-06-16 18:39:56 +02:00
|
|
|
res.AccountCreated = false
|
2020-06-17 12:22:26 +02:00
|
|
|
res.Account = &api.Account{
|
|
|
|
AppServiceID: req.AppServiceID,
|
|
|
|
Localpart: req.Localpart,
|
2022-10-26 13:59:19 +02:00
|
|
|
ServerName: serverName,
|
|
|
|
UserID: fmt.Sprintf("@%s:%s", req.Localpart, serverName),
|
2022-02-16 18:55:38 +01:00
|
|
|
AccountType: req.AccountType,
|
2020-06-17 12:22:26 +02:00
|
|
|
}
|
2021-03-03 15:01:34 +01:00
|
|
|
return nil
|
2020-06-16 18:39:56 +02:00
|
|
|
}
|
2020-07-01 14:35:58 +02:00
|
|
|
|
2022-04-25 19:04:46 +02:00
|
|
|
// Inform the SyncAPI about the newly created push_rules
|
2022-07-25 18:30:07 +02:00
|
|
|
if err = a.SyncProducer.SendAccountData(acc.UserID, eventutil.AccountData{
|
|
|
|
Type: "m.push_rules",
|
|
|
|
}); err != nil {
|
2022-04-25 19:04:46 +02:00
|
|
|
util.GetLogger(ctx).WithFields(logrus.Fields{
|
|
|
|
"user_id": acc.UserID,
|
|
|
|
}).WithError(err).Warn("failed to send account data to the SyncAPI")
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:55:38 +01:00
|
|
|
if req.AccountType == api.AccountTypeGuest {
|
|
|
|
res.AccountCreated = true
|
|
|
|
res.Account = acc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-11 17:41:37 +01:00
|
|
|
if _, _, err = a.DB.SetDisplayName(ctx, req.Localpart, serverName, req.Localpart); err != nil {
|
|
|
|
return fmt.Errorf("a.DB.SetDisplayName: %w", err)
|
2020-07-01 14:35:58 +02:00
|
|
|
}
|
|
|
|
|
2023-02-20 14:58:03 +01:00
|
|
|
postRegisterJoinRooms(a.Config, acc, a.RSAPI)
|
2022-10-26 11:04:53 +02:00
|
|
|
|
2020-06-16 18:39:56 +02:00
|
|
|
res.AccountCreated = true
|
2020-06-17 12:22:26 +02:00
|
|
|
res.Account = acc
|
2020-06-16 18:39:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-09-04 16:16:13 +02:00
|
|
|
|
|
|
|
func (a *UserInternalAPI) PerformPasswordUpdate(ctx context.Context, req *api.PerformPasswordUpdateRequest, res *api.PerformPasswordUpdateResponse) error {
|
2022-11-11 17:41:37 +01:00
|
|
|
if !a.Config.Matrix.IsLocalServerName(req.ServerName) {
|
|
|
|
return fmt.Errorf("server name %s is not local", req.ServerName)
|
|
|
|
}
|
|
|
|
if err := a.DB.SetPassword(ctx, req.Localpart, req.ServerName, req.Password); err != nil {
|
2020-09-04 16:16:13 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-08-12 13:00:07 +02:00
|
|
|
if req.LogoutDevices {
|
2022-11-11 17:41:37 +01:00
|
|
|
if _, err := a.DB.RemoveAllDevices(context.Background(), req.Localpart, req.ServerName, ""); err != nil {
|
2022-08-12 13:00:07 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-09-04 16:16:13 +02:00
|
|
|
res.PasswordUpdated = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:39:56 +02:00
|
|
|
func (a *UserInternalAPI) PerformDeviceCreation(ctx context.Context, req *api.PerformDeviceCreationRequest, res *api.PerformDeviceCreationResponse) error {
|
2022-10-26 13:59:19 +02:00
|
|
|
serverName := req.ServerName
|
|
|
|
if serverName == "" {
|
|
|
|
serverName = a.Config.Matrix.ServerName
|
|
|
|
}
|
2022-11-11 17:41:37 +01:00
|
|
|
if !a.Config.Matrix.IsLocalServerName(serverName) {
|
|
|
|
return fmt.Errorf("server name %s is not local", serverName)
|
|
|
|
}
|
2023-02-20 15:26:09 +01:00
|
|
|
// If a device ID was specified, check if it already exists and
|
|
|
|
// avoid sending an empty device list update which would remove
|
|
|
|
// existing device keys.
|
|
|
|
isExisting := false
|
|
|
|
if req.DeviceID != nil && *req.DeviceID != "" {
|
|
|
|
existingDev, err := a.DB.GetDeviceByID(ctx, req.Localpart, req.ServerName, *req.DeviceID)
|
|
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
isExisting = existingDev.ID == *req.DeviceID
|
|
|
|
}
|
2020-08-10 13:38:33 +02:00
|
|
|
util.GetLogger(ctx).WithFields(logrus.Fields{
|
|
|
|
"localpart": req.Localpart,
|
|
|
|
"device_id": req.DeviceID,
|
|
|
|
"display_name": req.DeviceDisplayName,
|
|
|
|
}).Info("PerformDeviceCreation")
|
2022-11-11 17:41:37 +01:00
|
|
|
dev, err := a.DB.CreateDevice(ctx, req.Localpart, serverName, req.DeviceID, req.AccessToken, req.DeviceDisplayName, req.IPAddr, req.UserAgent)
|
2020-06-16 18:39:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res.DeviceCreated = true
|
2020-06-17 12:22:26 +02:00
|
|
|
res.Device = dev
|
2023-02-20 15:26:09 +01:00
|
|
|
if req.NoDeviceListUpdate || isExisting {
|
2021-12-03 18:18:35 +01:00
|
|
|
return nil
|
|
|
|
}
|
2020-07-31 15:40:45 +02:00
|
|
|
// create empty device keys and upload them to trigger device list changes
|
2024-01-20 21:20:37 +01:00
|
|
|
return a.deviceListUpdate(dev.UserID, []string{dev.ID}, req.FromRegistration)
|
2020-06-16 18:39:56 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 19:00:56 +02:00
|
|
|
func (a *UserInternalAPI) PerformDeviceDeletion(ctx context.Context, req *api.PerformDeviceDeletionRequest, res *api.PerformDeviceDeletionResponse) error {
|
|
|
|
util.GetLogger(ctx).WithField("user_id", req.UserID).WithField("devices", req.DeviceIDs).Info("PerformDeviceDeletion")
|
|
|
|
local, domain, err := gomatrixserverlib.SplitID('@', req.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-26 13:59:19 +02:00
|
|
|
if !a.Config.Matrix.IsLocalServerName(domain) {
|
|
|
|
return fmt.Errorf("cannot PerformDeviceDeletion of remote users (server name %s)", domain)
|
2020-07-30 19:00:56 +02:00
|
|
|
}
|
2020-08-27 19:53:40 +02:00
|
|
|
deletedDeviceIDs := req.DeviceIDs
|
|
|
|
if len(req.DeviceIDs) == 0 {
|
|
|
|
var devices []api.Device
|
2022-11-11 17:41:37 +01:00
|
|
|
devices, err = a.DB.RemoveAllDevices(ctx, local, domain, req.ExceptDeviceID)
|
2020-08-27 19:53:40 +02:00
|
|
|
for _, d := range devices {
|
|
|
|
deletedDeviceIDs = append(deletedDeviceIDs, d.ID)
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-11 17:41:37 +01:00
|
|
|
err = a.DB.RemoveDevices(ctx, local, domain, req.DeviceIDs)
|
2020-08-27 19:53:40 +02:00
|
|
|
}
|
2020-07-30 19:00:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-18 13:07:09 +02:00
|
|
|
// Ask the keyserver to delete device keys and signatures for those devices
|
2023-02-20 14:58:03 +01:00
|
|
|
deleteReq := &api.PerformDeleteKeysRequest{
|
2021-08-18 13:07:09 +02:00
|
|
|
UserID: req.UserID,
|
|
|
|
}
|
|
|
|
for _, keyID := range req.DeviceIDs {
|
|
|
|
deleteReq.KeyIDs = append(deleteReq.KeyIDs, gomatrixserverlib.KeyID(keyID))
|
|
|
|
}
|
2023-02-20 14:58:03 +01:00
|
|
|
deleteRes := &api.PerformDeleteKeysResponse{}
|
|
|
|
if err := a.PerformDeleteKeys(ctx, deleteReq, deleteRes); err != nil {
|
2022-08-11 16:29:33 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-08-18 13:07:09 +02:00
|
|
|
if err := deleteRes.Error; err != nil {
|
|
|
|
return fmt.Errorf("a.KeyAPI.PerformDeleteKeys: %w", err)
|
|
|
|
}
|
2020-07-30 19:00:56 +02:00
|
|
|
// create empty device keys and upload them to delete what was once there and trigger device list changes
|
2024-01-20 21:20:37 +01:00
|
|
|
return a.deviceListUpdate(req.UserID, deletedDeviceIDs, false)
|
2020-07-31 15:40:45 +02:00
|
|
|
}
|
|
|
|
|
2024-01-20 21:20:37 +01:00
|
|
|
func (a *UserInternalAPI) deviceListUpdate(userID string, deviceIDs []string, fromRegistration bool) error {
|
2023-02-20 14:58:03 +01:00
|
|
|
deviceKeys := make([]api.DeviceKeys, len(deviceIDs))
|
2020-07-31 15:40:45 +02:00
|
|
|
for i, did := range deviceIDs {
|
2023-02-20 14:58:03 +01:00
|
|
|
deviceKeys[i] = api.DeviceKeys{
|
2020-07-31 15:40:45 +02:00
|
|
|
UserID: userID,
|
2020-07-30 19:00:56 +02:00
|
|
|
DeviceID: did,
|
|
|
|
KeyJSON: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 14:58:03 +01:00
|
|
|
var uploadRes api.PerformUploadKeysResponse
|
|
|
|
if err := a.PerformUploadKeys(context.Background(), &api.PerformUploadKeysRequest{
|
2024-01-20 21:20:37 +01:00
|
|
|
UserID: userID,
|
|
|
|
DeviceKeys: deviceKeys,
|
|
|
|
FromRegistration: fromRegistration,
|
2022-08-11 16:29:33 +02:00
|
|
|
}, &uploadRes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-30 19:00:56 +02:00
|
|
|
if uploadRes.Error != nil {
|
2021-09-08 18:31:03 +02:00
|
|
|
return fmt.Errorf("failed to delete device keys: %v", uploadRes.Error)
|
2020-07-30 19:00:56 +02:00
|
|
|
}
|
|
|
|
if len(uploadRes.KeyErrors) > 0 {
|
2021-09-08 18:31:03 +02:00
|
|
|
return fmt.Errorf("failed to delete device keys, key errors: %+v", uploadRes.KeyErrors)
|
2020-07-30 19:00:56 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-20 12:29:02 +01:00
|
|
|
func (a *UserInternalAPI) PerformLastSeenUpdate(
|
|
|
|
ctx context.Context,
|
|
|
|
req *api.PerformLastSeenUpdateRequest,
|
|
|
|
res *api.PerformLastSeenUpdateResponse,
|
|
|
|
) error {
|
2022-11-11 17:41:37 +01:00
|
|
|
localpart, domain, err := gomatrixserverlib.SplitID('@', req.UserID)
|
2020-11-20 12:29:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("gomatrixserverlib.SplitID: %w", err)
|
|
|
|
}
|
2022-11-11 17:41:37 +01:00
|
|
|
if !a.Config.Matrix.IsLocalServerName(domain) {
|
|
|
|
return fmt.Errorf("server name %s is not local", domain)
|
|
|
|
}
|
|
|
|
if err := a.DB.UpdateDeviceLastSeen(ctx, localpart, domain, req.DeviceID, req.RemoteAddr, req.UserAgent); err != nil {
|
2020-11-20 12:29:02 +01:00
|
|
|
return fmt.Errorf("a.DeviceDB.UpdateDeviceLastSeen: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-31 15:40:45 +02:00
|
|
|
func (a *UserInternalAPI) PerformDeviceUpdate(ctx context.Context, req *api.PerformDeviceUpdateRequest, res *api.PerformDeviceUpdateResponse) error {
|
2022-11-11 17:41:37 +01:00
|
|
|
localpart, domain, err := gomatrixserverlib.SplitID('@', req.RequestingUserID)
|
2020-07-31 15:40:45 +02:00
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("gomatrixserverlib.SplitID failed")
|
|
|
|
return err
|
|
|
|
}
|
2022-11-11 17:41:37 +01:00
|
|
|
if !a.Config.Matrix.IsLocalServerName(domain) {
|
|
|
|
return fmt.Errorf("server name %s is not local", domain)
|
|
|
|
}
|
|
|
|
dev, err := a.DB.GetDeviceByID(ctx, localpart, domain, req.DeviceID)
|
2020-07-31 15:40:45 +02:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
res.DeviceExists = false
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("deviceDB.GetDeviceByID failed")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res.DeviceExists = true
|
|
|
|
|
2022-11-11 17:41:37 +01:00
|
|
|
err = a.DB.UpdateDevice(ctx, localpart, domain, req.DeviceID, req.DisplayName)
|
2020-07-31 15:40:45 +02:00
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("deviceDB.UpdateDevice failed")
|
|
|
|
return err
|
|
|
|
}
|
2020-08-12 23:43:02 +02:00
|
|
|
if req.DisplayName != nil && dev.DisplayName != *req.DisplayName {
|
|
|
|
// display name has changed: update the device key
|
2023-02-20 14:58:03 +01:00
|
|
|
var uploadRes api.PerformUploadKeysResponse
|
|
|
|
if err := a.PerformUploadKeys(context.Background(), &api.PerformUploadKeysRequest{
|
2021-03-02 12:40:20 +01:00
|
|
|
UserID: req.RequestingUserID,
|
2023-02-20 14:58:03 +01:00
|
|
|
DeviceKeys: []api.DeviceKeys{
|
2020-08-12 23:43:02 +02:00
|
|
|
{
|
|
|
|
DeviceID: dev.ID,
|
|
|
|
DisplayName: *req.DisplayName,
|
|
|
|
KeyJSON: nil,
|
|
|
|
UserID: dev.UserID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
OnlyDisplayNameUpdates: true,
|
2022-08-11 16:29:33 +02:00
|
|
|
}, &uploadRes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-12 23:43:02 +02:00
|
|
|
if uploadRes.Error != nil {
|
2021-09-08 18:31:03 +02:00
|
|
|
return fmt.Errorf("failed to update device key display name: %v", uploadRes.Error)
|
2020-08-12 23:43:02 +02:00
|
|
|
}
|
|
|
|
if len(uploadRes.KeyErrors) > 0 {
|
2021-09-08 18:31:03 +02:00
|
|
|
return fmt.Errorf("failed to update device key display name, key errors: %+v", uploadRes.KeyErrors)
|
2020-08-12 23:43:02 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-31 15:40:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-03 20:19:26 +02:00
|
|
|
var (
|
|
|
|
ErrIsRemoteServer = errors.New("cannot query profile of remote users")
|
|
|
|
)
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) QueryProfile(ctx context.Context, userID string) (*authtypes.Profile, error) {
|
|
|
|
local, domain, err := gomatrixserverlib.SplitID('@', userID)
|
2020-06-15 10:54:11 +02:00
|
|
|
if err != nil {
|
2023-04-03 20:19:26 +02:00
|
|
|
return nil, err
|
2020-06-15 10:54:11 +02:00
|
|
|
}
|
2022-10-26 13:59:19 +02:00
|
|
|
if !a.Config.Matrix.IsLocalServerName(domain) {
|
2023-04-03 20:19:26 +02:00
|
|
|
return nil, ErrIsRemoteServer
|
2020-06-15 10:54:11 +02:00
|
|
|
}
|
2022-11-11 17:41:37 +01:00
|
|
|
prof, err := a.DB.GetProfileByLocalpart(ctx, local, domain)
|
2020-06-15 10:54:11 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
2023-04-03 20:19:26 +02:00
|
|
|
return nil, appserviceAPI.ErrProfileNotExists
|
2020-06-15 10:54:11 +02:00
|
|
|
}
|
2023-04-03 20:19:26 +02:00
|
|
|
return nil, err
|
2020-06-15 10:54:11 +02:00
|
|
|
}
|
2023-04-03 20:19:26 +02:00
|
|
|
return prof, nil
|
2020-07-28 11:53:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) QuerySearchProfiles(ctx context.Context, req *api.QuerySearchProfilesRequest, res *api.QuerySearchProfilesResponse) error {
|
2022-02-18 12:31:05 +01:00
|
|
|
profiles, err := a.DB.SearchProfiles(ctx, req.SearchString, req.Limit)
|
2020-07-28 11:53:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res.Profiles = profiles
|
|
|
|
return nil
|
2020-06-15 10:54:11 +02:00
|
|
|
}
|
2020-06-16 15:10:55 +02:00
|
|
|
|
2020-07-22 18:04:57 +02:00
|
|
|
func (a *UserInternalAPI) QueryDeviceInfos(ctx context.Context, req *api.QueryDeviceInfosRequest, res *api.QueryDeviceInfosResponse) error {
|
2022-02-18 12:31:05 +01:00
|
|
|
devices, err := a.DB.GetDevicesByID(ctx, req.DeviceIDs)
|
2020-07-22 18:04:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res.DeviceInfo = make(map[string]struct {
|
|
|
|
DisplayName string
|
|
|
|
UserID string
|
|
|
|
})
|
|
|
|
for _, d := range devices {
|
|
|
|
res.DeviceInfo[d.ID] = struct {
|
|
|
|
DisplayName string
|
|
|
|
UserID string
|
|
|
|
}{
|
|
|
|
DisplayName: d.DisplayName,
|
|
|
|
UserID: d.UserID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-16 15:53:19 +02:00
|
|
|
func (a *UserInternalAPI) QueryDevices(ctx context.Context, req *api.QueryDevicesRequest, res *api.QueryDevicesResponse) error {
|
|
|
|
local, domain, err := gomatrixserverlib.SplitID('@', req.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-26 13:59:19 +02:00
|
|
|
if !a.Config.Matrix.IsLocalServerName(domain) {
|
|
|
|
return fmt.Errorf("cannot query devices of remote users (server name %s)", domain)
|
2020-06-16 15:53:19 +02:00
|
|
|
}
|
2022-11-11 17:41:37 +01:00
|
|
|
devs, err := a.DB.GetDevicesByLocalpart(ctx, local, domain)
|
2020-06-16 15:53:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-18 12:31:05 +01:00
|
|
|
res.UserExists = true
|
2020-06-16 15:53:19 +02:00
|
|
|
res.Devices = devs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:05:38 +02:00
|
|
|
func (a *UserInternalAPI) QueryAccountData(ctx context.Context, req *api.QueryAccountDataRequest, res *api.QueryAccountDataResponse) error {
|
|
|
|
local, domain, err := gomatrixserverlib.SplitID('@', req.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-26 13:59:19 +02:00
|
|
|
if !a.Config.Matrix.IsLocalServerName(domain) {
|
|
|
|
return fmt.Errorf("cannot query account data of remote users (server name %s)", domain)
|
2020-06-16 18:05:38 +02:00
|
|
|
}
|
|
|
|
if req.DataType != "" {
|
2020-06-18 19:36:03 +02:00
|
|
|
var data json.RawMessage
|
2022-11-11 17:41:37 +01:00
|
|
|
data, err = a.DB.GetAccountDataByType(ctx, local, domain, req.RoomID, req.DataType)
|
2020-06-16 18:05:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-18 19:36:03 +02:00
|
|
|
res.RoomAccountData = make(map[string]map[string]json.RawMessage)
|
|
|
|
res.GlobalAccountData = make(map[string]json.RawMessage)
|
|
|
|
if data != nil {
|
2020-06-16 18:05:38 +02:00
|
|
|
if req.RoomID != "" {
|
2020-06-18 19:36:03 +02:00
|
|
|
if _, ok := res.RoomAccountData[req.RoomID]; !ok {
|
|
|
|
res.RoomAccountData[req.RoomID] = make(map[string]json.RawMessage)
|
|
|
|
}
|
|
|
|
res.RoomAccountData[req.RoomID][req.DataType] = data
|
2020-06-16 18:05:38 +02:00
|
|
|
} else {
|
2020-06-18 19:36:03 +02:00
|
|
|
res.GlobalAccountData[req.DataType] = data
|
2020-06-16 18:05:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-11 17:41:37 +01:00
|
|
|
global, rooms, err := a.DB.GetAccountData(ctx, local, domain)
|
2020-06-16 18:05:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res.RoomAccountData = rooms
|
|
|
|
res.GlobalAccountData = global
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-16 15:10:55 +02:00
|
|
|
func (a *UserInternalAPI) QueryAccessToken(ctx context.Context, req *api.QueryAccessTokenRequest, res *api.QueryAccessTokenResponse) error {
|
|
|
|
if req.AppServiceUserID != "" {
|
|
|
|
appServiceDevice, err := a.queryAppServiceToken(ctx, req.AccessToken, req.AppServiceUserID)
|
2023-09-12 16:44:51 +02:00
|
|
|
if err != nil || appServiceDevice != nil {
|
|
|
|
if err != nil {
|
|
|
|
res.Err = err.Error()
|
|
|
|
}
|
|
|
|
res.Device = appServiceDevice
|
2021-12-03 18:18:35 +01:00
|
|
|
|
2023-09-12 16:44:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// If the provided token wasn't an as_token (both err and appServiceDevice are nil), continue with normal auth.
|
2020-06-16 15:10:55 +02:00
|
|
|
}
|
2022-02-18 12:31:05 +01:00
|
|
|
device, err := a.DB.GetDeviceByAccessToken(ctx, req.AccessToken)
|
2020-06-16 15:10:55 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2022-10-26 13:59:19 +02:00
|
|
|
localPart, domain, err := gomatrixserverlib.SplitID('@', device.UserID)
|
2022-02-16 18:55:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-26 13:59:19 +02:00
|
|
|
if !a.Config.Matrix.IsLocalServerName(domain) {
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-11 17:41:37 +01:00
|
|
|
acc, err := a.DB.GetAccountByLocalpart(ctx, localPart, domain)
|
2022-02-16 18:55:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
device.AccountType = acc.AccountType
|
2020-06-16 15:10:55 +02:00
|
|
|
res.Device = device
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-22 13:05:59 +01:00
|
|
|
func (a *UserInternalAPI) QueryAccountByLocalpart(ctx context.Context, req *api.QueryAccountByLocalpartRequest, res *api.QueryAccountByLocalpartResponse) (err error) {
|
|
|
|
res.Account, err = a.DB.GetAccountByLocalpart(ctx, req.Localpart, req.ServerName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-16 15:10:55 +02:00
|
|
|
// Return the appservice 'device' or nil if the token is not an appservice. Returns an error if there was a problem
|
|
|
|
// creating a 'device'.
|
|
|
|
func (a *UserInternalAPI) queryAppServiceToken(ctx context.Context, token, appServiceUserID string) (*api.Device, error) {
|
|
|
|
// Search for app service with given access_token
|
|
|
|
var appService *config.ApplicationService
|
|
|
|
for _, as := range a.AppServices {
|
|
|
|
if as.ASToken == token {
|
|
|
|
appService = &as
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if appService == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a dummy device for AS user
|
|
|
|
dev := api.Device{
|
|
|
|
// Use AS dummy device ID
|
2022-09-01 10:20:40 +02:00
|
|
|
ID: "AS_Device",
|
2020-06-16 15:10:55 +02:00
|
|
|
// AS dummy device has AS's token.
|
2021-03-03 17:27:44 +01:00
|
|
|
AccessToken: token,
|
|
|
|
AppserviceID: appService.ID,
|
2022-02-16 18:55:38 +01:00
|
|
|
AccountType: api.AccountTypeAppService,
|
2020-06-16 15:10:55 +02:00
|
|
|
}
|
|
|
|
|
2022-11-11 17:41:37 +01:00
|
|
|
localpart, domain, err := userutil.ParseUsernameParam(appServiceUserID, a.Config.Matrix)
|
2020-06-16 15:10:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if localpart != "" { // AS is masquerading as another user
|
|
|
|
// Verify that the user is registered
|
2022-11-11 17:41:37 +01:00
|
|
|
account, err := a.DB.GetAccountByLocalpart(ctx, localpart, domain)
|
2021-01-04 11:43:23 +01:00
|
|
|
// Verify that the account exists and either appServiceID matches or
|
|
|
|
// it belongs to the appservice user namespaces
|
|
|
|
if err == nil && (account.AppServiceID == appService.ID || appService.IsInterestedInUserID(appServiceUserID)) {
|
2020-06-16 15:10:55 +02:00
|
|
|
// Set the userID of dummy device
|
|
|
|
dev.UserID = appServiceUserID
|
|
|
|
return &dev, nil
|
|
|
|
}
|
|
|
|
return nil, &api.ErrorForbidden{Message: "appservice has not registered this user"}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AS is not masquerading as any user, so use AS's sender_localpart
|
|
|
|
dev.UserID = appService.SenderLocalpart
|
|
|
|
return &dev, nil
|
|
|
|
}
|
2020-10-02 18:18:20 +02:00
|
|
|
|
|
|
|
// PerformAccountDeactivation deactivates the user's account, removing all ability for the user to login again.
|
|
|
|
func (a *UserInternalAPI) PerformAccountDeactivation(ctx context.Context, req *api.PerformAccountDeactivationRequest, res *api.PerformAccountDeactivationResponse) error {
|
2022-10-26 13:59:19 +02:00
|
|
|
serverName := req.ServerName
|
|
|
|
if serverName == "" {
|
|
|
|
serverName = a.Config.Matrix.ServerName
|
|
|
|
}
|
|
|
|
if !a.Config.Matrix.IsLocalServerName(serverName) {
|
|
|
|
return fmt.Errorf("server name %q not locally configured", serverName)
|
|
|
|
}
|
|
|
|
|
2023-04-28 17:46:01 +02:00
|
|
|
userID := fmt.Sprintf("@%s:%s", req.Localpart, serverName)
|
|
|
|
_, err := a.RSAPI.PerformAdminEvacuateUser(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).WithField("userID", userID).Errorf("Failed to evacuate user after account deactivation")
|
2022-06-29 16:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
deviceReq := &api.PerformDeviceDeletionRequest{
|
2022-10-26 13:59:19 +02:00
|
|
|
UserID: fmt.Sprintf("@%s:%s", req.Localpart, serverName),
|
2022-06-29 16:29:39 +02:00
|
|
|
}
|
|
|
|
deviceRes := &api.PerformDeviceDeletionResponse{}
|
2023-04-28 17:46:01 +02:00
|
|
|
if err = a.PerformDeviceDeletion(ctx, deviceReq, deviceRes); err != nil {
|
2022-06-29 16:29:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pusherReq := &api.PerformPusherDeletionRequest{
|
|
|
|
Localpart: req.Localpart,
|
|
|
|
}
|
2023-04-28 17:46:01 +02:00
|
|
|
if err = a.PerformPusherDeletion(ctx, pusherReq, &struct{}{}); err != nil {
|
2022-06-29 16:29:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-28 17:46:01 +02:00
|
|
|
err = a.DB.DeactivateAccount(ctx, req.Localpart, serverName)
|
2020-10-02 18:18:20 +02:00
|
|
|
res.AccountDeactivated = err == nil
|
|
|
|
return err
|
|
|
|
}
|
2021-04-07 14:26:20 +02:00
|
|
|
|
|
|
|
// PerformOpenIDTokenCreation creates a new token that a relying party uses to authenticate a user
|
|
|
|
func (a *UserInternalAPI) PerformOpenIDTokenCreation(ctx context.Context, req *api.PerformOpenIDTokenCreationRequest, res *api.PerformOpenIDTokenCreationResponse) error {
|
|
|
|
token := util.RandomString(24)
|
|
|
|
|
2022-02-18 12:31:05 +01:00
|
|
|
exp, err := a.DB.CreateOpenIDToken(ctx, token, req.UserID)
|
2021-04-07 14:26:20 +02:00
|
|
|
|
|
|
|
res.Token = api.OpenIDToken{
|
|
|
|
Token: token,
|
|
|
|
UserID: req.UserID,
|
|
|
|
ExpiresAtMS: exp,
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryOpenIDToken validates that the OpenID token was issued for the user, the replying party uses this for validation
|
|
|
|
func (a *UserInternalAPI) QueryOpenIDToken(ctx context.Context, req *api.QueryOpenIDTokenRequest, res *api.QueryOpenIDTokenResponse) error {
|
2022-02-18 12:31:05 +01:00
|
|
|
openIDTokenAttrs, err := a.DB.GetOpenIDTokenAttributes(ctx, req.Token)
|
2021-04-07 14:26:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Sub = openIDTokenAttrs.UserID
|
|
|
|
res.ExpiresAtMS = openIDTokenAttrs.ExpiresAtMS
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-27 13:47:32 +02:00
|
|
|
|
2023-04-28 17:49:38 +02:00
|
|
|
func (a *UserInternalAPI) DeleteKeyBackup(ctx context.Context, userID, version string) (bool, error) {
|
|
|
|
return a.DB.DeleteKeyBackup(ctx, userID, version)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) PerformKeyBackup(ctx context.Context, req *api.PerformKeyBackupRequest) (string, error) {
|
2021-07-27 18:08:53 +02:00
|
|
|
// Create metadata
|
2023-04-28 17:49:38 +02:00
|
|
|
return a.DB.CreateKeyBackup(ctx, req.UserID, req.Algorithm, req.AuthData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) UpdateBackupKeyAuthData(ctx context.Context, req *api.PerformKeyBackupRequest) (*api.PerformKeyBackupResponse, error) {
|
|
|
|
res := &api.PerformKeyBackupResponse{}
|
2021-07-27 18:08:53 +02:00
|
|
|
// Update metadata
|
|
|
|
if len(req.Keys.Rooms) == 0 {
|
2022-02-18 12:31:05 +01:00
|
|
|
err := a.DB.UpdateKeyBackupAuthData(ctx, req.UserID, req.Version, req.AuthData)
|
2021-07-27 20:29:32 +02:00
|
|
|
res.Exists = err == nil
|
2021-07-27 18:08:53 +02:00
|
|
|
res.Version = req.Version
|
2023-04-28 17:49:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return res, fmt.Errorf("failed to update backup: %w", err)
|
|
|
|
}
|
|
|
|
return res, nil
|
2021-07-27 18:08:53 +02:00
|
|
|
}
|
|
|
|
// Upload Keys for a specific version metadata
|
2023-04-28 17:49:38 +02:00
|
|
|
return a.uploadBackupKeys(ctx, req)
|
2021-07-27 18:08:53 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 17:49:38 +02:00
|
|
|
func (a *UserInternalAPI) uploadBackupKeys(ctx context.Context, req *api.PerformKeyBackupRequest) (*api.PerformKeyBackupResponse, error) {
|
|
|
|
res := &api.PerformKeyBackupResponse{}
|
2021-07-27 20:29:32 +02:00
|
|
|
// you can only upload keys for the CURRENT version
|
2022-02-18 12:31:05 +01:00
|
|
|
version, _, _, _, deleted, err := a.DB.GetKeyBackup(ctx, req.UserID, "")
|
2021-07-27 13:47:32 +02:00
|
|
|
if err != nil {
|
2023-04-28 17:49:38 +02:00
|
|
|
return res, fmt.Errorf("failed to query version: %w", err)
|
2021-07-27 18:08:53 +02:00
|
|
|
}
|
|
|
|
if deleted {
|
2023-04-28 17:49:38 +02:00
|
|
|
return res, fmt.Errorf("backup was deleted")
|
2021-07-27 18:08:53 +02:00
|
|
|
}
|
2021-07-27 20:29:32 +02:00
|
|
|
if version != req.Version {
|
2023-05-10 00:46:49 +02:00
|
|
|
return res, spec.WrongBackupVersionError(version)
|
2021-07-27 20:29:32 +02:00
|
|
|
}
|
2021-07-27 18:08:53 +02:00
|
|
|
res.Exists = true
|
|
|
|
res.Version = version
|
|
|
|
|
|
|
|
// map keys to a form we can upload more easily - the map ensures we have no duplicates.
|
|
|
|
var uploads []api.InternalKeyBackupSession
|
|
|
|
for roomID, data := range req.Keys.Rooms {
|
|
|
|
for sessionID, sessionData := range data.Sessions {
|
|
|
|
uploads = append(uploads, api.InternalKeyBackupSession{
|
|
|
|
RoomID: roomID,
|
|
|
|
SessionID: sessionID,
|
|
|
|
KeyBackupSession: sessionData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-02-18 12:31:05 +01:00
|
|
|
count, etag, err := a.DB.UpsertBackupKeys(ctx, version, req.UserID, uploads)
|
2021-07-27 18:08:53 +02:00
|
|
|
if err != nil {
|
2023-04-28 17:49:38 +02:00
|
|
|
return res, fmt.Errorf("failed to upsert keys: %w", err)
|
2021-07-27 13:47:32 +02:00
|
|
|
}
|
2021-07-27 18:08:53 +02:00
|
|
|
res.KeyCount = count
|
|
|
|
res.KeyETag = etag
|
2023-04-28 17:49:38 +02:00
|
|
|
return res, nil
|
2021-07-27 13:47:32 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 17:49:38 +02:00
|
|
|
func (a *UserInternalAPI) QueryKeyBackup(ctx context.Context, req *api.QueryKeyBackupRequest) (*api.QueryKeyBackupResponse, error) {
|
|
|
|
res := &api.QueryKeyBackupResponse{}
|
2022-02-18 12:31:05 +01:00
|
|
|
version, algorithm, authData, etag, deleted, err := a.DB.GetKeyBackup(ctx, req.UserID, req.Version)
|
2021-07-27 13:47:32 +02:00
|
|
|
res.Version = version
|
|
|
|
if err != nil {
|
2023-04-28 17:49:38 +02:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return res, nil
|
2021-07-27 13:47:32 +02:00
|
|
|
}
|
2023-04-28 17:49:38 +02:00
|
|
|
if errors.Is(err, strconv.ErrSyntax) {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
return res, fmt.Errorf("failed to query key backup: %s", err)
|
2021-07-27 13:47:32 +02:00
|
|
|
}
|
|
|
|
res.Algorithm = algorithm
|
|
|
|
res.AuthData = authData
|
2021-07-27 20:29:32 +02:00
|
|
|
res.ETag = etag
|
2021-07-27 13:47:32 +02:00
|
|
|
res.Exists = !deleted
|
|
|
|
|
2021-07-27 20:29:32 +02:00
|
|
|
if !req.ReturnKeys {
|
2022-02-18 12:31:05 +01:00
|
|
|
res.Count, err = a.DB.CountBackupKeys(ctx, version, req.UserID)
|
2021-07-27 20:29:32 +02:00
|
|
|
if err != nil {
|
2023-04-28 17:49:38 +02:00
|
|
|
return res, fmt.Errorf("failed to count keys: %w", err)
|
2021-07-27 20:29:32 +02:00
|
|
|
}
|
2023-04-28 17:49:38 +02:00
|
|
|
return res, nil
|
2021-07-27 20:29:32 +02:00
|
|
|
}
|
|
|
|
|
2022-02-18 12:31:05 +01:00
|
|
|
result, err := a.DB.GetBackupKeys(ctx, version, req.UserID, req.KeysForRoomID, req.KeysForSessionID)
|
2021-07-27 20:29:32 +02:00
|
|
|
if err != nil {
|
2023-04-28 17:49:38 +02:00
|
|
|
return res, fmt.Errorf("failed to query keys: %s", err)
|
2021-07-27 20:29:32 +02:00
|
|
|
}
|
|
|
|
res.Keys = result
|
2023-04-28 17:49:38 +02:00
|
|
|
return res, nil
|
2021-07-27 13:47:32 +02:00
|
|
|
}
|
2022-03-03 12:40:53 +01:00
|
|
|
|
|
|
|
func (a *UserInternalAPI) QueryNotifications(ctx context.Context, req *api.QueryNotificationsRequest, res *api.QueryNotificationsResponse) error {
|
|
|
|
if req.Limit == 0 || req.Limit > 1000 {
|
|
|
|
req.Limit = 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
var fromID int64
|
|
|
|
var err error
|
|
|
|
if req.From != "" {
|
|
|
|
fromID, err = strconv.ParseInt(req.From, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("QueryNotifications: parsing 'from': %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var filter tables.NotificationFilter = tables.AllNotifications
|
|
|
|
if req.Only == "highlight" {
|
|
|
|
filter = tables.HighlightNotifications
|
|
|
|
}
|
2022-11-11 17:41:37 +01:00
|
|
|
notifs, lastID, err := a.DB.GetNotifications(ctx, req.Localpart, req.ServerName, fromID, req.Limit, filter)
|
2022-03-03 12:40:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if notifs == nil {
|
|
|
|
// This ensures empty is JSON-encoded as [] instead of null.
|
|
|
|
notifs = []*api.Notification{}
|
|
|
|
}
|
|
|
|
res.Notifications = notifs
|
|
|
|
if lastID >= 0 {
|
|
|
|
res.NextToken = strconv.FormatInt(lastID+1, 10)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) PerformPusherSet(ctx context.Context, req *api.PerformPusherSetRequest, res *struct{}) error {
|
|
|
|
util.GetLogger(ctx).WithFields(logrus.Fields{
|
|
|
|
"localpart": req.Localpart,
|
|
|
|
"pushkey": req.Pusher.PushKey,
|
|
|
|
"display_name": req.Pusher.AppDisplayName,
|
|
|
|
}).Info("PerformPusherCreation")
|
|
|
|
if !req.Append {
|
|
|
|
err := a.DB.RemovePushers(ctx, req.Pusher.AppID, req.Pusher.PushKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if req.Pusher.Kind == "" {
|
2022-11-11 17:41:37 +01:00
|
|
|
return a.DB.RemovePusher(ctx, req.Pusher.AppID, req.Pusher.PushKey, req.Localpart, req.ServerName)
|
2022-03-03 12:40:53 +01:00
|
|
|
}
|
|
|
|
if req.Pusher.PushKeyTS == 0 {
|
2022-04-19 14:57:02 +02:00
|
|
|
req.Pusher.PushKeyTS = int64(time.Now().Unix())
|
2022-03-03 12:40:53 +01:00
|
|
|
}
|
2022-11-11 17:41:37 +01:00
|
|
|
return a.DB.UpsertPusher(ctx, req.Pusher, req.Localpart, req.ServerName)
|
2022-03-03 12:40:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) PerformPusherDeletion(ctx context.Context, req *api.PerformPusherDeletionRequest, res *struct{}) error {
|
2022-11-11 17:41:37 +01:00
|
|
|
pushers, err := a.DB.GetPushers(ctx, req.Localpart, req.ServerName)
|
2022-03-03 12:40:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range pushers {
|
|
|
|
logrus.Warnf("pusher session: %d, req session: %d", pushers[i].SessionID, req.SessionID)
|
|
|
|
if pushers[i].SessionID != req.SessionID {
|
2022-11-11 17:41:37 +01:00
|
|
|
err := a.DB.RemovePusher(ctx, pushers[i].AppID, pushers[i].PushKey, req.Localpart, req.ServerName)
|
2022-03-03 12:40:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) QueryPushers(ctx context.Context, req *api.QueryPushersRequest, res *api.QueryPushersResponse) error {
|
|
|
|
var err error
|
2022-11-11 17:41:37 +01:00
|
|
|
res.Pushers, err = a.DB.GetPushers(ctx, req.Localpart, req.ServerName)
|
2022-03-03 12:40:53 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) PerformPushRulesPut(
|
|
|
|
ctx context.Context,
|
2023-04-14 13:35:27 +02:00
|
|
|
userID string,
|
|
|
|
ruleSets *pushrules.AccountRuleSets,
|
2022-03-03 12:40:53 +01:00
|
|
|
) error {
|
2023-04-14 13:35:27 +02:00
|
|
|
bs, err := json.Marshal(ruleSets)
|
2022-03-03 12:40:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
userReq := api.InputAccountDataRequest{
|
2023-04-14 13:35:27 +02:00
|
|
|
UserID: userID,
|
2022-03-03 12:40:53 +01:00
|
|
|
DataType: pushRulesAccountDataType,
|
|
|
|
AccountData: json.RawMessage(bs),
|
|
|
|
}
|
|
|
|
var userRes api.InputAccountDataResponse // empty
|
2023-04-14 13:35:27 +02:00
|
|
|
return a.InputAccountData(ctx, &userReq, &userRes)
|
2022-03-03 12:40:53 +01:00
|
|
|
}
|
|
|
|
|
2023-04-14 13:35:27 +02:00
|
|
|
func (a *UserInternalAPI) QueryPushRules(ctx context.Context, userID string) (*pushrules.AccountRuleSets, error) {
|
|
|
|
localpart, domain, err := gomatrixserverlib.SplitID('@', userID)
|
2022-09-09 13:56:33 +02:00
|
|
|
if err != nil {
|
2023-04-14 13:35:27 +02:00
|
|
|
return nil, fmt.Errorf("failed to split user ID %q for push rules", userID)
|
2022-03-03 12:40:53 +01:00
|
|
|
}
|
2023-04-14 13:35:27 +02:00
|
|
|
return a.DB.QueryPushRules(ctx, localpart, domain)
|
2022-03-03 12:40:53 +01:00
|
|
|
}
|
|
|
|
|
2023-04-19 16:50:33 +02:00
|
|
|
func (a *UserInternalAPI) SetAvatarURL(ctx context.Context, localpart string, serverName spec.ServerName, avatarURL string) (*authtypes.Profile, bool, error) {
|
2023-04-03 20:19:26 +02:00
|
|
|
return a.DB.SetAvatarURL(ctx, localpart, serverName, avatarURL)
|
2022-03-24 22:45:44 +01:00
|
|
|
}
|
|
|
|
|
2022-11-11 17:41:37 +01:00
|
|
|
func (a *UserInternalAPI) QueryNumericLocalpart(ctx context.Context, req *api.QueryNumericLocalpartRequest, res *api.QueryNumericLocalpartResponse) error {
|
|
|
|
id, err := a.DB.GetNewNumericLocalpart(ctx, req.ServerName)
|
2022-03-24 22:45:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res.ID = id
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) QueryAccountAvailability(ctx context.Context, req *api.QueryAccountAvailabilityRequest, res *api.QueryAccountAvailabilityResponse) error {
|
2022-03-28 14:31:17 +02:00
|
|
|
var err error
|
2022-11-11 17:41:37 +01:00
|
|
|
res.Available, err = a.DB.CheckAccountAvailability(ctx, req.Localpart, req.ServerName)
|
2022-03-24 22:45:44 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) QueryAccountByPassword(ctx context.Context, req *api.QueryAccountByPasswordRequest, res *api.QueryAccountByPasswordResponse) error {
|
2022-11-11 17:41:37 +01:00
|
|
|
acc, err := a.DB.GetAccountByPassword(ctx, req.Localpart, req.ServerName, req.PlaintextPassword)
|
2022-03-24 22:45:44 +01:00
|
|
|
switch err {
|
|
|
|
case sql.ErrNoRows: // user does not exist
|
|
|
|
return nil
|
|
|
|
case bcrypt.ErrMismatchedHashAndPassword: // user exists, but password doesn't match
|
|
|
|
return nil
|
2022-10-10 11:39:29 +02:00
|
|
|
case bcrypt.ErrHashTooShort: // user exists, but probably a passwordless account
|
|
|
|
return nil
|
2022-03-24 22:45:44 +01:00
|
|
|
default:
|
|
|
|
res.Exists = true
|
|
|
|
res.Account = acc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 16:50:33 +02:00
|
|
|
func (a *UserInternalAPI) SetDisplayName(ctx context.Context, localpart string, serverName spec.ServerName, displayName string) (*authtypes.Profile, bool, error) {
|
2023-04-03 20:19:26 +02:00
|
|
|
return a.DB.SetDisplayName(ctx, localpart, serverName, displayName)
|
2022-03-24 22:45:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) QueryLocalpartForThreePID(ctx context.Context, req *api.QueryLocalpartForThreePIDRequest, res *api.QueryLocalpartForThreePIDResponse) error {
|
2022-11-11 17:41:37 +01:00
|
|
|
localpart, domain, err := a.DB.GetLocalpartForThreePID(ctx, req.ThreePID, req.Medium)
|
2022-03-24 22:45:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res.Localpart = localpart
|
2022-11-11 17:41:37 +01:00
|
|
|
res.ServerName = domain
|
2022-03-24 22:45:44 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) QueryThreePIDsForLocalpart(ctx context.Context, req *api.QueryThreePIDsForLocalpartRequest, res *api.QueryThreePIDsForLocalpartResponse) error {
|
2022-11-11 17:41:37 +01:00
|
|
|
r, err := a.DB.GetThreePIDsForLocalpart(ctx, req.Localpart, req.ServerName)
|
2022-03-24 22:45:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res.ThreePIDs = r
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) PerformForgetThreePID(ctx context.Context, req *api.PerformForgetThreePIDRequest, res *struct{}) error {
|
|
|
|
return a.DB.RemoveThreePIDAssociation(ctx, req.ThreePID, req.Medium)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UserInternalAPI) PerformSaveThreePIDAssociation(ctx context.Context, req *api.PerformSaveThreePIDAssociationRequest, res *struct{}) error {
|
2022-11-11 17:41:37 +01:00
|
|
|
return a.DB.SaveThreePIDAssociation(ctx, req.ThreePID, req.Localpart, req.ServerName, req.Medium)
|
2022-03-24 22:45:44 +01:00
|
|
|
}
|
|
|
|
|
2022-03-03 12:40:53 +01:00
|
|
|
const pushRulesAccountDataType = "m.push_rules"
|