2017-09-01 10:13:10 +01:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-02-13 17:27:33 +00:00
|
|
|
package sqlite3
|
2017-09-01 10:13:10 +01:00
|
|
|
|
|
|
|
import (
|
2017-09-18 14:15:27 +01:00
|
|
|
"context"
|
2017-09-01 10:13:10 +01:00
|
|
|
"database/sql"
|
|
|
|
|
2020-05-21 14:40:13 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-06-12 14:55:57 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2022-02-18 13:51:59 +00:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/tables"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2017-09-18 14:15:27 +01:00
|
|
|
|
2017-09-01 10:13:10 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
const threepidSchema = `
|
|
|
|
-- Stores data about third party identifiers
|
2022-10-18 15:59:08 +01:00
|
|
|
CREATE TABLE IF NOT EXISTS userapi_threepids (
|
2017-09-01 10:13:10 +01:00
|
|
|
-- The third party identifier
|
|
|
|
threepid TEXT NOT NULL,
|
|
|
|
-- The 3PID medium
|
|
|
|
medium TEXT NOT NULL DEFAULT 'email',
|
|
|
|
-- The localpart of the Matrix user ID associated to this 3PID
|
|
|
|
localpart TEXT NOT NULL,
|
2022-11-11 16:41:37 +00:00
|
|
|
server_name TEXT NOT NULL,
|
2017-09-01 10:13:10 +01:00
|
|
|
|
|
|
|
PRIMARY KEY(threepid, medium)
|
|
|
|
);
|
|
|
|
|
2022-11-11 16:41:37 +00:00
|
|
|
CREATE INDEX IF NOT EXISTS account_threepid_localpart ON userapi_threepids(localpart, server_name);
|
2017-09-01 10:13:10 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
const selectLocalpartForThreePIDSQL = "" +
|
2022-11-11 16:41:37 +00:00
|
|
|
"SELECT localpart, server_name FROM userapi_threepids WHERE threepid = $1 AND medium = $2"
|
2017-09-01 10:13:10 +01:00
|
|
|
|
|
|
|
const selectThreePIDsForLocalpartSQL = "" +
|
2022-11-11 16:41:37 +00:00
|
|
|
"SELECT threepid, medium FROM userapi_threepids WHERE localpart = $1 AND server_name = $2"
|
2017-09-01 10:13:10 +01:00
|
|
|
|
|
|
|
const insertThreePIDSQL = "" +
|
2022-11-11 16:41:37 +00:00
|
|
|
"INSERT INTO userapi_threepids (threepid, medium, localpart, server_name) VALUES ($1, $2, $3, $4)"
|
2017-09-01 10:13:10 +01:00
|
|
|
|
|
|
|
const deleteThreePIDSQL = "" +
|
2022-10-18 15:59:08 +01:00
|
|
|
"DELETE FROM userapi_threepids WHERE threepid = $1 AND medium = $2"
|
2017-09-01 10:13:10 +01:00
|
|
|
|
|
|
|
type threepidStatements struct {
|
2020-07-21 15:48:21 +01:00
|
|
|
db *sql.DB
|
2017-09-01 10:13:10 +01:00
|
|
|
selectLocalpartForThreePIDStmt *sql.Stmt
|
|
|
|
selectThreePIDsForLocalpartStmt *sql.Stmt
|
|
|
|
insertThreePIDStmt *sql.Stmt
|
|
|
|
deleteThreePIDStmt *sql.Stmt
|
|
|
|
}
|
|
|
|
|
2022-02-18 13:51:59 +00:00
|
|
|
func NewSQLiteThreePIDTable(db *sql.DB) (tables.ThreePIDTable, error) {
|
|
|
|
s := &threepidStatements{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
_, err := db.Exec(threepidSchema)
|
2017-09-01 10:13:10 +01:00
|
|
|
if err != nil {
|
2022-02-18 13:51:59 +00:00
|
|
|
return nil, err
|
2017-09-01 10:13:10 +01:00
|
|
|
}
|
2022-02-18 13:51:59 +00:00
|
|
|
return s, sqlutil.StatementList{
|
2021-07-28 18:30:04 +01:00
|
|
|
{&s.selectLocalpartForThreePIDStmt, selectLocalpartForThreePIDSQL},
|
|
|
|
{&s.selectThreePIDsForLocalpartStmt, selectThreePIDsForLocalpartSQL},
|
|
|
|
{&s.insertThreePIDStmt, insertThreePIDSQL},
|
|
|
|
{&s.deleteThreePIDStmt, deleteThreePIDSQL},
|
|
|
|
}.Prepare(db)
|
2017-09-01 10:13:10 +01:00
|
|
|
}
|
|
|
|
|
2022-02-18 13:51:59 +00:00
|
|
|
func (s *threepidStatements) SelectLocalpartForThreePID(
|
2017-09-18 14:15:27 +01:00
|
|
|
ctx context.Context, txn *sql.Tx, threepid string, medium string,
|
2023-04-19 15:50:33 +01:00
|
|
|
) (localpart string, serverName spec.ServerName, err error) {
|
2020-06-12 14:55:57 +01:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectLocalpartForThreePIDStmt)
|
2022-11-11 16:41:37 +00:00
|
|
|
err = stmt.QueryRowContext(ctx, threepid, medium).Scan(&localpart, &serverName)
|
2017-09-01 10:13:10 +01:00
|
|
|
if err == sql.ErrNoRows {
|
2022-11-11 16:41:37 +00:00
|
|
|
return "", "", nil
|
2017-09-01 10:13:10 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-18 13:51:59 +00:00
|
|
|
func (s *threepidStatements) SelectThreePIDsForLocalpart(
|
2022-11-11 16:41:37 +00:00
|
|
|
ctx context.Context,
|
2023-04-19 15:50:33 +01:00
|
|
|
localpart string, serverName spec.ServerName,
|
2017-09-18 14:15:27 +01:00
|
|
|
) (threepids []authtypes.ThreePID, err error) {
|
2022-11-11 16:41:37 +00:00
|
|
|
rows, err := s.selectThreePIDsForLocalpartStmt.QueryContext(ctx, localpart, serverName)
|
2017-09-01 10:13:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectThreePIDsForLocalpart: rows.close() failed")
|
2017-09-01 10:13:10 +01:00
|
|
|
|
|
|
|
threepids = []authtypes.ThreePID{}
|
|
|
|
for rows.Next() {
|
|
|
|
var threepid string
|
|
|
|
var medium string
|
|
|
|
if err = rows.Scan(&threepid, &medium); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-09-18 14:15:27 +01:00
|
|
|
threepids = append(threepids, authtypes.ThreePID{
|
|
|
|
Address: threepid,
|
|
|
|
Medium: medium,
|
|
|
|
})
|
2017-09-01 10:13:10 +01:00
|
|
|
}
|
2020-02-11 14:12:21 +00:00
|
|
|
return threepids, rows.Err()
|
2017-09-01 10:13:10 +01:00
|
|
|
}
|
|
|
|
|
2022-02-18 13:51:59 +00:00
|
|
|
func (s *threepidStatements) InsertThreePID(
|
2022-11-11 16:41:37 +00:00
|
|
|
ctx context.Context, txn *sql.Tx, threepid, medium,
|
2023-04-19 15:50:33 +01:00
|
|
|
localpart string, serverName spec.ServerName,
|
2017-09-18 14:15:27 +01:00
|
|
|
) (err error) {
|
2020-09-01 11:28:35 +01:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.insertThreePIDStmt)
|
2022-11-11 16:41:37 +00:00
|
|
|
_, err = stmt.ExecContext(ctx, threepid, medium, localpart, serverName)
|
2020-09-01 11:28:35 +01:00
|
|
|
return err
|
2017-09-01 10:13:10 +01:00
|
|
|
}
|
|
|
|
|
2022-02-18 13:51:59 +00:00
|
|
|
func (s *threepidStatements) DeleteThreePID(
|
2020-09-01 11:28:35 +01:00
|
|
|
ctx context.Context, txn *sql.Tx, threepid string, medium string) (err error) {
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.deleteThreePIDStmt)
|
|
|
|
_, err = stmt.ExecContext(ctx, threepid, medium)
|
|
|
|
return err
|
2017-09-01 10:13:10 +01:00
|
|
|
}
|