본문 바로가기
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 API는 오래 유지되는 API key를 사용하고 OAuth가 기본 인증은 아니야. OAuth bearer token은 OpenAI 호환 요청 구조를 다른 기업 identity layer 뒤에 둔 별도 host에서 나타나.

OAuth를 만나는 곳

  • ChatGPT Pro endpoint — cwkPippa Codex가 사용해.
  • Google Cloud Code Assist — cwkPippa Gemini가 사용해.
  • Azure OpenAI와 Entra ID
  • 기업 gateway — 사용자별 인증 문맥을 요구해.

공통 인증 흐름

refresh token을 안전하게 보관하고, 5~60분짜리 짧은 bearer token으로 교환해 Authorization: Bearer ... header에 넣어. 401이 오면 한 번 refresh한 뒤 원래 호출을 한 번만 다시 시도해. 이 wrapper를 OAuth 앞단을 둔 provider들이 함께 쓸 수 있어.

cwkPippa Codex adapter

ChatGPT Pro session cookie로 짧은 bearer token을 교환하고, 401이면 refresh한 뒤 한 번 다시 시도해. 인증을 제외한 request body 구조는 openai.com과 호환돼.

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

올바른 token에는 200, 그 외에는 401을 반환하는 OAuth 보호 provider mock을 만들어. Python adapter로 감싸 401이면 자동 refresh하고 원래 호출을 한 번만 다시 시도해.

Progress

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

댓글 0

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

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