From 2d3462714e48dca46dd54b17ca29188a17261e28 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Tue, 18 Aug 2015 14:22:02 +0100 Subject: Issue macaroons as opaque auth tokens This just replaces random bytes with macaroons. The macaroons are not inspected by the client or server. In particular, they claim to have an expiry time, but nothing verifies that they have not expired. Follow-up commits will actually enforce the expiration, and allow for token refresh. See https://bit.ly/matrix-auth for more information --- synapse/handlers/register.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'synapse/handlers') diff --git a/synapse/handlers/register.py b/synapse/handlers/register.py index 39392d9fdd..86bacdda1d 100644 --- a/synapse/handlers/register.py +++ b/synapse/handlers/register.py @@ -25,9 +25,9 @@ import synapse.util.stringutils as stringutils from synapse.util.async import run_on_reactor from synapse.http.client import CaptchaServerHttpClient -import base64 import bcrypt import logging +import pymacaroons import urllib logger = logging.getLogger(__name__) @@ -274,11 +274,18 @@ class RegistrationHandler(BaseHandler): ) def generate_token(self, user_id): - # urlsafe variant uses _ and - so use . as the separator and replace - # all =s with .s so http clients don't quote =s when it is used as - # query params. - return (base64.urlsafe_b64encode(user_id).replace('=', '.') + '.' + - stringutils.random_string(18)) + macaroon = pymacaroons.Macaroon( + location = self.hs.config.server_name, + identifier = "key", + key = self.hs.config.macaroon_secret_key) + macaroon.add_first_party_caveat("gen = 1") + macaroon.add_first_party_caveat("user_id = %s" % user_id) + macaroon.add_first_party_caveat("type = access") + now = self.hs.get_clock().time() + expiry = now + 60 * 60 + macaroon.add_first_party_caveat("time < %s" % expiry) + + return macaroon.serialize() def _generate_user_id(self): return "-" + stringutils.random_string(18) -- cgit 1.4.1 From 3e6fdfda002de6971b74aba7805ebdeb2b1b426d Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Tue, 18 Aug 2015 15:18:50 +0100 Subject: Fix some formatting to use tuples --- synapse/handlers/register.py | 8 ++++---- tests/handlers/test_register.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'synapse/handlers') diff --git a/synapse/handlers/register.py b/synapse/handlers/register.py index 86bacdda1d..c391c1bdf5 100644 --- a/synapse/handlers/register.py +++ b/synapse/handlers/register.py @@ -279,11 +279,11 @@ class RegistrationHandler(BaseHandler): identifier = "key", key = self.hs.config.macaroon_secret_key) macaroon.add_first_party_caveat("gen = 1") - macaroon.add_first_party_caveat("user_id = %s" % user_id) + macaroon.add_first_party_caveat("user_id = %s" % (user_id,)) macaroon.add_first_party_caveat("type = access") - now = self.hs.get_clock().time() - expiry = now + 60 * 60 - macaroon.add_first_party_caveat("time < %s" % expiry) + now = self.hs.get_clock().time_msec() + expiry = now + (60 * 60 * 1000) + macaroon.add_first_party_caveat("time < %d" % (expiry,)) return macaroon.serialize() diff --git a/tests/handlers/test_register.py b/tests/handlers/test_register.py index b28b1a7ef0..0766affe81 100644 --- a/tests/handlers/test_register.py +++ b/tests/handlers/test_register.py @@ -67,4 +67,4 @@ class RegisterTestCase(unittest.TestCase): v.satisfy_general(verify_user) v.satisfy_general(verify_type) v.satisfy_general(verify_expiry) - v.verify(macaroon, self.hs.config.macaroon_secret_key) \ No newline at end of file + v.verify(macaroon, self.hs.config.macaroon_secret_key) -- cgit 1.4.1 From ce832c38d4ba1412cd5b5f8a4fb9328cb2d444fa Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Tue, 18 Aug 2015 17:39:26 +0100 Subject: Remove padding space around caveat operators --- synapse/handlers/register.py | 8 ++++---- tests/handlers/test_register.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'synapse/handlers') diff --git a/synapse/handlers/register.py b/synapse/handlers/register.py index c391c1bdf5..557aec4e6c 100644 --- a/synapse/handlers/register.py +++ b/synapse/handlers/register.py @@ -278,12 +278,12 @@ class RegistrationHandler(BaseHandler): location = self.hs.config.server_name, identifier = "key", key = self.hs.config.macaroon_secret_key) - macaroon.add_first_party_caveat("gen = 1") - macaroon.add_first_party_caveat("user_id = %s" % (user_id,)) - macaroon.add_first_party_caveat("type = access") + macaroon.add_first_party_caveat("gen=1") + macaroon.add_first_party_caveat("user_id=%s" % (user_id,)) + macaroon.add_first_party_caveat("type=access") now = self.hs.get_clock().time_msec() expiry = now + (60 * 60 * 1000) - macaroon.add_first_party_caveat("time < %d" % (expiry,)) + macaroon.add_first_party_caveat("time<%d" % (expiry,)) return macaroon.serialize() diff --git a/tests/handlers/test_register.py b/tests/handlers/test_register.py index 91cc90242f..18507c547d 100644 --- a/tests/handlers/test_register.py +++ b/tests/handlers/test_register.py @@ -51,16 +51,16 @@ class RegisterTestCase(unittest.TestCase): macaroon = pymacaroons.Macaroon.deserialize(token) def verify_gen(caveat): - return caveat == "gen = 1" + return caveat == "gen=1" def verify_user(caveat): - return caveat == "user_id = a_user" + return caveat == "user_id=a_user" def verify_type(caveat): - return caveat == "type = access" + return caveat == "type=access" def verify_expiry(caveat): - return caveat == "time < 8600000" + return caveat == "time<8600000" v = pymacaroons.Verifier() v.satisfy_general(verify_gen) -- cgit 1.4.1 From 70e265e695a67a412b5ac76cc9bae71e9d384e80 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Wed, 19 Aug 2015 14:30:31 +0100 Subject: Re-add whitespace around caveat operators --- synapse/handlers/register.py | 8 ++++---- tests/handlers/test_register.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'synapse/handlers') diff --git a/synapse/handlers/register.py b/synapse/handlers/register.py index 557aec4e6c..c391c1bdf5 100644 --- a/synapse/handlers/register.py +++ b/synapse/handlers/register.py @@ -278,12 +278,12 @@ class RegistrationHandler(BaseHandler): location = self.hs.config.server_name, identifier = "key", key = self.hs.config.macaroon_secret_key) - macaroon.add_first_party_caveat("gen=1") - macaroon.add_first_party_caveat("user_id=%s" % (user_id,)) - macaroon.add_first_party_caveat("type=access") + macaroon.add_first_party_caveat("gen = 1") + macaroon.add_first_party_caveat("user_id = %s" % (user_id,)) + macaroon.add_first_party_caveat("type = access") now = self.hs.get_clock().time_msec() expiry = now + (60 * 60 * 1000) - macaroon.add_first_party_caveat("time<%d" % (expiry,)) + macaroon.add_first_party_caveat("time < %d" % (expiry,)) return macaroon.serialize() diff --git a/tests/handlers/test_register.py b/tests/handlers/test_register.py index 18507c547d..91cc90242f 100644 --- a/tests/handlers/test_register.py +++ b/tests/handlers/test_register.py @@ -51,16 +51,16 @@ class RegisterTestCase(unittest.TestCase): macaroon = pymacaroons.Macaroon.deserialize(token) def verify_gen(caveat): - return caveat == "gen=1" + return caveat == "gen = 1" def verify_user(caveat): - return caveat == "user_id=a_user" + return caveat == "user_id = a_user" def verify_type(caveat): - return caveat == "type=access" + return caveat == "type = access" def verify_expiry(caveat): - return caveat == "time<8600000" + return caveat == "time < 8600000" v = pymacaroons.Verifier() v.satisfy_general(verify_gen) -- cgit 1.4.1