2015-08-18 15:22:02 +02:00
|
|
|
#
|
2023-11-21 21:29:58 +01:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2015-08-18 15:22:02 +02:00
|
|
|
#
|
|
|
|
#
|
2022-10-26 12:45:41 +02:00
|
|
|
from typing import Optional
|
2023-08-25 01:38:46 +02:00
|
|
|
from unittest.mock import AsyncMock
|
2018-07-31 14:16:20 +02:00
|
|
|
|
2015-08-18 15:22:02 +02:00
|
|
|
import pymacaroons
|
2018-07-09 08:09:20 +02:00
|
|
|
|
2022-03-11 13:07:15 +01:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
|
|
|
|
2021-02-11 16:29:09 +01:00
|
|
|
from synapse.api.errors import AuthError, ResourceLimitError
|
2021-05-12 16:04:51 +02:00
|
|
|
from synapse.rest import admin
|
2022-10-26 12:45:41 +02:00
|
|
|
from synapse.rest.client import login
|
2022-03-11 13:07:15 +01:00
|
|
|
from synapse.server import HomeServer
|
|
|
|
from synapse.util import Clock
|
2018-07-09 08:09:20 +02:00
|
|
|
|
2015-08-18 15:22:02 +02:00
|
|
|
from tests import unittest
|
|
|
|
|
2016-08-08 18:20:38 +02:00
|
|
|
|
2021-02-11 16:29:09 +01:00
|
|
|
class AuthTestCase(unittest.HomeserverTestCase):
|
2021-05-12 16:04:51 +02:00
|
|
|
servlets = [
|
|
|
|
admin.register_servlets,
|
2022-10-26 12:45:41 +02:00
|
|
|
login.register_servlets,
|
2021-05-12 16:04:51 +02:00
|
|
|
]
|
|
|
|
|
2022-03-11 13:07:15 +01:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2021-02-11 16:29:09 +01:00
|
|
|
self.auth_handler = hs.get_auth_handler()
|
|
|
|
self.macaroon_generator = hs.get_macaroon_generator()
|
2020-05-06 16:54:58 +02:00
|
|
|
|
2018-07-30 16:55:57 +02:00
|
|
|
# MAU tests
|
2020-05-06 16:54:58 +02:00
|
|
|
# AuthBlocking reads from the hs' config on initialization. We need to
|
|
|
|
# modify its config instead of the hs'
|
2022-06-14 10:51:15 +02:00
|
|
|
self.auth_blocking = hs.get_auth_blocking()
|
2020-05-06 16:54:58 +02:00
|
|
|
self.auth_blocking._max_mau_value = 50
|
|
|
|
|
2018-07-30 16:55:57 +02:00
|
|
|
self.small_number_of_users = 1
|
|
|
|
self.large_number_of_users = 100
|
2015-08-18 15:22:02 +02:00
|
|
|
|
2021-05-12 16:04:51 +02:00
|
|
|
self.user1 = self.register_user("a_user", "pass")
|
2015-08-18 15:22:02 +02:00
|
|
|
|
2022-10-26 12:45:41 +02:00
|
|
|
def token_login(self, token: str) -> Optional[str]:
|
|
|
|
body = {
|
|
|
|
"type": "m.login.token",
|
|
|
|
"token": token,
|
|
|
|
}
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
"/_matrix/client/v3/login",
|
|
|
|
body,
|
|
|
|
)
|
|
|
|
|
|
|
|
if channel.code == 200:
|
|
|
|
return channel.json_body["user_id"]
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2022-03-11 13:07:15 +01:00
|
|
|
def test_macaroon_caveats(self) -> None:
|
2021-05-12 16:04:51 +02:00
|
|
|
token = self.macaroon_generator.generate_guest_access_token("a_user")
|
2015-08-18 15:22:02 +02:00
|
|
|
macaroon = pymacaroons.Macaroon.deserialize(token)
|
|
|
|
|
2022-03-11 13:07:15 +01:00
|
|
|
def verify_gen(caveat: str) -> bool:
|
2015-08-19 15:30:31 +02:00
|
|
|
return caveat == "gen = 1"
|
2015-08-18 15:22:02 +02:00
|
|
|
|
2022-03-11 13:07:15 +01:00
|
|
|
def verify_user(caveat: str) -> bool:
|
2015-08-19 15:30:31 +02:00
|
|
|
return caveat == "user_id = a_user"
|
2015-08-18 15:22:02 +02:00
|
|
|
|
2022-03-11 13:07:15 +01:00
|
|
|
def verify_type(caveat: str) -> bool:
|
2015-08-19 15:30:31 +02:00
|
|
|
return caveat == "type = access"
|
2015-08-18 15:22:02 +02:00
|
|
|
|
2022-03-11 13:07:15 +01:00
|
|
|
def verify_nonce(caveat: str) -> bool:
|
2016-11-28 10:55:21 +01:00
|
|
|
return caveat.startswith("nonce =")
|
2015-08-18 15:22:02 +02:00
|
|
|
|
2022-03-11 13:07:15 +01:00
|
|
|
def verify_guest(caveat: str) -> bool:
|
2021-05-12 16:04:51 +02:00
|
|
|
return caveat == "guest = true"
|
|
|
|
|
2015-08-18 15:22:02 +02:00
|
|
|
v = pymacaroons.Verifier()
|
|
|
|
v.satisfy_general(verify_gen)
|
|
|
|
v.satisfy_general(verify_user)
|
|
|
|
v.satisfy_general(verify_type)
|
2016-11-28 10:55:21 +01:00
|
|
|
v.satisfy_general(verify_nonce)
|
2021-05-12 16:04:51 +02:00
|
|
|
v.satisfy_general(verify_guest)
|
2021-09-23 18:03:01 +02:00
|
|
|
v.verify(macaroon, self.hs.config.key.macaroon_secret_key)
|
2016-08-08 17:34:07 +02:00
|
|
|
|
2022-10-26 12:45:41 +02:00
|
|
|
def test_login_token_gives_user_id(self) -> None:
|
|
|
|
token = self.get_success(
|
|
|
|
self.auth_handler.create_login_token_for_user_id(
|
|
|
|
self.user1,
|
|
|
|
duration_ms=(5 * 1000),
|
|
|
|
)
|
2016-08-08 17:34:07 +02:00
|
|
|
)
|
2022-10-26 12:45:41 +02:00
|
|
|
|
|
|
|
res = self.get_success(self.auth_handler.consume_login_token(token))
|
2021-05-12 16:04:51 +02:00
|
|
|
self.assertEqual(self.user1, res.user_id)
|
2022-10-26 12:45:41 +02:00
|
|
|
self.assertEqual(None, res.auth_provider_id)
|
2016-08-08 17:34:07 +02:00
|
|
|
|
2022-10-26 12:45:41 +02:00
|
|
|
def test_login_token_reuse_fails(self) -> None:
|
|
|
|
token = self.get_success(
|
|
|
|
self.auth_handler.create_login_token_for_user_id(
|
|
|
|
self.user1,
|
|
|
|
duration_ms=(5 * 1000),
|
|
|
|
)
|
2021-02-11 16:29:09 +01:00
|
|
|
)
|
2016-08-08 17:34:07 +02:00
|
|
|
|
2022-10-26 12:45:41 +02:00
|
|
|
self.get_success(self.auth_handler.consume_login_token(token))
|
2021-03-04 15:44:22 +01:00
|
|
|
|
2022-10-26 12:45:41 +02:00
|
|
|
self.get_failure(
|
|
|
|
self.auth_handler.consume_login_token(token),
|
|
|
|
AuthError,
|
2021-03-04 15:44:22 +01:00
|
|
|
)
|
2016-08-08 17:34:07 +02:00
|
|
|
|
2022-10-26 12:45:41 +02:00
|
|
|
def test_login_token_expires(self) -> None:
|
|
|
|
token = self.get_success(
|
|
|
|
self.auth_handler.create_login_token_for_user_id(
|
|
|
|
self.user1,
|
|
|
|
duration_ms=(5 * 1000),
|
|
|
|
)
|
2018-08-01 11:21:56 +02:00
|
|
|
)
|
2016-08-08 17:34:07 +02:00
|
|
|
|
2022-10-26 12:45:41 +02:00
|
|
|
# when we advance the clock, the token should be rejected
|
|
|
|
self.reactor.advance(6)
|
2021-02-11 16:29:09 +01:00
|
|
|
self.get_failure(
|
2022-10-26 12:45:41 +02:00
|
|
|
self.auth_handler.consume_login_token(token),
|
2021-02-11 16:29:09 +01:00
|
|
|
AuthError,
|
|
|
|
)
|
2018-07-30 16:55:57 +02:00
|
|
|
|
2022-10-26 12:45:41 +02:00
|
|
|
def test_login_token_gives_auth_provider(self) -> None:
|
|
|
|
token = self.get_success(
|
|
|
|
self.auth_handler.create_login_token_for_user_id(
|
|
|
|
self.user1,
|
|
|
|
auth_provider_id="my_idp",
|
|
|
|
auth_provider_session_id="11-22-33-44",
|
|
|
|
duration_ms=(5 * 1000),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
res = self.get_success(self.auth_handler.consume_login_token(token))
|
|
|
|
self.assertEqual(self.user1, res.user_id)
|
|
|
|
self.assertEqual("my_idp", res.auth_provider_id)
|
|
|
|
self.assertEqual("11-22-33-44", res.auth_provider_session_id)
|
|
|
|
|
2022-03-11 13:07:15 +01:00
|
|
|
def test_mau_limits_disabled(self) -> None:
|
2020-05-06 16:54:58 +02:00
|
|
|
self.auth_blocking._limit_usage_by_mau = False
|
2018-07-30 16:55:57 +02:00
|
|
|
# Ensure does not throw exception
|
2021-02-11 16:29:09 +01:00
|
|
|
self.get_success(
|
2021-11-17 15:10:57 +01:00
|
|
|
self.auth_handler.create_access_token_for_user_id(
|
2021-05-12 16:04:51 +02:00
|
|
|
self.user1, device_id=None, valid_until_ms=None
|
2020-04-15 18:40:18 +02:00
|
|
|
)
|
2019-07-12 18:26:02 +02:00
|
|
|
)
|
2018-07-30 16:55:57 +02:00
|
|
|
|
2022-10-26 12:45:41 +02:00
|
|
|
token = self.get_success(
|
|
|
|
self.auth_handler.create_login_token_for_user_id(self.user1)
|
2018-07-30 16:55:57 +02:00
|
|
|
)
|
|
|
|
|
2022-10-26 12:45:41 +02:00
|
|
|
self.assertIsNotNone(self.token_login(token))
|
|
|
|
|
2022-03-11 13:07:15 +01:00
|
|
|
def test_mau_limits_exceeded_large(self) -> None:
|
2020-05-06 16:54:58 +02:00
|
|
|
self.auth_blocking._limit_usage_by_mau = True
|
2023-08-25 01:38:46 +02:00
|
|
|
self.hs.get_datastores().main.get_monthly_active_count = AsyncMock(
|
|
|
|
return_value=self.large_number_of_users
|
2018-07-30 16:55:57 +02:00
|
|
|
)
|
2018-08-01 11:21:56 +02:00
|
|
|
|
2021-02-11 16:29:09 +01:00
|
|
|
self.get_failure(
|
2021-11-17 15:10:57 +01:00
|
|
|
self.auth_handler.create_access_token_for_user_id(
|
2021-05-12 16:04:51 +02:00
|
|
|
self.user1, device_id=None, valid_until_ms=None
|
2021-02-11 16:29:09 +01:00
|
|
|
),
|
|
|
|
ResourceLimitError,
|
|
|
|
)
|
2018-08-01 11:21:56 +02:00
|
|
|
|
2023-08-25 01:38:46 +02:00
|
|
|
self.hs.get_datastores().main.get_monthly_active_count = AsyncMock(
|
|
|
|
return_value=self.large_number_of_users
|
2018-08-01 11:21:56 +02:00
|
|
|
)
|
2022-10-26 12:45:41 +02:00
|
|
|
token = self.get_success(
|
|
|
|
self.auth_handler.create_login_token_for_user_id(self.user1)
|
2021-02-11 16:29:09 +01:00
|
|
|
)
|
2022-10-26 12:45:41 +02:00
|
|
|
self.assertIsNone(self.token_login(token))
|
2018-07-30 16:55:57 +02:00
|
|
|
|
2022-03-11 13:07:15 +01:00
|
|
|
def test_mau_limits_parity(self) -> None:
|
2021-02-11 16:29:09 +01:00
|
|
|
# Ensure we're not at the unix epoch.
|
|
|
|
self.reactor.advance(1)
|
2020-05-06 16:54:58 +02:00
|
|
|
self.auth_blocking._limit_usage_by_mau = True
|
2018-08-14 16:04:48 +02:00
|
|
|
|
2021-02-11 16:29:09 +01:00
|
|
|
# Set the server to be at the edge of too many users.
|
2023-08-25 01:38:46 +02:00
|
|
|
self.hs.get_datastores().main.get_monthly_active_count = AsyncMock(
|
|
|
|
return_value=self.auth_blocking._max_mau_value
|
2018-08-14 16:04:48 +02:00
|
|
|
)
|
|
|
|
|
2021-02-11 16:29:09 +01:00
|
|
|
# If not in monthly active cohort
|
|
|
|
self.get_failure(
|
2021-11-17 15:10:57 +01:00
|
|
|
self.auth_handler.create_access_token_for_user_id(
|
2021-05-12 16:04:51 +02:00
|
|
|
self.user1, device_id=None, valid_until_ms=None
|
2021-02-11 16:29:09 +01:00
|
|
|
),
|
|
|
|
ResourceLimitError,
|
2018-08-14 16:04:48 +02:00
|
|
|
)
|
2022-10-26 12:45:41 +02:00
|
|
|
token = self.get_success(
|
|
|
|
self.auth_handler.create_login_token_for_user_id(self.user1)
|
2021-02-11 16:29:09 +01:00
|
|
|
)
|
2022-10-26 12:45:41 +02:00
|
|
|
self.assertIsNone(self.token_login(token))
|
2021-02-11 16:29:09 +01:00
|
|
|
|
2018-08-14 16:04:48 +02:00
|
|
|
# If in monthly active cohort
|
2023-08-25 01:38:46 +02:00
|
|
|
self.hs.get_datastores().main.user_last_seen_monthly_active = AsyncMock(
|
|
|
|
return_value=self.clock.time_msec()
|
2018-08-14 16:04:48 +02:00
|
|
|
)
|
2021-02-11 16:29:09 +01:00
|
|
|
self.get_success(
|
2021-11-17 15:10:57 +01:00
|
|
|
self.auth_handler.create_access_token_for_user_id(
|
2021-05-12 16:04:51 +02:00
|
|
|
self.user1, device_id=None, valid_until_ms=None
|
2020-04-15 18:40:18 +02:00
|
|
|
)
|
2019-07-12 18:26:02 +02:00
|
|
|
)
|
2022-10-26 12:45:41 +02:00
|
|
|
token = self.get_success(
|
|
|
|
self.auth_handler.create_login_token_for_user_id(self.user1)
|
2018-08-14 16:04:48 +02:00
|
|
|
)
|
2022-10-26 12:45:41 +02:00
|
|
|
self.assertIsNotNone(self.token_login(token))
|
2018-08-14 16:04:48 +02:00
|
|
|
|
2022-03-11 13:07:15 +01:00
|
|
|
def test_mau_limits_not_exceeded(self) -> None:
|
2020-05-06 16:54:58 +02:00
|
|
|
self.auth_blocking._limit_usage_by_mau = True
|
2018-08-01 11:21:56 +02:00
|
|
|
|
2023-08-25 01:38:46 +02:00
|
|
|
self.hs.get_datastores().main.get_monthly_active_count = AsyncMock(
|
|
|
|
return_value=self.small_number_of_users
|
2018-07-30 16:55:57 +02:00
|
|
|
)
|
|
|
|
# Ensure does not raise exception
|
2021-02-11 16:29:09 +01:00
|
|
|
self.get_success(
|
2021-11-17 15:10:57 +01:00
|
|
|
self.auth_handler.create_access_token_for_user_id(
|
2021-05-12 16:04:51 +02:00
|
|
|
self.user1, device_id=None, valid_until_ms=None
|
2020-04-15 18:40:18 +02:00
|
|
|
)
|
2019-07-12 18:26:02 +02:00
|
|
|
)
|
2018-08-01 15:02:10 +02:00
|
|
|
|
2023-08-25 01:38:46 +02:00
|
|
|
self.hs.get_datastores().main.get_monthly_active_count = AsyncMock(
|
|
|
|
return_value=self.small_number_of_users
|
2018-08-01 15:02:10 +02:00
|
|
|
)
|
2022-10-26 12:45:41 +02:00
|
|
|
token = self.get_success(
|
|
|
|
self.auth_handler.create_login_token_for_user_id(self.user1)
|
2021-03-04 15:44:22 +01:00
|
|
|
)
|
2022-10-26 12:45:41 +02:00
|
|
|
self.assertIsNotNone(self.token_login(token))
|