Repository navigation
feat: add caching for Token Vault connection token exchanges #139
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| """Caching for Token Vault (federated connection) exchanges: lookup and write.""" | ||
|
|
||
| import logging | ||
| import time | ||
| from typing import Any, Optional | ||
|
|
||
| from ..token_store import AbstractTokenStore, TokenSet | ||
| from .cache_keys import _normalized_scopes, token_vault_cache_key | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class TokenVaultCache: | ||
| """Caches Token Vault exchange results in a token store, keyed by caller and connection. | ||
|
|
||
| ApiClient builds one TokenVaultCache when a token_store is configured and delegates | ||
| every cache decision to it. | ||
| """ | ||
|
|
||
| def __init__(self, token_store: AbstractTokenStore) -> None: | ||
| self._store = token_store | ||
|
|
||
| def cache_key( | ||
| self, tenant: str, client_id: str, sub: Any, connection: str, login_hint: Optional[str] = None | ||
| ) -> Optional[str]: | ||
| """Return the cache key, or None when sub is not a usable string (cache skipped with a warning).""" | ||
| if not isinstance(sub, str) or not sub: | ||
| logger.warning("Access token has no usable sub claim, skipping Token Vault cache") | ||
| return None | ||
| return token_vault_cache_key( | ||
| sub=sub, connection=connection, tenant=tenant, client_id=client_id, login_hint=login_hint | ||
| ) | ||
|
|
||
| async def lookup(self, key: str) -> Optional[dict]: | ||
| """Return a live cached token or None. Miss cases: absent, malformed, expired.""" | ||
| cached: Optional[TokenSet] = None | ||
| try: | ||
| cached = await self._store.get(key) | ||
| except Exception as exc: | ||
| logger.warning("Token store read failed, treating as cache miss: %s", exc) | ||
| return None | ||
|
|
||
| if cached is None: | ||
| return None | ||
| access_token = cached.get("access_token") | ||
| expires_at = cached.get("expires_at") | ||
| if not isinstance(access_token, str) or not access_token or not isinstance(expires_at, int): | ||
| logger.warning("Token store returned a malformed entry, treating as cache miss") | ||
| return None | ||
| now = int(time.time()) | ||
| if expires_at <= now: | ||
| return None | ||
|
|
||
| return { | ||
| "access_token": access_token, | ||
| "expires_in": expires_at - now, | ||
| "expires_at": expires_at, | ||
| "scope": cached.get("granted_scopes") or "", | ||
| } | ||
|
|
||
| async def write(self, key: str, result: dict) -> None: | ||
| """Store a freshly exchanged connection token. Failures are logged and swallowed.""" | ||
| entry: TokenSet = { | ||
| "access_token": result["access_token"], | ||
| "expires_at": result["expires_at"], | ||
| "granted_scopes": _normalized_scopes(result.get("scope")), | ||
| } | ||
| try: | ||
| await self._store.set(key, entry) | ||
| except Exception as exc: | ||
| logger.warning("Token store write failed, token still returned: %s", exc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.