2020-07-15 11:02:34 +00: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 sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2020-08-05 12:41:16 +00:00
|
|
|
"strings"
|
2020-07-15 11:02:34 +00:00
|
|
|
"time"
|
|
|
|
|
2020-07-15 17:40:41 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-08-05 10:01:37 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2023-02-20 13:58:03 +00:00
|
|
|
"github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/tables"
|
2020-07-15 11:02:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var deviceKeysSchema = `
|
|
|
|
-- Stores device keys for users
|
|
|
|
CREATE TABLE IF NOT EXISTS keyserver_device_keys (
|
|
|
|
user_id TEXT NOT NULL,
|
|
|
|
device_id TEXT NOT NULL,
|
|
|
|
ts_added_secs BIGINT NOT NULL,
|
|
|
|
key_json TEXT NOT NULL,
|
2020-08-03 16:07:06 +00:00
|
|
|
stream_id BIGINT NOT NULL,
|
2020-08-07 16:32:13 +00:00
|
|
|
display_name TEXT,
|
2020-07-15 11:02:34 +00:00
|
|
|
-- Clobber based on tuple of user/device.
|
|
|
|
UNIQUE (user_id, device_id)
|
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
const upsertDeviceKeysSQL = "" +
|
2020-08-07 16:32:13 +00:00
|
|
|
"INSERT INTO keyserver_device_keys (user_id, device_id, ts_added_secs, key_json, stream_id, display_name)" +
|
|
|
|
" VALUES ($1, $2, $3, $4, $5, $6)" +
|
2020-07-15 11:02:34 +00:00
|
|
|
" ON CONFLICT (user_id, device_id)" +
|
2020-08-07 16:32:13 +00:00
|
|
|
" DO UPDATE SET key_json = $4, stream_id = $5, display_name = $6"
|
2020-07-15 11:02:34 +00:00
|
|
|
|
|
|
|
const selectDeviceKeysSQL = "" +
|
2020-08-07 16:32:13 +00:00
|
|
|
"SELECT key_json, stream_id, display_name FROM keyserver_device_keys WHERE user_id=$1 AND device_id=$2"
|
2020-07-15 11:02:34 +00:00
|
|
|
|
2020-07-15 17:40:41 +00:00
|
|
|
const selectBatchDeviceKeysSQL = "" +
|
2020-09-08 16:47:54 +00:00
|
|
|
"SELECT device_id, key_json, stream_id, display_name FROM keyserver_device_keys WHERE user_id=$1 AND key_json <> ''"
|
2020-08-03 16:07:06 +00:00
|
|
|
|
2022-02-18 11:31:05 +00:00
|
|
|
const selectBatchDeviceKeysWithEmptiesSQL = "" +
|
|
|
|
"SELECT device_id, key_json, stream_id, display_name FROM keyserver_device_keys WHERE user_id=$1"
|
|
|
|
|
2020-08-03 16:07:06 +00:00
|
|
|
const selectMaxStreamForUserSQL = "" +
|
|
|
|
"SELECT MAX(stream_id) FROM keyserver_device_keys WHERE user_id=$1"
|
2020-07-15 17:40:41 +00:00
|
|
|
|
2020-08-05 12:41:16 +00:00
|
|
|
const countStreamIDsForUserSQL = "" +
|
|
|
|
"SELECT COUNT(*) FROM keyserver_device_keys WHERE user_id=$1 AND stream_id IN ($2)"
|
|
|
|
|
2021-08-18 11:07:09 +00:00
|
|
|
const deleteDeviceKeysSQL = "" +
|
|
|
|
"DELETE FROM keyserver_device_keys WHERE user_id=$1 AND device_id=$2"
|
|
|
|
|
2020-08-12 21:43:02 +00:00
|
|
|
const deleteAllDeviceKeysSQL = "" +
|
|
|
|
"DELETE FROM keyserver_device_keys WHERE user_id=$1"
|
|
|
|
|
2020-07-15 11:02:34 +00:00
|
|
|
type deviceKeysStatements struct {
|
2022-02-18 11:31:05 +00:00
|
|
|
db *sql.DB
|
|
|
|
upsertDeviceKeysStmt *sql.Stmt
|
|
|
|
selectDeviceKeysStmt *sql.Stmt
|
|
|
|
selectBatchDeviceKeysStmt *sql.Stmt
|
|
|
|
selectBatchDeviceKeysWithEmptiesStmt *sql.Stmt
|
|
|
|
selectMaxStreamForUserStmt *sql.Stmt
|
|
|
|
deleteDeviceKeysStmt *sql.Stmt
|
|
|
|
deleteAllDeviceKeysStmt *sql.Stmt
|
2020-07-15 11:02:34 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 09:29:45 +00:00
|
|
|
func NewSqliteDeviceKeysTable(db *sql.DB) (tables.DeviceKeys, error) {
|
2020-07-15 11:02:34 +00:00
|
|
|
s := &deviceKeysStatements{
|
2020-08-25 09:29:45 +00:00
|
|
|
db: db,
|
2020-07-15 11:02:34 +00:00
|
|
|
}
|
|
|
|
_, err := db.Exec(deviceKeysSchema)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-20 13:58:03 +00:00
|
|
|
return s, sqlutil.StatementList{
|
|
|
|
{&s.upsertDeviceKeysStmt, upsertDeviceKeysSQL},
|
|
|
|
{&s.selectDeviceKeysStmt, selectDeviceKeysSQL},
|
|
|
|
{&s.selectBatchDeviceKeysStmt, selectBatchDeviceKeysSQL},
|
|
|
|
{&s.selectBatchDeviceKeysWithEmptiesStmt, selectBatchDeviceKeysWithEmptiesSQL},
|
|
|
|
{&s.selectMaxStreamForUserStmt, selectMaxStreamForUserSQL},
|
|
|
|
// {&s.countStreamIDsForUserStmt, countStreamIDsForUserSQL}, // prepared at runtime
|
|
|
|
{&s.deleteDeviceKeysStmt, deleteDeviceKeysSQL},
|
|
|
|
{&s.deleteAllDeviceKeysStmt, deleteAllDeviceKeysSQL},
|
|
|
|
}.Prepare(db)
|
2020-07-15 11:02:34 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 11:07:09 +00:00
|
|
|
func (s *deviceKeysStatements) DeleteDeviceKeys(ctx context.Context, txn *sql.Tx, userID, deviceID string) error {
|
|
|
|
_, err := sqlutil.TxStmt(txn, s.deleteDeviceKeysStmt).ExecContext(ctx, userID, deviceID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-12 21:43:02 +00:00
|
|
|
func (s *deviceKeysStatements) DeleteAllDeviceKeys(ctx context.Context, txn *sql.Tx, userID string) error {
|
2020-09-24 10:10:14 +00:00
|
|
|
_, err := sqlutil.TxStmt(txn, s.deleteAllDeviceKeysStmt).ExecContext(ctx, userID)
|
2020-08-12 21:43:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-18 11:31:05 +00:00
|
|
|
func (s *deviceKeysStatements) SelectBatchDeviceKeys(ctx context.Context, userID string, deviceIDs []string, includeEmpty bool) ([]api.DeviceMessage, error) {
|
2020-07-15 17:40:41 +00:00
|
|
|
deviceIDMap := make(map[string]bool)
|
|
|
|
for _, d := range deviceIDs {
|
|
|
|
deviceIDMap[d] = true
|
|
|
|
}
|
2022-02-18 11:31:05 +00:00
|
|
|
var stmt *sql.Stmt
|
|
|
|
if includeEmpty {
|
|
|
|
stmt = s.selectBatchDeviceKeysWithEmptiesStmt
|
|
|
|
} else {
|
|
|
|
stmt = s.selectBatchDeviceKeysStmt
|
|
|
|
}
|
|
|
|
rows, err := stmt.QueryContext(ctx, userID)
|
2020-07-15 17:40:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectBatchDeviceKeysStmt: rows.close() failed")
|
2020-08-03 16:07:06 +00:00
|
|
|
var result []api.DeviceMessage
|
2022-10-07 08:54:42 +00:00
|
|
|
var displayName sql.NullString
|
2020-07-15 17:40:41 +00:00
|
|
|
for rows.Next() {
|
2021-08-11 08:44:14 +00:00
|
|
|
dk := api.DeviceMessage{
|
2022-10-07 08:54:42 +00:00
|
|
|
Type: api.TypeDeviceKeyUpdate,
|
|
|
|
DeviceKeys: &api.DeviceKeys{
|
|
|
|
UserID: userID,
|
|
|
|
},
|
2021-08-11 08:44:14 +00:00
|
|
|
}
|
2022-10-07 08:54:42 +00:00
|
|
|
if err := rows.Scan(&dk.DeviceID, &dk.KeyJSON, &dk.StreamID, &displayName); err != nil {
|
2020-07-15 17:40:41 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-07 16:32:13 +00:00
|
|
|
if displayName.Valid {
|
|
|
|
dk.DisplayName = displayName.String
|
|
|
|
}
|
2020-07-15 17:40:41 +00:00
|
|
|
// include the key if we want all keys (no device) or it was asked
|
|
|
|
if deviceIDMap[dk.DeviceID] || len(deviceIDs) == 0 {
|
|
|
|
result = append(result, dk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, rows.Err()
|
|
|
|
}
|
|
|
|
|
2020-08-03 16:07:06 +00:00
|
|
|
func (s *deviceKeysStatements) SelectDeviceKeysJSON(ctx context.Context, keys []api.DeviceMessage) error {
|
2020-07-15 11:02:34 +00:00
|
|
|
for i, key := range keys {
|
|
|
|
var keyJSONStr string
|
2022-03-10 13:17:28 +00:00
|
|
|
var streamID int64
|
2020-08-07 16:32:13 +00:00
|
|
|
var displayName sql.NullString
|
|
|
|
err := s.selectDeviceKeysStmt.QueryRowContext(ctx, key.UserID, key.DeviceID).Scan(&keyJSONStr, &streamID, &displayName)
|
2020-07-15 11:02:34 +00:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// this will be '' when there is no device
|
2021-08-11 08:44:14 +00:00
|
|
|
keys[i].Type = api.TypeDeviceKeyUpdate
|
2020-07-15 11:02:34 +00:00
|
|
|
keys[i].KeyJSON = []byte(keyJSONStr)
|
2020-08-03 16:07:06 +00:00
|
|
|
keys[i].StreamID = streamID
|
2020-08-07 16:32:13 +00:00
|
|
|
if displayName.Valid {
|
|
|
|
keys[i].DisplayName = displayName.String
|
|
|
|
}
|
2020-07-15 11:02:34 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-10 13:17:28 +00:00
|
|
|
func (s *deviceKeysStatements) SelectMaxStreamIDForUser(ctx context.Context, txn *sql.Tx, userID string) (streamID int64, err error) {
|
2020-08-03 16:07:06 +00:00
|
|
|
// nullable if there are no results
|
2022-03-10 13:17:28 +00:00
|
|
|
var nullStream sql.NullInt64
|
2020-09-24 10:10:14 +00:00
|
|
|
err = sqlutil.TxStmt(txn, s.selectMaxStreamForUserStmt).QueryRowContext(ctx, userID).Scan(&nullStream)
|
2020-08-03 16:07:06 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if nullStream.Valid {
|
2022-03-10 13:17:28 +00:00
|
|
|
streamID = nullStream.Int64
|
2020-08-03 16:07:06 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-05 12:41:16 +00:00
|
|
|
func (s *deviceKeysStatements) CountStreamIDsForUser(ctx context.Context, userID string, streamIDs []int64) (int, error) {
|
|
|
|
iStreamIDs := make([]interface{}, len(streamIDs)+1)
|
|
|
|
iStreamIDs[0] = userID
|
|
|
|
for i := range streamIDs {
|
|
|
|
iStreamIDs[i+1] = streamIDs[i]
|
|
|
|
}
|
|
|
|
query := strings.Replace(countStreamIDsForUserSQL, "($2)", sqlutil.QueryVariadicOffset(len(streamIDs), 1), 1)
|
|
|
|
// nullable if there are no results
|
2022-03-10 13:17:28 +00:00
|
|
|
var count sql.NullInt64
|
2020-08-05 12:41:16 +00:00
|
|
|
err := s.db.QueryRowContext(ctx, query, iStreamIDs...).Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if count.Valid {
|
2022-03-10 13:17:28 +00:00
|
|
|
return int(count.Int64), nil
|
2020-08-05 12:41:16 +00:00
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2020-08-03 16:07:06 +00:00
|
|
|
func (s *deviceKeysStatements) InsertDeviceKeys(ctx context.Context, txn *sql.Tx, keys []api.DeviceMessage) error {
|
2020-08-25 09:29:45 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
now := time.Now().Unix()
|
2020-09-24 10:10:14 +00:00
|
|
|
_, err := sqlutil.TxStmt(txn, s.upsertDeviceKeysStmt).ExecContext(
|
2020-08-25 09:29:45 +00:00
|
|
|
ctx, key.UserID, key.DeviceID, now, string(key.KeyJSON), key.StreamID, key.DisplayName,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-07-15 11:02:34 +00:00
|
|
|
}
|
2020-08-25 09:29:45 +00:00
|
|
|
}
|
|
|
|
return nil
|
2020-07-15 11:02:34 +00:00
|
|
|
}
|