Skip to content
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

Fixing http error codes in security tutorials #5044

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

haasal
Copy link

@haasal haasal commented Jun 16, 2022

The docs still have to be updated so the yellow marker is at the correct places.
Before I do this: Can somebody review if everything is more or less fine like this.

And I just noticed that tests still have to be written due to the error code changes

This PR addresses #4975

@haasal haasal marked this pull request as ready for review June 16, 2022 17:43
@haasal haasal closed this Jun 16, 2022
@haasal haasal reopened this Jun 16, 2022
@github-actions
Copy link
Contributor

📝 Docs preview for commit 27bf09c at: https://62ab6bfb9fe0210cb141f466--fastapi.netlify.app

@haasal
Copy link
Author

haasal commented Jun 16, 2022

I'm sorry for the close and reopen. I wanted to revert this back to a draft PR

Copy link
Contributor

@JarroVGIT JarroVGIT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am in agreement that some of the codes are wrong codes used in the tutorials and that it should be fixed. However, I am not sure that referencing to an Enum value would be beneficial for in the docs. Using int, it is inline with the signature of the init of HTTPException and might be easier to understand for people not experienced with FastAPI (which by definition are the majority of the people going through the documentation in the first place).

Also made comments where I disagree with your suggested code.

docs_src/security/tutorial003_py310.py Show resolved Hide resolved
docs_src/security/tutorial004.py Show resolved Hide resolved
@@ -119,7 +121,7 @@ async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
headers={"WWW-Authenticate": "Basic"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made me think for a bit, but I think I agree with you. Although we are using helper classes with OAuth2 stuff in them, it is basically a basic authentication. On the other hand, the username/password are following the Oauth2 standards (e.g. with the form) and not with base64 encoding (as is required by the Basic Authentication standard). So, it can be both ways I guess?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I don't know. Somehow it's neither of the two options. I found this on this topic. I guess there is no simple soultion. Maybe just leave the WWW-Authenticate out for simplicities sake or leave as is?
Bearer is definitely false in my opinion.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using basic auth, the browser prompts you with a username/passwd form. This might not even work, as the browser will probably put the credentials into the header instead of the body. I didn't test this out yet

docs_src/security/tutorial004_py310.py Show resolved Hide resolved
docs_src/security/tutorial005.py Show resolved Hide resolved
@haasal
Copy link
Author

haasal commented Jun 27, 2022

I think the error code constants make the code much more readable and show intent more clearly. Especially because new people are reading through the docs I think it's important to make constants the default. Also I'm not sure but I think in the docs the usage of constants and ints is rather inconsistent.

@@ -139,15 +134,21 @@ async def get_current_active_user(
current_user: User = Security(get_current_user, scopes=["me"])
):
if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user")
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="Inactive user"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

403 is better right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, 403 would be more appropriate imo.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Co-authored-by: Irfanuddin Shafi Ahmed <irfanudeen08@gmail.com>
@haasal
Copy link
Author

haasal commented Aug 3, 2022

I don't have time at this moment to write tests and apply all your suggestions. I will complete the PR once I have time again

@github-actions
Copy link
Contributor

github-actions bot commented Aug 3, 2022

📝 Docs preview for commit 72cdde5 at: https://62ea4cd99d56ce1019f2dfbe--fastapi.netlify.app

@github-actions
Copy link
Contributor

github-actions bot commented Sep 4, 2022

📝 Docs preview for commit 42536db at: https://63146aa3d8aeff43e433fcb1--fastapi.netlify.app

@tiangolo tiangolo added the docs Documentation about how to use FastAPI label Jun 28, 2023
@alejsdev alejsdev added the p4 label Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation about how to use FastAPI p4
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants