2016-01-07 05:26:29 +01:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2015-08-18 15:22:02 +02: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.
|
2021-04-09 19:44:38 +02:00
|
|
|
from unittest.mock import Mock
|
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
|
|
|
|
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
|
2018-07-09 08:09:20 +02:00
|
|
|
|
2015-08-18 15:22:02 +02:00
|
|
|
from tests import unittest
|
2020-08-27 23:24:37 +02:00
|
|
|
from tests.test_utils import make_awaitable
|
2015-08-18 15:22:02 +02:00
|
|
|
|
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,
|
|
|
|
]
|
|
|
|
|
2021-02-11 16:29:09 +01:00
|
|
|
def prepare(self, reactor, clock, hs):
|
|
|
|
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'
|
2021-02-11 16:29:09 +01:00
|
|
|
self.auth_blocking = hs.get_auth()._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
|
|
|
|
|
|
|
def test_macaroon_caveats(self):
|
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)
|
|
|
|
|
|
|
|
def verify_gen(caveat):
|
2015-08-19 15:30:31 +02:00
|
|
|
return caveat == "gen = 1"
|
2015-08-18 15:22:02 +02:00
|
|
|
|
|
|
|
def verify_user(caveat):
|
2015-08-19 15:30:31 +02:00
|
|
|
return caveat == "user_id = a_user"
|
2015-08-18 15:22:02 +02:00
|
|
|
|
|
|
|
def verify_type(caveat):
|
2015-08-19 15:30:31 +02:00
|
|
|
return caveat == "type = access"
|
2015-08-18 15:22:02 +02:00
|
|
|
|
2016-11-28 10:55:21 +01:00
|
|
|
def verify_nonce(caveat):
|
|
|
|
return caveat.startswith("nonce =")
|
2015-08-18 15:22:02 +02:00
|
|
|
|
2021-05-12 16:04:51 +02:00
|
|
|
def verify_guest(caveat):
|
|
|
|
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
|
|
|
|
|
|
|
def test_short_term_login_token_gives_user_id(self):
|
2021-03-04 15:44:22 +01:00
|
|
|
token = self.macaroon_generator.generate_short_term_login_token(
|
2021-05-12 16:04:51 +02:00
|
|
|
self.user1, "", 5000
|
2016-08-08 17:34:07 +02:00
|
|
|
)
|
2021-03-04 15:44:22 +01:00
|
|
|
res = self.get_success(self.auth_handler.validate_short_term_login_token(token))
|
2021-05-12 16:04:51 +02:00
|
|
|
self.assertEqual(self.user1, res.user_id)
|
2021-03-04 15:44:22 +01:00
|
|
|
self.assertEqual("", res.auth_provider_id)
|
2016-08-08 17:34:07 +02:00
|
|
|
|
|
|
|
# when we advance the clock, the token should be rejected
|
2021-02-11 16:29:09 +01:00
|
|
|
self.reactor.advance(6)
|
|
|
|
self.get_failure(
|
2021-03-04 15:44:22 +01:00
|
|
|
self.auth_handler.validate_short_term_login_token(token),
|
2021-02-11 16:29:09 +01:00
|
|
|
AuthError,
|
|
|
|
)
|
2016-08-08 17:34:07 +02:00
|
|
|
|
2021-03-04 15:44:22 +01:00
|
|
|
def test_short_term_login_token_gives_auth_provider(self):
|
|
|
|
token = self.macaroon_generator.generate_short_term_login_token(
|
2021-05-12 16:04:51 +02:00
|
|
|
self.user1, auth_provider_id="my_idp"
|
2021-03-04 15:44:22 +01:00
|
|
|
)
|
|
|
|
res = self.get_success(self.auth_handler.validate_short_term_login_token(token))
|
2021-05-12 16:04:51 +02:00
|
|
|
self.assertEqual(self.user1, res.user_id)
|
2021-03-04 15:44:22 +01:00
|
|
|
self.assertEqual("my_idp", res.auth_provider_id)
|
|
|
|
|
2016-08-08 17:34:07 +02:00
|
|
|
def test_short_term_login_token_cannot_replace_user_id(self):
|
2021-03-04 15:44:22 +01:00
|
|
|
token = self.macaroon_generator.generate_short_term_login_token(
|
2021-05-12 16:04:51 +02:00
|
|
|
self.user1, "", 5000
|
2021-03-04 15:44:22 +01:00
|
|
|
)
|
2016-08-08 17:34:07 +02:00
|
|
|
macaroon = pymacaroons.Macaroon.deserialize(token)
|
|
|
|
|
2021-03-04 15:44:22 +01:00
|
|
|
res = self.get_success(
|
|
|
|
self.auth_handler.validate_short_term_login_token(macaroon.serialize())
|
2018-08-01 11:21:56 +02:00
|
|
|
)
|
2021-05-12 16:04:51 +02:00
|
|
|
self.assertEqual(self.user1, res.user_id)
|
2016-08-08 17:34:07 +02:00
|
|
|
|
|
|
|
# add another "user_id" caveat, which might allow us to override the
|
|
|
|
# user_id.
|
|
|
|
macaroon.add_first_party_caveat("user_id = b_user")
|
|
|
|
|
2021-02-11 16:29:09 +01:00
|
|
|
self.get_failure(
|
2021-03-04 15:44:22 +01:00
|
|
|
self.auth_handler.validate_short_term_login_token(macaroon.serialize()),
|
2021-02-11 16:29:09 +01:00
|
|
|
AuthError,
|
|
|
|
)
|
2018-07-30 16:55:57 +02:00
|
|
|
|
|
|
|
def test_mau_limits_disabled(self):
|
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(
|
2020-04-15 18:40:18 +02:00
|
|
|
self.auth_handler.get_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
|
|
|
|
2021-02-11 16:29:09 +01:00
|
|
|
self.get_success(
|
2021-03-04 15:44:22 +01:00
|
|
|
self.auth_handler.validate_short_term_login_token(
|
2020-04-15 18:40:18 +02:00
|
|
|
self._get_macaroon().serialize()
|
|
|
|
)
|
2018-07-30 16:55:57 +02:00
|
|
|
)
|
|
|
|
|
2018-08-14 16:04:48 +02:00
|
|
|
def test_mau_limits_exceeded_large(self):
|
2020-05-06 16:54:58 +02:00
|
|
|
self.auth_blocking._limit_usage_by_mau = True
|
2018-08-02 17:57:35 +02:00
|
|
|
self.hs.get_datastore().get_monthly_active_count = Mock(
|
2020-09-08 13:26:55 +02:00
|
|
|
return_value=make_awaitable(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(
|
|
|
|
self.auth_handler.get_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
|
|
|
|
2018-08-02 17:57:35 +02:00
|
|
|
self.hs.get_datastore().get_monthly_active_count = Mock(
|
2020-09-08 13:26:55 +02:00
|
|
|
return_value=make_awaitable(self.large_number_of_users)
|
2018-08-01 11:21:56 +02:00
|
|
|
)
|
2021-02-11 16:29:09 +01:00
|
|
|
self.get_failure(
|
2021-03-04 15:44:22 +01:00
|
|
|
self.auth_handler.validate_short_term_login_token(
|
2021-02-11 16:29:09 +01:00
|
|
|
self._get_macaroon().serialize()
|
|
|
|
),
|
|
|
|
ResourceLimitError,
|
|
|
|
)
|
2018-07-30 16:55:57 +02:00
|
|
|
|
2018-08-14 16:04:48 +02:00
|
|
|
def test_mau_limits_parity(self):
|
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.
|
2018-08-14 16:04:48 +02:00
|
|
|
self.hs.get_datastore().get_monthly_active_count = Mock(
|
2020-09-08 13:26:55 +02:00
|
|
|
return_value=make_awaitable(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(
|
|
|
|
self.auth_handler.get_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
|
|
|
)
|
2021-02-11 16:29:09 +01:00
|
|
|
self.get_failure(
|
2021-03-04 15:44:22 +01:00
|
|
|
self.auth_handler.validate_short_term_login_token(
|
2021-02-11 16:29:09 +01:00
|
|
|
self._get_macaroon().serialize()
|
|
|
|
),
|
|
|
|
ResourceLimitError,
|
|
|
|
)
|
|
|
|
|
2018-08-14 16:04:48 +02:00
|
|
|
# If in monthly active cohort
|
|
|
|
self.hs.get_datastore().user_last_seen_monthly_active = Mock(
|
2021-02-11 16:29:09 +01:00
|
|
|
return_value=make_awaitable(self.clock.time_msec())
|
2018-08-14 16:04:48 +02:00
|
|
|
)
|
2021-02-11 16:29:09 +01:00
|
|
|
self.get_success(
|
2020-04-15 18:40:18 +02:00
|
|
|
self.auth_handler.get_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
|
|
|
)
|
2021-02-11 16:29:09 +01:00
|
|
|
self.get_success(
|
2021-03-04 15:44:22 +01:00
|
|
|
self.auth_handler.validate_short_term_login_token(
|
2020-04-15 18:40:18 +02:00
|
|
|
self._get_macaroon().serialize()
|
|
|
|
)
|
2018-08-14 16:04:48 +02:00
|
|
|
)
|
|
|
|
|
2018-07-30 16:55:57 +02:00
|
|
|
def test_mau_limits_not_exceeded(self):
|
2020-05-06 16:54:58 +02:00
|
|
|
self.auth_blocking._limit_usage_by_mau = True
|
2018-08-01 11:21:56 +02:00
|
|
|
|
2018-08-02 17:57:35 +02:00
|
|
|
self.hs.get_datastore().get_monthly_active_count = Mock(
|
2020-09-08 13:26:55 +02:00
|
|
|
return_value=make_awaitable(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(
|
2020-04-15 18:40:18 +02:00
|
|
|
self.auth_handler.get_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
|
|
|
|
2018-08-02 17:57:35 +02:00
|
|
|
self.hs.get_datastore().get_monthly_active_count = Mock(
|
2020-09-08 13:26:55 +02:00
|
|
|
return_value=make_awaitable(self.small_number_of_users)
|
2018-08-01 15:02:10 +02:00
|
|
|
)
|
2021-02-11 16:29:09 +01:00
|
|
|
self.get_success(
|
2021-03-04 15:44:22 +01:00
|
|
|
self.auth_handler.validate_short_term_login_token(
|
2020-04-15 18:40:18 +02:00
|
|
|
self._get_macaroon().serialize()
|
|
|
|
)
|
2018-07-30 16:55:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def _get_macaroon(self):
|
2021-03-04 15:44:22 +01:00
|
|
|
token = self.macaroon_generator.generate_short_term_login_token(
|
2021-05-12 16:04:51 +02:00
|
|
|
self.user1, "", 5000
|
2021-03-04 15:44:22 +01:00
|
|
|
)
|
2018-07-30 16:55:57 +02:00
|
|
|
return pymacaroons.Macaroon.deserialize(token)
|