Skip to content

Commit

Permalink
Add server-wide registration-token support
Browse files Browse the repository at this point in the history
  • Loading branch information
gnunicorn committed Nov 15, 2023
1 parent 9177662 commit 1fb6927
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion synapse_super_invites/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ def run_alembic(engine):
@attr.define
class SynapseSuperInvitesConfig:
sql_url: str
generate_registration_token : bool = attr.field(default = True)
generate_registration_token : bool = attr.field(default = False)

7 changes: 7 additions & 0 deletions synapse_super_invites/resource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,16 @@ async def _async_render_POST(self, request: SynapseRequest) -> Tuple[int, JsonDi
token = Token(token=token_id, create_dm=create_dm, owner=str(requester.user), rooms=rooms)
session.add(token)


session.flush()

token_data = serialize_token(token)

if self.config.generate_registration_token:
token_id = token_data["token"]
# FIXME: it'd be great if we didn't have to resort to using internal args...
if not (await self.api._store.registration_token_is_valid(token_id)):
await self.api._store.create_registration_token(token=token_id, uses_allowed=None, expiry_time=None)

return 200, {"token": token_data}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ConfigParsingTests(TestCase):

def test_default_config(self) -> None:
config = SynapseSuperInvites.parse_config(DEFAULT_CONFIG)
self.assertEqual(config.generate_registration_token, True)
self.assertEqual(config.generate_registration_token, False)

def test_fails_no_sql(self) -> None:
with self.assertRaises(ConfigError):
Expand Down
21 changes: 17 additions & 4 deletions tests/test_integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from synapse.types import JsonDict, Dict
from synapse.server import HomeServer
from synapse.util import Clock
from synapse.rest.client import login, room, profile, sync
from synapse.rest.client import login, room, profile, sync, register
from synapse.rest import admin
from twisted.web.resource import Resource

Expand All @@ -16,7 +16,6 @@
from .test_config import DEFAULT_CONFIG as DEFAULT_MODULE_CFG

DEFAULT_CONFIG = {

"modules": [{
"module": "synapse_super_invites.SynapseSuperInvites",
"config": DEFAULT_MODULE_CFG
Expand All @@ -34,6 +33,7 @@ class SuperInviteHomeserverTestCase(HomeserverTestCase):
room.register_servlets,
profile.register_servlets,
sync.register_servlets,
register.register_servlets,
]


Expand Down Expand Up @@ -149,7 +149,17 @@ def test_simple_invite_token_test(self) -> None:
self.assertCountEqual(channel.json_body["rooms"]["invite"].keys(), rooms_to_invite)


@override_config(DEFAULT_CONFIG)
@override_config({
"enable_registration": True,
"registration_requires_token": True,
"modules": [{
"module": "synapse_super_invites.SynapseSuperInvites",
"config": {
"sql_url": "sqlite:///",
"generate_registration_token": True,
}
}]
})
def test_simple_invite_as_registration_token_test(self) -> None:
m_id = self.register_user("meeko", "password")
m_access_token = self.login("meeko", "password")
Expand All @@ -164,4 +174,7 @@ def test_simple_invite_as_registration_token_test(self) -> None:
self.assertEqual(channel.code, 200, msg=channel.result)
token = channel.json_body["token"]

# FIXME: trying to register with that new token
# let's see if the token exists and is valid
channel = self.make_request("GET", "/_matrix/client/v1/register/m.login.registration_token/validity?token={token}".format(token=token["token"]))
self.assertEqual(channel.code, 200, msg=channel.result)
self.assertTrue(channel.json_body["valid"])

0 comments on commit 1fb6927

Please sign in to comment.