Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /calkit_calkit /hub /backend /app /github.py
| """GitHub related functionality.""" | |
| import os | |
| import threading | |
| import time | |
| from datetime import datetime | |
| import jwt | |
| import requests | |
| from fastapi import HTTPException | |
| from app.config import settings | |
| class GitHubAppNotConfigured(Exception): | |
| """Raised when the GitHub App private key isn't available.""" | |
| def _load_app_signing_key() -> bytes: | |
| """Return the GitHub App private key (PEM) from settings. | |
| Raises GitHubAppNotConfigured if GH_APP_PRIVATE_KEY isn't set. | |
| """ | |
| if not settings.GH_APP_PRIVATE_KEY: | |
| raise GitHubAppNotConfigured( | |
| "GitHub App private key not configured (set GH_APP_PRIVATE_KEY)" | |
| ) | |
| # Env vars commonly carry the PEM with escaped newlines; restore them so | |
| # the key parses whether it was provided escaped or as a real multi-line. | |
| return settings.GH_APP_PRIVATE_KEY.replace("\\n", "\n").encode() | |
| def create_app_token() -> str: | |
| client_id = os.environ["GH_CLIENT_ID"] | |
| signing_key = _load_app_signing_key() | |
| payload = { | |
| # Issued at time | |
| "iat": int(time.time()), | |
| # JWT expiration time (10 minutes maximum) | |
| "exp": int(time.time()) + 600, | |
| # GitHub App's client ID | |
| "iss": client_id, | |
| } | |
| # Create JWT | |
| encoded_jwt = jwt.encode(payload, signing_key, algorithm="RS256") | |
| return encoded_jwt | |
| # Cache of GitHub App installation tokens, keyed by (owner, repo) -> (token, | |
| # expiry_epoch). Installation tokens are valid ~1 hour; reusing them until just | |
| # before expiry means a GitHub-less user's request doesn't mint a fresh token | |
| # (two GitHub API calls) on every repo operation. This is a per-worker | |
| # in-process cache, so each worker mints at most once per repo per ~hour. | |
| _installation_token_cache: dict[tuple[str, str, bool], tuple[str, float]] = {} | |
| _installation_token_cache_lock = threading.Lock() | |
| # Refresh this long before GitHub's stated expiry so a cached token can't lapse | |
| # mid-request. | |
| _INSTALLATION_TOKEN_SAFETY_SECONDS = 300 | |
| def get_app_installation_token( | |
| owner_name: str, repo_name: str, read_only: bool = False | |
| ) -> str: | |
| """Return a GitHub App installation access token scoped to one repo. | |
| Used to perform git operations on behalf of users who have native Calkit | |
| access to a project but no personal GitHub token (e.g. email/Google | |
| signups). Tokens are cached in-process and reused until shortly before they | |
| expire. The caller must have authorized the user's access first. | |
| ``read_only`` narrows the token to ``contents: read``, so it can clone | |
| and fetch but not push. The shared read-only checkout takes one of | |
| these: it is read by everyone with access to the project, and a token | |
| that cannot write is a better guarantee than one that merely shouldn't. | |
| Cached separately, so a reader never picks up a writable token. | |
| """ | |
| cache_key = (owner_name.lower(), repo_name.lower(), read_only) | |
| now = time.time() | |
| with _installation_token_cache_lock: | |
| cached = _installation_token_cache.get(cache_key) | |
| if cached is not None and cached[1] > now: | |
| return cached[0] | |
| # Miss or expired: mint a fresh token. Done outside the lock so requests | |
| # for different repos don't serialize; a rare concurrent double-mint just | |
| # yields two valid tokens. | |
| token, expires_at = _mint_app_installation_token( | |
| owner_name, repo_name, read_only=read_only | |
| ) | |
| # ~50 min fallback if GitHub omits expires_at for some reason. | |
| expiry = now + 3000.0 | |
| if expires_at: | |
| try: | |
| parsed = datetime.fromisoformat(expires_at.replace("Z", "+00:00")) | |
| expiry = parsed.timestamp() - _INSTALLATION_TOKEN_SAFETY_SECONDS | |
| except ValueError: | |
| pass | |
| with _installation_token_cache_lock: | |
| _installation_token_cache[cache_key] = (token, expiry) | |
| return token | |
| def _mint_app_installation_token( | |
| owner_name: str, repo_name: str, read_only: bool = False | |
| ) -> tuple[str, str | None]: | |
| """Mint a fresh installation token, returning (token, expires_at).""" | |
| app_jwt = create_app_token() | |
| headers = { | |
| "Authorization": f"Bearer {app_jwt}", | |
| "Accept": "application/vnd.github+json", | |
| } | |
| resp = requests.get( | |
| f"https://api.github.com/repos/{owner_name}/{repo_name}/installation", | |
| headers=headers, | |
| timeout=15, | |
| ) | |
| if resp.status_code != 200: | |
| # Include GitHub's status and message so real causes are visible: a | |
| # 401 ("could not be decoded") means GH_APP_PRIVATE_KEY doesn't match | |
| # the App for GH_CLIENT_ID; a 404 means the App isn't installed on the | |
| # repo. Reporting a bare "installation not found" hides the difference. | |
| raise HTTPException( | |
| 502, | |
| "Could not look up the Calkit GitHub App installation for " | |
| f"{owner_name}/{repo_name}: GitHub returned {resp.status_code} " | |
| f"({resp.text[:200]})", | |
| ) | |
| installation_id = resp.json()["id"] | |
| resp = requests.post( | |
| f"https://api.github.com/app/installations/{installation_id}" | |
| "/access_tokens", | |
| headers=headers, | |
| json=( | |
| {"repositories": [repo_name], "permissions": {"contents": "read"}} | |
| if read_only | |
| # Otherwise the installation's own permissions, which is what a | |
| # push needs. Narrowing is allowed; widening is not. | |
| else {"repositories": [repo_name]} | |
| ), | |
| timeout=15, | |
| ) | |
| if resp.status_code not in (200, 201): | |
| raise HTTPException( | |
| 502, | |
| "Could not mint a Calkit GitHub App installation token for " | |
| f"{owner_name}/{repo_name}: GitHub returned {resp.status_code} " | |
| f"({resp.text[:200]})", | |
| ) | |
| data = resp.json() | |
| return data["token"], data.get("expires_at") | |
| def token_resp_text_to_dict(resp_text: str) -> dict: | |
| items = resp_text.split("&") | |
| out = {} | |
| for item in items: | |
| key, value = item.split("=") | |
| out[key] = value | |
| return out | |
| def resolve_email(access_token: str) -> tuple[str, bool]: | |
| """Pick the email to identify a GitHub user by, and say if it's verified. | |
| Returns the primary address when there is one, since that's the address | |
| the user thinks of as theirs, falling back to the first verified one and | |
| then to the first listed. The verified flag comes back with it rather | |
| than being enforced here: creating a brand new account from an | |
| unverified address is harmless, but attaching a GitHub identity to an | |
| account that already exists is not, and only the caller knows which of | |
| those it's doing. | |
| """ | |
| resp = requests.get( | |
| "https://api.github.com/user/emails", | |
| headers={"Authorization": f"Bearer {access_token}"}, | |
| ) | |
| emails = resp.json() if resp.status_code == 200 else None | |
| if not isinstance(emails, list): | |
| raise HTTPException(400, "Could not fetch your GitHub email addresses") | |
| if not emails: | |
| raise HTTPException(400, "Your GitHub account has no email address") | |
| primary = [e for e in emails if e.get("primary")] | |
| verified = [e for e in emails if e.get("verified")] | |
| chosen = (primary or verified or emails)[0] | |
| email = chosen.get("email") | |
| if not email: | |
| raise HTTPException(400, "Your GitHub account has no email address") | |
| return email, bool(chosen.get("verified")) | |
Xet Storage Details
- Size:
- 7.38 kB
- Xet hash:
- 0dae62864d6e9bd010038a9a66b60d0cc9b826e4844a618227d786762bb766df
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.