aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorclarkzjw <[email protected]>2023-02-27 15:59:50 -0800
committerclarkzjw <[email protected]>2023-02-27 15:59:50 -0800
commit9bb00ee842649590cbd30af3e047e40ca9da9d90 (patch)
tree9d917eeb71166cb85758ff119f33a89f0fa80ec2 /util.py
parent3bf39b0d32d0e0580457d0f85aef90a9c449a6ad (diff)
parenta1c7c5a223d22017508998599388c5adf9a90713 (diff)
downloadswarm2fediverse-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.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/util.py b/util.py
index 61efa0b..a0cda5e 100644
--- a/util.py
+++ b/util.py
@@ -1,4 +1,7 @@
1from cryptography.fernet import Fernet 1from cryptography.fernet import Fernet
2from telegram import Update
3from dbstore.peewee_store import db, User, get_user_by_id
4import functools
2 5
3 6
4def encrypt(input: str, key: str) -> str: 7def 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
17def 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)
Powered by cgit v1.2.3 (git 2.41.0)