Skip to content

Commit

Permalink
remove circular dependency between auth and key providers, use pubsub
Browse files Browse the repository at this point in the history
  • Loading branch information
byewokko committed Mar 11, 2025
1 parent eb3cbad commit deb0181
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
5 changes: 2 additions & 3 deletions asab/web/auth/providers/id_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ def __init__(self, app, public_key_providers: typing.Iterable[PublicKeyProviderA

self.Authorizations = {}

self.App.PubSub.subscribe("PublicKey.updated!", self.collect_keys)
self.App.PubSub.subscribe("Application.housekeeping!", self._delete_invalid_authorizations)
self.App.TaskService.schedule(self._update_public_keys())


def register_key_provider(self, provider: PublicKeyProviderABC):
if self not in provider.AuthProviders:
provider.AuthProviders.add(self)
self._KeyProviders.add(provider)
self.collect_keys()

Expand All @@ -54,7 +53,7 @@ async def authorize(self, request: aiohttp.web.Request) -> Authorization:
return authz


def collect_keys(self):
def collect_keys(self, *args, **kwargs):
"""
Collect public keys from all key providers into a single trusted JWK set.
"""
Expand Down
4 changes: 1 addition & 3 deletions asab/web/auth/providers/key_providers/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class PublicKeyProviderABC(abc.ABC):

def __init__(self, app):
self.App = app
self.AuthProviders = set() # Auth providers that use this public key provider
self.TaskService = self.App.get_service("asab.TaskService")
self.PublicKeySet: jwcrypto.jwk.JWKSet = jwcrypto.jwk.JWKSet()

Expand All @@ -42,5 +41,4 @@ def _set_keys(self, keys: typing.Optional[typing.Union[jwcrypto.jwk.JWK, jwcrypt
else:
raise ValueError("Invalid public_key type.")

for auth_provider in self.AuthProviders:
auth_provider.collect_keys()
self.App.PubSub.publish("PublicKey.updated!", self)

0 comments on commit deb0181

Please sign in to comment.