Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Move check_from_context to EventAuthHandler.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Jun 30, 2021
1 parent 6b05530 commit 7aa680d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
13 changes: 0 additions & 13 deletions synapse/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
InvalidClientTokenError,
MissingClientTokenError,
)
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
from synapse.appservice import ApplicationService
from synapse.events import EventBase
from synapse.http import get_request_user_agent
Expand Down Expand Up @@ -89,18 +88,6 @@ def __init__(self, hs: "HomeServer"):
self._macaroon_secret_key = hs.config.macaroon_secret_key
self._force_tracing_for_users = hs.config.tracing.force_tracing_for_users

async def check_from_context(

This comment was marked as spam.

Copy link
@mrymzf

mrymzf Jul 24, 2021

  • +989101404305
self, room_version: str, event, context, do_sig_check=True

This comment was marked as spam.

Copy link
@mrymzf
) -> None:
auth_event_ids = event.auth_event_ids()
auth_events_by_id = await self.store.get_events(auth_event_ids)
auth_events = {(e.type, e.state_key): e for e in auth_events_by_id.values()}

room_version_obj = KNOWN_ROOM_VERSIONS[room_version]
event_auth.check(
room_version_obj, event, auth_events=auth_events, do_sig_check=do_sig_check
)

async def check_user_in_room(
self,
room_id: str,
Expand Down
14 changes: 13 additions & 1 deletion synapse/handlers/event_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
RestrictedJoinRuleTypes,
)
from synapse.api.errors import AuthError
from synapse.api.room_versions import RoomVersion
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, RoomVersion
from synapse.events import EventBase
from synapse.events.builder import EventBuilder
from synapse.types import StateMap
Expand All @@ -38,6 +38,18 @@ class EventAuthHandler:
def __init__(self, hs: "HomeServer"):
self._store = hs.get_datastore()

async def check_from_context(
self, room_version: str, event, context, do_sig_check=True
) -> None:
auth_event_ids = event.auth_event_ids()
auth_events_by_id = await self._store.get_events(auth_event_ids)
auth_events = {(e.type, e.state_key): e for e in auth_events_by_id.values()}

room_version_obj = KNOWN_ROOM_VERSIONS[room_version]
event_auth.check(
room_version_obj, event, auth_events=auth_events, do_sig_check=do_sig_check
)

def compute_auth_events(
self,
event: Union[EventBase, EventBuilder],
Expand Down
14 changes: 9 additions & 5 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,7 @@ async def on_make_join_request(

# The remote hasn't signed it yet, obviously. We'll do the full checks
# when we get the event back in `on_send_join_request`
await self.auth.check_from_context(

This comment was marked as spam.

Copy link
@mrymzf

mrymzf Jul 24, 2021

1708

await self._event_auth_handler.check_from_context(
room_version, event, context, do_sig_check=False
)

Expand Down Expand Up @@ -1877,7 +1877,7 @@ async def on_make_leave_request(
try:
# The remote hasn't signed it yet, obviously. We'll do the full checks
# when we get the event back in `on_send_leave_request`
await self.auth.check_from_context(
await self._event_auth_handler.check_from_context(
room_version, event, context, do_sig_check=False
)
except AuthError as e:
Expand Down Expand Up @@ -1939,7 +1939,7 @@ async def on_make_knock_request(
try:
# The remote hasn't signed it yet, obviously. We'll do the full checks
# when we get the event back in `on_send_knock_request`
await self.auth.check_from_context(
await self._event_auth_handler.check_from_context(
room_version, event, context, do_sig_check=False
)
except AuthError as e:
Expand Down Expand Up @@ -3011,7 +3011,9 @@ async def exchange_third_party_invite(
event.internal_metadata.send_on_behalf_of = self.hs.hostname

try:
await self.auth.check_from_context(room_version, event, context)
await self._event_auth_handler.check_from_context(
room_version, event, context
)
except AuthError as e:
logger.warning("Denying new third party invite %r because %s", event, e)
raise e
Expand Down Expand Up @@ -3054,7 +3056,9 @@ async def on_exchange_third_party_invite_request(
)

try:
await self.auth.check_from_context(room_version, event, context)
await self._event_auth_handler.check_from_context(
room_version, event, context
)
except AuthError as e:
logger.warning("Denying third party invite %r because %s", event, e)
raise e
Expand Down
4 changes: 3 additions & 1 deletion synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,9 @@ async def handle_new_client_event(
assert event.content["membership"] == Membership.LEAVE
else:
try:
await self.auth.check_from_context(room_version, event, context)
await self._event_auth_handler.check_from_context(
room_version, event, context
)
except AuthError as err:
logger.warning("Denying new event %r because %s", event, err)
raise err
Expand Down
3 changes: 2 additions & 1 deletion synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def __init__(self, hs: "HomeServer"):
self.spam_checker = hs.get_spam_checker()
self.event_creation_handler = hs.get_event_creation_handler()
self.room_member_handler = hs.get_room_member_handler()
self._event_auth_handler = hs.get_event_auth_handler()
self.config = hs.config

# Room state based off defined presets
Expand Down Expand Up @@ -226,7 +227,7 @@ async def _upgrade_room(
},
)
old_room_version = await self.store.get_room_version_id(old_room_id)
await self.auth.check_from_context(
await self._event_auth_handler.check_from_context(
old_room_version, tombstone_event, tombstone_context
)

Expand Down

0 comments on commit 7aa680d

Please sign in to comment.