Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using insecure or unsupported hashing schemes like md5 or sha256.
Forgetting to put quotes around the scheme name.
✗ Incorrect
The CryptContext needs the bcrypt scheme to hash passwords securely.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'verify' instead of 'hash' to create a password hash.
Using non-existent methods like 'encrypt' or 'hashing'.
✗ Incorrect
The hash method hashes the password string securely.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'hash' instead of 'verify' to check passwords.
Using non-existent methods like 'check' or 'compare'.
✗ Incorrect
The verify method compares a plain password with its hashed version.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the plain password instead of the hashed one.
Using 'verify' instead of 'hash' to create the hash.
✗ Incorrect
The endpoint hashes the password using hash and returns the hashed value stored in hashed.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'hash' instead of 'verify' to check passwords.
Returning success message even if password is wrong.
✗ Incorrect
The verify method checks the password. If it fails, an HTTP 401 error with message 'Invalid password' is raised. Otherwise, it returns 'Login successful'.
