Back to all reviewers

Secure JWT authentication

fastapi/fastapi
Based on 3 comments
Markdown

Implement secure authentication using JWT tokens by following these best practices: 1. **Use secure libraries**: Prefer well-maintained libraries like `PyJWT` for JWT handling and `passlib` with bcrypt for password hashing:

Security Markdown

Reviewer Prompt

Implement secure authentication using JWT tokens by following these best practices:

  1. Use secure libraries: Prefer well-maintained libraries like PyJWT for JWT handling and passlib with bcrypt for password hashing:
from passlib.context import CryptContext
import jwt

# Password hashing setup
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

# JWT configuration
SECRET_KEY = "your-secure-secret-key"  # Generate with: openssl rand -hex 32
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30

# Functions for security
def verify_password(plain_password, hashed_password):
    return pwd_context.verify(plain_password, hashed_password)
    
def get_password_hash(password):
    return pwd_context.hash(password)
    
def create_access_token(data: dict, expires_delta: timedelta):
    to_encode = data.copy()
    expire = datetime.utcnow() + expires_delta
    to_encode.update({"exp": expire})
    return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
  1. Never store plaintext passwords: Always hash passwords before storage and compare hashed values during verification.

  2. Set appropriate token expiration: Balance security with user experience by setting reasonable token expiration times.

  3. Distinguish authentication from authorization: Authentication verifies user identity while authorization determines their permissions. Implement both correctly.

  4. Configure secure settings: Avoid security risks in configurations. For example, never use allow_credentials=True with allow_origins=['*'] in CORS settings, as this would allow any third-party origin to access sensitive cookie information.

  5. Protect JWT tokens: Store tokens securely (HTTP-only cookies or secure client-side storage), validate them on each request, and implement proper token revocation strategies.

3
Comments Analyzed
Markdown
Primary Language
Security
Category

Source Discussions