halfapi/halfapi/lib/domain_middleware.py
Maxime Alves LIRMM f7879c6388 [release] 0.6.10
- Add "x-out" field in HTTP headers when "out" parameters are specified for a
  route
- Add "out" kwarg for not-async functions that specify it

- Hide data fields in args_check logs

- Fix testing lib for domains (add default secret and debug option)

- Domains now need to include the following variables in their __init__.py
    - __name__ (str, optional)
    - __id__ (str, optional)
- halfapi domain

- Mounts domain routers with their ACLs as decorator
- Configuration example files for systemd and a system-wide halfapi install
- Runs projects
- Handles JWT authentication middleware
2022-03-08 19:24:24 +01:00

67 lines
2.1 KiB
Python

"""
DomainMiddleware
"""
from starlette.datastructures import URL
from starlette.middleware.base import (BaseHTTPMiddleware,
RequestResponseEndpoint)
from starlette.requests import Request
from starlette.responses import Response
from ..logging import logger
class DomainMiddleware(BaseHTTPMiddleware):
"""
DomainMiddleware adds the api routes and acls to the following scope keys :
- api
- acl
"""
def __init__(self, app, domain):
""" app: HalfAPI instance
"""
logger.info('DomainMiddleware app:%s domain:%s', app, domain)
super().__init__(app)
self.domain = domain
self.request = None
async def dispatch(self, request: Request,
call_next: RequestResponseEndpoint) -> Response:
"""
Call of the route fonction (decorated or not)
"""
request.scope['domain'] = self.domain['name']
if hasattr(request.app, 'config') \
and isinstance(request.app.config, dict):
request.scope['config'] = { **request.app.config }
else:
logger.debug('%s', request.app)
logger.debug('%s', getattr(request.app, 'config', None))
response = await call_next(request)
if 'acl_pass' in request.scope:
# Set the http header "x-acl" if an acl was used on the route
response.headers['x-acl'] = request.scope['acl_pass']
if 'args' in request.scope:
# Set the http headers "x-args-required" and "x-args-optional"
if len(request.scope['args'].get('required', set())):
response.headers['x-args-required'] = \
','.join(request.scope['args']['required'])
if len(request.scope['args'].get('optional', set())):
response.headers['x-args-optional'] = \
','.join(request.scope['args']['optional'])
if len(request.scope.get('out', set())):
response.headers['x-out'] = \
','.join(request.scope['out'])
response.headers['x-domain'] = self.domain['name']
return response