Authentications
Variable:
AUTHENTICATION
&WS_AUTHENTICATION
Type:
str
Default:
None
You can set Authentication class in your configs
Panther use it, to authenticate every API/ WS if auth=True
and give you the user or raise HTTP_401_UNAUTHORIZED
The user
will be in request.user
in APIs and in self.user
in WSs
We implemented 2 built-in authentication classes which use JWT
for authentication.
But, You can use your own custom authentication class too.
JWTAuthentication#
This class will
- Get the
token
fromAuthorization
header of request. - Check the
Bearer
decode
thetoken
- Find the matched
user
JWTAuthentication
is going to usepanther.db.models.BaseUser
if you didn't set theUSER_MODEL
in yourconfigs
You can customize these 4 variables for JWTAuthentication
in your configs
as JWTConfig
like below (JWTConfig
is optional):
...
from datetime import timedelta
from panther.utils import load_env
from pathlib import Path
BASE_DIR = Path(__name__).resolve().parent
env = load_env(BASE_DIR / '.env')
SECRET_KEY = env['SECRET_KEY']
JWTConfig = {
'key': SECRET_KEY,
'algorithm': 'HS256',
'life_time': timedelta(days=2),
'refresh_life_time': timedelta(days=10),
}
key --> default is
SECRET_KEY
algorithm --> default is
HS256
life_time --> default is
timedelta(days=1)
refresh_life_time --> default is
multiply 2 of life_time
QueryParamJWTAuthentication#
-
This class is same as
JWTAuthentication
and the only difference is that, this class is looking for token inquery params
not in theheaders
-
You should pass the token like this:
https://example.com/path?authorization=Bearer%20access_token
Websocket Authentication#
The QueryParamJWTAuthentication
is very useful when you are trying to authenticate the user in websocket, you just have to add this into your configs
:
WS_AUTHENTICATION = 'panther.authentications.QueryParamJWTAuthentication'
Custom Authentication#
-
Create a class and inherits it from
panther.authentications.BaseAuthentication
-
Implement
async authentication(cls, request: Request | Websocket)
method- Process the
request.headers.authorization
or ... - Return Instance of
USER_MODEL
- Or raise
panther.exceptions.AuthenticationAPIError
- Process the
-
Add it into your
configs
AUTHENTICATION = 'project_name.core.authentications.CustomAuthentication'
You can see the source code of JWTAuthentication [here]