From 6a6d28038494626ed74fc842c814477b16af1e75 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Tue, 1 Dec 2020 09:54:22 +0200 Subject: Create BaseUser implementation for JWT authentication --- backend/authentication/__init__.py | 3 +++ backend/authentication/user.py | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 backend/authentication/__init__.py create mode 100644 backend/authentication/user.py (limited to 'backend/authentication') diff --git a/backend/authentication/__init__.py b/backend/authentication/__init__.py new file mode 100644 index 0000000..35b01f3 --- /dev/null +++ b/backend/authentication/__init__.py @@ -0,0 +1,3 @@ +from .user import User + +__all__ = ["User"] diff --git a/backend/authentication/user.py b/backend/authentication/user.py new file mode 100644 index 0000000..afa243f --- /dev/null +++ b/backend/authentication/user.py @@ -0,0 +1,22 @@ +import typing as t +from abc import ABC + +from starlette.authentication import BaseUser + + +class User(BaseUser, ABC): + """Starlette BaseUser implementation for JWT authentication.""" + + def __init__(self, token: str, payload: t.Dict) -> None: + self.token = token + self.payload = payload + + @property + def is_authenticated(self) -> bool: + """Returns True because user is always authenticated at this stage.""" + return True + + @property + def display_name(self) -> str: + """Return username and discriminator as display name.""" + return f"{self.payload['username']}#{self.payload['discriminator']}" -- cgit v1.2.3