Test 7 Pushers created with a the same access token are not deleted on password change

This commit is contained in:
Dan Peleg 2021-05-03 21:33:38 +03:00
parent 4c4cf8020a
commit 763354e371
11 changed files with 335 additions and 71 deletions

View file

@ -23,19 +23,19 @@ import (
"github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/dendrite/userapi/api"
"github.com/matrix-org/gomatrixserverlib"
"github.com/sirupsen/logrus"
)
const pushersSchema = `
-- Stores data about pushers.
CREATE TABLE IF NOT EXISTS pusher_pushers (
id SERIAL PRIMARY KEY,
-- The Matrix user ID localpart for this pusher
localpart TEXT NOT NULL PRIMARY KEY,
-- This is a unique identifier for this pusher.
-- The value you should use for this is the routing or destination address information for the notification, for example,
-- the APNS token for APNS or the Registration ID for GCM. If your notification client has no such concept, use any unique identifier.
-- If the kind is "email", this is the email address to send notifications to.
-- Max length, 512 bytes.
pushkey VARCHAR(512) NOT NULL,
localpart TEXT NOT NULL,
-- The Session ID used to create the Pusher
session_id BIGINT DEFAULT NULL,
-- This string determines which set of device specific rules this pusher executes.
profile_tag TEXT NOT NULL,
-- The kind of pusher. "http" is a pusher that sends HTTP pokes.
kind TEXT,
-- This is a reverse-DNS style identifier for the application. Max length, 64 chars.
@ -44,8 +44,12 @@ CREATE TABLE IF NOT EXISTS pusher_pushers (
app_display_name TEXT,
-- A string that will allow the user to identify what device owns this pusher.
device_display_name TEXT,
-- This string determines which set of device specific rules this pusher executes.
profile_tag TEXT,
-- This is a unique identifier for this pusher.
-- The value you should use for this is the routing or destination address information for the notification, for example,
-- the APNS token for APNS or the Registration ID for GCM. If your notification client has no such concept, use any unique identifier.
-- If the kind is "email", this is the email address to send notifications to.
-- Max length, 512 bytes.
pushkey VARCHAR(512) NOT NULL,
-- The preferred language for receiving notifications (e.g. 'en' or 'en-US')
lang TEXT,
-- Required if kind is http. The URL to use to send notifications to.
@ -55,26 +59,29 @@ CREATE TABLE IF NOT EXISTS pusher_pushers (
);
-- Pushkey must be unique for a given user.
CREATE UNIQUE INDEX IF NOT EXISTS pusher_localpart_pushkey_idx ON pusher_pushers(localpart, pushkey);
CREATE UNIQUE INDEX IF NOT EXISTS pusher_app_id_pushkey_localpart_idx ON pusher_pushers(app_id, pushkey, localpart);
`
const insertPusherSQL = "" +
"INSERT INTO pusher_pushers(localpart, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, url, format) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)"
"INSERT INTO pusher_pushers(localpart, session_id, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, url, format) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)"
const selectPushersByLocalpartSQL = "" +
"SELECT pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, url, format FROM pusher_pushers WHERE localpart = $1"
"SELECT session_id, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, url, format FROM pusher_pushers WHERE localpart = $1"
const selectPusherByPushkeySQL = "" +
"SELECT pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, url, format FROM pusher_pushers WHERE localpart = $1 AND pushkey = $2"
"SELECT session_id, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, url, format FROM pusher_pushers WHERE localpart = $1 AND pushkey = $2"
const updatePusherSQL = "" +
"UPDATE pusher_pushers SET kind = $1, app_id = $2, app_display_name = $3, device_display_name = $4, profile_tag = $5, lang = $6, url = $7, format = $8 WHERE localpart = $9 AND pushkey = $10"
const deletePusherSQL = "" +
"DELETE FROM pusher_pushers WHERE pushkey = $1 AND localpart = $2"
"DELETE FROM pusher_pushers WHERE app_id = $1 AND pushkey = $2 AND localpart = $3"
type pushersStatements struct {
insertPusherStmt *sql.Stmt
selectPushersByLocalpartStmt *sql.Stmt
selectPusherByPushkeyStmt *sql.Stmt
updatePusherStmt *sql.Stmt
deletePusherStmt *sql.Stmt
serverName gomatrixserverlib.ServerName
}
@ -94,6 +101,9 @@ func (s *pushersStatements) prepare(db *sql.DB, server gomatrixserverlib.ServerN
if s.selectPusherByPushkeyStmt, err = db.Prepare(selectPusherByPushkeySQL); err != nil {
return
}
if s.updatePusherStmt, err = db.Prepare(updatePusherSQL); err != nil {
return
}
if s.deletePusherStmt, err = db.Prepare(deletePusherSQL); err != nil {
return
}
@ -105,19 +115,12 @@ func (s *pushersStatements) prepare(db *sql.DB, server gomatrixserverlib.ServerN
// Returns an error if the user already has a pusher with the given pusher pushkey.
// Returns nil error success.
func (s *pushersStatements) insertPusher(
ctx context.Context, txn *sql.Tx, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string,
ctx context.Context, txn *sql.Tx, session_id int64,
pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string,
) error {
stmt := sqlutil.TxStmt(txn, s.insertPusherStmt)
_, err := stmt.ExecContext(ctx, localpart, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format)
return err
}
// deletePusher removes a single pusher by pushkey and user localpart.
func (s *pushersStatements) deletePusher(
ctx context.Context, txn *sql.Tx, pushkey, localpart string,
) error {
stmt := sqlutil.TxStmt(txn, s.deletePusherStmt)
_, err := stmt.ExecContext(ctx, pushkey, localpart)
_, err := stmt.ExecContext(ctx, localpart, session_id, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format)
logrus.Debugf("🥳 Created pusher %d", session_id)
return err
}
@ -134,11 +137,15 @@ func (s *pushersStatements) selectPushersByLocalpart(
for rows.Next() {
var pusher api.Pusher
var sessionid sql.NullInt64
var pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format sql.NullString
err = rows.Scan(&pushkey, &kind, &appid, &appdisplayname, &devicedisplayname, &profiletag, &lang, &url, &format)
err = rows.Scan(&sessionid, &pushkey, &kind, &appid, &appdisplayname, &devicedisplayname, &profiletag, &lang, &url, &format)
if err != nil {
return pushers, err
}
if sessionid.Valid {
pusher.SessionID = sessionid.Int64
}
if pushkey.Valid {
pusher.PushKey = pushkey.String
}
@ -171,6 +178,7 @@ func (s *pushersStatements) selectPushersByLocalpart(
pushers = append(pushers, pusher)
}
logrus.Debugf("🤓 Database returned %d pushers", len(pushers))
return pushers, rows.Err()
}
@ -178,12 +186,16 @@ func (s *pushersStatements) selectPusherByPushkey(
ctx context.Context, localpart, pushkey string,
) (*api.Pusher, error) {
var pusher api.Pusher
var id, key, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format sql.NullString
var sessionid sql.NullInt64
var key, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format sql.NullString
stmt := s.selectPusherByPushkeyStmt
err := stmt.QueryRowContext(ctx, localpart, pushkey).Scan(&id, &key, &kind, &appid, &appdisplayname, &devicedisplayname, &profiletag, &lang, &url, &format)
err := stmt.QueryRowContext(ctx, localpart, pushkey).Scan(&sessionid, &key, &kind, &appid, &appdisplayname, &devicedisplayname, &profiletag, &lang, &url, &format)
if err == nil {
if sessionid.Valid {
pusher.SessionID = sessionid.Int64
}
if key.Valid {
pusher.PushKey = key.String
}
@ -217,3 +229,20 @@ func (s *pushersStatements) selectPusherByPushkey(
return &pusher, err
}
func (s *pushersStatements) updatePusher(
ctx context.Context, txn *sql.Tx, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string,
) error {
stmt := sqlutil.TxStmt(txn, s.updatePusherStmt)
_, err := stmt.ExecContext(ctx, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart, pushkey)
return err
}
// deletePusher removes a single pusher by pushkey and user localpart.
func (s *pushersStatements) deletePusher(
ctx context.Context, txn *sql.Tx, appid, pushkey, localpart string,
) error {
stmt := sqlutil.TxStmt(txn, s.deletePusherStmt)
_, err := stmt.ExecContext(ctx, appid, pushkey, localpart)
return err
}

View file

@ -22,6 +22,7 @@ import (
"github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/dendrite/userapi/api"
"github.com/matrix-org/gomatrixserverlib"
"github.com/sirupsen/logrus"
)
// Database represents a pusher database.
@ -56,9 +57,10 @@ func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserver
}
func (d *Database) CreatePusher(
ctx context.Context, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string,
ctx context.Context, session_id int64,
pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string,
) error {
return d.pushers.insertPusher(ctx, nil, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart)
return d.pushers.insertPusher(ctx, nil, session_id, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart)
}
// GetPushersByLocalpart returns the pushers matching the given localpart.
@ -75,16 +77,28 @@ func (d *Database) GetPusherByPushkey(
return d.pushers.selectPusherByPushkey(ctx, localpart, pushkey)
}
// UpdatePusher updates the given pusher with the display name.
// Returns SQL error if there are problems and nil on success.
func (d *Database) UpdatePusher(
ctx context.Context, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string,
) error {
return sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
return d.pushers.updatePusher(ctx, txn, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart)
})
}
// RemovePusher revokes a pusher by deleting the entry in the database
// matching with the given pushkey and user ID localpart.
// If the pusher doesn't exist, it will not return an error
// If something went wrong during the deletion, it will return the SQL error.
func (d *Database) RemovePusher(
ctx context.Context, pushkey, localpart string,
ctx context.Context, appid, pushkey, localpart string,
) error {
return sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
if err := d.pushers.deletePusher(ctx, txn, pushkey, localpart); err != sql.ErrNoRows {
if err := d.pushers.deletePusher(ctx, txn, appid, pushkey, localpart); err != sql.ErrNoRows {
return err
} else {
logrus.WithError(err).Debug("RemovePusher Error")
}
return nil
})