File size: 8,601 Bytes
6918275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from __future__ import annotations

import secrets
import webbrowser
from typing import Any

from plugins._oauth.helpers.providers.base import (
    CODEX_PROVIDER_ID,
    DUMMY_API_KEY,
    CallbackResult,
    LoginPollResult,
    LoginStartResult,
    OAuthProviderMetadata,
)
from plugins._oauth.helpers.state import (
    get_device_attempt,
    pop_device_attempt,
    put_attempt,
    put_device_attempt,
)


CODEX_DEFAULT_MODEL = "gpt-5.5"
CODEX_FALLBACK_CONFIG = {
    "enabled": True,
    "models": [],
    "proxy_base_path": "/oauth/codex",
    "callback_path": "/auth/callback",
    "open_browser_from_server": False,
}


class CodexOAuthProvider:
    provider_id = CODEX_PROVIDER_ID

    def metadata(self) -> OAuthProviderMetadata:
        cfg = _codex_config()
        models = list(cfg["models"])
        return OAuthProviderMetadata(
            provider_id=CODEX_PROVIDER_ID,
            display_name="Codex/ChatGPT",
            short_name="Codex",
            model_provider_id=CODEX_PROVIDER_ID,
            icon="openai",
            auth_flow="device_code",
            default_model=models[0] if models else CODEX_DEFAULT_MODEL,
            default_models=models,
            proxy_base_path=cfg["proxy_base_path"],
            callback_path=cfg["callback_path"],
        )

    def status(self) -> dict[str, Any]:
        from plugins._oauth.helpers import codex

        cfg = _codex_config()
        return {
            **self.metadata().to_dict(),
            **codex.status(),
            "enabled": cfg["enabled"],
            "proxy_base_path": cfg["proxy_base_path"],
            "callback_path": cfg["callback_path"],
            "v1_base_path": f'{cfg["proxy_base_path"]}/v1',
        }

    def start_login(self, input: dict[str, Any] | None = None, request: Any = None) -> LoginStartResult:
        del input, request
        from plugins._oauth.helpers import codex

        cfg = _codex_config()
        if not cfg["enabled"]:
            return LoginStartResult(
                ok=False,
                provider_id=CODEX_PROVIDER_ID,
                flow="device_code",
                error="Codex/ChatGPT account connection is disabled.",
            )

        try:
            device = codex.request_device_code()
            attempt_id = secrets.token_urlsafe(24)
            attempt = put_device_attempt(
                attempt_id,
                device["device_auth_id"],
                device["user_code"],
                device["interval"],
                device["expires_at"],
                provider_id=CODEX_PROVIDER_ID,
            )
        except Exception as exc:
            return LoginStartResult(
                ok=False,
                provider_id=CODEX_PROVIDER_ID,
                flow="device_code",
                error=str(exc),
            )

        return LoginStartResult(
            ok=True,
            provider_id=CODEX_PROVIDER_ID,
            flow="device_code",
            attempt_id=attempt.attempt_id,
            verification_url=device["verification_url"],
            user_code=attempt.user_code,
            interval=attempt.interval,
            expires_at=attempt.expires_at,
        )

    def start_browser_login(
        self,
        input: dict[str, Any] | None = None,
        request: Any = None,
    ) -> LoginStartResult:
        del input
        cfg = _codex_config()
        if not cfg["enabled"]:
            return LoginStartResult(
                ok=False,
                provider_id=CODEX_PROVIDER_ID,
                flow="browser_pkce",
                error="Codex/ChatGPT account connection is disabled.",
            )

        from plugins._oauth.helpers import codex

        redirect_uri = _redirect_uri(request, cfg["callback_path"])
        pkce = codex.generate_pkce()
        state = codex.generate_state()
        attempt = put_attempt(
            state,
            pkce.verifier,
            redirect_uri,
            provider_id=CODEX_PROVIDER_ID,
        )
        auth_url = codex.build_authorize_url(redirect_uri, state, pkce)

        if cfg["open_browser_from_server"]:
            try:
                webbrowser.open(auth_url)
            except Exception:
                pass

        return LoginStartResult(
            ok=True,
            provider_id=CODEX_PROVIDER_ID,
            flow="browser_pkce",
            auth_url=auth_url,
            redirect_uri=redirect_uri,
            expires_at=attempt.expires_at,
        )

    def poll_login(self, input: dict[str, Any] | None = None, request: Any = None) -> LoginPollResult:
        del request
        data = input or {}
        attempt_id = str(data.get("attempt_id") or "").strip()
        if not attempt_id:
            return LoginPollResult(
                ok=False,
                provider_id=CODEX_PROVIDER_ID,
                error="Missing device authorization attempt.",
            )

        attempt = get_device_attempt(attempt_id)
        if attempt is None:
            return LoginPollResult(
                ok=False,
                provider_id=CODEX_PROVIDER_ID,
                expired=True,
                error="Device authorization expired.",
            )
        if attempt.provider_id != CODEX_PROVIDER_ID:
            return LoginPollResult(
                ok=False,
                provider_id=CODEX_PROVIDER_ID,
                error="Device authorization provider mismatch.",
            )

        try:
            from plugins._oauth.helpers import codex

            result = codex.poll_device_authorization(
                attempt.device_auth_id,
                attempt.user_code,
            )
        except Exception as exc:
            return LoginPollResult(ok=False, provider_id=CODEX_PROVIDER_ID, error=str(exc))

        if result.get("completed"):
            pop_device_attempt(attempt_id)
            account_id = str(result.get("account_id") or "")
            return LoginPollResult(
                ok=True,
                provider_id=CODEX_PROVIDER_ID,
                completed=True,
                account_label=str(result.get("account_label") or account_id),
                account_id=account_id,
            )

        return LoginPollResult(
            ok=True,
            provider_id=CODEX_PROVIDER_ID,
            completed=False,
            interval=attempt.interval,
            expires_at=attempt.expires_at,
        )

    def complete_callback(
        self,
        args: dict[str, Any],
        request: Any = None,
    ) -> CallbackResult:
        del args, request
        return CallbackResult(
            ok=False,
            provider_id=CODEX_PROVIDER_ID,
            error="Codex OAuth callback is handled by compatibility routes.",
        )

    def manual_callback(
        self,
        input: dict[str, Any],
        request: Any = None,
    ) -> LoginPollResult:
        del input, request
        return LoginPollResult(
            ok=False,
            provider_id=CODEX_PROVIDER_ID,
            error="Codex uses device-code login in this UI.",
        )

    def models(self) -> list[str]:
        from plugins._oauth.helpers import codex

        return codex.fetch_models()

    def model_catalog(self) -> list[dict[str, Any]]:
        from plugins._oauth.helpers import codex

        return codex.fetch_model_catalog()

    def disconnect(self) -> dict[str, Any]:
        from plugins._oauth.helpers import codex

        return codex.disconnect_auth()

    def api_key(self) -> str:
        return DUMMY_API_KEY

    def register_routes(self, app: Any) -> None:
        del app
        return None


def _redirect_uri(request: Any, callback_path: str) -> str:
    origin = ""
    if request is not None:
        origin = (getattr(request, "headers", {}).get("Origin") or "").rstrip("/")
        if not _is_local_origin(origin):
            origin = getattr(request, "url_root", "").rstrip("/")
    return f"{origin}{callback_path}"


def _codex_config() -> dict[str, Any]:
    try:
        from plugins._oauth.helpers.config import codex_config

        return codex_config()
    except ModuleNotFoundError as exc:
        if exc.name and exc.name.startswith("plugins._oauth"):
            raise
        return dict(CODEX_FALLBACK_CONFIG)


def _is_local_origin(origin: str) -> bool:
    if not origin:
        return False
    return (
        origin.startswith("http://localhost:")
        or origin == "http://localhost"
        or origin.startswith("http://127.0.0.1:")
        or origin == "http://127.0.0.1"
        or origin.startswith("http://[::1]:")
        or origin == "http://[::1]"
    )