Skip to content

Commit

Permalink
Merge pull request #621 from TeskaLabs/feature/discovery-request-context
Browse files Browse the repository at this point in the history
Implicit discovery session authorization using Request context variable
  • Loading branch information
byewokko authored Oct 17, 2024
2 parents 7f7a0a7 + 6694cc0 commit 6da2080
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 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
@@ -1,3 +1,6 @@
import contextvars

Tenant = contextvars.ContextVar("Tenant")

# 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

0 comments on commit 6da2080

Please sign in to comment.