2019-03-15 13:46:16 -04:00
|
|
|
import json
|
2020-06-01 12:55:07 -04:00
|
|
|
import time
|
2020-03-02 11:36:32 -05:00
|
|
|
import urllib.parse
|
|
|
|
|
|
|
|
from mock import Mock
|
2019-03-15 13:46:16 -04:00
|
|
|
|
2020-06-01 12:55:07 -04:00
|
|
|
import jwt
|
|
|
|
|
2019-05-01 10:32:38 -04:00
|
|
|
import synapse.rest.admin
|
2020-09-18 09:55:13 -04:00
|
|
|
from synapse.appservice import ApplicationService
|
2020-05-14 11:32:49 -04:00
|
|
|
from synapse.rest.client.v1 import login, logout
|
2020-09-18 09:55:13 -04:00
|
|
|
from synapse.rest.client.v2_alpha import devices, register
|
2019-07-12 12:26:02 -04:00
|
|
|
from synapse.rest.client.v2_alpha.account import WhoamiRestServlet
|
2019-03-15 13:46:16 -04:00
|
|
|
|
|
|
|
from tests import unittest
|
2019-07-12 12:26:02 -04:00
|
|
|
from tests.unittest import override_config
|
2019-03-15 13:46:16 -04:00
|
|
|
|
|
|
|
LOGIN_URL = b"/_matrix/client/r0/login"
|
2019-07-12 12:26:02 -04:00
|
|
|
TEST_URL = b"/_matrix/client/r0/account/whoami"
|
2019-03-15 13:46:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
class LoginRestServletTestCase(unittest.HomeserverTestCase):
|
|
|
|
|
|
|
|
servlets = [
|
2019-05-02 06:59:16 -04:00
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
2019-03-15 13:46:16 -04:00
|
|
|
login.register_servlets,
|
2020-05-14 11:32:49 -04:00
|
|
|
logout.register_servlets,
|
2019-07-12 12:26:02 -04:00
|
|
|
devices.register_servlets,
|
|
|
|
lambda hs, http_server: WhoamiRestServlet(hs).register(http_server),
|
2019-03-15 13:46:16 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
def make_homeserver(self, reactor, clock):
|
|
|
|
self.hs = self.setup_test_homeserver()
|
|
|
|
self.hs.config.enable_registration = True
|
|
|
|
self.hs.config.registrations_require_3pid = []
|
|
|
|
self.hs.config.auto_join_rooms = []
|
|
|
|
self.hs.config.enable_registration_captcha = False
|
|
|
|
|
|
|
|
return self.hs
|
|
|
|
|
2020-06-05 05:47:20 -04:00
|
|
|
@override_config(
|
|
|
|
{
|
|
|
|
"rc_login": {
|
|
|
|
"address": {"per_second": 0.17, "burst_count": 5},
|
|
|
|
# Prevent the account login ratelimiter from raising first
|
|
|
|
#
|
|
|
|
# This is normally covered by the default test homeserver config
|
|
|
|
# which sets these values to 10000, but as we're overriding the entire
|
|
|
|
# rc_login dict here, we need to set this manually as well
|
|
|
|
"account": {"per_second": 10000, "burst_count": 10000},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2019-03-15 13:46:16 -04:00
|
|
|
def test_POST_ratelimiting_per_address(self):
|
|
|
|
# Create different users so we're sure not to be bothered by the per-user
|
|
|
|
# ratelimiter.
|
|
|
|
for i in range(0, 6):
|
|
|
|
self.register_user("kermit" + str(i), "monkey")
|
|
|
|
|
|
|
|
for i in range(0, 6):
|
|
|
|
params = {
|
|
|
|
"type": "m.login.password",
|
2019-05-10 01:12:11 -04:00
|
|
|
"identifier": {"type": "m.id.user", "user": "kermit" + str(i)},
|
2019-03-15 13:46:16 -04:00
|
|
|
"password": "monkey",
|
|
|
|
}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"POST", LOGIN_URL, params)
|
2019-03-15 13:46:16 -04:00
|
|
|
|
|
|
|
if i == 5:
|
|
|
|
self.assertEquals(channel.result["code"], b"429", channel.result)
|
|
|
|
retry_after_ms = int(channel.json_body["retry_after_ms"])
|
|
|
|
else:
|
|
|
|
self.assertEquals(channel.result["code"], b"200", channel.result)
|
|
|
|
|
|
|
|
# Since we're ratelimiting at 1 request/min, retry_after_ms should be lower
|
|
|
|
# than 1min.
|
|
|
|
self.assertTrue(retry_after_ms < 6000)
|
|
|
|
|
2020-08-19 08:07:57 -04:00
|
|
|
self.reactor.advance(retry_after_ms / 1000.0 + 1.0)
|
2019-03-15 13:46:16 -04:00
|
|
|
|
|
|
|
params = {
|
|
|
|
"type": "m.login.password",
|
2019-05-10 01:12:11 -04:00
|
|
|
"identifier": {"type": "m.id.user", "user": "kermit" + str(i)},
|
2019-03-15 13:46:16 -04:00
|
|
|
"password": "monkey",
|
|
|
|
}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"POST", LOGIN_URL, params)
|
2019-03-15 13:46:16 -04:00
|
|
|
|
|
|
|
self.assertEquals(channel.result["code"], b"200", channel.result)
|
|
|
|
|
2020-06-05 05:47:20 -04:00
|
|
|
@override_config(
|
|
|
|
{
|
|
|
|
"rc_login": {
|
|
|
|
"account": {"per_second": 0.17, "burst_count": 5},
|
|
|
|
# Prevent the address login ratelimiter from raising first
|
|
|
|
#
|
|
|
|
# This is normally covered by the default test homeserver config
|
|
|
|
# which sets these values to 10000, but as we're overriding the entire
|
|
|
|
# rc_login dict here, we need to set this manually as well
|
|
|
|
"address": {"per_second": 10000, "burst_count": 10000},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2019-03-15 13:46:16 -04:00
|
|
|
def test_POST_ratelimiting_per_account(self):
|
|
|
|
self.register_user("kermit", "monkey")
|
|
|
|
|
|
|
|
for i in range(0, 6):
|
|
|
|
params = {
|
|
|
|
"type": "m.login.password",
|
2019-05-10 01:12:11 -04:00
|
|
|
"identifier": {"type": "m.id.user", "user": "kermit"},
|
2019-03-15 13:46:16 -04:00
|
|
|
"password": "monkey",
|
|
|
|
}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"POST", LOGIN_URL, params)
|
2019-03-15 13:46:16 -04:00
|
|
|
|
|
|
|
if i == 5:
|
|
|
|
self.assertEquals(channel.result["code"], b"429", channel.result)
|
|
|
|
retry_after_ms = int(channel.json_body["retry_after_ms"])
|
|
|
|
else:
|
|
|
|
self.assertEquals(channel.result["code"], b"200", channel.result)
|
|
|
|
|
|
|
|
# Since we're ratelimiting at 1 request/min, retry_after_ms should be lower
|
|
|
|
# than 1min.
|
|
|
|
self.assertTrue(retry_after_ms < 6000)
|
|
|
|
|
2019-05-10 01:12:11 -04:00
|
|
|
self.reactor.advance(retry_after_ms / 1000.0)
|
2019-03-15 13:46:16 -04:00
|
|
|
|
|
|
|
params = {
|
|
|
|
"type": "m.login.password",
|
2019-05-10 01:12:11 -04:00
|
|
|
"identifier": {"type": "m.id.user", "user": "kermit"},
|
2019-03-15 13:46:16 -04:00
|
|
|
"password": "monkey",
|
|
|
|
}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"POST", LOGIN_URL, params)
|
2019-03-15 13:46:16 -04:00
|
|
|
|
|
|
|
self.assertEquals(channel.result["code"], b"200", channel.result)
|
2019-03-18 08:57:20 -04:00
|
|
|
|
2020-06-05 05:47:20 -04:00
|
|
|
@override_config(
|
|
|
|
{
|
|
|
|
"rc_login": {
|
|
|
|
# Prevent the address login ratelimiter from raising first
|
|
|
|
#
|
|
|
|
# This is normally covered by the default test homeserver config
|
|
|
|
# which sets these values to 10000, but as we're overriding the entire
|
|
|
|
# rc_login dict here, we need to set this manually as well
|
|
|
|
"address": {"per_second": 10000, "burst_count": 10000},
|
|
|
|
"failed_attempts": {"per_second": 0.17, "burst_count": 5},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2019-03-18 08:57:20 -04:00
|
|
|
def test_POST_ratelimiting_per_account_failed_attempts(self):
|
|
|
|
self.register_user("kermit", "monkey")
|
|
|
|
|
|
|
|
for i in range(0, 6):
|
|
|
|
params = {
|
|
|
|
"type": "m.login.password",
|
2019-05-10 01:12:11 -04:00
|
|
|
"identifier": {"type": "m.id.user", "user": "kermit"},
|
2019-03-18 08:57:20 -04:00
|
|
|
"password": "notamonkey",
|
|
|
|
}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"POST", LOGIN_URL, params)
|
2019-03-18 08:57:20 -04:00
|
|
|
|
|
|
|
if i == 5:
|
|
|
|
self.assertEquals(channel.result["code"], b"429", channel.result)
|
|
|
|
retry_after_ms = int(channel.json_body["retry_after_ms"])
|
|
|
|
else:
|
|
|
|
self.assertEquals(channel.result["code"], b"403", channel.result)
|
|
|
|
|
|
|
|
# Since we're ratelimiting at 1 request/min, retry_after_ms should be lower
|
|
|
|
# than 1min.
|
|
|
|
self.assertTrue(retry_after_ms < 6000)
|
|
|
|
|
2020-08-19 08:07:57 -04:00
|
|
|
self.reactor.advance(retry_after_ms / 1000.0 + 1.0)
|
2019-03-18 08:57:20 -04:00
|
|
|
|
|
|
|
params = {
|
|
|
|
"type": "m.login.password",
|
2019-05-10 01:12:11 -04:00
|
|
|
"identifier": {"type": "m.id.user", "user": "kermit"},
|
2019-03-18 08:57:20 -04:00
|
|
|
"password": "notamonkey",
|
|
|
|
}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"POST", LOGIN_URL, params)
|
2019-03-18 08:57:20 -04:00
|
|
|
|
|
|
|
self.assertEquals(channel.result["code"], b"403", channel.result)
|
2019-07-12 12:26:02 -04:00
|
|
|
|
|
|
|
@override_config({"session_lifetime": "24h"})
|
|
|
|
def test_soft_logout(self):
|
|
|
|
self.register_user("kermit", "monkey")
|
|
|
|
|
|
|
|
# we shouldn't be able to make requests without an access token
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"GET", TEST_URL)
|
2019-07-12 12:26:02 -04:00
|
|
|
self.assertEquals(channel.result["code"], b"401", channel.result)
|
|
|
|
self.assertEquals(channel.json_body["errcode"], "M_MISSING_TOKEN")
|
|
|
|
|
|
|
|
# log in as normal
|
|
|
|
params = {
|
|
|
|
"type": "m.login.password",
|
|
|
|
"identifier": {"type": "m.id.user", "user": "kermit"},
|
|
|
|
"password": "monkey",
|
|
|
|
}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"POST", LOGIN_URL, params)
|
2019-07-12 12:26:02 -04:00
|
|
|
|
|
|
|
self.assertEquals(channel.code, 200, channel.result)
|
|
|
|
access_token = channel.json_body["access_token"]
|
|
|
|
device_id = channel.json_body["device_id"]
|
|
|
|
|
|
|
|
# we should now be able to make requests with the access token
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
|
2019-07-12 12:26:02 -04:00
|
|
|
self.assertEquals(channel.code, 200, channel.result)
|
|
|
|
|
|
|
|
# time passes
|
|
|
|
self.reactor.advance(24 * 3600)
|
|
|
|
|
|
|
|
# ... and we should be soft-logouted
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
|
2019-07-12 12:26:02 -04:00
|
|
|
self.assertEquals(channel.code, 401, channel.result)
|
|
|
|
self.assertEquals(channel.json_body["errcode"], "M_UNKNOWN_TOKEN")
|
|
|
|
self.assertEquals(channel.json_body["soft_logout"], True)
|
|
|
|
|
|
|
|
#
|
|
|
|
# test behaviour after deleting the expired device
|
|
|
|
#
|
|
|
|
|
|
|
|
# we now log in as a different device
|
|
|
|
access_token_2 = self.login("kermit", "monkey")
|
|
|
|
|
|
|
|
# more requests with the expired token should still return a soft-logout
|
|
|
|
self.reactor.advance(3600)
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
|
2019-07-12 12:26:02 -04:00
|
|
|
self.assertEquals(channel.code, 401, channel.result)
|
|
|
|
self.assertEquals(channel.json_body["errcode"], "M_UNKNOWN_TOKEN")
|
|
|
|
self.assertEquals(channel.json_body["soft_logout"], True)
|
|
|
|
|
|
|
|
# ... but if we delete that device, it will be a proper logout
|
|
|
|
self._delete_device(access_token_2, "kermit", "monkey", device_id)
|
|
|
|
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
|
2019-07-12 12:26:02 -04:00
|
|
|
self.assertEquals(channel.code, 401, channel.result)
|
|
|
|
self.assertEquals(channel.json_body["errcode"], "M_UNKNOWN_TOKEN")
|
|
|
|
self.assertEquals(channel.json_body["soft_logout"], False)
|
|
|
|
|
|
|
|
def _delete_device(self, access_token, user_id, password, device_id):
|
|
|
|
"""Perform the UI-Auth to delete a device"""
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2019-07-12 12:26:02 -04:00
|
|
|
b"DELETE", "devices/" + device_id, access_token=access_token
|
|
|
|
)
|
|
|
|
self.assertEquals(channel.code, 401, channel.result)
|
|
|
|
# check it's a UI-Auth fail
|
|
|
|
self.assertEqual(
|
|
|
|
set(channel.json_body.keys()),
|
|
|
|
{"flows", "params", "session"},
|
|
|
|
channel.result,
|
|
|
|
)
|
|
|
|
|
|
|
|
auth = {
|
|
|
|
"type": "m.login.password",
|
|
|
|
# https://github.com/matrix-org/synapse/issues/5665
|
|
|
|
# "identifier": {"type": "m.id.user", "user": user_id},
|
|
|
|
"user": user_id,
|
|
|
|
"password": password,
|
|
|
|
"session": channel.json_body["session"],
|
|
|
|
}
|
|
|
|
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2019-07-12 12:26:02 -04:00
|
|
|
b"DELETE",
|
|
|
|
"devices/" + device_id,
|
|
|
|
access_token=access_token,
|
|
|
|
content={"auth": auth},
|
|
|
|
)
|
|
|
|
self.assertEquals(channel.code, 200, channel.result)
|
2020-03-02 11:36:32 -05:00
|
|
|
|
2020-05-14 11:32:49 -04:00
|
|
|
@override_config({"session_lifetime": "24h"})
|
|
|
|
def test_session_can_hard_logout_after_being_soft_logged_out(self):
|
|
|
|
self.register_user("kermit", "monkey")
|
|
|
|
|
|
|
|
# log in as normal
|
|
|
|
access_token = self.login("kermit", "monkey")
|
|
|
|
|
|
|
|
# we should now be able to make requests with the access token
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
|
2020-05-14 11:32:49 -04:00
|
|
|
self.assertEquals(channel.code, 200, channel.result)
|
|
|
|
|
|
|
|
# time passes
|
|
|
|
self.reactor.advance(24 * 3600)
|
|
|
|
|
|
|
|
# ... and we should be soft-logouted
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
|
2020-05-14 11:32:49 -04:00
|
|
|
self.assertEquals(channel.code, 401, channel.result)
|
|
|
|
self.assertEquals(channel.json_body["errcode"], "M_UNKNOWN_TOKEN")
|
|
|
|
self.assertEquals(channel.json_body["soft_logout"], True)
|
|
|
|
|
|
|
|
# Now try to hard logout this session
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"POST", "/logout", access_token=access_token)
|
2020-05-14 11:32:49 -04:00
|
|
|
self.assertEquals(channel.result["code"], b"200", channel.result)
|
|
|
|
|
|
|
|
@override_config({"session_lifetime": "24h"})
|
|
|
|
def test_session_can_hard_logout_all_sessions_after_being_soft_logged_out(self):
|
|
|
|
self.register_user("kermit", "monkey")
|
|
|
|
|
|
|
|
# log in as normal
|
|
|
|
access_token = self.login("kermit", "monkey")
|
|
|
|
|
|
|
|
# we should now be able to make requests with the access token
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
|
2020-05-14 11:32:49 -04:00
|
|
|
self.assertEquals(channel.code, 200, channel.result)
|
|
|
|
|
|
|
|
# time passes
|
|
|
|
self.reactor.advance(24 * 3600)
|
|
|
|
|
|
|
|
# ... and we should be soft-logouted
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"GET", TEST_URL, access_token=access_token)
|
2020-05-14 11:32:49 -04:00
|
|
|
self.assertEquals(channel.code, 401, channel.result)
|
|
|
|
self.assertEquals(channel.json_body["errcode"], "M_UNKNOWN_TOKEN")
|
|
|
|
self.assertEquals(channel.json_body["soft_logout"], True)
|
|
|
|
|
|
|
|
# Now try to hard log out all of the user's sessions
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"POST", "/logout/all", access_token=access_token)
|
2020-05-14 11:32:49 -04:00
|
|
|
self.assertEquals(channel.result["code"], b"200", channel.result)
|
|
|
|
|
2020-03-02 11:36:32 -05:00
|
|
|
|
2020-04-09 13:28:13 -04:00
|
|
|
class CASTestCase(unittest.HomeserverTestCase):
|
2020-03-02 11:36:32 -05:00
|
|
|
|
|
|
|
servlets = [
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
|
|
|
|
def make_homeserver(self, reactor, clock):
|
|
|
|
self.base_url = "https://matrix.goodserver.com/"
|
|
|
|
self.redirect_path = "_synapse/client/login/sso/redirect/confirm"
|
|
|
|
|
|
|
|
config = self.default_config()
|
|
|
|
config["cas_config"] = {
|
|
|
|
"enabled": True,
|
|
|
|
"server_url": "https://fake.test",
|
|
|
|
"service_url": "https://matrix.goodserver.com:8448",
|
|
|
|
}
|
|
|
|
|
2020-04-09 13:28:13 -04:00
|
|
|
cas_user_id = "username"
|
|
|
|
self.user_id = "@%s:test" % cas_user_id
|
|
|
|
|
2020-03-02 11:36:32 -05:00
|
|
|
async def get_raw(uri, args):
|
|
|
|
"""Return an example response payload from a call to the `/proxyValidate`
|
|
|
|
endpoint of a CAS server, copied from
|
|
|
|
https://apereo.github.io/cas/5.0.x/protocol/CAS-Protocol-V2-Specification.html#26-proxyvalidate-cas-20
|
|
|
|
|
|
|
|
This needs to be returned by an async function (as opposed to set as the
|
|
|
|
mock's return value) because the corresponding Synapse code awaits on it.
|
|
|
|
"""
|
2020-04-09 13:28:13 -04:00
|
|
|
return (
|
|
|
|
"""
|
2020-03-02 11:36:32 -05:00
|
|
|
<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
|
|
|
|
<cas:authenticationSuccess>
|
2020-04-09 13:28:13 -04:00
|
|
|
<cas:user>%s</cas:user>
|
2020-03-02 11:36:32 -05:00
|
|
|
<cas:proxyGrantingTicket>PGTIOU-84678-8a9d...</cas:proxyGrantingTicket>
|
|
|
|
<cas:proxies>
|
|
|
|
<cas:proxy>https://proxy2/pgtUrl</cas:proxy>
|
|
|
|
<cas:proxy>https://proxy1/pgtUrl</cas:proxy>
|
|
|
|
</cas:proxies>
|
|
|
|
</cas:authenticationSuccess>
|
|
|
|
</cas:serviceResponse>
|
|
|
|
"""
|
2020-04-09 13:28:13 -04:00
|
|
|
% cas_user_id
|
2020-07-10 14:30:08 -04:00
|
|
|
).encode("utf-8")
|
2020-03-02 11:36:32 -05:00
|
|
|
|
|
|
|
mocked_http_client = Mock(spec=["get_raw"])
|
|
|
|
mocked_http_client.get_raw.side_effect = get_raw
|
|
|
|
|
|
|
|
self.hs = self.setup_test_homeserver(
|
|
|
|
config=config, proxied_http_client=mocked_http_client,
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.hs
|
|
|
|
|
2020-04-09 13:28:13 -04:00
|
|
|
def prepare(self, reactor, clock, hs):
|
|
|
|
self.deactivate_account_handler = hs.get_deactivate_account_handler()
|
|
|
|
|
2020-03-02 11:36:32 -05:00
|
|
|
def test_cas_redirect_confirm(self):
|
|
|
|
"""Tests that the SSO login flow serves a confirmation page before redirecting a
|
|
|
|
user to the redirect URL.
|
|
|
|
"""
|
2020-03-02 12:05:09 -05:00
|
|
|
base_url = "/_matrix/client/r0/login/cas/ticket?redirectUrl"
|
2020-03-02 11:36:32 -05:00
|
|
|
redirect_url = "https://dodgy-site.com/"
|
|
|
|
|
|
|
|
url_parts = list(urllib.parse.urlparse(base_url))
|
|
|
|
query = dict(urllib.parse.parse_qsl(url_parts[4]))
|
|
|
|
query.update({"redirectUrl": redirect_url})
|
|
|
|
query.update({"ticket": "ticket"})
|
|
|
|
url_parts[4] = urllib.parse.urlencode(query)
|
|
|
|
cas_ticket_url = urllib.parse.urlunparse(url_parts)
|
|
|
|
|
|
|
|
# Get Synapse to call the fake CAS and serve the template.
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request("GET", cas_ticket_url)
|
2020-03-02 11:36:32 -05:00
|
|
|
|
|
|
|
# Test that the response is HTML.
|
2020-03-02 12:05:09 -05:00
|
|
|
self.assertEqual(channel.code, 200)
|
2020-03-02 11:36:32 -05:00
|
|
|
content_type_header_value = ""
|
|
|
|
for header in channel.result.get("headers", []):
|
|
|
|
if header[0] == b"Content-Type":
|
|
|
|
content_type_header_value = header[1].decode("utf8")
|
|
|
|
|
|
|
|
self.assertTrue(content_type_header_value.startswith("text/html"))
|
|
|
|
|
|
|
|
# Test that the body isn't empty.
|
|
|
|
self.assertTrue(len(channel.result["body"]) > 0)
|
|
|
|
|
|
|
|
# And that it contains our redirect link
|
|
|
|
self.assertIn(redirect_url, channel.result["body"].decode("UTF-8"))
|
2020-03-02 12:05:09 -05:00
|
|
|
|
|
|
|
@override_config(
|
|
|
|
{
|
|
|
|
"sso": {
|
|
|
|
"client_whitelist": [
|
|
|
|
"https://legit-site.com/",
|
|
|
|
"https://other-site.com/",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
def test_cas_redirect_whitelisted(self):
|
|
|
|
"""Tests that the SSO login flow serves a redirect to a whitelisted url
|
|
|
|
"""
|
2020-03-27 16:24:52 -04:00
|
|
|
self._test_redirect("https://legit-site.com/")
|
|
|
|
|
|
|
|
@override_config({"public_baseurl": "https://example.com"})
|
|
|
|
def test_cas_redirect_login_fallback(self):
|
|
|
|
self._test_redirect("https://example.com/_matrix/static/client/login")
|
|
|
|
|
|
|
|
def _test_redirect(self, redirect_url):
|
|
|
|
"""Tests that the SSO login flow serves a redirect for the given redirect URL."""
|
2020-03-02 12:05:09 -05:00
|
|
|
cas_ticket_url = (
|
|
|
|
"/_matrix/client/r0/login/cas/ticket?redirectUrl=%s&ticket=ticket"
|
|
|
|
% (urllib.parse.quote(redirect_url))
|
|
|
|
)
|
|
|
|
|
|
|
|
# Get Synapse to call the fake CAS and serve the template.
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request("GET", cas_ticket_url)
|
2020-03-02 12:05:09 -05:00
|
|
|
|
|
|
|
self.assertEqual(channel.code, 302)
|
|
|
|
location_headers = channel.headers.getRawHeaders("Location")
|
|
|
|
self.assertEqual(location_headers[0][: len(redirect_url)], redirect_url)
|
2020-04-09 13:28:13 -04:00
|
|
|
|
|
|
|
@override_config({"sso": {"client_whitelist": ["https://legit-site.com/"]}})
|
|
|
|
def test_deactivated_user(self):
|
|
|
|
"""Logging in as a deactivated account should error."""
|
|
|
|
redirect_url = "https://legit-site.com/"
|
|
|
|
|
|
|
|
# First login (to create the user).
|
|
|
|
self._test_redirect(redirect_url)
|
|
|
|
|
|
|
|
# Deactivate the account.
|
|
|
|
self.get_success(
|
|
|
|
self.deactivate_account_handler.deactivate_account(self.user_id, False)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Request the CAS ticket.
|
|
|
|
cas_ticket_url = (
|
|
|
|
"/_matrix/client/r0/login/cas/ticket?redirectUrl=%s&ticket=ticket"
|
|
|
|
% (urllib.parse.quote(redirect_url))
|
|
|
|
)
|
|
|
|
|
|
|
|
# Get Synapse to call the fake CAS and serve the template.
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request("GET", cas_ticket_url)
|
2020-04-09 13:28:13 -04:00
|
|
|
|
|
|
|
# Because the user is deactivated they are served an error template.
|
|
|
|
self.assertEqual(channel.code, 403)
|
|
|
|
self.assertIn(b"SSO account deactivated", channel.result["body"])
|
2020-06-01 12:55:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
class JWTTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
|
|
|
|
jwt_secret = "secret"
|
2020-07-14 07:16:43 -04:00
|
|
|
jwt_algorithm = "HS256"
|
2020-06-01 12:55:07 -04:00
|
|
|
|
|
|
|
def make_homeserver(self, reactor, clock):
|
|
|
|
self.hs = self.setup_test_homeserver()
|
|
|
|
self.hs.config.jwt_enabled = True
|
|
|
|
self.hs.config.jwt_secret = self.jwt_secret
|
2020-07-14 07:16:43 -04:00
|
|
|
self.hs.config.jwt_algorithm = self.jwt_algorithm
|
2020-06-01 12:55:07 -04:00
|
|
|
return self.hs
|
|
|
|
|
|
|
|
def jwt_encode(self, token, secret=jwt_secret):
|
2020-07-14 07:16:43 -04:00
|
|
|
return jwt.encode(token, secret, self.jwt_algorithm).decode("ascii")
|
2020-06-01 12:55:07 -04:00
|
|
|
|
|
|
|
def jwt_login(self, *args):
|
2020-06-24 05:23:55 -04:00
|
|
|
params = json.dumps(
|
|
|
|
{"type": "org.matrix.login.jwt", "token": self.jwt_encode(*args)}
|
|
|
|
)
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"POST", LOGIN_URL, params)
|
2020-06-01 12:55:07 -04:00
|
|
|
return channel
|
|
|
|
|
|
|
|
def test_login_jwt_valid_registered(self):
|
|
|
|
self.register_user("kermit", "monkey")
|
|
|
|
channel = self.jwt_login({"sub": "kermit"})
|
|
|
|
self.assertEqual(channel.result["code"], b"200", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["user_id"], "@kermit:test")
|
|
|
|
|
|
|
|
def test_login_jwt_valid_unregistered(self):
|
|
|
|
channel = self.jwt_login({"sub": "frog"})
|
|
|
|
self.assertEqual(channel.result["code"], b"200", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["user_id"], "@frog:test")
|
|
|
|
|
|
|
|
def test_login_jwt_invalid_signature(self):
|
|
|
|
channel = self.jwt_login({"sub": "frog"}, "notsecret")
|
2020-07-15 07:10:21 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"403", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
|
2020-07-14 07:16:43 -04:00
|
|
|
self.assertEqual(
|
|
|
|
channel.json_body["error"],
|
|
|
|
"JWT validation failed: Signature verification failed",
|
|
|
|
)
|
2020-06-01 12:55:07 -04:00
|
|
|
|
|
|
|
def test_login_jwt_expired(self):
|
|
|
|
channel = self.jwt_login({"sub": "frog", "exp": 864000})
|
2020-07-15 07:10:21 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"403", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
|
2020-07-14 07:16:43 -04:00
|
|
|
self.assertEqual(
|
|
|
|
channel.json_body["error"], "JWT validation failed: Signature has expired"
|
|
|
|
)
|
2020-06-01 12:55:07 -04:00
|
|
|
|
|
|
|
def test_login_jwt_not_before(self):
|
|
|
|
now = int(time.time())
|
|
|
|
channel = self.jwt_login({"sub": "frog", "nbf": now + 3600})
|
2020-07-15 07:10:21 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"403", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
|
2020-07-14 07:16:43 -04:00
|
|
|
self.assertEqual(
|
|
|
|
channel.json_body["error"],
|
|
|
|
"JWT validation failed: The token is not yet valid (nbf)",
|
|
|
|
)
|
2020-06-01 12:55:07 -04:00
|
|
|
|
|
|
|
def test_login_no_sub(self):
|
|
|
|
channel = self.jwt_login({"username": "root"})
|
2020-07-15 07:10:21 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"403", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
|
2020-06-01 12:55:07 -04:00
|
|
|
self.assertEqual(channel.json_body["error"], "Invalid JWT")
|
|
|
|
|
2020-07-14 07:16:43 -04:00
|
|
|
@override_config(
|
|
|
|
{
|
|
|
|
"jwt_config": {
|
|
|
|
"jwt_enabled": True,
|
|
|
|
"secret": jwt_secret,
|
|
|
|
"algorithm": jwt_algorithm,
|
|
|
|
"issuer": "test-issuer",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
def test_login_iss(self):
|
|
|
|
"""Test validating the issuer claim."""
|
|
|
|
# A valid issuer.
|
|
|
|
channel = self.jwt_login({"sub": "kermit", "iss": "test-issuer"})
|
|
|
|
self.assertEqual(channel.result["code"], b"200", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["user_id"], "@kermit:test")
|
|
|
|
|
|
|
|
# An invalid issuer.
|
|
|
|
channel = self.jwt_login({"sub": "kermit", "iss": "invalid"})
|
2020-07-15 07:10:21 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"403", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
|
2020-07-14 07:16:43 -04:00
|
|
|
self.assertEqual(
|
|
|
|
channel.json_body["error"], "JWT validation failed: Invalid issuer"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Not providing an issuer.
|
|
|
|
channel = self.jwt_login({"sub": "kermit"})
|
2020-07-15 07:10:21 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"403", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
|
2020-07-14 07:16:43 -04:00
|
|
|
self.assertEqual(
|
|
|
|
channel.json_body["error"],
|
|
|
|
'JWT validation failed: Token is missing the "iss" claim',
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_login_iss_no_config(self):
|
|
|
|
"""Test providing an issuer claim without requiring it in the configuration."""
|
|
|
|
channel = self.jwt_login({"sub": "kermit", "iss": "invalid"})
|
|
|
|
self.assertEqual(channel.result["code"], b"200", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["user_id"], "@kermit:test")
|
|
|
|
|
|
|
|
@override_config(
|
|
|
|
{
|
|
|
|
"jwt_config": {
|
|
|
|
"jwt_enabled": True,
|
|
|
|
"secret": jwt_secret,
|
|
|
|
"algorithm": jwt_algorithm,
|
|
|
|
"audiences": ["test-audience"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
def test_login_aud(self):
|
|
|
|
"""Test validating the audience claim."""
|
|
|
|
# A valid audience.
|
|
|
|
channel = self.jwt_login({"sub": "kermit", "aud": "test-audience"})
|
|
|
|
self.assertEqual(channel.result["code"], b"200", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["user_id"], "@kermit:test")
|
|
|
|
|
|
|
|
# An invalid audience.
|
|
|
|
channel = self.jwt_login({"sub": "kermit", "aud": "invalid"})
|
2020-07-15 07:10:21 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"403", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
|
2020-07-14 07:16:43 -04:00
|
|
|
self.assertEqual(
|
|
|
|
channel.json_body["error"], "JWT validation failed: Invalid audience"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Not providing an audience.
|
|
|
|
channel = self.jwt_login({"sub": "kermit"})
|
2020-07-15 07:10:21 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"403", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
|
2020-07-14 07:16:43 -04:00
|
|
|
self.assertEqual(
|
|
|
|
channel.json_body["error"],
|
|
|
|
'JWT validation failed: Token is missing the "aud" claim',
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_login_aud_no_config(self):
|
|
|
|
"""Test providing an audience without requiring it in the configuration."""
|
|
|
|
channel = self.jwt_login({"sub": "kermit", "aud": "invalid"})
|
2020-07-15 07:10:21 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"403", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
|
2020-07-14 07:16:43 -04:00
|
|
|
self.assertEqual(
|
|
|
|
channel.json_body["error"], "JWT validation failed: Invalid audience"
|
|
|
|
)
|
|
|
|
|
2020-06-01 12:55:07 -04:00
|
|
|
def test_login_no_token(self):
|
2020-06-24 05:23:55 -04:00
|
|
|
params = json.dumps({"type": "org.matrix.login.jwt"})
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"POST", LOGIN_URL, params)
|
2020-07-15 07:10:21 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"403", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
|
2020-06-01 12:55:07 -04:00
|
|
|
self.assertEqual(channel.json_body["error"], "Token field for JWT is missing")
|
|
|
|
|
|
|
|
|
|
|
|
# The JWTPubKeyTestCase is a complement to JWTTestCase where we instead use
|
|
|
|
# RSS256, with a public key configured in synapse as "jwt_secret", and tokens
|
|
|
|
# signed by the private key.
|
|
|
|
class JWTPubKeyTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
|
|
|
|
# This key's pubkey is used as the jwt_secret setting of synapse. Valid
|
|
|
|
# tokens are signed by this and validated using the pubkey. It is generated
|
|
|
|
# with `openssl genrsa 512` (not a secure way to generate real keys, but
|
|
|
|
# good enough for tests!)
|
|
|
|
jwt_privatekey = "\n".join(
|
|
|
|
[
|
|
|
|
"-----BEGIN RSA PRIVATE KEY-----",
|
|
|
|
"MIIBPAIBAAJBAM50f1Q5gsdmzifLstzLHb5NhfajiOt7TKO1vSEWdq7u9x8SMFiB",
|
|
|
|
"492RM9W/XFoh8WUfL9uL6Now6tPRDsWv3xsCAwEAAQJAUv7OOSOtiU+wzJq82rnk",
|
|
|
|
"yR4NHqt7XX8BvkZPM7/+EjBRanmZNSp5kYZzKVaZ/gTOM9+9MwlmhidrUOweKfB/",
|
|
|
|
"kQIhAPZwHazbjo7dYlJs7wPQz1vd+aHSEH+3uQKIysebkmm3AiEA1nc6mDdmgiUq",
|
|
|
|
"TpIN8A4MBKmfZMWTLq6z05y/qjKyxb0CIQDYJxCwTEenIaEa4PdoJl+qmXFasVDN",
|
|
|
|
"ZU0+XtNV7yul0wIhAMI9IhiStIjS2EppBa6RSlk+t1oxh2gUWlIh+YVQfZGRAiEA",
|
|
|
|
"tqBR7qLZGJ5CVKxWmNhJZGt1QHoUtOch8t9C4IdOZ2g=",
|
|
|
|
"-----END RSA PRIVATE KEY-----",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Generated with `openssl rsa -in foo.key -pubout`, with the the above
|
|
|
|
# private key placed in foo.key (jwt_privatekey).
|
|
|
|
jwt_pubkey = "\n".join(
|
|
|
|
[
|
|
|
|
"-----BEGIN PUBLIC KEY-----",
|
|
|
|
"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAM50f1Q5gsdmzifLstzLHb5NhfajiOt7",
|
|
|
|
"TKO1vSEWdq7u9x8SMFiB492RM9W/XFoh8WUfL9uL6Now6tPRDsWv3xsCAwEAAQ==",
|
|
|
|
"-----END PUBLIC KEY-----",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
# This key is used to sign tokens that shouldn't be accepted by synapse.
|
|
|
|
# Generated just like jwt_privatekey.
|
|
|
|
bad_privatekey = "\n".join(
|
|
|
|
[
|
|
|
|
"-----BEGIN RSA PRIVATE KEY-----",
|
|
|
|
"MIIBOgIBAAJBAL//SQrKpKbjCCnv/FlasJCv+t3k/MPsZfniJe4DVFhsktF2lwQv",
|
|
|
|
"gLjmQD3jBUTz+/FndLSBvr3F4OHtGL9O/osCAwEAAQJAJqH0jZJW7Smzo9ShP02L",
|
|
|
|
"R6HRZcLExZuUrWI+5ZSP7TaZ1uwJzGFspDrunqaVoPobndw/8VsP8HFyKtceC7vY",
|
|
|
|
"uQIhAPdYInDDSJ8rFKGiy3Ajv5KWISBicjevWHF9dbotmNO9AiEAxrdRJVU+EI9I",
|
|
|
|
"eB4qRZpY6n4pnwyP0p8f/A3NBaQPG+cCIFlj08aW/PbxNdqYoBdeBA0xDrXKfmbb",
|
|
|
|
"iwYxBkwL0JCtAiBYmsi94sJn09u2Y4zpuCbJeDPKzWkbuwQh+W1fhIWQJQIhAKR0",
|
|
|
|
"KydN6cRLvphNQ9c/vBTdlzWxzcSxREpguC7F1J1m",
|
|
|
|
"-----END RSA PRIVATE KEY-----",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
def make_homeserver(self, reactor, clock):
|
|
|
|
self.hs = self.setup_test_homeserver()
|
|
|
|
self.hs.config.jwt_enabled = True
|
|
|
|
self.hs.config.jwt_secret = self.jwt_pubkey
|
|
|
|
self.hs.config.jwt_algorithm = "RS256"
|
|
|
|
return self.hs
|
|
|
|
|
|
|
|
def jwt_encode(self, token, secret=jwt_privatekey):
|
|
|
|
return jwt.encode(token, secret, "RS256").decode("ascii")
|
|
|
|
|
|
|
|
def jwt_login(self, *args):
|
2020-06-24 05:23:55 -04:00
|
|
|
params = json.dumps(
|
|
|
|
{"type": "org.matrix.login.jwt", "token": self.jwt_encode(*args)}
|
|
|
|
)
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"POST", LOGIN_URL, params)
|
2020-06-01 12:55:07 -04:00
|
|
|
return channel
|
|
|
|
|
|
|
|
def test_login_jwt_valid(self):
|
|
|
|
channel = self.jwt_login({"sub": "kermit"})
|
|
|
|
self.assertEqual(channel.result["code"], b"200", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["user_id"], "@kermit:test")
|
|
|
|
|
|
|
|
def test_login_jwt_invalid_signature(self):
|
|
|
|
channel = self.jwt_login({"sub": "frog"}, self.bad_privatekey)
|
2020-07-15 07:10:21 -04:00
|
|
|
self.assertEqual(channel.result["code"], b"403", channel.result)
|
|
|
|
self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
|
2020-07-14 07:16:43 -04:00
|
|
|
self.assertEqual(
|
|
|
|
channel.json_body["error"],
|
|
|
|
"JWT validation failed: Signature verification failed",
|
|
|
|
)
|
2020-09-18 09:55:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
AS_USER = "as_user_alice"
|
|
|
|
|
|
|
|
|
|
|
|
class AppserviceLoginRestServletTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
login.register_servlets,
|
|
|
|
register.register_servlets,
|
|
|
|
]
|
|
|
|
|
|
|
|
def register_as_user(self, username):
|
2020-12-15 17:28:06 -05:00
|
|
|
self.make_request(
|
2020-09-18 09:55:13 -04:00
|
|
|
b"POST",
|
|
|
|
"/_matrix/client/r0/register?access_token=%s" % (self.service.token,),
|
|
|
|
{"username": username},
|
|
|
|
)
|
|
|
|
|
|
|
|
def make_homeserver(self, reactor, clock):
|
|
|
|
self.hs = self.setup_test_homeserver()
|
|
|
|
|
|
|
|
self.service = ApplicationService(
|
|
|
|
id="unique_identifier",
|
|
|
|
token="some_token",
|
|
|
|
hostname="example.com",
|
|
|
|
sender="@asbot:example.com",
|
|
|
|
namespaces={
|
|
|
|
ApplicationService.NS_USERS: [
|
|
|
|
{"regex": r"@as_user.*", "exclusive": False}
|
|
|
|
],
|
|
|
|
ApplicationService.NS_ROOMS: [],
|
|
|
|
ApplicationService.NS_ALIASES: [],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.another_service = ApplicationService(
|
|
|
|
id="another__identifier",
|
|
|
|
token="another_token",
|
|
|
|
hostname="example.com",
|
|
|
|
sender="@as2bot:example.com",
|
|
|
|
namespaces={
|
|
|
|
ApplicationService.NS_USERS: [
|
|
|
|
{"regex": r"@as2_user.*", "exclusive": False}
|
|
|
|
],
|
|
|
|
ApplicationService.NS_ROOMS: [],
|
|
|
|
ApplicationService.NS_ALIASES: [],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.hs.get_datastore().services_cache.append(self.service)
|
|
|
|
self.hs.get_datastore().services_cache.append(self.another_service)
|
|
|
|
return self.hs
|
|
|
|
|
|
|
|
def test_login_appservice_user(self):
|
|
|
|
"""Test that an appservice user can use /login
|
|
|
|
"""
|
|
|
|
self.register_as_user(AS_USER)
|
|
|
|
|
|
|
|
params = {
|
|
|
|
"type": login.LoginRestServlet.APPSERVICE_TYPE,
|
|
|
|
"identifier": {"type": "m.id.user", "user": AS_USER},
|
|
|
|
}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-09-18 09:55:13 -04:00
|
|
|
b"POST", LOGIN_URL, params, access_token=self.service.token
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEquals(channel.result["code"], b"200", channel.result)
|
|
|
|
|
|
|
|
def test_login_appservice_user_bot(self):
|
|
|
|
"""Test that the appservice bot can use /login
|
|
|
|
"""
|
|
|
|
self.register_as_user(AS_USER)
|
|
|
|
|
|
|
|
params = {
|
|
|
|
"type": login.LoginRestServlet.APPSERVICE_TYPE,
|
|
|
|
"identifier": {"type": "m.id.user", "user": self.service.sender},
|
|
|
|
}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-09-18 09:55:13 -04:00
|
|
|
b"POST", LOGIN_URL, params, access_token=self.service.token
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEquals(channel.result["code"], b"200", channel.result)
|
|
|
|
|
|
|
|
def test_login_appservice_wrong_user(self):
|
|
|
|
"""Test that non-as users cannot login with the as token
|
|
|
|
"""
|
|
|
|
self.register_as_user(AS_USER)
|
|
|
|
|
|
|
|
params = {
|
|
|
|
"type": login.LoginRestServlet.APPSERVICE_TYPE,
|
|
|
|
"identifier": {"type": "m.id.user", "user": "fibble_wibble"},
|
|
|
|
}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-09-18 09:55:13 -04:00
|
|
|
b"POST", LOGIN_URL, params, access_token=self.service.token
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEquals(channel.result["code"], b"403", channel.result)
|
|
|
|
|
|
|
|
def test_login_appservice_wrong_as(self):
|
|
|
|
"""Test that as users cannot login with wrong as token
|
|
|
|
"""
|
|
|
|
self.register_as_user(AS_USER)
|
|
|
|
|
|
|
|
params = {
|
|
|
|
"type": login.LoginRestServlet.APPSERVICE_TYPE,
|
|
|
|
"identifier": {"type": "m.id.user", "user": AS_USER},
|
|
|
|
}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-09-18 09:55:13 -04:00
|
|
|
b"POST", LOGIN_URL, params, access_token=self.another_service.token
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEquals(channel.result["code"], b"403", channel.result)
|
|
|
|
|
|
|
|
def test_login_appservice_no_token(self):
|
|
|
|
"""Test that users must provide a token when using the appservice
|
|
|
|
login method
|
|
|
|
"""
|
|
|
|
self.register_as_user(AS_USER)
|
|
|
|
|
|
|
|
params = {
|
|
|
|
"type": login.LoginRestServlet.APPSERVICE_TYPE,
|
|
|
|
"identifier": {"type": "m.id.user", "user": AS_USER},
|
|
|
|
}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(b"POST", LOGIN_URL, params)
|
2020-09-18 09:55:13 -04:00
|
|
|
|
|
|
|
self.assertEquals(channel.result["code"], b"401", channel.result)
|