2018-10-19 20:16:55 -04:00
|
|
|
from __future__ import print_function
|
2015-05-06 06:46:02 -04:00
|
|
|
|
|
|
|
import argparse
|
|
|
|
import itertools
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
|
2018-10-19 20:16:55 -04:00
|
|
|
from mock import Mock
|
|
|
|
|
|
|
|
from synapse.api.auth import Auth
|
|
|
|
from synapse.events import FrozenEvent
|
|
|
|
|
2015-05-06 06:46:02 -04:00
|
|
|
|
|
|
|
def check_auth(auth, auth_chain, events):
|
|
|
|
auth_chain.sort(key=lambda e: e.depth)
|
|
|
|
|
2018-10-19 20:16:55 -04:00
|
|
|
auth_map = {e.event_id: e for e in auth_chain}
|
2015-05-06 06:46:02 -04:00
|
|
|
|
|
|
|
create_events = {}
|
|
|
|
for e in auth_chain:
|
|
|
|
if e.type == "m.room.create":
|
|
|
|
create_events[e.room_id] = e
|
|
|
|
|
|
|
|
for e in itertools.chain(auth_chain, events):
|
|
|
|
auth_events_list = [auth_map[i] for i, _ in e.auth_events]
|
|
|
|
|
2018-10-19 20:16:55 -04:00
|
|
|
auth_events = {(e.type, e.state_key): e for e in auth_events_list}
|
2015-05-06 06:46:02 -04:00
|
|
|
|
|
|
|
auth_events[("m.room.create", "")] = create_events[e.room_id]
|
|
|
|
|
|
|
|
try:
|
|
|
|
auth.check(e, auth_events=auth_events)
|
|
|
|
except Exception as ex:
|
2018-10-19 20:16:55 -04:00
|
|
|
print("Failed:", e.event_id, e.type, e.state_key)
|
|
|
|
print("Auth_events:", auth_events)
|
|
|
|
print(ex)
|
|
|
|
print(json.dumps(e.get_dict(), sort_keys=True, indent=4))
|
2015-05-06 06:46:02 -04:00
|
|
|
# raise
|
2018-10-19 20:16:55 -04:00
|
|
|
print("Success:", e.event_id, e.type, e.state_key)
|
|
|
|
|
2015-05-06 06:46:02 -04:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
if __name__ == "__main__":
|
2015-05-06 06:46:02 -04:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
|
|
|
parser.add_argument(
|
2019-06-20 05:32:02 -04:00
|
|
|
"json", nargs="?", type=argparse.FileType("r"), default=sys.stdin
|
2015-05-06 06:46:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
js = json.load(args.json)
|
|
|
|
|
|
|
|
auth = Auth(Mock())
|
|
|
|
check_auth(
|
|
|
|
auth,
|
|
|
|
[FrozenEvent(d) for d in js["auth_chain"]],
|
2015-09-09 08:47:14 -04:00
|
|
|
[FrozenEvent(d) for d in js.get("pdus", [])],
|
2015-05-06 06:46:02 -04:00
|
|
|
)
|