mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-29 01:33:10 -06:00
Glue REST API to database
This commit is contained in:
parent
32ffde9b8d
commit
3c8bc8bf7e
|
|
@ -540,3 +540,4 @@ Key notary server must not overwrite a valid key with a spurious result from the
|
||||||
GET /rooms/:room_id/aliases lists aliases
|
GET /rooms/:room_id/aliases lists aliases
|
||||||
Only room members can list aliases of a room
|
Only room members can list aliases of a room
|
||||||
Users with sufficient power-level can delete other's aliases
|
Users with sufficient power-level can delete other's aliases
|
||||||
|
Can create more than 10 backup versions
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,7 @@ type PerformKeyBackupResponse struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type QueryKeyBackupRequest struct {
|
type QueryKeyBackupRequest struct {
|
||||||
|
UserID string
|
||||||
Version string // the version to query, if blank it means the latest
|
Version string // the version to query, if blank it means the latest
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -444,18 +444,58 @@ func (a *UserInternalAPI) QueryOpenIDToken(ctx context.Context, req *api.QueryOp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *UserInternalAPI) PerformKeyBackup(ctx context.Context, req *api.PerformKeyBackupRequest, res *api.PerformKeyBackupResponse) {
|
func (a *UserInternalAPI) PerformKeyBackup(ctx context.Context, req *api.PerformKeyBackupRequest, res *api.PerformKeyBackupResponse) {
|
||||||
|
// Delete
|
||||||
if req.DeleteBackup {
|
if req.DeleteBackup {
|
||||||
if req.Version == "" {
|
if req.Version == "" {
|
||||||
res.BadInput = true
|
res.BadInput = true
|
||||||
res.Error = "must specify a version to delete"
|
res.Error = "must specify a version to delete"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
exists, err := a.AccountDB.DeleteKeyBackup(ctx, req.UserID, req.Version)
|
||||||
|
if err != nil {
|
||||||
|
res.Error = fmt.Sprintf("failed to delete backup: %s", err)
|
||||||
|
}
|
||||||
|
res.Exists = exists
|
||||||
|
res.Version = req.Version
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if req.Version != "" {
|
// Create
|
||||||
// modifying a backup, check the algorithm matches
|
if req.Version == "" {
|
||||||
|
version, err := a.AccountDB.CreateKeyBackup(ctx, req.UserID, req.Algorithm, req.AuthData)
|
||||||
|
if err != nil {
|
||||||
|
res.Error = fmt.Sprintf("failed to create backup: %s", err)
|
||||||
|
}
|
||||||
|
res.Exists = err == nil
|
||||||
|
res.Version = version
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
// Update
|
||||||
|
err := a.AccountDB.UpdateKeyBackupAuthData(ctx, req.UserID, req.Version, req.AuthData)
|
||||||
|
if err != nil {
|
||||||
|
res.Error = fmt.Sprintf("failed to update backup: %s", err)
|
||||||
|
}
|
||||||
|
res.Version = req.Version
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
func (a *UserInternalAPI) QueryKeyBackup(ctx context.Context, req *api.QueryKeyBackupRequest, res *api.QueryKeyBackupResponse) {
|
func (a *UserInternalAPI) QueryKeyBackup(ctx context.Context, req *api.QueryKeyBackupRequest, res *api.QueryKeyBackupResponse) {
|
||||||
|
version, algorithm, authData, deleted, err := a.AccountDB.GetKeyBackup(ctx, req.UserID, req.Version)
|
||||||
|
res.Version = version
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
res.Exists = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res.Error = fmt.Sprintf("failed to query key backup: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res.Algorithm = algorithm
|
||||||
|
res.AuthData = authData
|
||||||
|
res.Exists = !deleted
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
res.Count = 0
|
||||||
|
res.ETag = ""
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ type Database interface {
|
||||||
// Key backups
|
// Key backups
|
||||||
CreateKeyBackup(ctx context.Context, userID, algorithm string, authData json.RawMessage) (version string, err error)
|
CreateKeyBackup(ctx context.Context, userID, algorithm string, authData json.RawMessage) (version string, err error)
|
||||||
UpdateKeyBackupAuthData(ctx context.Context, userID, version string, authData json.RawMessage) (err error)
|
UpdateKeyBackupAuthData(ctx context.Context, userID, version string, authData json.RawMessage) (err error)
|
||||||
DeleteKeyBackup(ctx context.Context, userID, version string) (deleted bool, err error)
|
DeleteKeyBackup(ctx context.Context, userID, version string) (exists bool, err error)
|
||||||
GetKeyBackup(ctx context.Context, userID, version string) (versionResult, algorithm string, authData json.RawMessage, deleted bool, err error)
|
GetKeyBackup(ctx context.Context, userID, version string) (versionResult, algorithm string, authData json.RawMessage, deleted bool, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ const updateKeyBackupAuthDataSQL = "" + // TODO: do we need to WHERE algorithm =
|
||||||
"UPDATE account_e2e_room_keys_versions SET auth_data = $1 WHERE user_id = $2 AND version = $3"
|
"UPDATE account_e2e_room_keys_versions SET auth_data = $1 WHERE user_id = $2 AND version = $3"
|
||||||
|
|
||||||
const deleteKeyBackupSQL = "" +
|
const deleteKeyBackupSQL = "" +
|
||||||
"UPDATE account_e2e_room_keys_versions SET deleted=1 WHERE user_id = $1 AND version = $2 AND deleted=0"
|
"UPDATE account_e2e_room_keys_versions SET deleted=1 WHERE user_id = $1 AND version = $2"
|
||||||
|
|
||||||
const selectKeyBackupSQL = "" +
|
const selectKeyBackupSQL = "" +
|
||||||
"SELECT algorithm, auth_data, deleted FROM account_e2e_room_keys_versions WHERE user_id = $1 AND version = $2"
|
"SELECT algorithm, auth_data, deleted FROM account_e2e_room_keys_versions WHERE user_id = $1 AND version = $2"
|
||||||
|
|
|
||||||
|
|
@ -394,9 +394,9 @@ func (d *Database) UpdateKeyBackupAuthData(
|
||||||
|
|
||||||
func (d *Database) DeleteKeyBackup(
|
func (d *Database) DeleteKeyBackup(
|
||||||
ctx context.Context, userID, version string,
|
ctx context.Context, userID, version string,
|
||||||
) (deleted bool, err error) {
|
) (exists bool, err error) {
|
||||||
err = sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
|
err = sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
|
||||||
deleted, err = d.keyBackups.deleteKeyBackup(ctx, txn, userID, version)
|
exists, err = d.keyBackups.deleteKeyBackup(ctx, txn, userID, version)
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ const updateKeyBackupAuthDataSQL = "" + // TODO: do we need to WHERE algorithm =
|
||||||
"UPDATE account_e2e_room_keys_versions SET auth_data = $1 WHERE user_id = $2 AND version = $3"
|
"UPDATE account_e2e_room_keys_versions SET auth_data = $1 WHERE user_id = $2 AND version = $3"
|
||||||
|
|
||||||
const deleteKeyBackupSQL = "" +
|
const deleteKeyBackupSQL = "" +
|
||||||
"UPDATE account_e2e_room_keys_versions SET deleted=1 WHERE user_id = $1 AND version = $2 AND deleted=0"
|
"UPDATE account_e2e_room_keys_versions SET deleted=1 WHERE user_id = $1 AND version = $2"
|
||||||
|
|
||||||
const selectKeyBackupSQL = "" +
|
const selectKeyBackupSQL = "" +
|
||||||
"SELECT algorithm, auth_data, deleted FROM account_e2e_room_keys_versions WHERE user_id = $1 AND version = $2"
|
"SELECT algorithm, auth_data, deleted FROM account_e2e_room_keys_versions WHERE user_id = $1 AND version = $2"
|
||||||
|
|
|
||||||
|
|
@ -432,9 +432,9 @@ func (d *Database) UpdateKeyBackupAuthData(
|
||||||
|
|
||||||
func (d *Database) DeleteKeyBackup(
|
func (d *Database) DeleteKeyBackup(
|
||||||
ctx context.Context, userID, version string,
|
ctx context.Context, userID, version string,
|
||||||
) (deleted bool, err error) {
|
) (exists bool, err error) {
|
||||||
err = sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
|
err = sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
|
||||||
deleted, err = d.keyBackups.deleteKeyBackup(ctx, txn, userID, version)
|
exists, err = d.keyBackups.deleteKeyBackup(ctx, txn, userID, version)
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue