aboutsummaryrefslogtreecommitdiffstats
path: root/backend/authentication/user.py
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2021-03-08 14:26:25 +0300
committerGravatar Hassan Abouelela <[email protected]>2021-03-08 14:29:58 +0300
commit3f2f8ca1900a4651b1831bef65f4eb324e138538 (patch)
tree7808d2596bebb0201a8ec5a4ae32d1fa3100df79 /backend/authentication/user.py
parentMerge branch 'main' into dependabot/pip/sentry-sdk-0.20.3 (diff)
parentMerge pull request #69 from python-discord/dependabot/pip/flake8-annotations-... (diff)
Merge branch 'main' into dependabot/pip/sentry-sdk-0.20.3
Signed-off-by: Hassan Abouelela <[email protected]> # Conflicts: # backend/__init__.py # poetry.lock
Diffstat (limited to 'backend/authentication/user.py')
-rw-r--r--backend/authentication/user.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/backend/authentication/user.py b/backend/authentication/user.py
index f40c68c..857c2ed 100644
--- a/backend/authentication/user.py
+++ b/backend/authentication/user.py
@@ -1,6 +1,11 @@
import typing as t
+import jwt
from starlette.authentication import BaseUser
+from starlette.requests import Request
+
+from backend.constants import SECRET_KEY
+from backend.discord import fetch_user_details
class User(BaseUser):
@@ -9,6 +14,7 @@ class User(BaseUser):
def __init__(self, token: str, payload: dict[str, t.Any]) -> None:
self.token = token
self.payload = payload
+ self.admin = False
@property
def is_authenticated(self) -> bool:
@@ -23,3 +29,23 @@ class User(BaseUser):
@property
def discord_mention(self) -> str:
return f"<@{self.payload['id']}>"
+
+ @property
+ def decoded_token(self) -> dict[str, any]:
+ return jwt.decode(self.token, SECRET_KEY, algorithms=["HS256"])
+
+ async def fetch_admin_status(self, request: Request) -> bool:
+ self.admin = await request.state.db.admins.find_one(
+ {"_id": self.payload["id"]}
+ ) is not None
+
+ return self.admin
+
+ async def refresh_data(self) -> None:
+ """Fetches user data from discord, and updates the instance."""
+ self.payload = await fetch_user_details(self.decoded_token.get("token"))
+
+ updated_info = self.decoded_token
+ updated_info["user_details"] = self.payload
+
+ self.token = jwt.encode(updated_info, SECRET_KEY, algorithm="HS256")