/
utils.py
31 lines (22 loc) · 825 Bytes
/
utils.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
import binascii
import os
from functools import wraps
from sanic import exceptions
from pubgate.api.v1.db.models import User
def make_label(activity):
label = activity["type"]
if isinstance(activity["object"], dict):
label = f'{label}: {activity["object"]["type"]}'
return label
def random_object_id() -> str:
"""Generates a random object ID."""
return binascii.hexlify(os.urandom(8)).decode("utf-8")
def auth_required(handler=None):
@wraps(handler)
async def wrapper(request, *args, **kwargs):
user = await User.find_one(dict(username=kwargs["user_id"],
token=request.token))
if not user:
raise exceptions.Unauthorized("Auth required.")
return await handler(request, *args, **kwargs)
return wrapper