[lib.user] move JWTUser, Nobody and CheckUser to lib.user
This commit is contained in:
parent
ce672eeb30
commit
908eab5fdc
@ -21,6 +21,7 @@ from starlette.authentication import (
|
||||
from starlette.requests import HTTPConnection
|
||||
from starlette.exceptions import HTTPException
|
||||
|
||||
from .user import CheckUser, JWTUser, Nobody
|
||||
from ..logging import logger
|
||||
|
||||
SECRET=None
|
||||
@ -30,80 +31,6 @@ except ImportError as exc:
|
||||
logger.error('Could not import SECRET variable from conf module,'\
|
||||
' using HALFAPI_SECRET environment variable')
|
||||
|
||||
class Nobody(UnauthenticatedUser):
|
||||
""" Nobody class
|
||||
|
||||
The default class when no token is passed
|
||||
"""
|
||||
@property
|
||||
def json(self):
|
||||
return {
|
||||
'id' : '',
|
||||
'token': '',
|
||||
'payload': ''
|
||||
}
|
||||
|
||||
|
||||
class JWTUser(BaseUser):
|
||||
""" JWTUser class
|
||||
|
||||
Is used to store authentication informations
|
||||
"""
|
||||
def __init__(self, user_id: UUID, token: str, payload: dict) -> None:
|
||||
self.__id = user_id
|
||||
self.token = token
|
||||
self.payload = payload
|
||||
|
||||
def __str__(self):
|
||||
return str(self.json)
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
return {
|
||||
'id' : str(self.__id),
|
||||
'token': self.token,
|
||||
'payload': self.payload
|
||||
}
|
||||
|
||||
@property
|
||||
def is_authenticated(self) -> bool:
|
||||
return True
|
||||
|
||||
@property
|
||||
def display_name(self) -> str:
|
||||
return ' '.join(
|
||||
(self.payload.get('name'), self.payload.get('firstname')))
|
||||
|
||||
@property
|
||||
def id(self) -> str:
|
||||
return self.__id
|
||||
|
||||
|
||||
class CheckUser(BaseUser):
|
||||
""" CheckUser class
|
||||
|
||||
Is used to call checks with give user_id, to know if it passes the ACLs for
|
||||
the given route.
|
||||
|
||||
It should never be able to run a route function.
|
||||
"""
|
||||
def __init__(self, user_id: UUID) -> None:
|
||||
self.__id = user_id
|
||||
|
||||
|
||||
@property
|
||||
def is_authenticated(self) -> bool:
|
||||
return True
|
||||
|
||||
@property
|
||||
def display_name(self) -> str:
|
||||
return 'check_user'
|
||||
|
||||
@property
|
||||
def id(self) -> str:
|
||||
return self.__id
|
||||
|
||||
|
||||
|
||||
class JWTAuthenticationBackend(AuthenticationBackend):
|
||||
def __init__(self, secret_key: str = SECRET,
|
||||
|
@ -22,7 +22,7 @@ import orjson
|
||||
# asgi framework
|
||||
from starlette.responses import PlainTextResponse, Response, JSONResponse
|
||||
|
||||
from .jwt_middleware import JWTUser, Nobody
|
||||
from .user import JWTUser, Nobody
|
||||
|
||||
|
||||
__all__ = [
|
||||
|
79
halfapi/lib/user.py
Normal file
79
halfapi/lib/user.py
Normal file
@ -0,0 +1,79 @@
|
||||
from uuid import UUID
|
||||
from starlette.authentication import BaseUser, UnauthenticatedUser
|
||||
|
||||
class Nobody(UnauthenticatedUser):
|
||||
""" Nobody class
|
||||
|
||||
The default class when no token is passed
|
||||
"""
|
||||
@property
|
||||
def json(self):
|
||||
return {
|
||||
'id' : '',
|
||||
'token': '',
|
||||
'payload': ''
|
||||
}
|
||||
|
||||
|
||||
class JWTUser(BaseUser):
|
||||
""" JWTUser class
|
||||
|
||||
Is used to store authentication informations
|
||||
"""
|
||||
def __init__(self, user_id: UUID, token: str, payload: dict) -> None:
|
||||
self.__id = user_id
|
||||
self.token = token
|
||||
self.payload = payload
|
||||
|
||||
def __str__(self):
|
||||
return str(self.json)
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
return {
|
||||
'id' : str(self.__id),
|
||||
'token': self.token,
|
||||
'payload': self.payload
|
||||
}
|
||||
|
||||
@property
|
||||
def is_authenticated(self) -> bool:
|
||||
return True
|
||||
|
||||
@property
|
||||
def display_name(self) -> str:
|
||||
return ' '.join(
|
||||
(self.payload.get('name'), self.payload.get('firstname')))
|
||||
|
||||
@property
|
||||
def id(self) -> str:
|
||||
return self.__id
|
||||
|
||||
|
||||
class CheckUser(BaseUser):
|
||||
""" CheckUser class
|
||||
|
||||
Is used to call checks with give user_id, to know if it passes the ACLs for
|
||||
the given route.
|
||||
|
||||
It should never be able to run a route function.
|
||||
"""
|
||||
def __init__(self, user_id: UUID) -> None:
|
||||
self.__id = user_id
|
||||
|
||||
|
||||
@property
|
||||
def is_authenticated(self) -> bool:
|
||||
return True
|
||||
|
||||
@property
|
||||
def display_name(self) -> str:
|
||||
return 'check_user'
|
||||
|
||||
@property
|
||||
def id(self) -> str:
|
||||
return self.__id
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user