Skip to content

Commit

Permalink
fix: added support for inner handler as sync
Browse files Browse the repository at this point in the history
  • Loading branch information
IA-PieroCV committed Nov 23, 2024
1 parent 72b675f commit 0f96d7b
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions robyn/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,27 +283,39 @@ def add_route(

def add_auth_middleware(self, endpoint: str):
"""
This method adds an authentication middleware to the specified endpoint.
Adds an authentication middleware to the specified endpoint.
"""

injected_dependencies: dict = {}
injected_dependencies = {}

def decorator(handler):
@wraps(handler)
async def inner_handler(request: Request, *args):
if not self.authentication_handler:
raise AuthenticationNotConfiguredError()
# Check if `authenticate` is async or sync
if inspect.iscoroutinefunction(self.authentication_handler.authenticate):

@wraps(handler)
async def inner_handler(request: Request, *args):
if not self.authentication_handler:
raise AuthenticationNotConfiguredError()

if inspect.iscoroutinefunction(self.authentication_handler.authenticate):
identity = await self.authentication_handler.authenticate(request)
else:
identity = self.authentication_handler.authenticate(request)
if identity is None:
return self.authentication_handler.unauthorized_response
request.identity = identity

if identity is None:
return self.authentication_handler.unauthorized_response
request.identity = identity
return request
else:

@wraps(handler)
def inner_handler(request: Request, *args):
if not self.authentication_handler:
raise AuthenticationNotConfiguredError()

identity = self.authentication_handler.authenticate(request)
if identity is None:
return self.authentication_handler.unauthorized_response
request.identity = identity

return request
return request

self.add_route(
MiddlewareType.BEFORE_REQUEST,
Expand Down

0 comments on commit 0f96d7b

Please sign in to comment.