forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
255 lines (238 loc) · 7.11 KB
/
Copy pathserver.ts
File metadata and controls
255 lines (238 loc) · 7.11 KB
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
import { createServer } from "node:http";
import { ANTIGRAVITY_REDIRECT_URI } from "../constants";
interface OAuthListenerOptions {
/**
* How long to wait for the OAuth redirect before timing out (in milliseconds).
*/
timeoutMs?: number;
}
export interface OAuthListener {
/**
* Resolves with the callback URL once Google redirects back to the local server.
*/
waitForCallback(): Promise<URL>;
/**
* Cleanly stop listening for callbacks.
*/
close(): Promise<void>;
}
const redirectUri = new URL(ANTIGRAVITY_REDIRECT_URI);
const callbackPath = redirectUri.pathname || "/";
/**
* Starts a lightweight HTTP server that listens for the Antigravity OAuth redirect
* and resolves with the captured callback URL.
*/
export async function startOAuthListener(
{ timeoutMs = 5 * 60 * 1000 }: OAuthListenerOptions = {},
): Promise<OAuthListener> {
const port = redirectUri.port
? Number.parseInt(redirectUri.port, 10)
: redirectUri.protocol === "https:"
? 443
: 80;
const origin = `${redirectUri.protocol}//${redirectUri.host}`;
let settled = false;
let resolveCallback: (url: URL) => void;
let rejectCallback: (error: Error) => void;
let timeoutHandle: NodeJS.Timeout;
const callbackPromise = new Promise<URL>((resolve, reject) => {
resolveCallback = (url: URL) => {
if (settled) return;
settled = true;
if (timeoutHandle) clearTimeout(timeoutHandle);
resolve(url);
};
rejectCallback = (error: Error) => {
if (settled) return;
settled = true;
if (timeoutHandle) clearTimeout(timeoutHandle);
reject(error);
};
});
const successResponse = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Authentication Successful</title>
<style>
:root {
--bg: #FAFAFA;
--card-bg: #FFFFFF;
--text-primary: #1F2937;
--text-secondary: #6B7280;
--accent: #2563EB;
--success: #10B981;
--border: #E5E7EB;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #111827;
--card-bg: #1F2937;
--text-primary: #F9FAFB;
--text-secondary: #9CA3AF;
--accent: #3B82F6;
--success: #34D399;
--border: #374151;
}
}
body {
margin: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background: var(--bg);
color: var(--text-primary);
padding: 1rem;
}
.card {
background: var(--card-bg);
border-radius: 16px;
padding: 3rem 2rem;
width: 100%;
max-width: 400px;
text-align: center;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
border: 1px solid var(--border);
}
.icon-wrapper {
width: 64px;
height: 64px;
background: rgba(16, 185, 129, 0.1);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto 1.5rem;
}
.icon {
width: 32px;
height: 32px;
color: var(--success);
}
h1 {
font-size: 1.5rem;
font-weight: 600;
margin: 0 0 0.5rem;
letter-spacing: -0.025em;
}
p {
color: var(--text-secondary);
font-size: 0.95rem;
line-height: 1.5;
margin: 0 0 2rem;
}
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
background: var(--text-primary);
color: var(--card-bg);
font-weight: 500;
padding: 0.75rem 1.5rem;
border-radius: 8px;
text-decoration: none;
transition: opacity 0.2s;
font-size: 0.95rem;
border: none;
cursor: pointer;
width: 100%;
box-sizing: border-box;
}
.btn:hover {
opacity: 0.9;
}
.sub-text {
margin-top: 1rem;
font-size: 0.8rem;
color: var(--text-secondary);
}
</style>
</head>
<body>
<div class="card">
<div class="icon-wrapper">
<svg class="icon" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M5 13l4 4L19 7" />
</svg>
</div>
<h1>All set!</h1>
<p>You've successfully authenticated with Antigravity. You can now return to Opencode.</p>
<button class="btn" onclick="closeWindow()">Close this tab</button>
<div class="sub-text">Usage Tip: Most browsers block auto-closing. If the button doesn't work, please close the tab manually.</div>
</div>
<script>
function closeWindow() {
window.close();
// Fallback if window.close() is blocked
document.querySelector('.btn').textContent = "Tab cannot be closed automatically";
document.querySelector('.btn').style.opacity = "0.5";
document.querySelector('.btn').style.cursor = "default";
}
</script>
</body>
</html>`;
timeoutHandle = setTimeout(() => {
rejectCallback(new Error("Timed out waiting for OAuth callback"));
}, timeoutMs);
timeoutHandle.unref?.();
const server = createServer((request, response) => {
if (!request.url) {
response.writeHead(400, { "Content-Type": "text/plain" });
response.end("Invalid request");
return;
}
const url = new URL(request.url, origin);
if (url.pathname !== callbackPath) {
response.writeHead(404, { "Content-Type": "text/plain" });
response.end("Not found");
return;
}
response.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
response.end(successResponse);
resolveCallback(url);
setImmediate(() => {
server.close();
});
});
await new Promise<void>((resolve, reject) => {
const handleError = (error: NodeJS.ErrnoException) => {
server.off("error", handleError);
if (error.code === "EADDRINUSE") {
reject(new Error(
`Port ${port} is already in use. ` +
`Another process is occupying this port. ` +
`Please terminate the process or try again later.`
));
return;
}
reject(error);
};
server.once("error", handleError);
server.listen(port, () => {
server.off("error", handleError);
resolve();
});
});
server.on("error", (error) => {
rejectCallback(error instanceof Error ? error : new Error(String(error)));
});
return {
waitForCallback: () => callbackPromise,
close: () =>
new Promise<void>((resolve, reject) => {
server.close((error) => {
if (error && (error as NodeJS.ErrnoException).code !== "ERR_SERVER_NOT_RUNNING") {
reject(error);
return;
}
if (!settled) {
rejectCallback(new Error("OAuth listener closed before callback"));
}
resolve();
});
}),
};
}