Skip to content

Commit

Permalink
feat: added body jwt token logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Jesus committed Sep 1, 2023
1 parent c1f2fdf commit eabd71b
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 172 deletions.
3 changes: 2 additions & 1 deletion fastapi_jwt_auth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""FastAPI extension that provides JWT Auth support (secure, easy to use and lightweight)"""

__version__ = "0.6.2"
__version__ = "0.6.3"

from .auth_jwt import AuthJWT
from .auth_jwt import AuthJWTRefresh
21 changes: 13 additions & 8 deletions fastapi_jwt_auth/auth_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from typing import Callable, List
from datetime import timedelta


class AuthConfig:
_token = None
_token_location = {'headers'}
_token_location = {"headers"}

_secret_key = None
_public_key = None
Expand All @@ -17,7 +18,7 @@ class AuthConfig:
_decode_issuer = None
_decode_audience = None
_denylist_enabled = False
_denylist_token_checks = {'access','refresh'}
_denylist_token_checks = {"access", "refresh"}
_header_name = "Authorization"
_header_type = "Bearer"
_token_in_denylist_callback = None
Expand All @@ -42,20 +43,24 @@ class AuthConfig:
_refresh_csrf_cookie_path = "/"
_access_csrf_header_name = "X-CSRF-Token"
_refresh_csrf_header_name = "X-CSRF-Token"
_csrf_methods = {'POST','PUT','PATCH','DELETE'}
_csrf_methods = {"POST", "PUT", "PATCH", "DELETE"}

@property
def jwt_in_cookies(self) -> bool:
return 'cookies' in self._token_location
return "cookies" in self._token_location

@property
def jwt_in_headers(self) -> bool:
return 'headers' in self._token_location
return "headers" in self._token_location

@property
def jwt_in_body(self) -> bool:
return "body" in self._token_location

@classmethod
def load_config(cls, settings: Callable[...,List[tuple]]) -> "AuthConfig":
def load_config(cls, settings: Callable[..., List[tuple]]) -> "AuthConfig":
try:
config = LoadConfig(**{key.lower():value for key,value in settings()})
config = LoadConfig(**{key.lower(): value for key, value in settings()})

cls._token_location = config.authjwt_token_location
cls._secret_key = config.authjwt_secret_key
Expand Down Expand Up @@ -97,7 +102,7 @@ def load_config(cls, settings: Callable[...,List[tuple]]) -> "AuthConfig":
raise TypeError("Config must be pydantic 'BaseSettings' or list of tuple")

@classmethod
def token_in_denylist_loader(cls, callback: Callable[...,bool]) -> "AuthConfig":
def token_in_denylist_loader(cls, callback: Callable[..., bool]) -> "AuthConfig":
"""
This decorator sets the callback function that will be called when
a protected endpoint is accessed and will check if the JWT has been
Expand Down
Loading

0 comments on commit eabd71b

Please sign in to comment.