HTTP Basic Auth

    The current page still doesn’t have a translation for this language.

    But you can help translating it: Contributing.

    For the simplest cases, you can use HTTP Basic Auth.

    In HTTP Basic Auth, the application expects a header that contains a username and a password.

    If it doesn’t receive it, it returns an HTTP 401 “Unauthorized” error.

    And returns a header WWW-Authenticate with a value of Basic, and an optional realm parameter.

    That tells the browser to show the integrated prompt for a username and password.

    Then, when you type that username and password, the browser sends them in the header automatically.

    • Import HTTPBasic and HTTPBasicCredentials.
    • Create a “security scheme” using HTTPBasic.
    • Use that security with a dependency in your path operation.
    • It returns an object of type HTTPBasicCredentials:
      • It contains the username and password sent.

    When you try to open the URL for the first time (or click the “Execute” button in the docs) the browser will ask you for your username and password:

    Check the username

    Use a dependency to check if the username and password are correct.

    For this, use the Python standard module to check the username and password.

    secrets.compare_digest() needs to take bytes or a str that only contains ASCII characters (the ones in English), this means it wouldn’t work with characters like á, as in Sebastián.

    To handle that, we first convert the username and password to bytes encoding them with UTF-8.

    Then we can use secrets.compare_digest() to ensure that credentials.username is "stanleyjobson", and that credentials.password is "swordfish".

    1. import secrets
    2. from fastapi import Depends, FastAPI, HTTPException, status
    3. from fastapi.security import HTTPBasic, HTTPBasicCredentials
    4. app = FastAPI()
    5. security = HTTPBasic()
    6. def get_current_username(credentials: HTTPBasicCredentials = Depends(security)):
    7. current_username_bytes = credentials.username.encode("utf8")
    8. correct_username_bytes = b"stanleyjobson"
    9. is_correct_username = secrets.compare_digest(
    10. )
    11. current_password_bytes = credentials.password.encode("utf8")
    12. correct_password_bytes = b"swordfish"
    13. is_correct_password = secrets.compare_digest(
    14. current_password_bytes, correct_password_bytes
    15. )
    16. if not (is_correct_username and is_correct_password):
    17. raise HTTPException(
    18. status_code=status.HTTP_401_UNAUTHORIZED,
    19. detail="Incorrect email or password",
    20. headers={"WWW-Authenticate": "Basic"},
    21. )
    22. return credentials.username
    23. @app.get("/users/me")
    24. def read_current_user(username: str = Depends(get_current_username)):
    25. return {"username": username}

    This would be similar to:

    But by using the secrets.compare_digest() it will be secure against a type of attacks called “timing attacks”.

    But what’s a “timing attack”?

    Let’s imagine some attackers are trying to guess the username and password.

    And they send a request with a username johndoe and a password love123.

    1. if "johndoe" == "stanleyjobson" and "love123" == "swordfish":
    2. ...

    But right at the moment Python compares the first j in johndoe to the first s in stanleyjobson, it will return False, because it already knows that those two strings are not the same, thinking that “there’s no need to waste more computation comparing the rest of the letters”. And your application will say “incorrect user or password”.

    But then the attackers try with username stanleyjobsox and password love123.

    And your application code does something like:

    Python will have to compare the whole stanleyjobso in both stanleyjobsox and stanleyjobson before realizing that both strings are not the same. So it will take some extra microseconds to reply back “incorrect user or password”.

    The time to answer helps the attackers

    At that point, by noticing that the server took some microseconds longer to send the “incorrect user or password” response, the attackers will know that they got something right, some of the initial letters were right.

    And then they can try again knowing that it’s probably something more similar to stanleyjobsox than to johndoe.

    A “professional” attack

    Of course, the attackers would not try all this by hand, they would write a program to do it, possibly with thousands or millions of tests per second. And would get just one extra correct letter at a time.

    But doing that, in some minutes or hours the attackers would have guessed the correct username and password, with the “help” of our application, just using the time taken to answer.

    Fix it with secrets.compare_digest()

    But in our code we are actually using secrets.compare_digest().

    In short, it will take the same time to compare stanleyjobsox to than it takes to compare johndoe to stanleyjobson. And the same for the password.

    Return the error

    After detecting that the credentials are incorrect, return an HTTPException with a status code 401 (the same returned when no credentials are provided) and add the header WWW-Authenticate to make the browser show the login prompt again:

    1. import secrets
    2. from fastapi import Depends, FastAPI, HTTPException, status
    3. from fastapi.security import HTTPBasic, HTTPBasicCredentials
    4. app = FastAPI()
    5. security = HTTPBasic()
    6. def get_current_username(credentials: HTTPBasicCredentials = Depends(security)):
    7. current_username_bytes = credentials.username.encode("utf8")
    8. correct_username_bytes = b"stanleyjobson"
    9. is_correct_username = secrets.compare_digest(
    10. current_username_bytes, correct_username_bytes
    11. )
    12. current_password_bytes = credentials.password.encode("utf8")
    13. correct_password_bytes = b"swordfish"
    14. is_correct_password = secrets.compare_digest(
    15. current_password_bytes, correct_password_bytes
    16. )
    17. if not (is_correct_username and is_correct_password):
    18. raise HTTPException(
    19. status_code=status.HTTP_401_UNAUTHORIZED,
    20. detail="Incorrect email or password",
    21. headers={"WWW-Authenticate": "Basic"},
    22. )
    23. return credentials.username
    24. @app.get("/users/me")
    25. def read_current_user(username: str = Depends(get_current_username)):
    26. return {"username": username}