mirror of
https://github.com/element-hq/synapse.git
synced 2024-12-21 03:42:55 +03:00
Send password reset from HS: database stuff (#5308)
Database component of new behaviour of sending password reset emails from Synapse instead of Sydent. Allows one to store threepid validation sessions along with password reset token attempts and retrieve them again.
This commit is contained in:
parent
f6dd12d1e2
commit
24f31dfb59
5 changed files with 293 additions and 4 deletions
1
changelog.d/5308.feature
Normal file
1
changelog.d/5308.feature
Normal file
|
@ -0,0 +1 @@
|
|||
Add ability to perform password reset via email without trusting the identity server.
|
|
@ -339,6 +339,15 @@ class UnsupportedRoomVersionError(SynapseError):
|
|||
)
|
||||
|
||||
|
||||
class ThreepidValidationError(SynapseError):
|
||||
"""An error raised when there was a problem authorising an event."""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
if "errcode" not in kwargs:
|
||||
kwargs["errcode"] = Codes.FORBIDDEN
|
||||
super(ThreepidValidationError, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class IncompatibleRoomVersionError(SynapseError):
|
||||
"""A server is trying to join a room whose version it does not support.
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ logger = logging.getLogger(__name__)
|
|||
|
||||
# Remember to update this number every time a change is made to database
|
||||
# schema files, so the users will be informed on server restarts.
|
||||
SCHEMA_VERSION = 54
|
||||
SCHEMA_VERSION = 55
|
||||
|
||||
dir_path = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
|
|
|
@ -17,12 +17,13 @@
|
|||
|
||||
import re
|
||||
|
||||
from six import iterkeys
|
||||
from six.moves import range
|
||||
|
||||
from twisted.internet import defer
|
||||
|
||||
from synapse.api.constants import UserTypes
|
||||
from synapse.api.errors import Codes, StoreError
|
||||
from synapse.api.errors import Codes, StoreError, ThreepidValidationError
|
||||
from synapse.storage import background_updates
|
||||
from synapse.storage._base import SQLBaseStore
|
||||
from synapse.types import UserID
|
||||
|
@ -422,7 +423,7 @@ class RegistrationWorkerStore(SQLBaseStore):
|
|||
defer.returnValue(None)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def get_user_id_by_threepid(self, medium, address):
|
||||
def get_user_id_by_threepid(self, medium, address, require_verified=False):
|
||||
"""Returns user id from threepid
|
||||
|
||||
Args:
|
||||
|
@ -963,7 +964,6 @@ class RegistrationStore(
|
|||
We do this by grandfathering in existing user threepids assuming that
|
||||
they used one of the server configured trusted identity servers.
|
||||
"""
|
||||
|
||||
id_servers = set(self.config.trusted_third_party_id_servers)
|
||||
|
||||
def _bg_user_threepids_grandfather_txn(txn):
|
||||
|
@ -984,3 +984,251 @@ class RegistrationStore(
|
|||
yield self._end_background_update("user_threepids_grandfather")
|
||||
|
||||
defer.returnValue(1)
|
||||
|
||||
def get_threepid_validation_session(
|
||||
self,
|
||||
medium,
|
||||
client_secret,
|
||||
address=None,
|
||||
sid=None,
|
||||
validated=None,
|
||||
):
|
||||
"""Gets a session_id and last_send_attempt (if available) for a
|
||||
client_secret/medium/(address|session_id) combo
|
||||
|
||||
Args:
|
||||
medium (str|None): The medium of the 3PID
|
||||
address (str|None): The address of the 3PID
|
||||
sid (str|None): The ID of the validation session
|
||||
client_secret (str|None): A unique string provided by the client to
|
||||
help identify this validation attempt
|
||||
validated (bool|None): Whether sessions should be filtered by
|
||||
whether they have been validated already or not. None to
|
||||
perform no filtering
|
||||
|
||||
Returns:
|
||||
deferred {str, int}|None: A dict containing the
|
||||
latest session_id and send_attempt count for this 3PID.
|
||||
Otherwise None if there hasn't been a previous attempt
|
||||
"""
|
||||
keyvalues = {
|
||||
"medium": medium,
|
||||
"client_secret": client_secret,
|
||||
"session_id": sid,
|
||||
"address": address,
|
||||
}
|
||||
cols_to_return = [
|
||||
"session_id", "medium", "address",
|
||||
"client_secret", "last_send_attempt", "validated_at",
|
||||
]
|
||||
|
||||
def get_threepid_validation_session_txn(txn):
|
||||
sql = "SELECT %s FROM threepid_validation_session WHERE %s" % (
|
||||
", ".join(cols_to_return),
|
||||
" AND ".join("%s = ?" % k for k in iterkeys(keyvalues)),
|
||||
)
|
||||
|
||||
if validated is not None:
|
||||
sql += " AND validated_at IS " + ("NOT NULL" if validated else "NULL")
|
||||
|
||||
sql += " LIMIT 1"
|
||||
|
||||
txn.execute(sql, list(keyvalues.values()))
|
||||
rows = self.cursor_to_dict(txn)
|
||||
if not rows:
|
||||
return None
|
||||
|
||||
return rows[0]
|
||||
|
||||
return self.runInteraction(
|
||||
"get_threepid_validation_session",
|
||||
get_threepid_validation_session_txn,
|
||||
)
|
||||
|
||||
def validate_threepid_session(
|
||||
self,
|
||||
session_id,
|
||||
client_secret,
|
||||
token,
|
||||
current_ts,
|
||||
):
|
||||
"""Attempt to validate a threepid session using a token
|
||||
|
||||
Args:
|
||||
session_id (str): The id of a validation session
|
||||
client_secret (str): A unique string provided by the client to
|
||||
help identify this validation attempt
|
||||
token (str): A validation token
|
||||
current_ts (int): The current unix time in milliseconds. Used for
|
||||
checking token expiry status
|
||||
|
||||
Returns:
|
||||
deferred str|None: A str representing a link to redirect the user
|
||||
to if there is one.
|
||||
"""
|
||||
# Insert everything into a transaction in order to run atomically
|
||||
def validate_threepid_session_txn(txn):
|
||||
row = self._simple_select_one_txn(
|
||||
txn,
|
||||
table="threepid_validation_session",
|
||||
keyvalues={"session_id": session_id},
|
||||
retcols=["client_secret", "validated_at"],
|
||||
allow_none=True,
|
||||
)
|
||||
|
||||
if not row:
|
||||
raise ThreepidValidationError(400, "Unknown session_id")
|
||||
retrieved_client_secret = row["client_secret"]
|
||||
validated_at = row["validated_at"]
|
||||
|
||||
if validated_at:
|
||||
raise ThreepidValidationError(
|
||||
400, "This session has already been validated",
|
||||
)
|
||||
if retrieved_client_secret != client_secret:
|
||||
raise ThreepidValidationError(
|
||||
400, "This client_secret does not match the provided session_id",
|
||||
)
|
||||
|
||||
row = self._simple_select_one_txn(
|
||||
txn,
|
||||
table="threepid_validation_token",
|
||||
keyvalues={"session_id": session_id, "token": token},
|
||||
retcols=["expires", "next_link"],
|
||||
allow_none=True,
|
||||
)
|
||||
|
||||
if not row:
|
||||
raise ThreepidValidationError(
|
||||
400, "Validation token not found or has expired",
|
||||
)
|
||||
expires = row["expires"]
|
||||
next_link = row["next_link"]
|
||||
|
||||
if expires <= current_ts:
|
||||
raise ThreepidValidationError(
|
||||
400, "This token has expired. Please request a new one",
|
||||
)
|
||||
|
||||
# Looks good. Validate the session
|
||||
self._simple_update_txn(
|
||||
txn,
|
||||
table="threepid_validation_session",
|
||||
keyvalues={"session_id": session_id},
|
||||
updatevalues={"validated_at": self.clock.time_msec()},
|
||||
)
|
||||
|
||||
return next_link
|
||||
|
||||
# Return next_link if it exists
|
||||
return self.runInteraction(
|
||||
"validate_threepid_session_txn",
|
||||
validate_threepid_session_txn,
|
||||
)
|
||||
|
||||
def upsert_threepid_validation_session(
|
||||
self,
|
||||
medium,
|
||||
address,
|
||||
client_secret,
|
||||
send_attempt,
|
||||
session_id,
|
||||
validated_at=None,
|
||||
):
|
||||
"""Upsert a threepid validation session
|
||||
|
||||
Args:
|
||||
medium (str): The medium of the 3PID
|
||||
address (str): The address of the 3PID
|
||||
client_secret (str): A unique string provided by the client to
|
||||
help identify this validation attempt
|
||||
session_id (str): The id of this validation session
|
||||
validated_at (int): The unix timestamp in milliseconds of when
|
||||
the session was marked as valid
|
||||
"""
|
||||
insertion_values = {
|
||||
"medium": medium,
|
||||
"address": address,
|
||||
"client_secret": client_secret,
|
||||
}
|
||||
|
||||
if validated_at:
|
||||
insertion_values["validated_at"] = validated_at
|
||||
|
||||
return self._simple_upsert(
|
||||
table="threepid_validation_session",
|
||||
keyvalues={"session_id": session_id},
|
||||
values={"last_send_attempt": send_attempt},
|
||||
insertion_values=insertion_values,
|
||||
desc="upsert_threepid_validation_session",
|
||||
)
|
||||
|
||||
def insert_threepid_validation_token(
|
||||
self,
|
||||
session_id,
|
||||
token,
|
||||
next_link,
|
||||
expires,
|
||||
):
|
||||
"""Insert a new 3PID validation token and details
|
||||
|
||||
Args:
|
||||
session_id (str): The id of the validation session this attempt
|
||||
is related to
|
||||
token (str): The validation token
|
||||
expires (int): The timestamp for which after this token will no
|
||||
longer be valid
|
||||
"""
|
||||
return self._simple_insert(
|
||||
table="threepid_validation_token",
|
||||
values={
|
||||
"session_id": session_id,
|
||||
"token": token,
|
||||
"next_link": next_link,
|
||||
"expires": expires,
|
||||
},
|
||||
desc="insert_threepid_validation_token",
|
||||
)
|
||||
|
||||
def cull_expired_threepid_validation_tokens(self):
|
||||
"""Remove threepid validation tokens with expiry dates that have passed"""
|
||||
def cull_expired_threepid_validation_tokens_txn(txn, ts):
|
||||
sql = """
|
||||
DELETE FROM threepid_validation_token WHERE
|
||||
expires < ?
|
||||
"""
|
||||
return txn.execute(sql, (ts,))
|
||||
|
||||
return self.runInteraction(
|
||||
"cull_expired_threepid_validation_tokens",
|
||||
cull_expired_threepid_validation_tokens_txn,
|
||||
self.clock.time_msec(),
|
||||
)
|
||||
|
||||
def delete_threepid_session(self, session_id):
|
||||
"""Removes a threepid validation session from the database. This can
|
||||
be done after validation has been performed and whatever action was
|
||||
waiting on it has been carried out
|
||||
|
||||
Args:
|
||||
session_id (str): The ID of the session to delete
|
||||
"""
|
||||
return self._simple_delete(
|
||||
table="threepid_validation_session",
|
||||
keyvalues={"session_id": session_id},
|
||||
desc="delete_threepid_session",
|
||||
)
|
||||
|
||||
def delete_threepid_tokens(self, session_id):
|
||||
"""Removes threepid validation tokens from the database which match a
|
||||
given session ID.
|
||||
|
||||
Args:
|
||||
session_id (str): The ID of the session to delete
|
||||
"""
|
||||
# Delete tokens associated with this session id
|
||||
return self._simple_delete_one(
|
||||
table="threepid_validation_token",
|
||||
keyvalues={"session_id": session_id},
|
||||
desc="delete_threepid_session_tokens",
|
||||
)
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
/* Copyright 2019 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.
|
||||
*/
|
||||
CREATE TABLE IF NOT EXISTS threepid_validation_session (
|
||||
session_id TEXT PRIMARY KEY,
|
||||
medium TEXT NOT NULL,
|
||||
address TEXT NOT NULL,
|
||||
client_secret TEXT NOT NULL,
|
||||
last_send_attempt BIGINT NOT NULL,
|
||||
validated_at BIGINT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS threepid_validation_token (
|
||||
token TEXT PRIMARY KEY,
|
||||
session_id TEXT NOT NULL,
|
||||
next_link TEXT,
|
||||
expires BIGINT NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX threepid_validation_token_session_id ON threepid_validation_token(session_id);
|
Loading…
Reference in a new issue