AnonymousSpottedToad/securityPatternsRecovery / codebase /repos /python /bluesky_tiled /tiled /client /auth.py
| import os | |
| from pathlib import Path | |
| from typing import Dict, Optional | |
| import httpx | |
| from .utils import SerializableLock, handle_error | |
| class CannotRefreshAuthentication(Exception): | |
| pass | |
| class TiledAuth(httpx.Auth): | |
| def __init__( | |
| self, | |
| refresh_url, | |
| csrf_token, | |
| token_directory, | |
| client_id: Optional[str] = None, | |
| scopes: Optional[str] = None, | |
| ): | |
| self.refresh_url = refresh_url | |
| self.csrf_token = csrf_token | |
| if token_directory is not None: | |
| token_directory = Path(token_directory) | |
| token_directory.mkdir(exist_ok=True, parents=True) | |
| self._check_writable_token_directory(token_directory) | |
| self.token_directory = token_directory | |
| self._sync_lock = SerializableLock() | |
| # self._async_lock = asyncio.Lock() | |
| self.tokens = {} | |
| self.client_id = client_id | |
| self.scopes = scopes | |
| def _check_writable_token_directory(token_directory): | |
| if not os.access(token_directory, os.W_OK | os.X_OK): | |
| raise ValueError(f"The token_directory {token_directory} is not writable.") | |
| def __getstate__(self): | |
| return ( | |
| self.refresh_url, | |
| self.csrf_token, | |
| self.token_directory, | |
| self._sync_lock, | |
| ) | |
| def __setstate__(self, state): | |
| # Omit the cached tokens (self.tokens) from the pickled bundle because: | |
| # 1. Sometimes users persist pickled data and handle it insecurely. | |
| # 2. The un-serialized instance of TiledAuth will need to be able to | |
| # read/write from token_directory anyway. | |
| self.tokens = {} | |
| (refresh_url, csrf_token, token_directory, sync_lock) = state | |
| self.refresh_url = refresh_url | |
| self.csrf_token = csrf_token | |
| if token_directory is not None: | |
| self._check_writable_token_directory(token_directory) | |
| self.token_directory = token_directory | |
| self._sync_lock = sync_lock | |
| def sync_get_token(self, key, reload_from_disk=False): | |
| if not reload_from_disk: | |
| # Try in-memory cached copy first. | |
| try: | |
| return self.tokens[key] | |
| except KeyError: | |
| # Continue on to try to read token form disk. | |
| pass | |
| if self.token_directory is None: | |
| # No token directory was configured. Give up. | |
| return None | |
| with self._sync_lock: | |
| filepath = self.token_directory / key | |
| try: | |
| with open(filepath, "r") as file: | |
| token = file.read() | |
| self.tokens[key] = token | |
| return token | |
| except FileNotFoundError: | |
| return None | |
| def sync_set_token(self, key, value): | |
| with self._sync_lock: | |
| if not isinstance(value, str): | |
| raise ValueError(f"Expected string value, got {value!r}") | |
| if self.token_directory is not None: | |
| filepath = self.token_directory / key | |
| filepath.touch(mode=0o600) # Set permissions. | |
| with open(filepath, "w") as file: | |
| file.write(value) | |
| self.tokens[key] = value # Update cached value. | |
| def sync_clear_token(self, key): | |
| with self._sync_lock: | |
| if self.token_directory is not None: | |
| filepath = self.token_directory / key | |
| filepath.unlink(missing_ok=True) | |
| self.tokens.pop(key, None) # Clear cached value. | |
| def sync_auth_flow(self, request, attempt=0): | |
| access_token = self.sync_get_token("access_token") | |
| if access_token is not None: | |
| request.headers["Authorization"] = f"Bearer {access_token}" | |
| response = yield request | |
| if (access_token is None) or (response.status_code == httpx.codes.UNAUTHORIZED): | |
| # Maybe the token cached in memory is stale. | |
| maybe_new_access_token = self.sync_get_token( | |
| "access_token", reload_from_disk=True | |
| ) | |
| if (attempt < 2) and (maybe_new_access_token != access_token): | |
| return (yield from self.sync_auth_flow(request, attempt=1 + attempt)) | |
| if access_token is not None: | |
| # The access token is stale or otherwise invalid. Discard. | |
| self.sync_clear_token("access_token") | |
| # Begin refresh flow to get a new access token. | |
| refresh_token = self.sync_get_token("refresh_token", reload_from_disk=True) | |
| if refresh_token is None: | |
| raise CannotRefreshAuthentication( | |
| "No refresh token was found in token cache. " | |
| "Provide fresh credentials. " | |
| "For a given client c, use c.context.authenticate()." | |
| ) | |
| token_request = build_refresh_request( | |
| self.refresh_url, | |
| refresh_token, | |
| self.csrf_token, | |
| self.client_id, | |
| self.scopes, | |
| ) | |
| token_response = yield token_request | |
| if token_response.status_code in { | |
| httpx.codes.UNAUTHORIZED, | |
| httpx.codes.BAD_REQUEST, | |
| }: | |
| # Refreshing the token failed. | |
| # Discard the expired (or otherwise invalid) refresh_token. | |
| self.sync_clear_token("refresh_token") | |
| raise CannotRefreshAuthentication( | |
| "Server rejected attempt to refresh token. " | |
| "Provide fresh credentials. " | |
| "For a given client c, use c.context.authenticate()." | |
| ) | |
| handle_error(token_response) | |
| token_response.read() | |
| tokens = token_response.json() | |
| # If we get this far, refreshing authentication worked. | |
| # Store the new refresh token. | |
| self.sync_tokens(tokens) | |
| request.headers["Authorization"] = f"Bearer {tokens['access_token']}" | |
| yield request | |
| def sync_tokens(self, tokens: Dict[str, str]): | |
| self.sync_set_token("refresh_token", tokens["refresh_token"]) | |
| self.sync_set_token("access_token", tokens["access_token"]) | |
| if id_token := tokens.get("id_token"): | |
| self.sync_set_token("id_token", id_token) | |
| async def async_get_token(self, key): | |
| raise NotImplementedError("Async support is planned but not yet implemented.") | |
| async def async_set_token(self, key, token): | |
| raise NotImplementedError("Async support is planned but not yet implemented.") | |
| async def async_clear_token(self, key): | |
| raise NotImplementedError("Async support is planned but not yet implemented.") | |
| async def async_auth_flow(self, request): | |
| raise NotImplementedError("Async support is planned but not yet implemented.") | |
| def build_refresh_request( | |
| refresh_url, | |
| refresh_token, | |
| csrf_token, | |
| client_id: Optional[str] = None, | |
| scopes: Optional[str] = None, | |
| ): | |
| if client_id: | |
| return httpx.Request( | |
| "POST", | |
| refresh_url, | |
| data={ | |
| "client_id": client_id, | |
| "grant_type": "refresh_token", | |
| "refresh_token": refresh_token, | |
| **({"scope": scopes} if scopes else {}), | |
| }, | |
| headers={"Content-Type": "application/x-www-form-urlencoded"}, | |
| ) | |
| else: | |
| return httpx.Request( | |
| "POST", | |
| refresh_url, | |
| json={"refresh_token": refresh_token}, | |
| # Submit CSRF token in both header and cookie. | |
| # https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#double-submit-cookie | |
| headers={"x-csrf": csrf_token}, | |
| ) | |
Xet Storage Details
- Size:
- 7.87 kB
- Xet hash:
- 47b491380bb75bd986a79e11fb6b32f5e695e284092b1b714a7fcbdaed99d009
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.