forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth.ts
More file actions
274 lines (240 loc) · 7.71 KB
/
Copy pathoauth.ts
File metadata and controls
274 lines (240 loc) · 7.71 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { generatePKCE } from "@openauthjs/openauth/pkce";
import {
ANTIGRAVITY_CLIENT_ID,
ANTIGRAVITY_CLIENT_SECRET,
ANTIGRAVITY_REDIRECT_URI,
ANTIGRAVITY_SCOPES,
ANTIGRAVITY_ENDPOINT_FALLBACKS,
ANTIGRAVITY_LOAD_ENDPOINTS,
getAntigravityHeaders,
GEMINI_CLI_HEADERS,
} from "../constants";
import { createLogger } from "../plugin/logger";
import { calculateTokenExpiry } from "../plugin/auth";
const log = createLogger("oauth");
interface PkcePair {
challenge: string;
verifier: string;
}
interface AntigravityAuthState {
verifier: string;
projectId: string;
}
/**
* Result returned to the caller after constructing an OAuth authorization URL.
*/
export interface AntigravityAuthorization {
url: string;
verifier: string;
projectId: string;
}
interface AntigravityTokenExchangeSuccess {
type: "success";
refresh: string;
access: string;
expires: number;
email?: string;
projectId: string;
}
interface AntigravityTokenExchangeFailure {
type: "failed";
error: string;
}
export type AntigravityTokenExchangeResult =
| AntigravityTokenExchangeSuccess
| AntigravityTokenExchangeFailure;
interface AntigravityTokenResponse {
access_token: string;
expires_in: number;
refresh_token: string;
}
interface AntigravityUserInfo {
email?: string;
}
/**
* Encode an object into a URL-safe base64 string.
*/
function encodeState(payload: AntigravityAuthState): string {
return Buffer.from(JSON.stringify(payload), "utf8").toString("base64url");
}
/**
* Decode an OAuth state parameter back into its structured representation.
*/
function decodeState(state: string): AntigravityAuthState {
const normalized = state.replace(/-/g, "+").replace(/_/g, "/");
const padded = normalized.padEnd(normalized.length + ((4 - normalized.length % 4) % 4), "=");
const json = Buffer.from(padded, "base64").toString("utf8");
const parsed = JSON.parse(json);
if (typeof parsed.verifier !== "string") {
throw new Error("Missing PKCE verifier in state");
}
return {
verifier: parsed.verifier,
projectId: typeof parsed.projectId === "string" ? parsed.projectId : "",
};
}
/**
* Build the Antigravity OAuth authorization URL including PKCE and optional project metadata.
*/
export async function authorizeAntigravity(projectId = ""): Promise<AntigravityAuthorization> {
const pkce = (await generatePKCE()) as PkcePair;
const url = new URL("https://accounts.google.com/o/oauth2/v2/auth");
url.searchParams.set("client_id", ANTIGRAVITY_CLIENT_ID);
url.searchParams.set("response_type", "code");
url.searchParams.set("redirect_uri", ANTIGRAVITY_REDIRECT_URI);
url.searchParams.set("scope", ANTIGRAVITY_SCOPES.join(" "));
url.searchParams.set("code_challenge", pkce.challenge);
url.searchParams.set("code_challenge_method", "S256");
url.searchParams.set(
"state",
encodeState({ verifier: pkce.verifier, projectId: projectId || "" }),
);
url.searchParams.set("access_type", "offline");
url.searchParams.set("prompt", "consent");
return {
url: url.toString(),
verifier: pkce.verifier,
projectId: projectId || "",
};
}
const FETCH_TIMEOUT_MS = 10000;
async function fetchWithTimeout(
url: string,
options: RequestInit,
timeoutMs = FETCH_TIMEOUT_MS,
): Promise<Response> {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutMs);
try {
return await fetch(url, { ...options, signal: controller.signal });
} finally {
clearTimeout(timeout);
}
}
async function fetchProjectID(accessToken: string): Promise<string> {
const errors: string[] = [];
const loadHeaders: Record<string, string> = {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
"User-Agent": GEMINI_CLI_HEADERS["User-Agent"],
"Client-Metadata": getAntigravityHeaders()["Client-Metadata"],
};
const loadEndpoints = Array.from(
new Set<string>([...ANTIGRAVITY_LOAD_ENDPOINTS, ...ANTIGRAVITY_ENDPOINT_FALLBACKS]),
);
for (const baseEndpoint of loadEndpoints) {
try {
const url = `${baseEndpoint}/v1internal:loadCodeAssist`;
const response = await fetchWithTimeout(url, {
method: "POST",
headers: loadHeaders,
body: JSON.stringify({
metadata: {
ideType: "ANTIGRAVITY",
platform: process.platform === "win32" ? "WINDOWS" : "MACOS",
pluginType: "GEMINI",
},
}),
});
if (!response.ok) {
const message = await response.text().catch(() => "");
errors.push(
`loadCodeAssist ${response.status} at ${baseEndpoint}${
message ? `: ${message}` : ""
}`,
);
continue;
}
const data = await response.json();
if (typeof data.cloudaicompanionProject === "string" && data.cloudaicompanionProject) {
return data.cloudaicompanionProject;
}
if (
data.cloudaicompanionProject &&
typeof data.cloudaicompanionProject.id === "string" &&
data.cloudaicompanionProject.id
) {
return data.cloudaicompanionProject.id;
}
errors.push(`loadCodeAssist missing project id at ${baseEndpoint}`);
} catch (e) {
errors.push(
`loadCodeAssist error at ${baseEndpoint}: ${
e instanceof Error ? e.message : String(e)
}`,
);
}
}
if (errors.length) {
log.warn("Failed to resolve Antigravity project via loadCodeAssist", { errors: errors.join("; ") });
}
return "";
}
/**
* Exchange an authorization code for Antigravity CLI access and refresh tokens.
*/
export async function exchangeAntigravity(
code: string,
state: string,
): Promise<AntigravityTokenExchangeResult> {
try {
const { verifier, projectId } = decodeState(state);
const startTime = Date.now();
const tokenResponse = await fetch("https://oauth2.googleapis.com/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"User-Agent": GEMINI_CLI_HEADERS["User-Agent"],
},
body: new URLSearchParams({
client_id: ANTIGRAVITY_CLIENT_ID,
client_secret: ANTIGRAVITY_CLIENT_SECRET,
code,
grant_type: "authorization_code",
redirect_uri: ANTIGRAVITY_REDIRECT_URI,
code_verifier: verifier,
}),
});
if (!tokenResponse.ok) {
const errorText = await tokenResponse.text();
return { type: "failed", error: errorText };
}
const tokenPayload = (await tokenResponse.json()) as AntigravityTokenResponse;
const userInfoResponse = await fetch(
"https://www.googleapis.com/oauth2/v1/userinfo?alt=json",
{
headers: {
Authorization: `Bearer ${tokenPayload.access_token}`,
"User-Agent": GEMINI_CLI_HEADERS["User-Agent"],
},
},
);
const userInfo = userInfoResponse.ok
? ((await userInfoResponse.json()) as AntigravityUserInfo)
: {};
const refreshToken = tokenPayload.refresh_token;
if (!refreshToken) {
return { type: "failed", error: "Missing refresh token in response" };
}
let effectiveProjectId = projectId;
if (!effectiveProjectId) {
effectiveProjectId = await fetchProjectID(tokenPayload.access_token);
}
const storedRefresh = `${refreshToken}|${effectiveProjectId || ""}`;
return {
type: "success",
refresh: storedRefresh,
access: tokenPayload.access_token,
expires: calculateTokenExpiry(startTime, tokenPayload.expires_in),
email: userInfo.email,
projectId: effectiveProjectId || "",
};
} catch (error) {
return {
type: "failed",
error: error instanceof Error ? error.message : "Unknown error",
};
}
}