C.W.K.
Stream
Lesson 05 of 07 · published

OAuth Authentication — non-OpenAI host

~22 min · oauth, bearer-tokens, non-openai

Level 0Tokenizer
0 XP0/54 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

openai.com 자체는 long-lived API key — OAuth 아님. OAuth bearer token 은 다른 host 에서 등장 — OpenAI-compatible wire shape 를 corporate identity layer 뒤에 두는 곳들.

OAuth 보이는 곳

  • ChatGPT Pro endpoint — cwkPippa Codex 가 사용
  • Google Cloud Code Assist — cwkPippa Gemini 가 사용
  • Azure OpenAI + Entra ID
  • 기업 gateway — user-context auth 요구

표준 패턴

Refresh token 보존 → 짧은 bearer token 으로 exchange (5-60 min) → Authorization: Bearer ... 로 wire → 401 시 refresh + retry 한 번. 같은 wrapper 를 모든 OAuth-fronted provider 에 재사용.

cwkPippa 의 Codex adapter

ChatGPT Pro session cookie → 짧은 bearer token exchange → 401 시 refresh 후 한 번 retry → 그 외엔 openai.com 와 같은 request body shape.

Code

OAuth bearer token in Authorization header·json
{
  "auth_mode": "chatgpt",
  "last_refresh": "2025-08-01T12:00:00Z",
  "tokens": {
    "access_token": "eyJ...",
    "id_token": "eyJ...",
    "refresh_token": "eyJ..."
  }
}
401 시 token refresh·python
import json
from pathlib import Path
from datetime import datetime, timezone, timedelta

CODEX_HOME = Path.home() / ".codex"
AUTH_FILE = CODEX_HOME / "auth.json"
STALE_THRESHOLD_DAYS = 8

def load_auth() -> dict:
    with open(AUTH_FILE) as f:
        return json.load(f)

def is_stale(auth: dict) -> bool:
    last_refresh_str = auth.get("last_refresh")
    if not last_refresh_str:
        return True
    last_refresh = datetime.fromisoformat(last_refresh_str.replace("Z", "+00:00"))
    return datetime.now(timezone.utc) - last_refresh > timedelta(days=STALE_THRESHOLD_DAYS)

def get_bearer_token() -> str:
    auth = load_auth()
    if auth.get("auth_mode") != "chatgpt":
        raise ValueError("Expected ChatGPT auth mode")
    if is_stale(auth):
        auth = refresh_tokens(auth)  # refresh via OAuth endpoint
    return auth["tokens"]["access_token"]

External links

Exercise

OAuth-protected provider mock (정상 token 200, 그 외 401). Python adapter 로 wrap — 401 시 자동 refresh, 원 호출 1 번 retry.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.