2017-09-01 11:13:10 +02: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 18:27:33 +01:00
|
|
|
package postgres
|
2017-09-01 11:13:10 +02:00
|
|
|
|
|
|
|
import (
|
2017-09-18 15:15:27 +02:00
|
|
|
"context"
|
2017-09-01 11:13:10 +02:00
|
|
|
"database/sql"
|
|
|
|
|
2020-06-12 15:55:57 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2022-02-18 14:51:59 +01:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/tables"
|
2017-09-18 15:15:27 +02:00
|
|
|
|
2017-09-01 11:13:10 +02:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
const threepidSchema = `
|
|
|
|
-- Stores data about third party identifiers
|
|
|
|
CREATE TABLE IF NOT EXISTS account_threepid (
|
|
|
|
-- 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,
|
|
|
|
|
|
|
|
PRIMARY KEY(threepid, medium)
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE INDEX IF NOT EXISTS account_threepid_localpart ON account_threepid(localpart);
|
|
|
|
`
|
|
|
|
|
|
|
|
const selectLocalpartForThreePIDSQL = "" +
|
|
|
|
"SELECT localpart FROM account_threepid WHERE threepid = $1 AND medium = $2"
|
|
|
|
|
|
|
|
const selectThreePIDsForLocalpartSQL = "" +
|
|
|
|
"SELECT threepid, medium FROM account_threepid WHERE localpart = $1"
|
|
|
|
|
|
|
|
const insertThreePIDSQL = "" +
|
|
|
|
"INSERT INTO account_threepid (threepid, medium, localpart) VALUES ($1, $2, $3)"
|
|
|
|
|
|
|
|
const deleteThreePIDSQL = "" +
|
|
|
|
"DELETE FROM account_threepid WHERE threepid = $1 AND medium = $2"
|
|
|
|
|
|
|
|
type threepidStatements struct {
|
|
|
|
selectLocalpartForThreePIDStmt *sql.Stmt
|
|
|
|
selectThreePIDsForLocalpartStmt *sql.Stmt
|
|
|
|
insertThreePIDStmt *sql.Stmt
|
|
|
|
deleteThreePIDStmt *sql.Stmt
|
|
|
|
}
|
|
|
|
|
2022-02-18 14:51:59 +01:00
|
|
|
func NewPostgresThreePIDTable(db *sql.DB) (tables.ThreePIDTable, error) {
|
|
|
|
s := &threepidStatements{}
|
|
|
|
_, err := db.Exec(threepidSchema)
|
2017-09-01 11:13:10 +02:00
|
|
|
if err != nil {
|
2022-02-18 14:51:59 +01:00
|
|
|
return nil, err
|
2017-09-01 11:13:10 +02:00
|
|
|
}
|
2022-02-18 14:51:59 +01:00
|
|
|
return s, sqlutil.StatementList{
|
2021-07-28 19:30:04 +02:00
|
|
|
{&s.selectLocalpartForThreePIDStmt, selectLocalpartForThreePIDSQL},
|
|
|
|
{&s.selectThreePIDsForLocalpartStmt, selectThreePIDsForLocalpartSQL},
|
|
|
|
{&s.insertThreePIDStmt, insertThreePIDSQL},
|
|
|
|
{&s.deleteThreePIDStmt, deleteThreePIDSQL},
|
|
|
|
}.Prepare(db)
|
2017-09-01 11:13:10 +02:00
|
|
|
}
|
|
|
|
|
2022-02-18 14:51:59 +01:00
|
|
|
func (s *threepidStatements) SelectLocalpartForThreePID(
|
2017-09-18 15:15:27 +02:00
|
|
|
ctx context.Context, txn *sql.Tx, threepid string, medium string,
|
|
|
|
) (localpart string, err error) {
|
2020-06-12 15:55:57 +02:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectLocalpartForThreePIDStmt)
|
2017-09-18 15:15:27 +02:00
|
|
|
err = stmt.QueryRowContext(ctx, threepid, medium).Scan(&localpart)
|
2017-09-01 11:13:10 +02:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-18 14:51:59 +01:00
|
|
|
func (s *threepidStatements) SelectThreePIDsForLocalpart(
|
2017-09-18 15:15:27 +02:00
|
|
|
ctx context.Context, localpart string,
|
|
|
|
) (threepids []authtypes.ThreePID, err error) {
|
|
|
|
rows, err := s.selectThreePIDsForLocalpartStmt.QueryContext(ctx, localpart)
|
2017-09-01 11:13:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
threepids = []authtypes.ThreePID{}
|
|
|
|
for rows.Next() {
|
|
|
|
var threepid string
|
|
|
|
var medium string
|
|
|
|
if err = rows.Scan(&threepid, &medium); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-09-18 15:15:27 +02:00
|
|
|
threepids = append(threepids, authtypes.ThreePID{
|
|
|
|
Address: threepid,
|
|
|
|
Medium: medium,
|
|
|
|
})
|
2017-09-01 11:13:10 +02:00
|
|
|
}
|
2020-02-13 18:27:33 +01:00
|
|
|
|
|
|
|
return
|
2017-09-01 11:13:10 +02:00
|
|
|
}
|
|
|
|
|
2022-02-18 14:51:59 +01:00
|
|
|
func (s *threepidStatements) InsertThreePID(
|
2017-09-18 15:15:27 +02:00
|
|
|
ctx context.Context, txn *sql.Tx, threepid, medium, localpart string,
|
|
|
|
) (err error) {
|
2020-06-12 15:55:57 +02:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.insertThreePIDStmt)
|
2017-09-18 15:15:27 +02:00
|
|
|
_, err = stmt.ExecContext(ctx, threepid, medium, localpart)
|
2017-09-01 11:13:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-18 14:51:59 +01:00
|
|
|
func (s *threepidStatements) DeleteThreePID(
|
|
|
|
ctx context.Context, txn *sql.Tx, threepid string, medium string) (err error) {
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.deleteThreePIDStmt)
|
|
|
|
_, err = stmt.ExecContext(ctx, threepid, medium)
|
2017-09-01 11:13:10 +02:00
|
|
|
return
|
|
|
|
}
|