2015-08-18 15:22:02 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
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.
|
|
|
|
|
|
|
|
import pymacaroons
|
2016-08-08 17:34:07 +02:00
|
|
|
from twisted.internet import defer
|
2015-08-18 15:22:02 +02:00
|
|
|
|
2016-08-08 17:34:07 +02:00
|
|
|
import synapse
|
|
|
|
import synapse.api.errors
|
2015-08-20 12:35:56 +02:00
|
|
|
from synapse.handlers.auth import AuthHandler
|
2015-08-18 15:22:02 +02:00
|
|
|
from tests import unittest
|
|
|
|
from tests.utils import setup_test_homeserver
|
|
|
|
|
2016-08-08 18:20:38 +02:00
|
|
|
|
2015-08-20 12:35:56 +02:00
|
|
|
class AuthHandlers(object):
|
2015-08-18 15:22:02 +02:00
|
|
|
def __init__(self, hs):
|
2015-08-20 12:35:56 +02:00
|
|
|
self.auth_handler = AuthHandler(hs)
|
2015-08-18 15:22:02 +02:00
|
|
|
|
|
|
|
|
2015-08-20 12:35:56 +02:00
|
|
|
class AuthTestCase(unittest.TestCase):
|
2015-08-18 15:22:02 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def setUp(self):
|
|
|
|
self.hs = yield setup_test_homeserver(handlers=None)
|
2015-08-20 12:35:56 +02:00
|
|
|
self.hs.handlers = AuthHandlers(self.hs)
|
2016-08-08 17:34:07 +02:00
|
|
|
self.auth_handler = self.hs.handlers.auth_handler
|
2017-02-02 11:53:36 +01:00
|
|
|
self.macaroon_generator = self.hs.get_macaroon_generator()
|
2015-08-18 15:22:02 +02:00
|
|
|
|
|
|
|
def test_token_is_a_macaroon(self):
|
2017-02-02 11:53:36 +01:00
|
|
|
token = self.macaroon_generator.generate_access_token("some_user")
|
2015-08-18 15:22:02 +02: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):
|
|
|
|
self.hs.clock.now = 5000
|
|
|
|
|
2017-02-02 11:53:36 +01:00
|
|
|
token = self.macaroon_generator.generate_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
|
|
|
|
|
|
|
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)
|
2015-08-18 16:18:50 +02:00
|
|
|
v.verify(macaroon, self.hs.config.macaroon_secret_key)
|
2016-08-08 17:34:07 +02:00
|
|
|
|
|
|
|
def test_short_term_login_token_gives_user_id(self):
|
|
|
|
self.hs.clock.now = 1000
|
|
|
|
|
2017-02-02 11:53:36 +01:00
|
|
|
token = self.macaroon_generator.generate_short_term_login_token(
|
2016-08-08 17:34:07 +02:00
|
|
|
"a_user", 5000
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
"a_user",
|
|
|
|
self.auth_handler.validate_short_term_login_token_and_get_user_id(
|
|
|
|
token
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# when we advance the clock, the token should be rejected
|
|
|
|
self.hs.clock.now = 6000
|
|
|
|
with self.assertRaises(synapse.api.errors.AuthError):
|
|
|
|
self.auth_handler.validate_short_term_login_token_and_get_user_id(
|
|
|
|
token
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_short_term_login_token_cannot_replace_user_id(self):
|
2017-02-02 11:53:36 +01:00
|
|
|
token = self.macaroon_generator.generate_short_term_login_token(
|
2016-08-08 17:34:07 +02:00
|
|
|
"a_user", 5000
|
|
|
|
)
|
|
|
|
macaroon = pymacaroons.Macaroon.deserialize(token)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
"a_user",
|
|
|
|
self.auth_handler.validate_short_term_login_token_and_get_user_id(
|
|
|
|
macaroon.serialize()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# 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):
|
|
|
|
self.auth_handler.validate_short_term_login_token_and_get_user_id(
|
|
|
|
macaroon.serialize()
|
|
|
|
)
|