|
| 1 | +""" |
| 2 | +OIDC Authentication helpers for the CodeCarbon CLI. |
| 3 | +
|
| 4 | +Handles the full token lifecycle: browser-based login (Authorization Code + |
| 5 | +PKCE), credential storage, JWKS validation, and transparent refresh. |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +import os |
| 10 | +import webbrowser |
| 11 | +from http.server import BaseHTTPRequestHandler, HTTPServer |
| 12 | +from pathlib import Path |
| 13 | +from urllib.parse import parse_qs, urlparse |
| 14 | + |
| 15 | +import requests |
| 16 | +from authlib.common.security import generate_token |
| 17 | +from authlib.integrations.requests_client import OAuth2Session |
| 18 | +from authlib.jose import JsonWebKey |
| 19 | +from authlib.jose import jwt as jose_jwt |
| 20 | +from authlib.oauth2.rfc7636 import create_s256_code_challenge |
| 21 | + |
| 22 | +AUTH_CLIENT_ID = os.environ.get( |
| 23 | + "AUTH_CLIENT_ID", |
| 24 | + "codecarbon-cli", |
| 25 | +) |
| 26 | +AUTH_SERVER_WELL_KNOWN = os.environ.get( |
| 27 | + "AUTH_SERVER_WELL_KNOWN", |
| 28 | + "https://authentication.codecarbon.io/realms/codecarbon/.well-known/openid-configuration", |
| 29 | +) |
| 30 | + |
| 31 | +_REDIRECT_PORT = 8090 |
| 32 | +_REDIRECT_URI = f"http://localhost:{_REDIRECT_PORT}/callback" |
| 33 | +_CREDENTIALS_FILE = Path("./credentials.json") |
| 34 | + |
| 35 | + |
| 36 | +# ── OAuth callback server ─────────────────────────────────────────── |
| 37 | + |
| 38 | + |
| 39 | +class _CallbackHandler(BaseHTTPRequestHandler): |
| 40 | + """HTTP handler that captures the OAuth2 authorization callback.""" |
| 41 | + |
| 42 | + callback_url = None |
| 43 | + error = None |
| 44 | + |
| 45 | + def do_GET(self): |
| 46 | + _CallbackHandler.callback_url = f"http://localhost:{_REDIRECT_PORT}{self.path}" |
| 47 | + parsed = urlparse(self.path) |
| 48 | + params = parse_qs(parsed.query) |
| 49 | + |
| 50 | + if "error" in params: |
| 51 | + _CallbackHandler.error = params["error"][0] |
| 52 | + self.send_response(400) |
| 53 | + self.send_header("Content-Type", "text/html") |
| 54 | + self.end_headers() |
| 55 | + msg = params.get("error_description", [params["error"][0]])[0] |
| 56 | + self.wfile.write( |
| 57 | + f"<html><body><h1>Login failed</h1><p>{msg}</p></body></html>".encode() |
| 58 | + ) |
| 59 | + else: |
| 60 | + self.send_response(200) |
| 61 | + self.send_header("Content-Type", "text/html") |
| 62 | + self.end_headers() |
| 63 | + self.wfile.write( |
| 64 | + b"<html><body><h1>Login successful!</h1>" |
| 65 | + b"<p>You can close this window.</p></body></html>" |
| 66 | + ) |
| 67 | + |
| 68 | + def log_message(self, format, *args): |
| 69 | + pass |
| 70 | + |
| 71 | + |
| 72 | +# ── OIDC discovery ────────────────────────────────────────────────── |
| 73 | + |
| 74 | + |
| 75 | +def _discover_endpoints(): |
| 76 | + """Fetch OpenID Connect discovery document.""" |
| 77 | + resp = requests.get(AUTH_SERVER_WELL_KNOWN) |
| 78 | + resp.raise_for_status() |
| 79 | + return resp.json() |
| 80 | + |
| 81 | + |
| 82 | +# ── Credential storage ────────────────────────────────────────────── |
| 83 | + |
| 84 | + |
| 85 | +def _save_credentials(tokens): |
| 86 | + """Save OAuth tokens to the local credentials file.""" |
| 87 | + with open(_CREDENTIALS_FILE, "w") as f: |
| 88 | + json.dump(tokens, f) |
| 89 | + |
| 90 | + |
| 91 | +def _load_credentials(): |
| 92 | + """Load OAuth tokens from the local credentials file.""" |
| 93 | + with open(_CREDENTIALS_FILE, "r") as f: |
| 94 | + return json.load(f) |
| 95 | + |
| 96 | + |
| 97 | +# ── Token validation & refresh ────────────────────────────────────── |
| 98 | + |
| 99 | + |
| 100 | +def _validate_access_token(access_token: str) -> bool: |
| 101 | + """Validate access token against the current OIDC provider's JWKS. |
| 102 | +
|
| 103 | + Returns False when the signature or expiry check fails (wrong provider, |
| 104 | + expired, tampered). Returns True on network errors so the caller can |
| 105 | + fall through to the API and let the server decide. |
| 106 | + """ |
| 107 | + try: |
| 108 | + discovery = _discover_endpoints() |
| 109 | + jwks_resp = requests.get(discovery["jwks_uri"]) |
| 110 | + jwks_resp.raise_for_status() |
| 111 | + keyset = JsonWebKey.import_key_set(jwks_resp.json()) |
| 112 | + claims = jose_jwt.decode(access_token, keyset) |
| 113 | + claims.validate() |
| 114 | + return True |
| 115 | + except requests.RequestException: |
| 116 | + return True # Can't reach auth server — let the API handle it |
| 117 | + except Exception: |
| 118 | + return False |
| 119 | + |
| 120 | + |
| 121 | +def _refresh_tokens(refresh_token: str) -> dict: |
| 122 | + """Exchange a refresh token for a new token set via the OIDC token endpoint.""" |
| 123 | + discovery = _discover_endpoints() |
| 124 | + resp = requests.post( |
| 125 | + discovery["token_endpoint"], |
| 126 | + data={ |
| 127 | + "grant_type": "refresh_token", |
| 128 | + "refresh_token": refresh_token, |
| 129 | + "client_id": AUTH_CLIENT_ID, |
| 130 | + }, |
| 131 | + ) |
| 132 | + resp.raise_for_status() |
| 133 | + return resp.json() |
| 134 | + |
| 135 | + |
| 136 | +# ── Public API ────────────────────────────────────────────────────── |
| 137 | + |
| 138 | + |
| 139 | +def authorize(): |
| 140 | + """Run the OAuth2 Authorization Code flow with PKCE.""" |
| 141 | + discovery = _discover_endpoints() |
| 142 | + |
| 143 | + session = OAuth2Session( |
| 144 | + client_id=AUTH_CLIENT_ID, |
| 145 | + redirect_uri=_REDIRECT_URI, |
| 146 | + scope="openid offline_access", |
| 147 | + token_endpoint_auth_method="none", |
| 148 | + ) |
| 149 | + |
| 150 | + code_verifier = generate_token(48) |
| 151 | + code_challenge = create_s256_code_challenge(code_verifier) |
| 152 | + |
| 153 | + uri, state = session.create_authorization_url( |
| 154 | + discovery["authorization_endpoint"], |
| 155 | + code_challenge=code_challenge, |
| 156 | + code_challenge_method="S256", |
| 157 | + ) |
| 158 | + |
| 159 | + _CallbackHandler.callback_url = None |
| 160 | + _CallbackHandler.error = None |
| 161 | + |
| 162 | + server = HTTPServer(("localhost", _REDIRECT_PORT), _CallbackHandler) |
| 163 | + |
| 164 | + print("Opening browser for authentication...") |
| 165 | + webbrowser.open(uri) |
| 166 | + |
| 167 | + server.handle_request() |
| 168 | + server.server_close() |
| 169 | + |
| 170 | + if _CallbackHandler.error: |
| 171 | + raise ValueError(f"Authorization failed: {_CallbackHandler.error}") |
| 172 | + |
| 173 | + if not _CallbackHandler.callback_url: |
| 174 | + raise ValueError("Authorization failed: no callback received") |
| 175 | + |
| 176 | + token = session.fetch_token( |
| 177 | + discovery["token_endpoint"], |
| 178 | + authorization_response=_CallbackHandler.callback_url, |
| 179 | + code_verifier=code_verifier, |
| 180 | + ) |
| 181 | + |
| 182 | + _save_credentials(token) |
| 183 | + return token |
| 184 | + |
| 185 | + |
| 186 | +def get_access_token() -> str: |
| 187 | + """Return a valid access token, refreshing or failing with a clear message.""" |
| 188 | + try: |
| 189 | + creds = _load_credentials() |
| 190 | + except Exception as e: |
| 191 | + raise ValueError( |
| 192 | + "Not able to retrieve the access token, " |
| 193 | + f"please run `codecarbon login` first! (error: {e})" |
| 194 | + ) |
| 195 | + |
| 196 | + access_token = creds.get("access_token") |
| 197 | + if not access_token: |
| 198 | + raise ValueError("No access token found. Please run `codecarbon login` first.") |
| 199 | + |
| 200 | + # Fast path: token is still valid for the current OIDC provider |
| 201 | + if _validate_access_token(access_token): |
| 202 | + return access_token |
| 203 | + |
| 204 | + # Token is expired or was issued by a different provider — try refresh |
| 205 | + refresh_token = creds.get("refresh_token") |
| 206 | + if refresh_token: |
| 207 | + try: |
| 208 | + new_tokens = _refresh_tokens(refresh_token) |
| 209 | + _save_credentials(new_tokens) |
| 210 | + return new_tokens["access_token"] |
| 211 | + except Exception: |
| 212 | + pass |
| 213 | + |
| 214 | + # Refresh failed — credentials are stale (e.g. auth provider migrated) |
| 215 | + _CREDENTIALS_FILE.unlink(missing_ok=True) |
| 216 | + raise ValueError( |
| 217 | + "Your session has expired or the authentication provider has changed. " |
| 218 | + "Please run `codecarbon login` again." |
| 219 | + ) |
| 220 | + |
| 221 | + |
| 222 | +def get_id_token() -> str: |
| 223 | + """Return the stored OIDC id_token.""" |
| 224 | + creds = _load_credentials() |
| 225 | + return creds["id_token"] |
0 commit comments