-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorators.py
39 lines (32 loc) · 1.19 KB
/
decorators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import functools
from flask import request
from config import config
# Flask decorators
def authenticated(func):
"""
Check that a request from the homeserver
to the bridge is authenticated correctly
"""
# https://spec.matrix.org/v1.6/application-service-api/#authorization
@functools.wraps(func)
def wrapped(*args, **kwargs):
if "Authorization" not in request.headers:
return {'errcode': 'edu.mit.sipb.zephyr_bridge_unauthorized'}, 401
token = request.headers["Authorization"].split(" ")[-1]
if token != config.hs_token:
return {'errcode': 'edu.mit.sipb.zephyr_bridge_forbidden'}, 403
return func(*args, **kwargs)
return wrapped
def idempotent(func):
"""
Makes sure this function call occurs only once,
by persisting the txnId after successfully completing
[bridging], and checking the storage
"""
# See https://spec.matrix.org/v1.6/application-service-api/#pushing-events
@functools.wraps(func)
def wrapped(txn_id, *args, **kwargs):
# TODO: actually do something
return func(txn_id, *args, **kwargs)
# check if 200, then store it in the database
return wrapped