diff options
Diffstat (limited to 'util.py')
-rw-r--r-- | util.py | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -1,4 +1,7 @@ | |||
1 | from cryptography.fernet import Fernet | 1 | from cryptography.fernet import Fernet |
2 | from telegram import Update | ||
3 | from dbstore.peewee_store import db, User, get_user_by_id | ||
4 | import functools | ||
2 | 5 | ||
3 | 6 | ||
4 | def encrypt(input: str, key: str) -> str: | 7 | def encrypt(input: str, key: str) -> str: |
@@ -11,6 +14,30 @@ def decrypt(input: str, key: str) -> str: | |||
11 | return f.decrypt(input).decode('utf-8') | 14 | return f.decrypt(input).decode('utf-8') |
12 | 15 | ||
13 | 16 | ||
17 | def check_user(fn): | ||
18 | """ | ||
19 | Decorator: loads User model and passes it to the function or stops the request. | ||
20 | Ref: https://shallowdepth.online/posts/2021/12/using-python-decorators-to-process-and-authorize-requests/ | ||
21 | """ | ||
22 | |||
23 | @functools.wraps(fn) | ||
24 | async def wrapper(*args, **kwargs): | ||
25 | # Expects that Update object is always the first arg | ||
26 | update: Update = args[0] | ||
27 | user = get_user_by_id(str(update.effective_user.id)) | ||
28 | |||
29 | if len(user) == 0: | ||
30 | await update.effective_message.reply_text("You are not logged in. Use `/login` to link your account first") | ||
31 | return | ||
32 | else: | ||
33 | # TODO | ||
34 | # check access_key is still valid | ||
35 | pass | ||
36 | return await fn(*args, **kwargs, user=user) | ||
37 | |||
38 | return wrapper | ||
39 | |||
40 | |||
14 | # if __name__ == "__main__": | 41 | # if __name__ == "__main__": |
15 | # key = Fernet.generate_key().decode('utf-8') | 42 | # key = Fernet.generate_key().decode('utf-8') |
16 | # print(key) | 43 | # print(key) |