from flask import request from utils.exceptions import Unauthorized def get_auth_token(): """Get access token from Authorization header. Returns: str: Authorization token. Raises: Unauthorized: If token is missing or invalid. """ auth = request.headers.get("Authorization") if not auth: raise Unauthorized("Authorization header is expected.") parts = auth.split() if parts[0].lower() != "bearer": raise Unauthorized("Authorization header must contain a Bearer token.") elif len(parts) == 1: raise Unauthorized("Token is missing.") elif len(parts) > 2: raise Unauthorized("Invalid token.") return parts[1]