diff options
author | clarkzjw <[email protected]> | 2023-02-27 15:59:50 -0800 |
---|---|---|
committer | clarkzjw <[email protected]> | 2023-02-27 15:59:50 -0800 |
commit | 9bb00ee842649590cbd30af3e047e40ca9da9d90 (patch) | |
tree | 9d917eeb71166cb85758ff119f33a89f0fa80ec2 /util.py | |
parent | 3bf39b0d32d0e0580457d0f85aef90a9c449a6ad (diff) | |
parent | a1c7c5a223d22017508998599388c5adf9a90713 (diff) | |
download | swarm2fediverse-9bb00ee842649590cbd30af3e047e40ca9da9d90.tar.gz |
Merge branch 'feature/user_permission'v0.1
add check_user decorator to check user login status
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) |