Securing a FastAPI application is super important if you want to keep user data safe and maintain the integrity of your system. One of the best ways to do this is by implementing OAuth2 with the password flow. This guide will walk you through the process of locking down your FastAPI app using OAuth2 password flow for both authentication and authorization.
The Basics of OAuth2 Password Flow
So, what even is OAuth2 password flow? It’s a popular method for handling authentication and authorization. Basically, it lets users log in directly with their username and password, without needing some third-party app. This is especially useful when everything is happening within the same application.
Getting Your FastAPI Set Up
First things first, you need to get your FastAPI app up and running. Here’s a basic setup to get you started:
from fastapi import FastAPI, Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from pydantic import BaseModel
from passlib.context import CryptContext
import jwt
app = FastAPI()
SECRET_KEY = "your_secret_key_here"
ALGORITHM = "HS256"
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# Mock database of users (replace with your user database logic)
fake_users_db = {
"johndoe": {
"username": "johndoe",
"password": "$2b$12$AQyHPwd7p1s.GQvax/A3ve5wMey6NZuDXW1/FVhDpi8s/MV/Fo1LC", # hashed password: 'password1'
"disabled": False,
},
"alice": {
"username": "alice",
"password": "$2b$12$AQyHPwd7p1s.GQvax/A3ve5wMey6NZuDXW1/FVhDpi8s/MV/Fo1LC", # hashed password: 'password1'
"disabled": True,
},
}
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
class User(BaseModel):
username: str
password: str
class UserInDB(User):
hashed_password: str
def get_user(db, username: str):
if username in db:
user_dict = db[username]
return UserInDB(**user_dict)
def authenticate_user(username: str, password: str):
user = get_user(fake_users_db, username)
if not user or not pwd_context.verify(password, user.hashed_password):
return False
return user
def create_access_token(data: dict):
encoded_jwt = jwt.encode(data, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
async def get_current_user(token: str = Depends(oauth2_scheme)):
user = authenticate_user(token, None) # This is a placeholder, you need to decode the token properly
if not user:
raise HTTPException(
status_code=401,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)
return user
async def get_current_active_user(current_user: User = Depends(get_current_user)):
if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user")
return current_user
In this chunk of code, you’ve got everything laid out – from setting up the app to handling user data.
How to Handle User Authentication
When a user logs in, they’ll send their username and password to a specific endpoint. Use OAuth2PasswordRequestForm
from FastAPI to take care of this:
@app.post("/token")
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
user = authenticate_user(form_data.username, form_data.password)
if not user:
raise HTTPException(
status_code=401,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token = create_access_token(data={"sub": user.username})
return {"access_token": access_token, "token_type": "bearer"}
Protecting Your Routes with OAuth2
To keep your routes safe, use the Depends
feature provided by FastAPI. Here’s how you can secure a route:
@app.get("/items/")
async def read_items(token: str = Depends(oauth2_scheme)):
return {"token": token}
The OAuth2 Flow in Action
Here’s how the OAuth2 password flow goes down in your FastAPI app:
- User Input: The user enters their username and password into a form.
- Sending Credentials: The frontend sends this username and password to the
/token
endpoint. - Authentication: The backend checks these credentials against the database.
- Token Generation: If everything checks out, the backend generates a JWT token and sends it back to the frontend.
- Token Storage: The frontend stores this token temporarily.
- Protected Routes: When the user tries to access protected routes, the frontend sends the token along with the
Authorization
header. - Verification: The backend makes sure the token is legit on each request to confirm the user is authenticated.
Best Practices for Staying Secure
Even with OAuth2, there are some best practices you should follow to ensure your app stays secure:
- Go HTTPS: Always serve your app over HTTPS to keep communication encrypted.
- Hash Those Passwords: Use a secure hashing algorithm like bcrypt for storing passwords.
- Token Expiration: Make sure tokens expire after a certain period to reduce risks.
- Security Audits: Conduct regular security audits and automated testing to spot any vulnerabilities.
Wrapping It All Up
Locking down your FastAPI app with OAuth2 password flow is a solid way to handle user authentication and authorization. Stick to the steps above and follow best practices to keep everything secure. Always keep an eye out for the latest security updates and guidelines to keep your app safe and sound. With a secure setup, you can rest easy knowing that your users’ data is well protected.