Buckets:

download
raw
11.2 kB
"""
FastJango API Authentication - DRF-like authentication classes using FastAPI.
"""
from typing import Any, Optional, Tuple
from fastapi import HTTPException, status, Depends, Request
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
import base64
import hashlib
import hmac
import time
class BaseAuthentication:
"""
Base authentication class that all authentication classes should inherit from.
This mimics DRF's BaseAuthentication while working with FastAPI.
"""
def authenticate(self, request: Request) -> Optional[Tuple[Any, Any]]:
"""
Authenticate the request and return a two-tuple of (user, auth).
Args:
request: The request object
Returns:
Tuple of (user, auth) or None if authentication fails
"""
raise NotImplementedError("Subclasses must implement authenticate()")
def authenticate_header(self, request: Request) -> Optional[str]:
"""
Return a string to be used as the value of the `WWW-Authenticate`
header in a `401 Unauthenticated` response.
Args:
request: The request object
Returns:
The WWW-Authenticate header value or None
"""
return None
class SessionAuthentication(BaseAuthentication):
"""
Session-based authentication.
"""
def authenticate(self, request: Request) -> Optional[Tuple[Any, Any]]:
"""Authenticate using session."""
session = getattr(request, 'session', None)
if session and 'user_id' in session:
# Here you would typically load the user from the database
# For now, we'll return a mock user
user = MockUser(id=session['user_id'])
return (user, None)
return None
def authenticate_header(self, request: Request) -> Optional[str]:
"""Return the WWW-Authenticate header."""
return 'Session'
class TokenAuthentication(BaseAuthentication):
"""
Token-based authentication.
"""
keyword = 'Token'
model = None
def get_model(self):
"""Get the token model."""
if self.model is not None:
return self.model
from fastjango.db import Model
return Model # This would be a proper Token model
def authenticate(self, request: Request) -> Optional[Tuple[Any, Any]]:
"""Authenticate using token."""
auth = request.headers.get('Authorization')
if not auth:
return None
parts = auth.split()
if len(parts) != 2 or parts[0].lower() != self.keyword.lower():
return None
token = parts[1]
return self.authenticate_credentials(token)
def authenticate_credentials(self, key: str) -> Optional[Tuple[Any, Any]]:
"""
Authenticate the given credentials.
Args:
key: The token key
Returns:
Tuple of (user, token) or None if authentication fails
"""
model = self.get_model()
try:
# Here you would typically look up the token in the database
# For now, we'll use a mock implementation
if key == 'valid_token':
user = MockUser(id=1, username='testuser')
return (user, key)
except Exception:
pass
return None
def authenticate_header(self, request: Request) -> Optional[str]:
"""Return the WWW-Authenticate header."""
return f'{self.keyword} realm="api"'
class BasicAuthentication(BaseAuthentication):
"""
HTTP Basic authentication.
"""
def authenticate(self, request: Request) -> Optional[Tuple[Any, Any]]:
"""Authenticate using HTTP Basic authentication."""
auth = request.headers.get('Authorization')
if not auth:
return None
try:
auth_parts = auth.split()
if len(auth_parts) != 2 or auth_parts[0].lower() != 'basic':
return None
credentials = base64.b64decode(auth_parts[1]).decode('utf-8')
username, password = credentials.split(':', 1)
return self.authenticate_credentials(username, password)
except Exception:
return None
def authenticate_credentials(self, username: str, password: str) -> Optional[Tuple[Any, Any]]:
"""
Authenticate the given credentials.
Args:
username: The username
password: The password
Returns:
Tuple of (user, auth) or None if authentication fails
"""
# Here you would typically validate against the database
# For now, we'll use a mock implementation
if username == 'admin' and password == 'password':
user = MockUser(id=1, username=username, is_staff=True)
return (user, None)
return None
def authenticate_header(self, request: Request) -> Optional[str]:
"""Return the WWW-Authenticate header."""
return 'Basic realm="api"'
class OAuth2Authentication(BaseAuthentication):
"""
OAuth2 token-based authentication.
"""
def authenticate(self, request: Request) -> Optional[Tuple[Any, Any]]:
"""Authenticate using OAuth2 token."""
auth = request.headers.get('Authorization')
if not auth:
return None
parts = auth.split()
if len(parts) != 2 or parts[0].lower() != 'bearer':
return None
token = parts[1]
return self.authenticate_credentials(token)
def authenticate_credentials(self, token: str) -> Optional[Tuple[Any, Any]]:
"""
Authenticate the given OAuth2 token.
Args:
token: The OAuth2 token
Returns:
Tuple of (user, token) or None if authentication fails
"""
# Here you would typically validate the OAuth2 token
# For now, we'll use a mock implementation
if token == 'valid_oauth_token':
user = MockUser(id=1, username='oauth_user', scope=['read', 'write'])
return (user, token)
return None
def authenticate_header(self, request: Request) -> Optional[str]:
"""Return the WWW-Authenticate header."""
return 'Bearer realm="api"'
class HMACAuthentication(BaseAuthentication):
"""
HMAC-based authentication for API keys.
"""
def authenticate(self, request: Request) -> Optional[Tuple[Any, Any]]:
"""Authenticate using HMAC signature."""
signature = request.headers.get('X-Signature')
timestamp = request.headers.get('X-Timestamp')
api_key = request.headers.get('X-API-Key')
if not all([signature, timestamp, api_key]):
return None
return self.authenticate_credentials(api_key, signature, timestamp, request)
def authenticate_credentials(self, api_key: str, signature: str, timestamp: str, request: Request) -> Optional[Tuple[Any, Any]]:
"""
Authenticate the given HMAC credentials.
Args:
api_key: The API key
signature: The HMAC signature
timestamp: The timestamp
request: The request object
Returns:
Tuple of (user, auth) or None if authentication fails
"""
# Here you would typically validate the HMAC signature
# For now, we'll use a mock implementation
if api_key == 'valid_api_key' and signature == 'valid_signature':
user = MockUser(id=1, username='api_user', api_key=api_key)
return (user, api_key)
return None
def authenticate_header(self, request: Request) -> Optional[str]:
"""Return the WWW-Authenticate header."""
return 'HMAC realm="api"'
class JWTAuthentication(BaseAuthentication):
"""
JWT token-based authentication.
"""
def authenticate(self, request: Request) -> Optional[Tuple[Any, Any]]:
"""Authenticate using JWT token."""
auth = request.headers.get('Authorization')
if not auth:
return None
parts = auth.split()
if len(parts) != 2 or parts[0].lower() != 'bearer':
return None
token = parts[1]
return self.authenticate_credentials(token)
def authenticate_credentials(self, token: str) -> Optional[Tuple[Any, Any]]:
"""
Authenticate the given JWT token.
Args:
token: The JWT token
Returns:
Tuple of (user, token) or None if authentication fails
"""
# Here you would typically decode and validate the JWT token
# For now, we'll use a mock implementation
if token == 'valid_jwt_token':
user = MockUser(id=1, username='jwt_user')
return (user, token)
return None
def authenticate_header(self, request: Request) -> Optional[str]:
"""Return the WWW-Authenticate header."""
return 'Bearer realm="api"'
# Mock user class for testing
class MockUser:
"""Mock user class for authentication testing."""
def __init__(self, id: int, username: str = None, is_staff: bool = False, scope: list = None, api_key: str = None):
self.id = id
self.username = username or f'user_{id}'
self.is_staff = is_staff
self.is_authenticated = True
self.scope = scope or []
self.api_key = api_key
def has_perm(self, perm: str) -> bool:
"""Check if user has permission."""
if self.is_staff:
return True
return perm in getattr(self, 'permissions', [])
def has_perms(self, perms: list) -> bool:
"""Check if user has all permissions."""
return all(self.has_perm(perm) for perm in perms)
# FastAPI dependency for authentication
def get_current_user(request: Request) -> Optional[MockUser]:
"""
FastAPI dependency to get the current authenticated user.
Args:
request: The request object
Returns:
The authenticated user or None
"""
# This would typically use the authentication classes
# For now, we'll return a mock user
return MockUser(id=1, username='current_user')
def get_required_user(request: Request) -> MockUser:
"""
FastAPI dependency to get the current authenticated user, raising an exception if not authenticated.
Args:
request: The request object
Returns:
The authenticated user
Raises:
HTTPException: If user is not authenticated
"""
user = get_current_user(request)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Authentication required",
headers={"WWW-Authenticate": "Bearer"},
)
return user

Xet Storage Details

Size:
11.2 kB
·
Xet hash:
857a11b921e199c280cdbdd81e8b822e00de5773efaafa86c6c361883296560b

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.