presenton/electron/servers/fastapi/utils/oauth/pkce.py
sudipnext 00be7ee946 feat: integrate Codex OAuth flow and enhance user configuration
- Added Codex-related fields to UserConfig and LLMConfig for managing OAuth tokens and model settings.
- Implemented Codex authentication endpoints and integrated them into the application.
- Enhanced LLMClient to support Codex as a valid provider, including token management and refresh logic.
- Updated UI components to include Codex configuration options and authentication status handling.
- Refactored environment variable utilities to accommodate Codex-specific settings.
2026-02-28 09:08:10 +05:45

23 lines
652 B
Python

"""
PKCE utilities using Python's secrets and hashlib.
Python port of pi-mono-main/packages/ai/src/utils/oauth/pkce.ts
"""
import base64
import hashlib
import secrets
def generate_pkce() -> tuple[str, str]:
"""
Generate PKCE code verifier and challenge (S256 method).
Returns:
(verifier, challenge) — both base64url-encoded, no padding
"""
verifier_bytes = secrets.token_bytes(32)
verifier = base64.urlsafe_b64encode(verifier_bytes).rstrip(b"=").decode()
digest = hashlib.sha256(verifier.encode()).digest()
challenge = base64.urlsafe_b64encode(digest).rstrip(b"=").decode()
return verifier, challenge