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