Bird
0
0
FastAPIframework~10 mins

Password hashing with bcrypt in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the bcrypt hashing function.

FastAPI
from passlib.context import CryptContext

pwd_context = CryptContext(schemes=[[1]], deprecated="auto")
Drag options to blanks, or click blank then click option'
A"sha256"
B"bcrypt"
C"md5"
D"argon2"
Attempts:
3 left
💡 Hint
Common Mistakes
Using insecure or unsupported hashing schemes like md5 or sha256.
Forgetting to put quotes around the scheme name.
2fill in blank
medium

Complete the function to hash a plain password using bcrypt.

FastAPI
def hash_password(password: str) -> str:
    return pwd_context.[1](password)
Drag options to blanks, or click blank then click option'
Averify
Bhashing
Cencrypt
Dhash
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'verify' instead of 'hash' to create a password hash.
Using non-existent methods like 'encrypt' or 'hashing'.
3fill in blank
hard

Fix the error in the password verification function.

FastAPI
def verify_password(plain_password: str, hashed_password: str) -> bool:
    return pwd_context.[1](plain_password, hashed_password)
Drag options to blanks, or click blank then click option'
Averify
Bhash
Ccheck
Dcompare
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'hash' instead of 'verify' to check passwords.
Using non-existent methods like 'check' or 'compare'.
4fill in blank
hard

Fill both blanks to create a FastAPI endpoint that hashes a password.

FastAPI
from fastapi import FastAPI, HTTPException

app = FastAPI()

@app.post("/hash_password")
async def hash_endpoint(password: str):
    hashed = pwd_context.[1](password)
    return {"hashed_password": [2]
Drag options to blanks, or click blank then click option'
Ahash
Bpassword
Chashed
Dverify
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the plain password instead of the hashed one.
Using 'verify' instead of 'hash' to create the hash.
5fill in blank
hard

Fill all three blanks to verify a password and raise an error if it fails.

FastAPI
from fastapi import FastAPI, HTTPException

app = FastAPI()

@app.post("/login")
async def login(password: str, hashed_password: str):
    if not pwd_context.[1](password, hashed_password):
        raise HTTPException(status_code=401, detail="[2]")
    return {"message": "[3]"}
Drag options to blanks, or click blank then click option'
Averify
BInvalid password
CLogin successful
Dhash
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'hash' instead of 'verify' to check passwords.
Returning success message even if password is wrong.