MCPcopy
hub / github.com/ValueCell-ai/ClawX / startLocalOAuthServer

Function startLocalOAuthServer

electron/utils/openai-codex-oauth.ts:160–217  ·  view source on GitHub ↗
(state: string)

Source from the content-addressed store, hash-verified

158}
159
160function startLocalOAuthServer(state: string): Promise<OpenAICodexLocalServer | null> {
161 let lastCode: string | null = null;
162
163 const server = createServer((req, res) => {
164 try {
165 const url = new URL(req.url || '', 'http://localhost');
166 if (url.pathname !== '/auth/callback') {
167 res.statusCode = 404;
168 res.end('Not found');
169 return;
170 }
171
172 if (url.searchParams.get('state') !== state) {
173 res.statusCode = 400;
174 res.end('State mismatch');
175 return;
176 }
177
178 const code = url.searchParams.get('code');
179 if (!code) {
180 res.statusCode = 400;
181 res.end('Missing authorization code');
182 return;
183 }
184
185 lastCode = code;
186 res.statusCode = 200;
187 res.setHeader('Content-Type', 'text/html; charset=utf-8');
188 res.end(SUCCESS_HTML);
189 } catch {
190 res.statusCode = 500;
191 res.end('Internal error');
192 }
193 });
194
195 return new Promise((resolve) => {
196 // Bind dual-stack loopback so both `localhost` and `127.0.0.1` redirects work.
197 server
198 .listen(1455, () => {
199 resolve({
200 close: () => server.close(),
201 waitForCode: async () => {
202 const sleep = () => new Promise((r) => setTimeout(r, 100));
203 for (let i = 0; i < 600; i += 1) {
204 if (lastCode) {
205 return { code: lastCode };
206 }
207 await sleep();
208 }
209 return null;
210 },
211 });
212 })
213 .on('error', () => {
214 resolve(null);
215 });
216 });
217}

Callers 1

loginOpenAICodexOAuthFunction · 0.85

Calls 3

sleepFunction · 0.70
getMethod · 0.65
onMethod · 0.65

Tested by

no test coverage detected