Complete the code to import the bcrypt hashing function.
from passlib.context import CryptContext pwd_context = CryptContext(schemes=[[1]], deprecated="auto")
The CryptContext needs the bcrypt scheme to hash passwords securely.
Complete the function to hash a plain password using bcrypt.
def hash_password(password: str) -> str: return pwd_context.[1](password)
The hash method hashes the password string securely.
Fix the error in the password verification function.
def verify_password(plain_password: str, hashed_password: str) -> bool: return pwd_context.[1](plain_password, hashed_password)
The verify method compares a plain password with its hashed version.
Fill both blanks to create a FastAPI endpoint that hashes a password.
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]
The endpoint hashes the password using hash and returns the hashed value stored in hashed.
Fill all three blanks to verify a password and raise an error if it fails.
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]"}
The verify method checks the password. If it fails, an HTTP 401 error with message 'Invalid password' is raised. Otherwise, it returns 'Login successful'.
