-
-
Notifications
You must be signed in to change notification settings - Fork 7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🛠 Native JWT support #3305
base: master
Are you sure you want to change the base?
🛠 Native JWT support #3305
Conversation
# Conflicts: # pyproject.toml
@k4black in security docs @tiangolo in the past used Pyjwt but later he moved to python-jose PR [#1610] so I think we should consider using python-jose for your pull request 🤔 |
@acutaia |
Cleaning a bit the code
# Conflicts: # pyproject.toml
♻️ Migrating to `python-jose` and cleaning up the code
# Conflicts: # pyproject.toml
Added expire_time parameter, additional expire_time tests and python-jose readme fix
@tiangolo Can you approve or decline this PR, please |
# Conflicts: # mypy.ini # pyproject.toml
Is this going to be approved or has this just been ignored? Seems like a nice, easy way to add authentication with the option of refresh tokens to me. |
|
||
# Check token exist | ||
if refresh_token is None: | ||
if not self.auto_error: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not reverse the condition add the raise keyword to the if block.
) -> Optional[JwtAuthorizationCredentials]: | ||
payload = self._get_payload(bearer, cookie) | ||
|
||
if payload: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where you should reverse the condition the success case should be at the bottom. It is called early return or fast fail.
@tiangolo, any update? |
python-jose is unmaintained. Please do not use it. |
latest version of fastapi-jwt allows to use authlib instead of python-jose (still present as fallback) |
Any update? |
Relevance
About half of the modern internet uses raw JWT (the other half uses OAuth2, which is already supported).
I think that in such a modern library as
fastapi
, native support of JWT will be a big advantage.Existed solutions
Only 1 external lib: github:fastapi-jwt-auth (192 stars) - an flask-jwt-auth customization.
The following drawbacks are significant:
fastapi
security style (Security
/Depends
function args wrapper)openapi
specification generationExisted Issues and Pull Requests:
Features
auto_error=False
supportopenapi
specification generationRequrements
python-jose
for tokens interaction andpytest-mock
for testing time-dependent functions.Usage example
All available use cases can be found in the test files. The simplest example is provided below:
Any suggestions for improvement are gladly discussed!