aboutsummaryrefslogtreecommitdiff
path: root/userapi/storage/accounts/postgres/storage.go
diff options
context:
space:
mode:
authorkegsay <kegan@matrix.org>2021-07-27 12:47:32 +0100
committerGitHub <noreply@github.com>2021-07-27 12:47:32 +0100
commit32538640db6bfbdb890729e8137151ffbbec9a28 (patch)
treebcd98d8b0d977058492fce03d69f43cc3a41be51 /userapi/storage/accounts/postgres/storage.go
parente3679799ea6f6387d36fb1a2f938f8ea9711ad2d (diff)
Key backups (1/2) : Add E2E session backup metadata tables (#1943)
* Initial key backup paths and userapi API * Fix unit tests * Add key backup table * Glue REST API to database * Linting * use writer on sqlite
Diffstat (limited to 'userapi/storage/accounts/postgres/storage.go')
-rw-r--r--userapi/storage/accounts/postgres/storage.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/userapi/storage/accounts/postgres/storage.go b/userapi/storage/accounts/postgres/storage.go
index c5e74ed1..719e9878 100644
--- a/userapi/storage/accounts/postgres/storage.go
+++ b/userapi/storage/accounts/postgres/storage.go
@@ -45,6 +45,7 @@ type Database struct {
accountDatas accountDataStatements
threepids threepidStatements
openIDTokens tokenStatements
+ keyBackups keyBackupVersionStatements
serverName gomatrixserverlib.ServerName
bcryptCost int
openIDTokenLifetimeMS int64
@@ -93,6 +94,9 @@ func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserver
if err = d.openIDTokens.prepare(db, serverName); err != nil {
return nil, err
}
+ if err = d.keyBackups.prepare(db); err != nil {
+ return nil, err
+ }
return d, nil
}
@@ -368,3 +372,42 @@ func (d *Database) GetOpenIDTokenAttributes(
) (*api.OpenIDTokenAttributes, error) {
return d.openIDTokens.selectOpenIDTokenAtrributes(ctx, token)
}
+
+func (d *Database) CreateKeyBackup(
+ ctx context.Context, userID, algorithm string, authData json.RawMessage,
+) (version string, err error) {
+ err = sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
+ version, err = d.keyBackups.insertKeyBackup(ctx, txn, userID, algorithm, authData)
+ return err
+ })
+ return
+}
+
+func (d *Database) UpdateKeyBackupAuthData(
+ ctx context.Context, userID, version string, authData json.RawMessage,
+) (err error) {
+ err = sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
+ return d.keyBackups.updateKeyBackupAuthData(ctx, txn, userID, version, authData)
+ })
+ return
+}
+
+func (d *Database) DeleteKeyBackup(
+ ctx context.Context, userID, version string,
+) (exists bool, err error) {
+ err = sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
+ exists, err = d.keyBackups.deleteKeyBackup(ctx, txn, userID, version)
+ return err
+ })
+ return
+}
+
+func (d *Database) GetKeyBackup(
+ ctx context.Context, userID, version string,
+) (versionResult, algorithm string, authData json.RawMessage, deleted bool, err error) {
+ err = sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
+ versionResult, algorithm, authData, deleted, err = d.keyBackups.selectKeyBackup(ctx, txn, userID, version)
+ return err
+ })
+ return
+}