Skip to content

Commit

Permalink
feat: add async support for auth handler
Browse files Browse the repository at this point in the history
  • Loading branch information
CASUP001 authored and CASUP001 committed Nov 22, 2024
1 parent 4029312 commit 13de8f1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions robyn/authentication.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import Optional
from typing import Any, Coroutine, Optional, Union

from robyn.robyn import Headers, Identity, Request, Response
from robyn.status_codes import HTTP_401_UNAUTHORIZED
Expand Down Expand Up @@ -64,7 +64,7 @@ def unauthorized_response(self) -> Response:
)

@abstractmethod
def authenticate(self, request: Request) -> Optional[Identity]:
def authenticate(self, request: Request) -> Union[Optional[Identity], Coroutine[Any, Any, Optional[Identity]]]:
"""
Authenticates the user.
:param request: The request object.
Expand Down
9 changes: 7 additions & 2 deletions robyn/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,15 @@ def add_auth_middleware(self, endpoint: str):

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

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
Expand Down

0 comments on commit 13de8f1

Please sign in to comment.