2022-02-22 10:10:10 -05:00
|
|
|
# Copyright 2022 The Matrix.org Foundation C.I.C.
|
2019-06-11 06:31:12 -04:00
|
|
|
#
|
|
|
|
# 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.
|
2019-06-14 08:18:24 -04:00
|
|
|
import json
|
2019-06-11 06:31:12 -04:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
from email.parser import Parser
|
2022-03-02 11:34:14 -05:00
|
|
|
from typing import Any, Dict, List, Optional, Union
|
2022-02-22 10:10:10 -05:00
|
|
|
from unittest.mock import Mock
|
2019-06-11 06:31:12 -04:00
|
|
|
|
|
|
|
import pkg_resources
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
from twisted.internet.interfaces import IReactorTCP
|
2022-02-22 10:10:10 -05:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
|
|
|
|
2019-06-11 06:31:12 -04:00
|
|
|
import synapse.rest.admin
|
2019-09-27 10:14:02 -04:00
|
|
|
from synapse.api.constants import LoginType, Membership
|
2021-01-28 12:39:21 -05:00
|
|
|
from synapse.api.errors import Codes, HttpResponseException
|
2021-07-27 01:28:20 -04:00
|
|
|
from synapse.appservice import ApplicationService
|
2022-02-22 10:10:10 -05:00
|
|
|
from synapse.rest import admin
|
2021-08-17 07:57:58 -04:00
|
|
|
from synapse.rest.client import account, login, register, room
|
2020-09-10 06:45:12 -04:00
|
|
|
from synapse.rest.synapse.client.password_reset import PasswordResetSubmitTokenResource
|
2022-02-22 10:10:10 -05:00
|
|
|
from synapse.server import HomeServer
|
2022-03-24 06:19:41 -04:00
|
|
|
from synapse.types import JsonDict, UserID
|
2022-02-22 10:10:10 -05:00
|
|
|
from synapse.util import Clock
|
2019-06-11 06:31:12 -04:00
|
|
|
|
|
|
|
from tests import unittest
|
2020-11-13 18:11:43 -05:00
|
|
|
from tests.server import FakeSite, make_request
|
2020-09-08 11:03:09 -04:00
|
|
|
from tests.unittest import override_config
|
2019-06-11 06:31:12 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PasswordResetTestCase(unittest.HomeserverTestCase):
|
|
|
|
|
|
|
|
servlets = [
|
|
|
|
account.register_servlets,
|
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
|
|
|
register.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
2019-06-11 06:31:12 -04:00
|
|
|
config = self.default_config()
|
|
|
|
|
|
|
|
# Email config.
|
|
|
|
config["email"] = {
|
|
|
|
"enable_notifs": False,
|
|
|
|
"template_dir": os.path.abspath(
|
|
|
|
pkg_resources.resource_filename("synapse", "res/templates")
|
|
|
|
),
|
|
|
|
"smtp_host": "127.0.0.1",
|
|
|
|
"smtp_port": 20,
|
|
|
|
"require_transport_security": False,
|
|
|
|
"smtp_user": None,
|
|
|
|
"smtp_pass": None,
|
|
|
|
"notif_from": "test@example.com",
|
|
|
|
}
|
|
|
|
config["public_baseurl"] = "https://example.com"
|
|
|
|
|
2021-08-06 06:13:34 -04:00
|
|
|
hs = self.setup_test_homeserver(config=config)
|
|
|
|
|
|
|
|
async def sendmail(
|
2022-03-02 11:34:14 -05:00
|
|
|
reactor: IReactorTCP,
|
|
|
|
smtphost: str,
|
|
|
|
smtpport: int,
|
|
|
|
from_addr: str,
|
|
|
|
to_addr: str,
|
|
|
|
msg_bytes: bytes,
|
|
|
|
*args: Any,
|
|
|
|
**kwargs: Any,
|
|
|
|
) -> None:
|
|
|
|
self.email_attempts.append(msg_bytes)
|
|
|
|
|
|
|
|
self.email_attempts: List[bytes] = []
|
2021-08-06 06:13:34 -04:00
|
|
|
hs.get_send_email_handler()._sendmail = sendmail
|
|
|
|
|
2019-06-11 06:31:12 -04:00
|
|
|
return hs
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2020-09-10 06:45:12 -04:00
|
|
|
self.submit_token_resource = PasswordResetSubmitTokenResource(hs)
|
2019-06-11 06:31:12 -04:00
|
|
|
|
2022-04-01 08:48:08 -04:00
|
|
|
def attempt_wrong_password_login(self, username: str, password: str) -> None:
|
|
|
|
"""Attempts to login as the user with the given password, asserting
|
|
|
|
that the attempt *fails*.
|
|
|
|
"""
|
|
|
|
body = {"type": "m.login.password", "user": username, "password": password}
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST", "/_matrix/client/r0/login", json.dumps(body).encode("utf8")
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 403, channel.result)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_basic_password_reset(self) -> None:
|
2019-06-11 06:31:12 -04:00
|
|
|
"""Test basic password reset flow"""
|
|
|
|
old_password = "monkey"
|
|
|
|
new_password = "kangeroo"
|
|
|
|
|
|
|
|
user_id = self.register_user("kermit", old_password)
|
|
|
|
self.login("kermit", old_password)
|
|
|
|
|
|
|
|
email = "test@example.com"
|
|
|
|
|
|
|
|
# Add a threepid
|
|
|
|
self.get_success(
|
|
|
|
self.store.user_add_threepid(
|
|
|
|
user_id=user_id,
|
|
|
|
medium="email",
|
|
|
|
address=email,
|
|
|
|
validated_at=0,
|
|
|
|
added_at=0,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
client_secret = "foobar"
|
|
|
|
session_id = self._request_token(email, client_secret)
|
|
|
|
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(len(self.email_attempts), 1)
|
2019-06-11 06:31:12 -04:00
|
|
|
link = self._get_link_from_email()
|
|
|
|
|
|
|
|
self._validate_token(link)
|
|
|
|
|
|
|
|
self._reset_password(new_password, session_id, client_secret)
|
|
|
|
|
|
|
|
# Assert we can log in with the new password
|
|
|
|
self.login("kermit", new_password)
|
|
|
|
|
|
|
|
# Assert we can't log in with the old password
|
|
|
|
self.attempt_wrong_password_login("kermit", old_password)
|
|
|
|
|
2021-01-28 12:39:21 -05:00
|
|
|
@override_config({"rc_3pid_validation": {"burst_count": 3}})
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_ratelimit_by_email(self) -> None:
|
2021-01-28 12:39:21 -05:00
|
|
|
"""Test that we ratelimit /requestToken for the same email."""
|
|
|
|
old_password = "monkey"
|
|
|
|
new_password = "kangeroo"
|
|
|
|
|
|
|
|
user_id = self.register_user("kermit", old_password)
|
|
|
|
self.login("kermit", old_password)
|
|
|
|
|
|
|
|
email = "test1@example.com"
|
|
|
|
|
|
|
|
# Add a threepid
|
|
|
|
self.get_success(
|
|
|
|
self.store.user_add_threepid(
|
|
|
|
user_id=user_id,
|
|
|
|
medium="email",
|
|
|
|
address=email,
|
|
|
|
validated_at=0,
|
|
|
|
added_at=0,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def reset(ip: str) -> None:
|
2021-01-28 12:39:21 -05:00
|
|
|
client_secret = "foobar"
|
|
|
|
session_id = self._request_token(email, client_secret, ip)
|
|
|
|
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(len(self.email_attempts), 1)
|
2021-01-28 12:39:21 -05:00
|
|
|
link = self._get_link_from_email()
|
|
|
|
|
|
|
|
self._validate_token(link)
|
|
|
|
|
|
|
|
self._reset_password(new_password, session_id, client_secret)
|
|
|
|
|
|
|
|
self.email_attempts.clear()
|
|
|
|
|
|
|
|
# We expect to be able to make three requests before getting rate
|
|
|
|
# limited.
|
|
|
|
#
|
|
|
|
# We change IPs to ensure that we're not being ratelimited due to the
|
|
|
|
# same IP
|
|
|
|
reset("127.0.0.1")
|
|
|
|
reset("127.0.0.2")
|
|
|
|
reset("127.0.0.3")
|
|
|
|
|
|
|
|
with self.assertRaises(HttpResponseException) as cm:
|
|
|
|
reset("127.0.0.4")
|
|
|
|
|
|
|
|
self.assertEqual(cm.exception.code, 429)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_basic_password_reset_canonicalise_email(self) -> None:
|
2020-07-03 09:03:13 -04:00
|
|
|
"""Test basic password reset flow
|
|
|
|
Request password reset with different spelling
|
|
|
|
"""
|
|
|
|
old_password = "monkey"
|
|
|
|
new_password = "kangeroo"
|
|
|
|
|
|
|
|
user_id = self.register_user("kermit", old_password)
|
|
|
|
self.login("kermit", old_password)
|
|
|
|
|
|
|
|
email_profile = "test@example.com"
|
|
|
|
email_passwort_reset = "TEST@EXAMPLE.COM"
|
|
|
|
|
|
|
|
# Add a threepid
|
|
|
|
self.get_success(
|
|
|
|
self.store.user_add_threepid(
|
|
|
|
user_id=user_id,
|
|
|
|
medium="email",
|
|
|
|
address=email_profile,
|
|
|
|
validated_at=0,
|
|
|
|
added_at=0,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
client_secret = "foobar"
|
|
|
|
session_id = self._request_token(email_passwort_reset, client_secret)
|
|
|
|
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(len(self.email_attempts), 1)
|
2020-07-03 09:03:13 -04:00
|
|
|
link = self._get_link_from_email()
|
|
|
|
|
|
|
|
self._validate_token(link)
|
|
|
|
|
|
|
|
self._reset_password(new_password, session_id, client_secret)
|
|
|
|
|
|
|
|
# Assert we can log in with the new password
|
|
|
|
self.login("kermit", new_password)
|
|
|
|
|
|
|
|
# Assert we can't log in with the old password
|
|
|
|
self.attempt_wrong_password_login("kermit", old_password)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_cant_reset_password_without_clicking_link(self) -> None:
|
2019-06-11 06:31:12 -04:00
|
|
|
"""Test that we do actually need to click the link in the email"""
|
|
|
|
old_password = "monkey"
|
|
|
|
new_password = "kangeroo"
|
|
|
|
|
|
|
|
user_id = self.register_user("kermit", old_password)
|
|
|
|
self.login("kermit", old_password)
|
|
|
|
|
|
|
|
email = "test@example.com"
|
|
|
|
|
|
|
|
# Add a threepid
|
|
|
|
self.get_success(
|
|
|
|
self.store.user_add_threepid(
|
|
|
|
user_id=user_id,
|
|
|
|
medium="email",
|
|
|
|
address=email,
|
|
|
|
validated_at=0,
|
|
|
|
added_at=0,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
client_secret = "foobar"
|
|
|
|
session_id = self._request_token(email, client_secret)
|
|
|
|
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(len(self.email_attempts), 1)
|
2019-06-11 06:31:12 -04:00
|
|
|
|
|
|
|
# Attempt to reset password without clicking the link
|
|
|
|
self._reset_password(new_password, session_id, client_secret, expected_code=401)
|
|
|
|
|
|
|
|
# Assert we can log in with the old password
|
|
|
|
self.login("kermit", old_password)
|
|
|
|
|
|
|
|
# Assert we can't log in with the new password
|
|
|
|
self.attempt_wrong_password_login("kermit", new_password)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_no_valid_token(self) -> None:
|
2019-06-11 06:31:12 -04:00
|
|
|
"""Test that we do actually need to request a token and can't just
|
|
|
|
make a session up.
|
|
|
|
"""
|
|
|
|
old_password = "monkey"
|
|
|
|
new_password = "kangeroo"
|
|
|
|
|
|
|
|
user_id = self.register_user("kermit", old_password)
|
|
|
|
self.login("kermit", old_password)
|
|
|
|
|
|
|
|
email = "test@example.com"
|
|
|
|
|
|
|
|
# Add a threepid
|
|
|
|
self.get_success(
|
|
|
|
self.store.user_add_threepid(
|
|
|
|
user_id=user_id,
|
|
|
|
medium="email",
|
|
|
|
address=email,
|
|
|
|
validated_at=0,
|
|
|
|
added_at=0,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
client_secret = "foobar"
|
|
|
|
session_id = "weasle"
|
|
|
|
|
|
|
|
# Attempt to reset password without even requesting an email
|
|
|
|
self._reset_password(new_password, session_id, client_secret, expected_code=401)
|
|
|
|
|
|
|
|
# Assert we can log in with the old password
|
|
|
|
self.login("kermit", old_password)
|
|
|
|
|
|
|
|
# Assert we can't log in with the new password
|
|
|
|
self.attempt_wrong_password_login("kermit", new_password)
|
|
|
|
|
2020-04-23 05:23:53 -04:00
|
|
|
@unittest.override_config({"request_token_inhibit_3pid_errors": True})
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_password_reset_bad_email_inhibit_error(self) -> None:
|
2020-04-23 05:23:53 -04:00
|
|
|
"""Test that triggering a password reset with an email address that isn't bound
|
|
|
|
to an account doesn't leak the lack of binding for that address if configured
|
|
|
|
that way.
|
|
|
|
"""
|
|
|
|
self.register_user("kermit", "monkey")
|
|
|
|
self.login("kermit", "monkey")
|
|
|
|
|
|
|
|
email = "test@example.com"
|
|
|
|
|
|
|
|
client_secret = "foobar"
|
|
|
|
session_id = self._request_token(email, client_secret)
|
|
|
|
|
|
|
|
self.assertIsNotNone(session_id)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def _request_token(
|
|
|
|
self,
|
|
|
|
email: str,
|
|
|
|
client_secret: str,
|
|
|
|
ip: str = "127.0.0.1",
|
|
|
|
) -> str:
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2019-06-11 06:31:12 -04:00
|
|
|
"POST",
|
|
|
|
b"account/password/email/requestToken",
|
|
|
|
{"client_secret": client_secret, "email": email, "send_attempt": 1},
|
2021-01-28 12:39:21 -05:00
|
|
|
client_ip=ip,
|
2019-06-11 06:31:12 -04:00
|
|
|
)
|
2021-01-28 12:39:21 -05:00
|
|
|
|
|
|
|
if channel.code != 200:
|
|
|
|
raise HttpResponseException(
|
|
|
|
channel.code,
|
|
|
|
channel.result["reason"],
|
|
|
|
channel.result["body"],
|
|
|
|
)
|
2019-06-11 06:31:12 -04:00
|
|
|
|
|
|
|
return channel.json_body["sid"]
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def _validate_token(self, link: str) -> None:
|
2019-06-11 06:31:12 -04:00
|
|
|
# Remove the host
|
|
|
|
path = link.replace("https://example.com", "")
|
|
|
|
|
2020-09-10 06:45:12 -04:00
|
|
|
# Load the password reset confirmation page
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = make_request(
|
2020-11-13 18:11:43 -05:00
|
|
|
self.reactor,
|
2021-09-24 06:01:25 -04:00
|
|
|
FakeSite(self.submit_token_resource, self.reactor),
|
2020-11-13 18:11:43 -05:00
|
|
|
"GET",
|
|
|
|
path,
|
|
|
|
shorthand=False,
|
|
|
|
)
|
|
|
|
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(200, channel.code, channel.result)
|
2020-09-10 06:45:12 -04:00
|
|
|
|
|
|
|
# Now POST to the same endpoint, mimicking the same behaviour as clicking the
|
|
|
|
# password reset confirm button
|
|
|
|
|
|
|
|
# Confirm the password reset
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = make_request(
|
2020-11-13 18:11:43 -05:00
|
|
|
self.reactor,
|
2021-09-24 06:01:25 -04:00
|
|
|
FakeSite(self.submit_token_resource, self.reactor),
|
2020-09-10 06:45:12 -04:00
|
|
|
"POST",
|
|
|
|
path,
|
2020-12-15 10:18:03 -05:00
|
|
|
content=b"",
|
2020-09-10 06:45:12 -04:00
|
|
|
shorthand=False,
|
|
|
|
content_is_form=True,
|
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(200, channel.code, channel.result)
|
2019-06-11 06:31:12 -04:00
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def _get_link_from_email(self) -> str:
|
2019-06-11 06:31:12 -04:00
|
|
|
assert self.email_attempts, "No emails have been sent"
|
|
|
|
|
|
|
|
raw_msg = self.email_attempts[-1].decode("UTF-8")
|
|
|
|
mail = Parser().parsestr(raw_msg)
|
|
|
|
|
|
|
|
text = None
|
|
|
|
for part in mail.walk():
|
|
|
|
if part.get_content_type() == "text/plain":
|
|
|
|
text = part.get_payload(decode=True).decode("UTF-8")
|
|
|
|
break
|
|
|
|
|
|
|
|
if not text:
|
|
|
|
self.fail("Could not find text portion of email to parse")
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
assert text is not None
|
2019-06-11 06:31:12 -04:00
|
|
|
match = re.search(r"https://example.com\S+", text)
|
|
|
|
assert match, "Could not find link in email"
|
|
|
|
|
|
|
|
return match.group(0)
|
|
|
|
|
|
|
|
def _reset_password(
|
2022-03-02 11:34:14 -05:00
|
|
|
self,
|
|
|
|
new_password: str,
|
|
|
|
session_id: str,
|
|
|
|
client_secret: str,
|
|
|
|
expected_code: int = 200,
|
|
|
|
) -> None:
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2019-06-11 06:31:12 -04:00
|
|
|
"POST",
|
|
|
|
b"account/password",
|
|
|
|
{
|
|
|
|
"new_password": new_password,
|
|
|
|
"auth": {
|
|
|
|
"type": LoginType.EMAIL_IDENTITY,
|
|
|
|
"threepid_creds": {
|
|
|
|
"client_secret": client_secret,
|
|
|
|
"sid": session_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(expected_code, channel.code, channel.result)
|
2019-06-14 08:18:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
class DeactivateTestCase(unittest.HomeserverTestCase):
|
|
|
|
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
|
|
|
login.register_servlets,
|
|
|
|
account.register_servlets,
|
2019-09-27 10:14:02 -04:00
|
|
|
room.register_servlets,
|
2019-06-14 08:18:24 -04:00
|
|
|
]
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
2019-09-27 10:14:02 -04:00
|
|
|
self.hs = self.setup_test_homeserver()
|
|
|
|
return self.hs
|
2019-06-14 08:18:24 -04:00
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_deactivate_account(self) -> None:
|
2019-06-14 08:18:24 -04:00
|
|
|
user_id = self.register_user("kermit", "test")
|
|
|
|
tok = self.login("kermit", "test")
|
|
|
|
|
2019-09-27 10:14:02 -04:00
|
|
|
self.deactivate(user_id, tok)
|
|
|
|
|
2022-02-23 06:04:02 -05:00
|
|
|
store = self.hs.get_datastores().main
|
2019-09-27 10:14:02 -04:00
|
|
|
|
|
|
|
# Check that the user has been marked as deactivated.
|
|
|
|
self.assertTrue(self.get_success(store.get_user_deactivated_status(user_id)))
|
|
|
|
|
|
|
|
# Check that this access token has been invalidated.
|
2021-07-27 01:28:20 -04:00
|
|
|
channel = self.make_request("GET", "account/whoami", access_token=tok)
|
2020-12-15 09:51:25 -05:00
|
|
|
self.assertEqual(channel.code, 401)
|
2019-09-27 10:14:02 -04:00
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_pending_invites(self) -> None:
|
2019-09-27 10:14:02 -04:00
|
|
|
"""Tests that deactivating a user rejects every pending invite for them."""
|
2022-02-23 06:04:02 -05:00
|
|
|
store = self.hs.get_datastores().main
|
2019-09-27 10:14:02 -04:00
|
|
|
|
|
|
|
inviter_id = self.register_user("inviter", "test")
|
|
|
|
inviter_tok = self.login("inviter", "test")
|
|
|
|
|
|
|
|
invitee_id = self.register_user("invitee", "test")
|
|
|
|
invitee_tok = self.login("invitee", "test")
|
|
|
|
|
|
|
|
# Make @inviter:test invite @invitee:test in a new room.
|
|
|
|
room_id = self.helper.create_room_as(inviter_id, tok=inviter_tok)
|
2019-09-27 10:23:07 -04:00
|
|
|
self.helper.invite(
|
|
|
|
room=room_id, src=inviter_id, targ=invitee_id, tok=inviter_tok
|
|
|
|
)
|
2019-09-27 10:14:02 -04:00
|
|
|
|
|
|
|
# Make sure the invite is here.
|
2020-01-15 09:59:33 -05:00
|
|
|
pending_invites = self.get_success(
|
|
|
|
store.get_invited_rooms_for_local_user(invitee_id)
|
|
|
|
)
|
2019-09-27 10:14:02 -04:00
|
|
|
self.assertEqual(len(pending_invites), 1, pending_invites)
|
|
|
|
self.assertEqual(pending_invites[0].room_id, room_id, pending_invites)
|
|
|
|
|
|
|
|
# Deactivate @invitee:test.
|
|
|
|
self.deactivate(invitee_id, invitee_tok)
|
|
|
|
|
|
|
|
# Check that the invite isn't there anymore.
|
2020-01-15 09:59:33 -05:00
|
|
|
pending_invites = self.get_success(
|
|
|
|
store.get_invited_rooms_for_local_user(invitee_id)
|
|
|
|
)
|
2019-09-27 10:14:02 -04:00
|
|
|
self.assertEqual(len(pending_invites), 0, pending_invites)
|
|
|
|
|
|
|
|
# Check that the membership of @invitee:test in the room is now "leave".
|
|
|
|
memberships = self.get_success(
|
2020-01-15 09:59:33 -05:00
|
|
|
store.get_rooms_for_local_user_where_membership_is(
|
|
|
|
invitee_id, [Membership.LEAVE]
|
|
|
|
)
|
2019-09-27 10:14:02 -04:00
|
|
|
)
|
|
|
|
self.assertEqual(len(memberships), 1, memberships)
|
|
|
|
self.assertEqual(memberships[0].room_id, room_id, memberships)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def deactivate(self, user_id: str, tok: str) -> None:
|
2019-06-14 08:18:24 -04:00
|
|
|
request_data = json.dumps(
|
|
|
|
{
|
|
|
|
"auth": {
|
|
|
|
"type": "m.login.password",
|
|
|
|
"user": user_id,
|
|
|
|
"password": "test",
|
|
|
|
},
|
|
|
|
"erase": False,
|
|
|
|
}
|
|
|
|
)
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2019-06-14 08:18:24 -04:00
|
|
|
"POST", "account/deactivate", request_data, access_token=tok
|
|
|
|
)
|
2020-12-15 09:51:25 -05:00
|
|
|
self.assertEqual(channel.code, 200)
|
2020-03-27 15:15:23 -04:00
|
|
|
|
|
|
|
|
2021-07-27 01:28:20 -04:00
|
|
|
class WhoamiTestCase(unittest.HomeserverTestCase):
|
|
|
|
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
|
|
|
login.register_servlets,
|
|
|
|
account.register_servlets,
|
|
|
|
register.register_servlets,
|
|
|
|
]
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def default_config(self) -> Dict[str, Any]:
|
2021-09-29 06:32:45 -04:00
|
|
|
config = super().default_config()
|
|
|
|
config["allow_guest_access"] = True
|
|
|
|
return config
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_GET_whoami(self) -> None:
|
2021-07-27 01:28:20 -04:00
|
|
|
device_id = "wouldgohere"
|
|
|
|
user_id = self.register_user("kermit", "test")
|
|
|
|
tok = self.login("kermit", "test", device_id=device_id)
|
|
|
|
|
2021-09-29 06:32:45 -04:00
|
|
|
whoami = self._whoami(tok)
|
|
|
|
self.assertEqual(
|
|
|
|
whoami,
|
|
|
|
{
|
|
|
|
"user_id": user_id,
|
|
|
|
"device_id": device_id,
|
2022-02-18 07:48:23 -05:00
|
|
|
"is_guest": False,
|
2021-09-29 06:32:45 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_GET_whoami_guests(self) -> None:
|
2021-09-29 06:32:45 -04:00
|
|
|
channel = self.make_request(
|
|
|
|
b"POST", b"/_matrix/client/r0/register?kind=guest", b"{}"
|
|
|
|
)
|
|
|
|
tok = channel.json_body["access_token"]
|
|
|
|
user_id = channel.json_body["user_id"]
|
|
|
|
device_id = channel.json_body["device_id"]
|
|
|
|
|
|
|
|
whoami = self._whoami(tok)
|
|
|
|
self.assertEqual(
|
|
|
|
whoami,
|
|
|
|
{
|
|
|
|
"user_id": user_id,
|
|
|
|
"device_id": device_id,
|
2022-02-18 07:48:23 -05:00
|
|
|
"is_guest": True,
|
2021-09-29 06:32:45 -04:00
|
|
|
},
|
|
|
|
)
|
2021-07-27 01:28:20 -04:00
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_GET_whoami_appservices(self) -> None:
|
2021-07-27 01:28:20 -04:00
|
|
|
user_id = "@as:test"
|
|
|
|
as_token = "i_am_an_app_service"
|
|
|
|
|
|
|
|
appservice = ApplicationService(
|
|
|
|
as_token,
|
2021-09-29 06:44:15 -04:00
|
|
|
self.hs.config.server.server_name,
|
2021-07-27 01:28:20 -04:00
|
|
|
id="1234",
|
|
|
|
namespaces={"users": [{"regex": user_id, "exclusive": True}]},
|
|
|
|
sender=user_id,
|
|
|
|
)
|
2022-02-23 06:04:02 -05:00
|
|
|
self.hs.get_datastores().main.services_cache.append(appservice)
|
2021-07-27 01:28:20 -04:00
|
|
|
|
2021-09-29 06:32:45 -04:00
|
|
|
whoami = self._whoami(as_token)
|
|
|
|
self.assertEqual(
|
|
|
|
whoami,
|
|
|
|
{
|
|
|
|
"user_id": user_id,
|
2022-02-18 07:48:23 -05:00
|
|
|
"is_guest": False,
|
2021-09-29 06:32:45 -04:00
|
|
|
},
|
|
|
|
)
|
2021-07-27 01:28:20 -04:00
|
|
|
self.assertFalse(hasattr(whoami, "device_id"))
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def _whoami(self, tok: str) -> JsonDict:
|
2021-07-27 01:28:20 -04:00
|
|
|
channel = self.make_request("GET", "account/whoami", {}, access_token=tok)
|
|
|
|
self.assertEqual(channel.code, 200)
|
|
|
|
return channel.json_body
|
|
|
|
|
|
|
|
|
2020-03-27 15:15:23 -04:00
|
|
|
class ThreepidEmailRestTestCase(unittest.HomeserverTestCase):
|
|
|
|
|
|
|
|
servlets = [
|
|
|
|
account.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
|
|
|
]
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
2020-03-27 15:15:23 -04:00
|
|
|
config = self.default_config()
|
|
|
|
|
|
|
|
# Email config.
|
|
|
|
config["email"] = {
|
|
|
|
"enable_notifs": False,
|
|
|
|
"template_dir": os.path.abspath(
|
|
|
|
pkg_resources.resource_filename("synapse", "res/templates")
|
|
|
|
),
|
|
|
|
"smtp_host": "127.0.0.1",
|
|
|
|
"smtp_port": 20,
|
|
|
|
"require_transport_security": False,
|
|
|
|
"smtp_user": None,
|
|
|
|
"smtp_pass": None,
|
|
|
|
"notif_from": "test@example.com",
|
|
|
|
}
|
|
|
|
config["public_baseurl"] = "https://example.com"
|
|
|
|
|
2021-08-06 06:13:34 -04:00
|
|
|
self.hs = self.setup_test_homeserver(config=config)
|
|
|
|
|
|
|
|
async def sendmail(
|
2022-03-02 11:34:14 -05:00
|
|
|
reactor: IReactorTCP,
|
|
|
|
smtphost: str,
|
|
|
|
smtpport: int,
|
|
|
|
from_addr: str,
|
|
|
|
to_addr: str,
|
|
|
|
msg_bytes: bytes,
|
|
|
|
*args: Any,
|
|
|
|
**kwargs: Any,
|
|
|
|
) -> None:
|
|
|
|
self.email_attempts.append(msg_bytes)
|
|
|
|
|
|
|
|
self.email_attempts: List[bytes] = []
|
2021-08-06 06:13:34 -04:00
|
|
|
self.hs.get_send_email_handler()._sendmail = sendmail
|
|
|
|
|
2020-03-27 15:15:23 -04:00
|
|
|
return self.hs
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2020-03-27 15:15:23 -04:00
|
|
|
|
|
|
|
self.user_id = self.register_user("kermit", "test")
|
|
|
|
self.user_id_tok = self.login("kermit", "test")
|
|
|
|
self.email = "test@example.com"
|
|
|
|
self.url_3pid = b"account/3pid"
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_add_valid_email(self) -> None:
|
|
|
|
self._add_email(self.email, self.email)
|
2020-03-27 15:15:23 -04:00
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_add_valid_email_second_time(self) -> None:
|
|
|
|
self._add_email(self.email, self.email)
|
|
|
|
self._request_token_invalid_email(
|
|
|
|
self.email,
|
|
|
|
expected_errcode=Codes.THREEPID_IN_USE,
|
|
|
|
expected_error="Email is already in use",
|
2020-07-03 09:03:13 -04:00
|
|
|
)
|
2020-03-27 15:15:23 -04:00
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_add_valid_email_second_time_canonicalise(self) -> None:
|
|
|
|
self._add_email(self.email, self.email)
|
|
|
|
self._request_token_invalid_email(
|
|
|
|
"TEST@EXAMPLE.COM",
|
|
|
|
expected_errcode=Codes.THREEPID_IN_USE,
|
|
|
|
expected_error="Email is already in use",
|
2020-07-03 09:03:13 -04:00
|
|
|
)
|
2020-03-27 15:15:23 -04:00
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_add_email_no_at(self) -> None:
|
|
|
|
self._request_token_invalid_email(
|
|
|
|
"address-without-at.bar",
|
|
|
|
expected_errcode=Codes.UNKNOWN,
|
|
|
|
expected_error="Unable to parse email address",
|
2020-03-27 15:15:23 -04:00
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_add_email_two_at(self) -> None:
|
|
|
|
self._request_token_invalid_email(
|
|
|
|
"foo@foo@test.bar",
|
|
|
|
expected_errcode=Codes.UNKNOWN,
|
|
|
|
expected_error="Unable to parse email address",
|
2020-07-03 09:03:13 -04:00
|
|
|
)
|
2020-03-27 15:15:23 -04:00
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_add_email_bad_format(self) -> None:
|
|
|
|
self._request_token_invalid_email(
|
|
|
|
"user@bad.example.net@good.example.com",
|
|
|
|
expected_errcode=Codes.UNKNOWN,
|
|
|
|
expected_error="Unable to parse email address",
|
2020-03-27 15:15:23 -04:00
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_add_email_domain_to_lower(self) -> None:
|
|
|
|
self._add_email("foo@TEST.BAR", "foo@test.bar")
|
2020-07-03 09:03:13 -04:00
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_add_email_domain_with_umlaut(self) -> None:
|
|
|
|
self._add_email("foo@Öumlaut.com", "foo@öumlaut.com")
|
2020-07-03 09:03:13 -04:00
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_add_email_address_casefold(self) -> None:
|
|
|
|
self._add_email("Strauß@Example.com", "strauss@example.com")
|
2020-07-03 09:03:13 -04:00
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_address_trim(self) -> None:
|
|
|
|
self._add_email(" foo@test.bar ", "foo@test.bar")
|
2020-03-27 15:15:23 -04:00
|
|
|
|
2021-01-28 12:39:21 -05:00
|
|
|
@override_config({"rc_3pid_validation": {"burst_count": 3}})
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_ratelimit_by_ip(self) -> None:
|
2021-01-28 12:39:21 -05:00
|
|
|
"""Tests that adding emails is ratelimited by IP"""
|
|
|
|
|
|
|
|
# We expect to be able to set three emails before getting ratelimited.
|
2022-03-02 11:34:14 -05:00
|
|
|
self._add_email("foo1@test.bar", "foo1@test.bar")
|
|
|
|
self._add_email("foo2@test.bar", "foo2@test.bar")
|
|
|
|
self._add_email("foo3@test.bar", "foo3@test.bar")
|
2021-01-28 12:39:21 -05:00
|
|
|
|
|
|
|
with self.assertRaises(HttpResponseException) as cm:
|
2022-03-02 11:34:14 -05:00
|
|
|
self._add_email("foo4@test.bar", "foo4@test.bar")
|
2021-01-28 12:39:21 -05:00
|
|
|
|
|
|
|
self.assertEqual(cm.exception.code, 429)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_add_email_if_disabled(self) -> None:
|
2020-03-27 15:15:23 -04:00
|
|
|
"""Test adding email to profile when doing so is disallowed"""
|
2021-10-04 07:18:54 -04:00
|
|
|
self.hs.config.registration.enable_3pid_changes = False
|
2020-03-27 15:15:23 -04:00
|
|
|
|
|
|
|
client_secret = "foobar"
|
|
|
|
session_id = self._request_token(self.email, client_secret)
|
|
|
|
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(len(self.email_attempts), 1)
|
2020-03-27 15:15:23 -04:00
|
|
|
link = self._get_link_from_email()
|
|
|
|
|
|
|
|
self._validate_token(link)
|
|
|
|
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-03-27 15:15:23 -04:00
|
|
|
"POST",
|
|
|
|
b"/_matrix/client/unstable/account/3pid/add",
|
|
|
|
{
|
|
|
|
"client_secret": client_secret,
|
|
|
|
"sid": session_id,
|
|
|
|
"auth": {
|
|
|
|
"type": "m.login.password",
|
|
|
|
"user": self.user_id,
|
|
|
|
"password": "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
access_token=self.user_id_tok,
|
|
|
|
)
|
2022-03-02 11:34:14 -05:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.result["body"])
|
2020-03-27 15:15:23 -04:00
|
|
|
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
|
|
|
|
|
|
|
|
# Get user
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-03-27 15:15:23 -04:00
|
|
|
"GET",
|
|
|
|
self.url_3pid,
|
|
|
|
access_token=self.user_id_tok,
|
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.result["body"])
|
2020-03-27 15:15:23 -04:00
|
|
|
self.assertFalse(channel.json_body["threepids"])
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_delete_email(self) -> None:
|
2020-03-27 15:15:23 -04:00
|
|
|
"""Test deleting an email from profile"""
|
|
|
|
# Add a threepid
|
|
|
|
self.get_success(
|
|
|
|
self.store.user_add_threepid(
|
|
|
|
user_id=self.user_id,
|
|
|
|
medium="email",
|
|
|
|
address=self.email,
|
|
|
|
validated_at=0,
|
|
|
|
added_at=0,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-03-27 15:15:23 -04:00
|
|
|
"POST",
|
|
|
|
b"account/3pid/delete",
|
|
|
|
{"medium": "email", "address": self.email},
|
|
|
|
access_token=self.user_id_tok,
|
|
|
|
)
|
2022-03-02 11:34:14 -05:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.result["body"])
|
2020-03-27 15:15:23 -04:00
|
|
|
|
|
|
|
# Get user
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-03-27 15:15:23 -04:00
|
|
|
"GET",
|
|
|
|
self.url_3pid,
|
|
|
|
access_token=self.user_id_tok,
|
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.result["body"])
|
2020-03-27 15:15:23 -04:00
|
|
|
self.assertFalse(channel.json_body["threepids"])
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_delete_email_if_disabled(self) -> None:
|
2020-03-27 15:15:23 -04:00
|
|
|
"""Test deleting an email from profile when disallowed"""
|
2021-10-04 07:18:54 -04:00
|
|
|
self.hs.config.registration.enable_3pid_changes = False
|
2020-03-27 15:15:23 -04:00
|
|
|
|
|
|
|
# Add a threepid
|
|
|
|
self.get_success(
|
|
|
|
self.store.user_add_threepid(
|
|
|
|
user_id=self.user_id,
|
|
|
|
medium="email",
|
|
|
|
address=self.email,
|
|
|
|
validated_at=0,
|
|
|
|
added_at=0,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-03-27 15:15:23 -04:00
|
|
|
"POST",
|
|
|
|
b"account/3pid/delete",
|
|
|
|
{"medium": "email", "address": self.email},
|
|
|
|
access_token=self.user_id_tok,
|
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.result["body"])
|
2020-03-27 15:15:23 -04:00
|
|
|
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
|
|
|
|
|
|
|
|
# Get user
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-03-27 15:15:23 -04:00
|
|
|
"GET",
|
|
|
|
self.url_3pid,
|
|
|
|
access_token=self.user_id_tok,
|
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.result["body"])
|
2020-03-27 15:15:23 -04:00
|
|
|
self.assertEqual("email", channel.json_body["threepids"][0]["medium"])
|
|
|
|
self.assertEqual(self.email, channel.json_body["threepids"][0]["address"])
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_cant_add_email_without_clicking_link(self) -> None:
|
2020-03-27 15:15:23 -04:00
|
|
|
"""Test that we do actually need to click the link in the email"""
|
|
|
|
client_secret = "foobar"
|
|
|
|
session_id = self._request_token(self.email, client_secret)
|
|
|
|
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(len(self.email_attempts), 1)
|
2020-03-27 15:15:23 -04:00
|
|
|
|
|
|
|
# Attempt to add email without clicking the link
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-03-27 15:15:23 -04:00
|
|
|
"POST",
|
|
|
|
b"/_matrix/client/unstable/account/3pid/add",
|
|
|
|
{
|
|
|
|
"client_secret": client_secret,
|
|
|
|
"sid": session_id,
|
|
|
|
"auth": {
|
|
|
|
"type": "m.login.password",
|
|
|
|
"user": self.user_id,
|
|
|
|
"password": "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
access_token=self.user_id_tok,
|
|
|
|
)
|
2022-03-02 11:34:14 -05:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.result["body"])
|
2020-03-27 15:15:23 -04:00
|
|
|
self.assertEqual(Codes.THREEPID_AUTH_FAILED, channel.json_body["errcode"])
|
|
|
|
|
|
|
|
# Get user
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-03-27 15:15:23 -04:00
|
|
|
"GET",
|
|
|
|
self.url_3pid,
|
|
|
|
access_token=self.user_id_tok,
|
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.result["body"])
|
2020-03-27 15:15:23 -04:00
|
|
|
self.assertFalse(channel.json_body["threepids"])
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_no_valid_token(self) -> None:
|
2020-03-27 15:15:23 -04:00
|
|
|
"""Test that we do actually need to request a token and can't just
|
|
|
|
make a session up.
|
|
|
|
"""
|
|
|
|
client_secret = "foobar"
|
|
|
|
session_id = "weasle"
|
|
|
|
|
|
|
|
# Attempt to add email without even requesting an email
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-03-27 15:15:23 -04:00
|
|
|
"POST",
|
|
|
|
b"/_matrix/client/unstable/account/3pid/add",
|
|
|
|
{
|
|
|
|
"client_secret": client_secret,
|
|
|
|
"sid": session_id,
|
|
|
|
"auth": {
|
|
|
|
"type": "m.login.password",
|
|
|
|
"user": self.user_id,
|
|
|
|
"password": "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
access_token=self.user_id_tok,
|
|
|
|
)
|
2022-03-02 11:34:14 -05:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.result["body"])
|
2020-03-27 15:15:23 -04:00
|
|
|
self.assertEqual(Codes.THREEPID_AUTH_FAILED, channel.json_body["errcode"])
|
|
|
|
|
|
|
|
# Get user
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-03-27 15:15:23 -04:00
|
|
|
"GET",
|
|
|
|
self.url_3pid,
|
|
|
|
access_token=self.user_id_tok,
|
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.result["body"])
|
2020-03-27 15:15:23 -04:00
|
|
|
self.assertFalse(channel.json_body["threepids"])
|
|
|
|
|
2020-09-08 11:03:09 -04:00
|
|
|
@override_config({"next_link_domain_whitelist": None})
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_next_link(self) -> None:
|
2020-09-08 11:03:09 -04:00
|
|
|
"""Tests a valid next_link parameter value with no whitelist (good case)"""
|
|
|
|
self._request_token(
|
|
|
|
"something@example.com",
|
|
|
|
"some_secret",
|
|
|
|
next_link="https://example.com/a/good/site",
|
|
|
|
expect_code=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_config({"next_link_domain_whitelist": None})
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_next_link_exotic_protocol(self) -> None:
|
2020-09-08 11:03:09 -04:00
|
|
|
"""Tests using a esoteric protocol as a next_link parameter value.
|
|
|
|
Someone may be hosting a client on IPFS etc.
|
|
|
|
"""
|
|
|
|
self._request_token(
|
|
|
|
"something@example.com",
|
|
|
|
"some_secret",
|
|
|
|
next_link="some-protocol://abcdefghijklmopqrstuvwxyz",
|
|
|
|
expect_code=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_config({"next_link_domain_whitelist": None})
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_next_link_file_uri(self) -> None:
|
2020-09-08 11:03:09 -04:00
|
|
|
"""Tests next_link parameters cannot be file URI"""
|
|
|
|
# Attempt to use a next_link value that points to the local disk
|
|
|
|
self._request_token(
|
|
|
|
"something@example.com",
|
|
|
|
"some_secret",
|
|
|
|
next_link="file:///host/path",
|
|
|
|
expect_code=400,
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_config({"next_link_domain_whitelist": ["example.com", "example.org"]})
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_next_link_domain_whitelist(self) -> None:
|
2020-09-08 11:03:09 -04:00
|
|
|
"""Tests next_link parameters must fit the whitelist if provided"""
|
2020-09-29 07:36:44 -04:00
|
|
|
|
|
|
|
# Ensure not providing a next_link parameter still works
|
|
|
|
self._request_token(
|
|
|
|
"something@example.com",
|
|
|
|
"some_secret",
|
|
|
|
next_link=None,
|
|
|
|
expect_code=200,
|
|
|
|
)
|
|
|
|
|
2020-09-08 11:03:09 -04:00
|
|
|
self._request_token(
|
|
|
|
"something@example.com",
|
|
|
|
"some_secret",
|
|
|
|
next_link="https://example.com/some/good/page",
|
|
|
|
expect_code=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
self._request_token(
|
|
|
|
"something@example.com",
|
|
|
|
"some_secret",
|
|
|
|
next_link="https://example.org/some/also/good/page",
|
|
|
|
expect_code=200,
|
|
|
|
)
|
|
|
|
|
|
|
|
self._request_token(
|
|
|
|
"something@example.com",
|
|
|
|
"some_secret",
|
|
|
|
next_link="https://bad.example.org/some/bad/page",
|
|
|
|
expect_code=400,
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_config({"next_link_domain_whitelist": []})
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_empty_next_link_domain_whitelist(self) -> None:
|
2020-09-08 11:03:09 -04:00
|
|
|
"""Tests an empty next_lint_domain_whitelist value, meaning next_link is essentially
|
|
|
|
disallowed
|
|
|
|
"""
|
|
|
|
self._request_token(
|
|
|
|
"something@example.com",
|
|
|
|
"some_secret",
|
|
|
|
next_link="https://example.com/a/page",
|
|
|
|
expect_code=400,
|
|
|
|
)
|
|
|
|
|
|
|
|
def _request_token(
|
|
|
|
self,
|
|
|
|
email: str,
|
|
|
|
client_secret: str,
|
|
|
|
next_link: Optional[str] = None,
|
|
|
|
expect_code: int = 200,
|
|
|
|
) -> str:
|
|
|
|
"""Request a validation token to add an email address to a user's account
|
|
|
|
|
|
|
|
Args:
|
|
|
|
email: The email address to validate
|
|
|
|
client_secret: A secret string
|
|
|
|
next_link: A link to redirect the user to after validation
|
|
|
|
expect_code: Expected return code of the call
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The ID of the new threepid validation session
|
|
|
|
"""
|
|
|
|
body = {"client_secret": client_secret, "email": email, "send_attempt": 1}
|
|
|
|
if next_link:
|
|
|
|
body["next_link"] = next_link
|
|
|
|
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
b"account/3pid/email/requestToken",
|
|
|
|
body,
|
|
|
|
)
|
2021-01-28 12:39:21 -05:00
|
|
|
|
|
|
|
if channel.code != expect_code:
|
|
|
|
raise HttpResponseException(
|
|
|
|
channel.code,
|
|
|
|
channel.result["reason"],
|
|
|
|
channel.result["body"],
|
|
|
|
)
|
2020-03-27 15:15:23 -04:00
|
|
|
|
2020-09-08 11:03:09 -04:00
|
|
|
return channel.json_body.get("sid")
|
2020-03-27 15:15:23 -04:00
|
|
|
|
2020-07-03 09:03:13 -04:00
|
|
|
def _request_token_invalid_email(
|
|
|
|
self,
|
2022-03-02 11:34:14 -05:00
|
|
|
email: str,
|
|
|
|
expected_errcode: str,
|
|
|
|
expected_error: str,
|
|
|
|
client_secret: str = "foobar",
|
|
|
|
) -> None:
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-07-03 09:03:13 -04:00
|
|
|
"POST",
|
|
|
|
b"account/3pid/email/requestToken",
|
|
|
|
{"client_secret": client_secret, "email": email, "send_attempt": 1},
|
|
|
|
)
|
2022-03-02 11:34:14 -05:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.result["body"])
|
2020-07-03 09:03:13 -04:00
|
|
|
self.assertEqual(expected_errcode, channel.json_body["errcode"])
|
|
|
|
self.assertEqual(expected_error, channel.json_body["error"])
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def _validate_token(self, link: str) -> None:
|
2020-03-27 15:15:23 -04:00
|
|
|
# Remove the host
|
|
|
|
path = link.replace("https://example.com", "")
|
|
|
|
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request("GET", path, shorthand=False)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(200, channel.code, channel.result)
|
2020-03-27 15:15:23 -04:00
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def _get_link_from_email(self) -> str:
|
2020-03-27 15:15:23 -04:00
|
|
|
assert self.email_attempts, "No emails have been sent"
|
|
|
|
|
|
|
|
raw_msg = self.email_attempts[-1].decode("UTF-8")
|
|
|
|
mail = Parser().parsestr(raw_msg)
|
|
|
|
|
|
|
|
text = None
|
|
|
|
for part in mail.walk():
|
|
|
|
if part.get_content_type() == "text/plain":
|
|
|
|
text = part.get_payload(decode=True).decode("UTF-8")
|
|
|
|
break
|
|
|
|
|
|
|
|
if not text:
|
|
|
|
self.fail("Could not find text portion of email to parse")
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
assert text is not None
|
2020-03-27 15:15:23 -04:00
|
|
|
match = re.search(r"https://example.com\S+", text)
|
|
|
|
assert match, "Could not find link in email"
|
|
|
|
|
|
|
|
return match.group(0)
|
2020-07-03 09:03:13 -04:00
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def _add_email(self, request_email: str, expected_email: str) -> None:
|
2020-07-03 09:03:13 -04:00
|
|
|
"""Test adding an email to profile"""
|
2021-01-28 12:39:21 -05:00
|
|
|
previous_email_attempts = len(self.email_attempts)
|
|
|
|
|
2020-07-03 09:03:13 -04:00
|
|
|
client_secret = "foobar"
|
|
|
|
session_id = self._request_token(request_email, client_secret)
|
|
|
|
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(len(self.email_attempts) - previous_email_attempts, 1)
|
2020-07-03 09:03:13 -04:00
|
|
|
link = self._get_link_from_email()
|
|
|
|
|
|
|
|
self._validate_token(link)
|
|
|
|
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-07-03 09:03:13 -04:00
|
|
|
"POST",
|
|
|
|
b"/_matrix/client/unstable/account/3pid/add",
|
|
|
|
{
|
|
|
|
"client_secret": client_secret,
|
|
|
|
"sid": session_id,
|
|
|
|
"auth": {
|
|
|
|
"type": "m.login.password",
|
|
|
|
"user": self.user_id,
|
|
|
|
"password": "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
access_token=self.user_id_tok,
|
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.result["body"])
|
2020-07-03 09:03:13 -04:00
|
|
|
|
|
|
|
# Get user
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-07-03 09:03:13 -04:00
|
|
|
"GET",
|
|
|
|
self.url_3pid,
|
|
|
|
access_token=self.user_id_tok,
|
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.result["body"])
|
2020-07-03 09:03:13 -04:00
|
|
|
self.assertEqual("email", channel.json_body["threepids"][0]["medium"])
|
2021-01-28 12:39:21 -05:00
|
|
|
|
|
|
|
threepids = {threepid["address"] for threepid in channel.json_body["threepids"]}
|
|
|
|
self.assertIn(expected_email, threepids)
|
2022-02-22 10:10:10 -05:00
|
|
|
|
|
|
|
|
|
|
|
class AccountStatusTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
account.register_servlets,
|
|
|
|
admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
|
|
|
|
url = "/_matrix/client/unstable/org.matrix.msc3720/account_status"
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
2022-02-22 10:10:10 -05:00
|
|
|
config = self.default_config()
|
|
|
|
config["experimental_features"] = {"msc3720_enabled": True}
|
|
|
|
|
|
|
|
return self.setup_test_homeserver(config=config)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2022-02-22 10:10:10 -05:00
|
|
|
self.requester = self.register_user("requester", "password")
|
|
|
|
self.requester_tok = self.login("requester", "password")
|
2022-03-02 11:34:14 -05:00
|
|
|
self.server_name = hs.config.server.server_name
|
2022-02-22 10:10:10 -05:00
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_missing_mxid(self) -> None:
|
2022-02-22 10:10:10 -05:00
|
|
|
"""Tests that not providing any MXID raises an error."""
|
|
|
|
self._test_status(
|
|
|
|
users=None,
|
|
|
|
expected_status_code=400,
|
|
|
|
expected_errcode=Codes.MISSING_PARAM,
|
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_invalid_mxid(self) -> None:
|
2022-02-22 10:10:10 -05:00
|
|
|
"""Tests that providing an invalid MXID raises an error."""
|
|
|
|
self._test_status(
|
|
|
|
users=["bad:test"],
|
|
|
|
expected_status_code=400,
|
|
|
|
expected_errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_local_user_not_exists(self) -> None:
|
2022-02-22 10:10:10 -05:00
|
|
|
"""Tests that the account status endpoints correctly reports that a user doesn't
|
|
|
|
exist.
|
|
|
|
"""
|
|
|
|
user = "@unknown:" + self.hs.config.server.server_name
|
|
|
|
|
|
|
|
self._test_status(
|
|
|
|
users=[user],
|
|
|
|
expected_statuses={
|
|
|
|
user: {
|
|
|
|
"exists": False,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected_failures=[],
|
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_local_user_exists(self) -> None:
|
2022-02-22 10:10:10 -05:00
|
|
|
"""Tests that the account status endpoint correctly reports that a user doesn't
|
|
|
|
exist.
|
|
|
|
"""
|
|
|
|
user = self.register_user("someuser", "password")
|
|
|
|
|
|
|
|
self._test_status(
|
|
|
|
users=[user],
|
|
|
|
expected_statuses={
|
|
|
|
user: {
|
|
|
|
"exists": True,
|
|
|
|
"deactivated": False,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected_failures=[],
|
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_local_user_deactivated(self) -> None:
|
2022-02-22 10:10:10 -05:00
|
|
|
"""Tests that the account status endpoint correctly reports a deactivated user."""
|
|
|
|
user = self.register_user("someuser", "password")
|
|
|
|
self.get_success(
|
2022-02-23 07:35:53 -05:00
|
|
|
self.hs.get_datastores().main.set_user_deactivated_status(
|
|
|
|
user, deactivated=True
|
|
|
|
)
|
2022-02-22 10:10:10 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
self._test_status(
|
|
|
|
users=[user],
|
|
|
|
expected_statuses={
|
|
|
|
user: {
|
|
|
|
"exists": True,
|
|
|
|
"deactivated": True,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected_failures=[],
|
|
|
|
)
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
def test_mixed_local_and_remote_users(self) -> None:
|
2022-02-22 10:10:10 -05:00
|
|
|
"""Tests that if some users are remote the account status endpoint correctly
|
|
|
|
merges the remote responses with the local result.
|
|
|
|
"""
|
|
|
|
# We use 3 users: one doesn't exist but belongs on the local homeserver, one is
|
|
|
|
# deactivated and belongs on one remote homeserver, and one belongs to another
|
|
|
|
# remote homeserver that didn't return any result (the federation code should
|
|
|
|
# mark that user as a failure).
|
|
|
|
users = [
|
|
|
|
"@unknown:" + self.hs.config.server.server_name,
|
|
|
|
"@deactivated:remote",
|
|
|
|
"@failed:otherremote",
|
|
|
|
"@bad:badremote",
|
|
|
|
]
|
|
|
|
|
2022-03-02 11:34:14 -05:00
|
|
|
async def post_json(
|
|
|
|
destination: str,
|
|
|
|
path: str,
|
|
|
|
data: Optional[JsonDict] = None,
|
|
|
|
*a: Any,
|
|
|
|
**kwa: Any,
|
|
|
|
) -> Union[JsonDict, list]:
|
2022-02-22 10:10:10 -05:00
|
|
|
if destination == "remote":
|
|
|
|
return {
|
|
|
|
"account_statuses": {
|
|
|
|
users[1]: {
|
|
|
|
"exists": True,
|
|
|
|
"deactivated": True,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-03-02 11:34:14 -05:00
|
|
|
elif destination == "badremote":
|
2022-02-22 10:10:10 -05:00
|
|
|
# badremote tries to overwrite the status of a user that doesn't belong
|
|
|
|
# to it (i.e. users[1]) with false data, which Synapse is expected to
|
|
|
|
# ignore.
|
|
|
|
return {
|
|
|
|
"account_statuses": {
|
|
|
|
users[3]: {
|
|
|
|
"exists": False,
|
|
|
|
},
|
|
|
|
users[1]: {
|
|
|
|
"exists": False,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-03-02 11:34:14 -05:00
|
|
|
# if destination == "otherremote"
|
|
|
|
else:
|
|
|
|
return {}
|
2022-02-22 10:10:10 -05:00
|
|
|
|
|
|
|
# Register a mock that will return the expected result depending on the remote.
|
|
|
|
self.hs.get_federation_http_client().post_json = Mock(side_effect=post_json)
|
|
|
|
|
|
|
|
# Check that we've got the correct response from the client-side endpoint.
|
|
|
|
self._test_status(
|
|
|
|
users=users,
|
|
|
|
expected_statuses={
|
|
|
|
users[0]: {
|
|
|
|
"exists": False,
|
|
|
|
},
|
|
|
|
users[1]: {
|
|
|
|
"exists": True,
|
|
|
|
"deactivated": True,
|
|
|
|
},
|
|
|
|
users[3]: {
|
|
|
|
"exists": False,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected_failures=[users[2]],
|
|
|
|
)
|
|
|
|
|
2022-03-24 06:19:41 -04:00
|
|
|
@unittest.override_config(
|
|
|
|
{
|
|
|
|
"use_account_validity_in_account_status": True,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
def test_no_account_validity(self) -> None:
|
|
|
|
"""Tests that if we decide to include account validity in the response but no
|
|
|
|
account validity 'is_user_expired' callback is provided, we default to marking all
|
|
|
|
users as not expired.
|
|
|
|
"""
|
|
|
|
user = self.register_user("someuser", "password")
|
|
|
|
|
|
|
|
self._test_status(
|
|
|
|
users=[user],
|
|
|
|
expected_statuses={
|
|
|
|
user: {
|
|
|
|
"exists": True,
|
|
|
|
"deactivated": False,
|
|
|
|
"org.matrix.expired": False,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected_failures=[],
|
|
|
|
)
|
|
|
|
|
|
|
|
@unittest.override_config(
|
|
|
|
{
|
|
|
|
"use_account_validity_in_account_status": True,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
def test_account_validity_expired(self) -> None:
|
|
|
|
"""Test that if we decide to include account validity in the response and the user
|
|
|
|
is expired, we return the correct info.
|
|
|
|
"""
|
|
|
|
user = self.register_user("someuser", "password")
|
|
|
|
|
|
|
|
async def is_expired(user_id: str) -> bool:
|
|
|
|
# We can't blindly say everyone is expired, otherwise the request to get the
|
|
|
|
# account status will fail.
|
|
|
|
return UserID.from_string(user_id).localpart == "someuser"
|
|
|
|
|
|
|
|
self.hs.get_account_validity_handler()._is_user_expired_callbacks.append(
|
|
|
|
is_expired
|
|
|
|
)
|
|
|
|
|
|
|
|
self._test_status(
|
|
|
|
users=[user],
|
|
|
|
expected_statuses={
|
|
|
|
user: {
|
|
|
|
"exists": True,
|
|
|
|
"deactivated": False,
|
|
|
|
"org.matrix.expired": True,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected_failures=[],
|
|
|
|
)
|
|
|
|
|
2022-02-22 10:10:10 -05:00
|
|
|
def _test_status(
|
|
|
|
self,
|
|
|
|
users: Optional[List[str]],
|
|
|
|
expected_status_code: int = 200,
|
|
|
|
expected_statuses: Optional[Dict[str, Dict[str, bool]]] = None,
|
|
|
|
expected_failures: Optional[List[str]] = None,
|
|
|
|
expected_errcode: Optional[str] = None,
|
2022-03-02 11:34:14 -05:00
|
|
|
) -> None:
|
2022-02-22 10:10:10 -05:00
|
|
|
"""Send a request to the account status endpoint and check that the response
|
|
|
|
matches with what's expected.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
users: The account(s) to request the status of, if any. If set to None, no
|
|
|
|
`user_id` query parameter will be included in the request.
|
|
|
|
expected_status_code: The expected HTTP status code.
|
|
|
|
expected_statuses: The expected account statuses, if any.
|
|
|
|
expected_failures: The expected failures, if any.
|
|
|
|
expected_errcode: The expected Matrix error code, if any.
|
|
|
|
"""
|
|
|
|
content = {}
|
|
|
|
if users is not None:
|
|
|
|
content["user_ids"] = users
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
method="POST",
|
|
|
|
path=self.url,
|
|
|
|
content=content,
|
|
|
|
access_token=self.requester_tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(channel.code, expected_status_code)
|
|
|
|
|
|
|
|
if expected_statuses is not None:
|
|
|
|
self.assertEqual(channel.json_body["account_statuses"], expected_statuses)
|
|
|
|
|
|
|
|
if expected_failures is not None:
|
|
|
|
self.assertEqual(channel.json_body["failures"], expected_failures)
|
|
|
|
|
|
|
|
if expected_errcode is not None:
|
|
|
|
self.assertEqual(channel.json_body["errcode"], expected_errcode)
|