Skip to content

Commit

Permalink
feat: several endpoint types can be registered
Browse files Browse the repository at this point in the history
AuthorizationServer.register_endpoint can be called several times for
one kind of endpoint.
  • Loading branch information
azmeuk committed Sep 5, 2023
1 parent 1846d6a commit 019e97a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
19 changes: 12 additions & 7 deletions authlib/oauth2/rfc6749/authorization_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .errors import (
OAuth2Error,
InvalidScopeError,
ContinueIteration,
UnsupportedResponseTypeError,
UnsupportedGrantTypeError,
)
Expand Down Expand Up @@ -186,7 +187,8 @@ def register_endpoint(self, endpoint_cls):
:param endpoint_cls: A endpoint class
"""
self._endpoints[endpoint_cls.ENDPOINT_NAME] = endpoint_cls(self)
endpoints = self._endpoints.setdefault(endpoint_cls.ENDPOINT_NAME, [])
endpoints.append(endpoint_cls(self))

def get_authorization_grant(self, request):
"""Find the authorization grant for current request.
Expand Down Expand Up @@ -231,12 +233,15 @@ def create_endpoint_response(self, name, request=None):
if name not in self._endpoints:
raise RuntimeError(f'There is no "{name}" endpoint.')

endpoint = self._endpoints[name]
request = endpoint.create_endpoint_request(request)
try:
return self.handle_response(*endpoint(request))
except OAuth2Error as error:
return self.handle_error_response(request, error)
endpoints = self._endpoints[name]
for endpoint in endpoints:
request = endpoint.create_endpoint_request(request)
try:
return self.handle_response(*endpoint(request))
except ContinueIteration:
continue
except OAuth2Error as error:
return self.handle_error_response(request, error)

def create_authorization_response(self, request=None, grant_user=None):
"""Validate authorization request and create authorization response.
Expand Down
7 changes: 7 additions & 0 deletions authlib/oauth2/rfc6749/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"""
from authlib.oauth2.base import OAuth2Error
from authlib.common.security import is_secure_transport
from authlib.common.errors import AuthlibBaseError


__all__ = [
'OAuth2Error',
Expand All @@ -42,6 +44,7 @@
'MissingAuthorizationError', 'UnsupportedTokenTypeError',
'MissingCodeException', 'MissingTokenException',
'MissingTokenTypeException', 'MismatchingStateException',
'ContinueIteration',
]


Expand Down Expand Up @@ -231,3 +234,7 @@ class MissingTokenTypeException(OAuth2Error):
class MismatchingStateException(OAuth2Error):
error = 'mismatching_state'
description = 'CSRF Warning! State not equal in request and response.'


class ContinueIteration(AuthlibBaseError):
pass

0 comments on commit 019e97a

Please sign in to comment.