2020-07-15 13:02:34 +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 sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2020-08-05 14:41:16 +02:00
|
|
|
"strings"
|
2020-07-15 13:02:34 +02:00
|
|
|
"time"
|
|
|
|
|
2020-07-15 19:40:41 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-08-05 12:01:37 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-07-15 13:02:34 +02:00
|
|
|
"github.com/matrix-org/dendrite/keyserver/api"
|
|
|
|
"github.com/matrix-org/dendrite/keyserver/storage/tables"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 18:07:06 +02:00
|
|
|
stream_id BIGINT NOT NULL,
|
2020-07-15 13:02:34 +02:00
|
|
|
-- Clobber based on tuple of user/device.
|
|
|
|
UNIQUE (user_id, device_id)
|
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
const upsertDeviceKeysSQL = "" +
|
2020-08-03 18:07:06 +02:00
|
|
|
"INSERT INTO keyserver_device_keys (user_id, device_id, ts_added_secs, key_json, stream_id)" +
|
|
|
|
" VALUES ($1, $2, $3, $4, $5)" +
|
2020-07-15 13:02:34 +02:00
|
|
|
" ON CONFLICT (user_id, device_id)" +
|
2020-08-03 18:07:06 +02:00
|
|
|
" DO UPDATE SET key_json = $4, stream_id = $5"
|
2020-07-15 13:02:34 +02:00
|
|
|
|
|
|
|
const selectDeviceKeysSQL = "" +
|
2020-08-03 18:07:06 +02:00
|
|
|
"SELECT key_json, stream_id FROM keyserver_device_keys WHERE user_id=$1 AND device_id=$2"
|
2020-07-15 13:02:34 +02:00
|
|
|
|
2020-07-15 19:40:41 +02:00
|
|
|
const selectBatchDeviceKeysSQL = "" +
|
2020-08-03 18:07:06 +02:00
|
|
|
"SELECT device_id, key_json, stream_id FROM keyserver_device_keys WHERE user_id=$1"
|
|
|
|
|
|
|
|
const selectMaxStreamForUserSQL = "" +
|
|
|
|
"SELECT MAX(stream_id) FROM keyserver_device_keys WHERE user_id=$1"
|
2020-07-15 19:40:41 +02:00
|
|
|
|
2020-08-05 14:41:16 +02:00
|
|
|
const countStreamIDsForUserSQL = "" +
|
|
|
|
"SELECT COUNT(*) FROM keyserver_device_keys WHERE user_id=$1 AND stream_id IN ($2)"
|
|
|
|
|
2020-07-15 13:02:34 +02:00
|
|
|
type deviceKeysStatements struct {
|
2020-08-03 18:07:06 +02:00
|
|
|
db *sql.DB
|
2020-08-05 12:01:37 +02:00
|
|
|
writer *sqlutil.TransactionWriter
|
2020-08-03 18:07:06 +02:00
|
|
|
upsertDeviceKeysStmt *sql.Stmt
|
|
|
|
selectDeviceKeysStmt *sql.Stmt
|
|
|
|
selectBatchDeviceKeysStmt *sql.Stmt
|
|
|
|
selectMaxStreamForUserStmt *sql.Stmt
|
2020-07-15 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSqliteDeviceKeysTable(db *sql.DB) (tables.DeviceKeys, error) {
|
|
|
|
s := &deviceKeysStatements{
|
2020-08-05 12:01:37 +02:00
|
|
|
db: db,
|
|
|
|
writer: sqlutil.NewTransactionWriter(),
|
2020-07-15 13:02:34 +02:00
|
|
|
}
|
|
|
|
_, err := db.Exec(deviceKeysSchema)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if s.upsertDeviceKeysStmt, err = db.Prepare(upsertDeviceKeysSQL); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if s.selectDeviceKeysStmt, err = db.Prepare(selectDeviceKeysSQL); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 19:40:41 +02:00
|
|
|
if s.selectBatchDeviceKeysStmt, err = db.Prepare(selectBatchDeviceKeysSQL); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-03 18:07:06 +02:00
|
|
|
if s.selectMaxStreamForUserStmt, err = db.Prepare(selectMaxStreamForUserSQL); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 13:02:34 +02:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2020-08-03 18:07:06 +02:00
|
|
|
func (s *deviceKeysStatements) SelectBatchDeviceKeys(ctx context.Context, userID string, deviceIDs []string) ([]api.DeviceMessage, error) {
|
2020-07-15 19:40:41 +02:00
|
|
|
deviceIDMap := make(map[string]bool)
|
|
|
|
for _, d := range deviceIDs {
|
|
|
|
deviceIDMap[d] = true
|
|
|
|
}
|
|
|
|
rows, err := s.selectBatchDeviceKeysStmt.QueryContext(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectBatchDeviceKeysStmt: rows.close() failed")
|
2020-08-03 18:07:06 +02:00
|
|
|
var result []api.DeviceMessage
|
2020-07-15 19:40:41 +02:00
|
|
|
for rows.Next() {
|
2020-08-03 18:07:06 +02:00
|
|
|
var dk api.DeviceMessage
|
2020-07-15 19:40:41 +02:00
|
|
|
dk.UserID = userID
|
|
|
|
var keyJSON string
|
2020-08-03 18:07:06 +02:00
|
|
|
var streamID int
|
|
|
|
if err := rows.Scan(&dk.DeviceID, &keyJSON, &streamID); err != nil {
|
2020-07-15 19:40:41 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dk.KeyJSON = []byte(keyJSON)
|
2020-08-03 18:07:06 +02:00
|
|
|
dk.StreamID = streamID
|
2020-07-15 19:40:41 +02: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 18:07:06 +02:00
|
|
|
func (s *deviceKeysStatements) SelectDeviceKeysJSON(ctx context.Context, keys []api.DeviceMessage) error {
|
2020-07-15 13:02:34 +02:00
|
|
|
for i, key := range keys {
|
|
|
|
var keyJSONStr string
|
2020-08-03 18:07:06 +02:00
|
|
|
var streamID int
|
|
|
|
err := s.selectDeviceKeysStmt.QueryRowContext(ctx, key.UserID, key.DeviceID).Scan(&keyJSONStr, &streamID)
|
2020-07-15 13:02:34 +02:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// this will be '' when there is no device
|
|
|
|
keys[i].KeyJSON = []byte(keyJSONStr)
|
2020-08-03 18:07:06 +02:00
|
|
|
keys[i].StreamID = streamID
|
2020-07-15 13:02:34 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-03 18:07:06 +02:00
|
|
|
func (s *deviceKeysStatements) SelectMaxStreamIDForUser(ctx context.Context, txn *sql.Tx, userID string) (streamID int32, err error) {
|
|
|
|
// nullable if there are no results
|
|
|
|
var nullStream sql.NullInt32
|
|
|
|
err = txn.Stmt(s.selectMaxStreamForUserStmt).QueryRowContext(ctx, userID).Scan(&nullStream)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if nullStream.Valid {
|
|
|
|
streamID = nullStream.Int32
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:41:16 +02: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
|
|
|
|
var count sql.NullInt32
|
|
|
|
err := s.db.QueryRowContext(ctx, query, iStreamIDs...).Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if count.Valid {
|
|
|
|
return int(count.Int32), nil
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2020-08-03 18:07:06 +02:00
|
|
|
func (s *deviceKeysStatements) InsertDeviceKeys(ctx context.Context, txn *sql.Tx, keys []api.DeviceMessage) error {
|
2020-08-05 12:01:37 +02:00
|
|
|
return s.writer.Do(s.db, txn, func(txn *sql.Tx) error {
|
|
|
|
for _, key := range keys {
|
|
|
|
now := time.Now().Unix()
|
|
|
|
_, err := txn.Stmt(s.upsertDeviceKeysStmt).ExecContext(
|
|
|
|
ctx, key.UserID, key.DeviceID, now, string(key.KeyJSON), key.StreamID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-15 13:02:34 +02:00
|
|
|
}
|
2020-08-05 12:01:37 +02:00
|
|
|
return nil
|
|
|
|
})
|
2020-07-15 13:02:34 +02:00
|
|
|
}
|