-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implicit discovery session authorization using Request context variable #621
Conversation
@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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm not sure if asab.web.container
is the right place for this piece of code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is that needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at the moment mostly because of DiscoveryService.session()
. most discovery session calls are (or soon will need to be) authorized which means you have to pass the authorization from the incoming request to the outgoing request.
instead of requiring devs to always pass on the request
object (like DiscoveryService.session(auth=request)
), it can be taken automatically from the context variable and used as the default auth
value.
also, HTTP request objects should not be passed from the handler layer to the service layer. it makes sense to treat the request like a context variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am already using this :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems ok, but I really now cannot image a use case.
Summary
asab.web.WebContainer
stores every incoming request inasab.contextvars.Request
.DiscoveryService.session
now uses the Authorization header from the Request context by default, so it is no longer necessary to specifyauth=request
.Usage