from aiohttp import web from apollo_utils.service.exceptions import Unauthorized from server import config def no_authorize(obj): """View function decorator to skip authorization for.""" obj._no_authorize = True return obj def check_authorization(request: web.Request): """Check api key if view function is not marked with '_no_authorize'. Raises: Unauthorized: if api key is not set or not matched. """ if getattr(request.match_info.handler, "_no_authorize", False): return if config.DEBUG: if request.path.startswith("/api/doc") or request.path.startswith("/static"): return api_key = request.headers.get("Authorization") if not api_key: raise Unauthorized("Authorization key is not provided.") if api_key != config.AUTH_API_KEY: raise Unauthorized("Invalid authorization key.")