Skip to content

Commit

Permalink
Merge branch 'master' into feature/auth-contextvar
Browse files Browse the repository at this point in the history
# Conflicts:
#	asab/contextvars.py
  • Loading branch information
byewokko committed Oct 17, 2024
2 parents 365365b + 6da2080 commit 7ca59c0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
16 changes: 11 additions & 5 deletions asab/api/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
jwcrypto = None

from .. import Service
from ..contextvars import Tenant
from ..contextvars import Tenant, Request


L = logging.getLogger(__name__)
Expand Down Expand Up @@ -401,19 +401,25 @@ def session(
...
"""
_headers = {}
if isinstance(auth, aiohttp.web.Request):
# TODO: This should be the default option. Use contextvar to access the request.

if auth is None:
# By default, use the authorization from the incoming request
request = Request.get(None)
if request is not None:
_headers["Authorization"] = request.headers.get("Authorization")

elif isinstance(auth, aiohttp.web.Request):
assert "Authorization" in auth.headers
_headers["Authorization"] = auth.headers.get("Authorization")

elif auth == "internal":
if jwcrypto is None:
raise ModuleNotFoundError(
"You are trying to use internal auth without 'jwcrypto' installed. "
"Please run 'pip install jwcrypto' or install asab with 'authz' optional dependency."
)
_headers["Authorization"] = "Bearer {}".format(self.InternalAuthToken.serialize())
elif auth is None:
pass

else:
raise ValueError(
"Invalid 'auth' value. "
Expand Down
3 changes: 3 additions & 0 deletions asab/contextvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@

# Contains asab.web.auth.Authorization object
Authz = contextvars.ContextVar("Authz")

# Contains aiohttp.web.Request
Request = contextvars.ContextVar("Request")
14 changes: 14 additions & 0 deletions asab/web/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ..tls import SSLContextBuilder
from .service import WebService
from ..application import Application
from ..contextvars import Request

#

Expand Down Expand Up @@ -136,6 +137,19 @@ def __init__(self, websvc: WebService, config_section_name: str, config: typing.
preflight_paths = re.split(r"[,\s]+", preflight_str, re.MULTILINE)
self.add_preflight_handlers(preflight_paths)

@aiohttp.web.middleware
async def set_request_context(request: aiohttp.web.Request, handler):
"""
Make sure that the incoming aiohttp.web.Request is available via Request context variable
"""
request_ctx = Request.set(request)
try:
return await handler(request)
finally:
Request.reset(request_ctx)

self.WebApp.middlewares.append(set_request_context)


async def _start(self, app: Application):
await self.WebAppRunner.setup()
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def run(self):
'aiohttp>=3.8.3,<4',
'fastjsonschema>=2.16.2,<3',
'kazoo>=2.9.0,<3',
'PyYAML>=6.0,<7'
'PyYAML>=6.0,<7',
'yarl<1.15.3', # aiohttp dependency, later versions break Python 3.8 compatibility
],
extras_require={
'git': 'pygit2<1.12', # For Alpine 3.16 / Python 3.10, use pygit2>=1.9,<1.10
Expand Down

0 comments on commit 7ca59c0

Please sign in to comment.