2015-08-18 16:22:02 +03:00
|
|
|
#
|
2023-11-21 23:29:58 +03:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 14:26:48 +03:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2023-11-21 23:29:58 +03:00
|
|
|
# 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 16:22:02 +03:00
|
|
|
#
|
|
|
|
#
|
2022-10-26 13:45:41 +03:00
|
|
|
from typing import Optional
|
2023-08-25 02:38:46 +03:00
|
|
|
from unittest.mock import AsyncMock
|
2018-07-31 15:16:20 +03:00
|
|
|
|
2015-08-18 16:22:02 +03:00
|
|
|
import pymacaroons
|
2018-07-09 09:09:20 +03:00
|
|
|
|
2022-03-11 15:07:15 +03:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
|
|
|
|
2021-02-11 18:29:09 +03:00
|
|
|
from synapse.api.errors import AuthError, ResourceLimitError
|
2021-05-12 17:04:51 +03:00
|
|
|
from synapse.rest import admin
|
2022-10-26 13:45:41 +03:00
|
|
|
from synapse.rest.client import login
|
2022-03-11 15:07:15 +03:00
|
|
|
from synapse.server import HomeServer
|
|
|
|
from synapse.util import Clock
|
2018-07-09 09:09:20 +03:00
|
|
|
|
2015-08-18 16:22:02 +03:00
|
|
|
from tests import unittest
|
|
|
|
|
2016-08-08 19:20:38 +03:00
|
|
|
|
2021-02-11 18:29:09 +03:00
|
|
|
class AuthTestCase(unittest.HomeserverTestCase):
|
2021-05-12 17:04:51 +03:00
|
|
|
servlets = [
|
|
|
|
admin.register_servlets,
|
2022-10-26 13:45:41 +03:00
|
|
|
login.register_servlets,
|
2021-05-12 17:04:51 +03:00
|
|
|
]
|
|
|
|
|
2022-03-11 15:07:15 +03:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2021-02-11 18:29:09 +03:00
|
|
|
self.auth_handler = hs.get_auth_handler()
|
|
|
|
self.macaroon_generator = hs.get_macaroon_generator()
|
2020-05-06 17:54:58 +03:00
|
|
|
|
2018-07-30 17:55:57 +03:00
|
|
|
# MAU tests
|
2020-05-06 17:54:58 +03:00
|
|
|
# AuthBlocking reads from the hs' config on initialization. We need to
|
|
|
|
# modify its config instead of the hs'
|
2022-06-14 11:51:15 +03:00
|
|
|
self.auth_blocking = hs.get_auth_blocking()
|
2020-05-06 17:54:58 +03:00
|
|
|
self.auth_blocking._max_mau_value = 50
|
|
|
|
|
2018-07-30 17:55:57 +03:00
|
|
|
self.small_number_of_users = 1
|
|
|
|
self.large_number_of_users = 100
|
2015-08-18 16:22:02 +03:00
|
|
|
|
2021-05-12 17:04:51 +03:00
|
|
|
self.user1 = self.register_user("a_user", "pass")
|
2015-08-18 16:22:02 +03:00
|
|
|
|
2022-10-26 13:45:41 +03: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 15:07:15 +03:00
|
|
|
def test_macaroon_caveats(self) -> None:
|
2021-05-12 17:04:51 +03:00
|
|
|
token = self.macaroon_generator.generate_guest_access_token("a_user")
|
2015-08-18 16:22:02 +03:00
|
|
|
macaroon = pymacaroons.Macaroon.deserialize(token)
|
|
|
|
|
2022-03-11 15:07:15 +03:00
|
|
|
def verify_gen(caveat: str) -> bool:
|
2015-08-19 16:30:31 +03:00
|
|
|
return caveat == "gen = 1"
|
2015-08-18 16:22:02 +03:00
|
|
|
|
2022-03-11 15:07:15 +03:00
|
|
|
def verify_user(caveat: str) -> bool:
|
2015-08-19 16:30:31 +03:00
|
|
|
return caveat == "user_id = a_user"
|
2015-08-18 16:22:02 +03:00
|
|
|
|
2022-03-11 15:07:15 +03:00
|
|
|
def verify_type(caveat: str) -> bool:
|
2015-08-19 16:30:31 +03:00
|
|
|
return caveat == "type = access"
|
2015-08-18 16:22:02 +03:00
|
|
|
|
2022-03-11 15:07:15 +03:00
|
|
|
def verify_nonce(caveat: str) -> bool:
|
2016-11-28 12:55:21 +03:00
|
|
|
return caveat.startswith("nonce =")
|
2015-08-18 16:22:02 +03:00
|
|
|
|
2022-03-11 15:07:15 +03:00
|
|
|
def verify_guest(caveat: str) -> bool:
|
2021-05-12 17:04:51 +03:00
|
|
|
return caveat == "guest = true"
|
|
|
|
|
2015-08-18 16:22:02 +03:00
|
|
|
v = pymacaroons.Verifier()
|
|
|
|
v.satisfy_general(verify_gen)
|
|
|
|
v.satisfy_general(verify_user)
|
|
|
|
v.satisfy_general(verify_type)
|
2016-11-28 12:55:21 +03:00
|
|
|
v.satisfy_general(verify_nonce)
|
2021-05-12 17:04:51 +03:00
|
|
|
v.satisfy_general(verify_guest)
|
2021-09-23 19:03:01 +03:00
|
|
|
v.verify(macaroon, self.hs.config.key.macaroon_secret_key)
|
2016-08-08 18:34:07 +03:00
|
|
|
|
2022-10-26 13:45:41 +03: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 18:34:07 +03:00
|
|
|
)
|
2022-10-26 13:45:41 +03:00
|
|
|
|
|
|
|
res = self.get_success(self.auth_handler.consume_login_token(token))
|
2021-05-12 17:04:51 +03:00
|
|
|
self.assertEqual(self.user1, res.user_id)
|
2022-10-26 13:45:41 +03:00
|
|
|
self.assertEqual(None, res.auth_provider_id)
|
2016-08-08 18:34:07 +03:00
|
|
|
|
2022-10-26 13:45:41 +03: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 18:29:09 +03:00
|
|
|
)
|
2016-08-08 18:34:07 +03:00
|
|
|
|
2022-10-26 13:45:41 +03:00
|
|
|
self.get_success(self.auth_handler.consume_login_token(token))
|
2021-03-04 17:44:22 +03:00
|
|
|
|
2022-10-26 13:45:41 +03:00
|
|
|
self.get_failure(
|
|
|
|
self.auth_handler.consume_login_token(token),
|
|
|
|
AuthError,
|
2021-03-04 17:44:22 +03:00
|
|
|
)
|
2016-08-08 18:34:07 +03:00
|
|
|
|
2022-10-26 13:45:41 +03: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 12:21:56 +03:00
|
|
|
)
|
2016-08-08 18:34:07 +03:00
|
|
|
|
2022-10-26 13:45:41 +03:00
|
|
|
# when we advance the clock, the token should be rejected
|
|
|
|
self.reactor.advance(6)
|
2021-02-11 18:29:09 +03:00
|
|
|
self.get_failure(
|
2022-10-26 13:45:41 +03:00
|
|
|
self.auth_handler.consume_login_token(token),
|
2021-02-11 18:29:09 +03:00
|
|
|
AuthError,
|
|
|
|
)
|
2018-07-30 17:55:57 +03:00
|
|
|
|
2022-10-26 13:45:41 +03: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 15:07:15 +03:00
|
|
|
def test_mau_limits_disabled(self) -> None:
|
2020-05-06 17:54:58 +03:00
|
|
|
self.auth_blocking._limit_usage_by_mau = False
|
2018-07-30 17:55:57 +03:00
|
|
|
# Ensure does not throw exception
|
2021-02-11 18:29:09 +03:00
|
|
|
self.get_success(
|
2021-11-17 17:10:57 +03:00
|
|
|
self.auth_handler.create_access_token_for_user_id(
|
2021-05-12 17:04:51 +03:00
|
|
|
self.user1, device_id=None, valid_until_ms=None
|
2020-04-15 19:40:18 +03:00
|
|
|
)
|
2019-07-12 19:26:02 +03:00
|
|
|
)
|
2018-07-30 17:55:57 +03:00
|
|
|
|
2022-10-26 13:45:41 +03:00
|
|
|
token = self.get_success(
|
|
|
|
self.auth_handler.create_login_token_for_user_id(self.user1)
|
2018-07-30 17:55:57 +03:00
|
|
|
)
|
|
|
|
|
2022-10-26 13:45:41 +03:00
|
|
|
self.assertIsNotNone(self.token_login(token))
|
|
|
|
|
2022-03-11 15:07:15 +03:00
|
|
|
def test_mau_limits_exceeded_large(self) -> None:
|
2020-05-06 17:54:58 +03:00
|
|
|
self.auth_blocking._limit_usage_by_mau = True
|
2023-08-25 02:38:46 +03:00
|
|
|
self.hs.get_datastores().main.get_monthly_active_count = AsyncMock(
|
|
|
|
return_value=self.large_number_of_users
|
2018-07-30 17:55:57 +03:00
|
|
|
)
|
2018-08-01 12:21:56 +03:00
|
|
|
|
2021-02-11 18:29:09 +03:00
|
|
|
self.get_failure(
|
2021-11-17 17:10:57 +03:00
|
|
|
self.auth_handler.create_access_token_for_user_id(
|
2021-05-12 17:04:51 +03:00
|
|
|
self.user1, device_id=None, valid_until_ms=None
|
2021-02-11 18:29:09 +03:00
|
|
|
),
|
|
|
|
ResourceLimitError,
|
|
|
|
)
|
2018-08-01 12:21:56 +03:00
|
|
|
|
2023-08-25 02:38:46 +03:00
|
|
|
self.hs.get_datastores().main.get_monthly_active_count = AsyncMock(
|
|
|
|
return_value=self.large_number_of_users
|
2018-08-01 12:21:56 +03:00
|
|
|
)
|
2022-10-26 13:45:41 +03:00
|
|
|
token = self.get_success(
|
|
|
|
self.auth_handler.create_login_token_for_user_id(self.user1)
|
2021-02-11 18:29:09 +03:00
|
|
|
)
|
2022-10-26 13:45:41 +03:00
|
|
|
self.assertIsNone(self.token_login(token))
|
2018-07-30 17:55:57 +03:00
|
|
|
|
2022-03-11 15:07:15 +03:00
|
|
|
def test_mau_limits_parity(self) -> None:
|
2021-02-11 18:29:09 +03:00
|
|
|
# Ensure we're not at the unix epoch.
|
|
|
|
self.reactor.advance(1)
|
2020-05-06 17:54:58 +03:00
|
|
|
self.auth_blocking._limit_usage_by_mau = True
|
2018-08-14 17:04:48 +03:00
|
|
|
|
2021-02-11 18:29:09 +03:00
|
|
|
# Set the server to be at the edge of too many users.
|
2023-08-25 02:38:46 +03:00
|
|
|
self.hs.get_datastores().main.get_monthly_active_count = AsyncMock(
|
|
|
|
return_value=self.auth_blocking._max_mau_value
|
2018-08-14 17:04:48 +03:00
|
|
|
)
|
|
|
|
|
2021-02-11 18:29:09 +03:00
|
|
|
# If not in monthly active cohort
|
|
|
|
self.get_failure(
|
2021-11-17 17:10:57 +03:00
|
|
|
self.auth_handler.create_access_token_for_user_id(
|
2021-05-12 17:04:51 +03:00
|
|
|
self.user1, device_id=None, valid_until_ms=None
|
2021-02-11 18:29:09 +03:00
|
|
|
),
|
|
|
|
ResourceLimitError,
|
2018-08-14 17:04:48 +03:00
|
|
|
)
|
2022-10-26 13:45:41 +03:00
|
|
|
token = self.get_success(
|
|
|
|
self.auth_handler.create_login_token_for_user_id(self.user1)
|
2021-02-11 18:29:09 +03:00
|
|
|
)
|
2022-10-26 13:45:41 +03:00
|
|
|
self.assertIsNone(self.token_login(token))
|
2021-02-11 18:29:09 +03:00
|
|
|
|
2018-08-14 17:04:48 +03:00
|
|
|
# If in monthly active cohort
|
2023-08-25 02:38:46 +03:00
|
|
|
self.hs.get_datastores().main.user_last_seen_monthly_active = AsyncMock(
|
|
|
|
return_value=self.clock.time_msec()
|
2018-08-14 17:04:48 +03:00
|
|
|
)
|
2021-02-11 18:29:09 +03:00
|
|
|
self.get_success(
|
2021-11-17 17:10:57 +03:00
|
|
|
self.auth_handler.create_access_token_for_user_id(
|
2021-05-12 17:04:51 +03:00
|
|
|
self.user1, device_id=None, valid_until_ms=None
|
2020-04-15 19:40:18 +03:00
|
|
|
)
|
2019-07-12 19:26:02 +03:00
|
|
|
)
|
2022-10-26 13:45:41 +03:00
|
|
|
token = self.get_success(
|
|
|
|
self.auth_handler.create_login_token_for_user_id(self.user1)
|
2018-08-14 17:04:48 +03:00
|
|
|
)
|
2022-10-26 13:45:41 +03:00
|
|
|
self.assertIsNotNone(self.token_login(token))
|
2018-08-14 17:04:48 +03:00
|
|
|
|
2022-03-11 15:07:15 +03:00
|
|
|
def test_mau_limits_not_exceeded(self) -> None:
|
2020-05-06 17:54:58 +03:00
|
|
|
self.auth_blocking._limit_usage_by_mau = True
|
2018-08-01 12:21:56 +03:00
|
|
|
|
2023-08-25 02:38:46 +03:00
|
|
|
self.hs.get_datastores().main.get_monthly_active_count = AsyncMock(
|
|
|
|
return_value=self.small_number_of_users
|
2018-07-30 17:55:57 +03:00
|
|
|
)
|
|
|
|
# Ensure does not raise exception
|
2021-02-11 18:29:09 +03:00
|
|
|
self.get_success(
|
2021-11-17 17:10:57 +03:00
|
|
|
self.auth_handler.create_access_token_for_user_id(
|
2021-05-12 17:04:51 +03:00
|
|
|
self.user1, device_id=None, valid_until_ms=None
|
2020-04-15 19:40:18 +03:00
|
|
|
)
|
2019-07-12 19:26:02 +03:00
|
|
|
)
|
2018-08-01 16:02:10 +03:00
|
|
|
|
2023-08-25 02:38:46 +03:00
|
|
|
self.hs.get_datastores().main.get_monthly_active_count = AsyncMock(
|
|
|
|
return_value=self.small_number_of_users
|
2018-08-01 16:02:10 +03:00
|
|
|
)
|
2022-10-26 13:45:41 +03:00
|
|
|
token = self.get_success(
|
|
|
|
self.auth_handler.create_login_token_for_user_id(self.user1)
|
2021-03-04 17:44:22 +03:00
|
|
|
)
|
2022-10-26 13:45:41 +03:00
|
|
|
self.assertIsNotNone(self.token_login(token))
|