2015-08-18 07:22:02 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2015-08-18 07:22:02 -06: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.
|
2018-07-30 08:55:57 -06:00
|
|
|
from mock import Mock
|
2018-07-31 06:16:20 -06:00
|
|
|
|
2015-08-18 07:22:02 -06:00
|
|
|
import pymacaroons
|
2018-07-09 00:09:20 -06:00
|
|
|
|
2016-08-08 09:34:07 -06:00
|
|
|
from twisted.internet import defer
|
2015-08-18 07:22:02 -06:00
|
|
|
|
2016-08-08 09:34:07 -06:00
|
|
|
import synapse
|
|
|
|
import synapse.api.errors
|
2018-08-16 11:02:02 -06:00
|
|
|
from synapse.api.errors import ResourceLimitError
|
2018-07-09 00:09:20 -06:00
|
|
|
|
2015-08-18 07:22:02 -06:00
|
|
|
from tests import unittest
|
2020-08-27 15:24:37 -06:00
|
|
|
from tests.test_utils import make_awaitable
|
2015-08-18 07:22:02 -06:00
|
|
|
from tests.utils import setup_test_homeserver
|
|
|
|
|
2016-08-08 10:20:38 -06:00
|
|
|
|
2015-08-20 04:35:56 -06:00
|
|
|
class AuthTestCase(unittest.TestCase):
|
2015-08-18 07:22:02 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def setUp(self):
|
2020-10-09 05:24:34 -06:00
|
|
|
self.hs = yield setup_test_homeserver(self.addCleanup)
|
|
|
|
self.auth_handler = self.hs.get_auth_handler()
|
2017-02-02 03:53:36 -07:00
|
|
|
self.macaroon_generator = self.hs.get_macaroon_generator()
|
2020-05-06 08:54:58 -06:00
|
|
|
|
2018-07-30 08:55:57 -06:00
|
|
|
# MAU tests
|
2020-05-06 08:54:58 -06:00
|
|
|
# AuthBlocking reads from the hs' config on initialization. We need to
|
|
|
|
# modify its config instead of the hs'
|
|
|
|
self.auth_blocking = self.hs.get_auth()._auth_blocking
|
|
|
|
self.auth_blocking._max_mau_value = 50
|
|
|
|
|
2018-07-30 08:55:57 -06:00
|
|
|
self.small_number_of_users = 1
|
|
|
|
self.large_number_of_users = 100
|
2015-08-18 07:22:02 -06:00
|
|
|
|
|
|
|
def test_token_is_a_macaroon(self):
|
2017-02-02 03:53:36 -07:00
|
|
|
token = self.macaroon_generator.generate_access_token("some_user")
|
2015-08-18 07:22:02 -06:00
|
|
|
# Check that we can parse the thing with pymacaroons
|
|
|
|
macaroon = pymacaroons.Macaroon.deserialize(token)
|
|
|
|
# The most basic of sanity checks
|
|
|
|
if "some_user" not in macaroon.inspect():
|
|
|
|
self.fail("some_user was not in %s" % macaroon.inspect())
|
|
|
|
|
|
|
|
def test_macaroon_caveats(self):
|
2020-11-30 11:28:44 -07:00
|
|
|
self.hs.get_clock().now = 5000
|
2015-08-18 07:22:02 -06:00
|
|
|
|
2017-02-02 03:53:36 -07:00
|
|
|
token = self.macaroon_generator.generate_access_token("a_user")
|
2015-08-18 07:22:02 -06:00
|
|
|
macaroon = pymacaroons.Macaroon.deserialize(token)
|
|
|
|
|
|
|
|
def verify_gen(caveat):
|
2015-08-19 07:30:31 -06:00
|
|
|
return caveat == "gen = 1"
|
2015-08-18 07:22:02 -06:00
|
|
|
|
|
|
|
def verify_user(caveat):
|
2015-08-19 07:30:31 -06:00
|
|
|
return caveat == "user_id = a_user"
|
2015-08-18 07:22:02 -06:00
|
|
|
|
|
|
|
def verify_type(caveat):
|
2015-08-19 07:30:31 -06:00
|
|
|
return caveat == "type = access"
|
2015-08-18 07:22:02 -06:00
|
|
|
|
2016-11-28 02:55:21 -07:00
|
|
|
def verify_nonce(caveat):
|
|
|
|
return caveat.startswith("nonce =")
|
2015-08-18 07:22:02 -06:00
|
|
|
|
|
|
|
v = pymacaroons.Verifier()
|
|
|
|
v.satisfy_general(verify_gen)
|
|
|
|
v.satisfy_general(verify_user)
|
|
|
|
v.satisfy_general(verify_type)
|
2016-11-28 02:55:21 -07:00
|
|
|
v.satisfy_general(verify_nonce)
|
2015-08-18 08:18:50 -06:00
|
|
|
v.verify(macaroon, self.hs.config.macaroon_secret_key)
|
2016-08-08 09:34:07 -06:00
|
|
|
|
2018-08-01 03:21:56 -06:00
|
|
|
@defer.inlineCallbacks
|
2016-08-08 09:34:07 -06:00
|
|
|
def test_short_term_login_token_gives_user_id(self):
|
2020-11-30 11:28:44 -07:00
|
|
|
self.hs.get_clock().now = 1000
|
2016-08-08 09:34:07 -06:00
|
|
|
|
2018-08-10 07:54:09 -06:00
|
|
|
token = self.macaroon_generator.generate_short_term_login_token("a_user", 5000)
|
2020-04-15 10:40:18 -06:00
|
|
|
user_id = yield defer.ensureDeferred(
|
|
|
|
self.auth_handler.validate_short_term_login_token_and_get_user_id(token)
|
2016-08-08 09:34:07 -06:00
|
|
|
)
|
2018-08-01 03:21:56 -06:00
|
|
|
self.assertEqual("a_user", user_id)
|
2016-08-08 09:34:07 -06:00
|
|
|
|
|
|
|
# when we advance the clock, the token should be rejected
|
2020-11-30 11:28:44 -07:00
|
|
|
self.hs.get_clock().now = 6000
|
2016-08-08 09:34:07 -06:00
|
|
|
with self.assertRaises(synapse.api.errors.AuthError):
|
2020-04-15 10:40:18 -06:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.auth_handler.validate_short_term_login_token_and_get_user_id(token)
|
2016-08-08 09:34:07 -06:00
|
|
|
)
|
|
|
|
|
2018-08-01 03:21:56 -06:00
|
|
|
@defer.inlineCallbacks
|
2016-08-08 09:34:07 -06:00
|
|
|
def test_short_term_login_token_cannot_replace_user_id(self):
|
2018-08-10 07:54:09 -06:00
|
|
|
token = self.macaroon_generator.generate_short_term_login_token("a_user", 5000)
|
2016-08-08 09:34:07 -06:00
|
|
|
macaroon = pymacaroons.Macaroon.deserialize(token)
|
|
|
|
|
2020-04-15 10:40:18 -06:00
|
|
|
user_id = yield defer.ensureDeferred(
|
|
|
|
self.auth_handler.validate_short_term_login_token_and_get_user_id(
|
|
|
|
macaroon.serialize()
|
|
|
|
)
|
2018-08-01 03:21:56 -06:00
|
|
|
)
|
2018-08-10 07:54:09 -06:00
|
|
|
self.assertEqual("a_user", user_id)
|
2016-08-08 09:34:07 -06:00
|
|
|
|
|
|
|
# add another "user_id" caveat, which might allow us to override the
|
|
|
|
# user_id.
|
|
|
|
macaroon.add_first_party_caveat("user_id = b_user")
|
|
|
|
|
|
|
|
with self.assertRaises(synapse.api.errors.AuthError):
|
2020-04-15 10:40:18 -06:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.auth_handler.validate_short_term_login_token_and_get_user_id(
|
|
|
|
macaroon.serialize()
|
|
|
|
)
|
2016-08-08 09:34:07 -06:00
|
|
|
)
|
2018-07-30 08:55:57 -06:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_mau_limits_disabled(self):
|
2020-05-06 08:54:58 -06:00
|
|
|
self.auth_blocking._limit_usage_by_mau = False
|
2018-07-30 08:55:57 -06:00
|
|
|
# Ensure does not throw exception
|
2020-04-15 10:40:18 -06:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.auth_handler.get_access_token_for_user_id(
|
|
|
|
"user_a", device_id=None, valid_until_ms=None
|
|
|
|
)
|
2019-07-12 10:26:02 -06:00
|
|
|
)
|
2018-07-30 08:55:57 -06:00
|
|
|
|
2020-04-15 10:40:18 -06:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.auth_handler.validate_short_term_login_token_and_get_user_id(
|
|
|
|
self._get_macaroon().serialize()
|
|
|
|
)
|
2018-07-30 08:55:57 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-08-14 08:04:48 -06:00
|
|
|
def test_mau_limits_exceeded_large(self):
|
2020-05-06 08:54:58 -06:00
|
|
|
self.auth_blocking._limit_usage_by_mau = True
|
2018-08-02 09:57:35 -06:00
|
|
|
self.hs.get_datastore().get_monthly_active_count = Mock(
|
2020-09-08 05:26:55 -06:00
|
|
|
return_value=make_awaitable(self.large_number_of_users)
|
2018-07-30 08:55:57 -06:00
|
|
|
)
|
2018-08-01 03:21:56 -06:00
|
|
|
|
2018-08-16 11:02:02 -06:00
|
|
|
with self.assertRaises(ResourceLimitError):
|
2020-04-15 10:40:18 -06:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.auth_handler.get_access_token_for_user_id(
|
|
|
|
"user_a", device_id=None, valid_until_ms=None
|
|
|
|
)
|
2019-07-12 10:26:02 -06:00
|
|
|
)
|
2018-08-01 03:21:56 -06:00
|
|
|
|
2018-08-02 09:57:35 -06:00
|
|
|
self.hs.get_datastore().get_monthly_active_count = Mock(
|
2020-09-08 05:26:55 -06:00
|
|
|
return_value=make_awaitable(self.large_number_of_users)
|
2018-08-01 03:21:56 -06:00
|
|
|
)
|
2018-08-16 11:02:02 -06:00
|
|
|
with self.assertRaises(ResourceLimitError):
|
2020-04-15 10:40:18 -06:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.auth_handler.validate_short_term_login_token_and_get_user_id(
|
|
|
|
self._get_macaroon().serialize()
|
|
|
|
)
|
2018-07-30 08:55:57 -06:00
|
|
|
)
|
|
|
|
|
2018-08-14 08:04:48 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_mau_limits_parity(self):
|
2020-05-06 08:54:58 -06:00
|
|
|
self.auth_blocking._limit_usage_by_mau = True
|
2018-08-14 08:04:48 -06:00
|
|
|
|
|
|
|
# If not in monthly active cohort
|
|
|
|
self.hs.get_datastore().get_monthly_active_count = Mock(
|
2020-09-08 05:26:55 -06:00
|
|
|
return_value=make_awaitable(self.auth_blocking._max_mau_value)
|
2018-08-14 08:04:48 -06:00
|
|
|
)
|
2018-08-16 11:02:02 -06:00
|
|
|
with self.assertRaises(ResourceLimitError):
|
2020-04-15 10:40:18 -06:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.auth_handler.get_access_token_for_user_id(
|
|
|
|
"user_a", device_id=None, valid_until_ms=None
|
|
|
|
)
|
2019-07-12 10:26:02 -06:00
|
|
|
)
|
2018-08-14 08:04:48 -06:00
|
|
|
|
|
|
|
self.hs.get_datastore().get_monthly_active_count = Mock(
|
2020-09-08 05:26:55 -06:00
|
|
|
return_value=make_awaitable(self.auth_blocking._max_mau_value)
|
2018-08-14 08:04:48 -06:00
|
|
|
)
|
2018-08-16 11:02:02 -06:00
|
|
|
with self.assertRaises(ResourceLimitError):
|
2020-04-15 10:40:18 -06:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.auth_handler.validate_short_term_login_token_and_get_user_id(
|
|
|
|
self._get_macaroon().serialize()
|
|
|
|
)
|
2018-08-14 08:04:48 -06:00
|
|
|
)
|
|
|
|
# If in monthly active cohort
|
|
|
|
self.hs.get_datastore().user_last_seen_monthly_active = Mock(
|
2020-09-08 05:26:55 -06:00
|
|
|
return_value=make_awaitable(self.hs.get_clock().time_msec())
|
2018-08-14 08:04:48 -06:00
|
|
|
)
|
|
|
|
self.hs.get_datastore().get_monthly_active_count = Mock(
|
2020-09-08 05:26:55 -06:00
|
|
|
return_value=make_awaitable(self.auth_blocking._max_mau_value)
|
2018-08-14 08:04:48 -06:00
|
|
|
)
|
2020-04-15 10:40:18 -06:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.auth_handler.get_access_token_for_user_id(
|
|
|
|
"user_a", device_id=None, valid_until_ms=None
|
|
|
|
)
|
2019-07-12 10:26:02 -06:00
|
|
|
)
|
2018-08-14 08:04:48 -06:00
|
|
|
self.hs.get_datastore().user_last_seen_monthly_active = Mock(
|
2020-09-08 05:26:55 -06:00
|
|
|
return_value=make_awaitable(self.hs.get_clock().time_msec())
|
2018-08-14 08:04:48 -06:00
|
|
|
)
|
|
|
|
self.hs.get_datastore().get_monthly_active_count = Mock(
|
2020-09-08 05:26:55 -06:00
|
|
|
return_value=make_awaitable(self.auth_blocking._max_mau_value)
|
2018-08-14 08:04:48 -06:00
|
|
|
)
|
2020-04-15 10:40:18 -06:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.auth_handler.validate_short_term_login_token_and_get_user_id(
|
|
|
|
self._get_macaroon().serialize()
|
|
|
|
)
|
2018-08-14 08:04:48 -06:00
|
|
|
)
|
|
|
|
|
2018-07-30 08:55:57 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_mau_limits_not_exceeded(self):
|
2020-05-06 08:54:58 -06:00
|
|
|
self.auth_blocking._limit_usage_by_mau = True
|
2018-08-01 03:21:56 -06:00
|
|
|
|
2018-08-02 09:57:35 -06:00
|
|
|
self.hs.get_datastore().get_monthly_active_count = Mock(
|
2020-09-08 05:26:55 -06:00
|
|
|
return_value=make_awaitable(self.small_number_of_users)
|
2018-07-30 08:55:57 -06:00
|
|
|
)
|
|
|
|
# Ensure does not raise exception
|
2020-04-15 10:40:18 -06:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.auth_handler.get_access_token_for_user_id(
|
|
|
|
"user_a", device_id=None, valid_until_ms=None
|
|
|
|
)
|
2019-07-12 10:26:02 -06:00
|
|
|
)
|
2018-08-01 07:02:10 -06:00
|
|
|
|
2018-08-02 09:57:35 -06:00
|
|
|
self.hs.get_datastore().get_monthly_active_count = Mock(
|
2020-09-08 05:26:55 -06:00
|
|
|
return_value=make_awaitable(self.small_number_of_users)
|
2018-08-01 07:02:10 -06:00
|
|
|
)
|
2020-04-15 10:40:18 -06:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.auth_handler.validate_short_term_login_token_and_get_user_id(
|
|
|
|
self._get_macaroon().serialize()
|
|
|
|
)
|
2018-07-30 08:55:57 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
def _get_macaroon(self):
|
2018-08-10 07:54:09 -06:00
|
|
|
token = self.macaroon_generator.generate_short_term_login_token("user_a", 5000)
|
2018-07-30 08:55:57 -06:00
|
|
|
return pymacaroons.Macaroon.deserialize(token)
|