Open genievn opened 1 year ago
Hello!
The same problem here when updating to fastapi==0.100.0
and pydantic==2.0.2
.
I get this error simply by importing fastapi_jwt_auth
or any fastapi_jwt_auth
Class. Even if the package/Class is not called.
fastapi==0.100.0
fastapi_jwt_auth==0.5.0
pydantic==2.0.2
For me I could fix the issue by downgrading to:
fastapi==0.99.1
fastapi_jwt_auth==0.5.0
pydantic==1.10.11
Since this repo has not seen any movement in two years, this might be the only solution available until a fork appears.
For me I could fix the issue by downgrading to:
fastapi==0.99.1 fastapi_jwt_auth==0.5.0 pydantic==1.10.11
Since this repo has not seen any movement in two years, this might be the only solution available until a fork appears.
Hey, I am also blocked right now by this problem. I can't downgrade the packages for some project reasons. I think I will also need an alternative or build my own solution. Hope to see an update on this lol. If anyone has a solution for this, ping me please
I get the very same error.
thanks for the workaround, which does the job for me for the moment.
@georg-mayer If that can help, this PR shows how I migrated a project of mine from fastapi-jwt-auth
to fastapi-jwt
to circumvent this issue. With pydantic 2.0.0
being out and fastapi-jwt-auth
not having been updated for the past 3 years, I think this might be a good way forward.
For me I could fix the issue by downgrading to:
fastapi==0.99.1 fastapi_jwt_auth==0.5.0 pydantic==1.10.11
Since this repo has not seen any movement in two years, this might be the only solution available until a fork appears.
thx for solution!
Same issue
Any good replacement for fastapi-jwt-auth?
Any good replacement for fastapi-jwt-auth?
No, the last time, I used https://github.com/GlitchCorp/fastapi-another-jwt-auth as an alternative but seems like the maintainer abandoned it.
To re-iterate https://github.com/IndominusByte/fastapi-jwt-auth/issues/98#issuecomment-1656652991: I've had good success using https://github.com/k4black/fastapi-jwt as a replacement library. Some changes in the codebase were required, but not much.
Is there any update regarding this to issue in order to make it work with pydantic==2..?
Is there any alternative or fork for fastapi-jwt-auth
?
You will need to change Sequence
type to List
type in config.py
file in the LoadConfig
class.
Here is my entire file
from datetime import timedelta
from typing import Optional, Union, Sequence, List
from pydantic import (
BaseModel,
validator,
StrictBool,
StrictInt,
StrictStr
)
class LoadConfig(BaseModel):
authjwt_token_location: Optional[List[StrictStr]] = ['headers']
authjwt_secret_key: Optional[StrictStr] = None
authjwt_public_key: Optional[StrictStr] = None
authjwt_private_key: Optional[StrictStr] = None
authjwt_algorithm: Optional[StrictStr] = "HS256"
authjwt_decode_algorithms: Optional[List[StrictStr]] = None
authjwt_decode_leeway: Optional[Union[StrictInt,timedelta]] = 0
authjwt_encode_issuer: Optional[StrictStr] = None
authjwt_decode_issuer: Optional[StrictStr] = None
authjwt_decode_audience: Optional[Union[StrictStr,Sequence[StrictStr]]] = None
authjwt_denylist_enabled: Optional[StrictBool] = False
authjwt_denylist_token_checks: Optional[List[StrictStr]] = ['access','refresh']
authjwt_header_name: Optional[StrictStr] = "Authorization"
authjwt_header_type: Optional[StrictStr] = "Bearer"
authjwt_access_token_expires: Optional[Union[StrictBool,StrictInt,timedelta]] = timedelta(minutes=15)
authjwt_refresh_token_expires: Optional[Union[StrictBool,StrictInt,timedelta]] = timedelta(days=30)
# # option for create cookies
authjwt_access_cookie_key: Optional[StrictStr] = "access_token_cookie"
authjwt_refresh_cookie_key: Optional[StrictStr] = "refresh_token_cookie"
authjwt_access_cookie_path: Optional[StrictStr] = "/"
authjwt_refresh_cookie_path: Optional[StrictStr] = "/"
authjwt_cookie_max_age: Optional[StrictInt] = None
authjwt_cookie_domain: Optional[StrictStr] = None
authjwt_cookie_secure: Optional[StrictBool] = False
authjwt_cookie_samesite: Optional[StrictStr] = None
# # option for double submit csrf protection
authjwt_cookie_csrf_protect: Optional[StrictBool] = True
authjwt_access_csrf_cookie_key: Optional[StrictStr] = "csrf_access_token"
authjwt_refresh_csrf_cookie_key: Optional[StrictStr] = "csrf_refresh_token"
authjwt_access_csrf_cookie_path: Optional[StrictStr] = "/"
authjwt_refresh_csrf_cookie_path: Optional[StrictStr] = "/"
authjwt_access_csrf_header_name: Optional[StrictStr] = "X-CSRF-Token"
authjwt_refresh_csrf_header_name: Optional[StrictStr] = "X-CSRF-Token"
authjwt_csrf_methods: Optional[List[StrictStr]] = ['POST','PUT','PATCH','DELETE']
@validator('authjwt_access_token_expires')
def validate_access_token_expires(cls, v):
if v is True:
raise ValueError("The 'authjwt_access_token_expires' only accept value False (bool)")
return v
@validator('authjwt_refresh_token_expires')
def validate_refresh_token_expires(cls, v):
if v is True:
raise ValueError("The 'authjwt_refresh_token_expires' only accept value False (bool)")
return v
@validator('authjwt_denylist_token_checks', each_item=True)
def validate_denylist_token_checks(cls, v):
if v not in ['access','refresh']:
raise ValueError("The 'authjwt_denylist_token_checks' must be between 'access' or 'refresh'")
return v
@validator('authjwt_token_location', each_item=True)
def validate_token_location(cls, v):
if v not in ['headers','cookies']:
raise ValueError("The 'authjwt_token_location' must be between 'headers' or 'cookies'")
return v
@validator('authjwt_cookie_samesite')
def validate_cookie_samesite(cls, v):
if v not in ['strict','lax','none']:
raise ValueError("The 'authjwt_cookie_samesite' must be between 'strict', 'lax', 'none'")
return v
@validator('authjwt_csrf_methods', each_item=True)
def validate_csrf_methods(cls, v):
if v.upper() not in ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"]:
raise ValueError("The 'authjwt_csrf_methods' must be between http request methods")
return v.upper()
class Config:
str_min_length = 1
str_strip_whitespace = True
Using it from this fork resolved the issue for me. I just bumped the pydantic version to 2.7.0 without any application errors.
I have problem belong TypeError: @validator(..., each_item=True)
cannot be applied to fields with a schema of json-or-python
can you help me to solve this problem
@jahongir7797 Use the mentioned fork in the above comment. That resolved it for me.
Hi, I'm getting this error when running the example https://indominusbyte.github.io/fastapi-jwt-auth/usage/basic/
Please help. Thank you!