Merge pull request #3254 from sususu98/fix/antigravity-project-id-onboard
fix: require antigravity project id
This commit is contained in:
@@ -48,10 +48,76 @@ func NewAntigravityAuth(cfg *config.Config, httpClient *http.Client) *Antigravit
|
||||
}
|
||||
}
|
||||
|
||||
func (o *AntigravityAuth) loadCodeAssistUserAgent() string {
|
||||
func (o *AntigravityAuth) shortUserAgent() string {
|
||||
return misc.AntigravityRequestUserAgent("")
|
||||
}
|
||||
|
||||
func (o *AntigravityAuth) nodeUserAgent() string {
|
||||
return misc.AntigravityLoadCodeAssistUserAgent("")
|
||||
}
|
||||
|
||||
func antigravityLoadCodeAssistMetadata() map[string]string {
|
||||
return map[string]string{
|
||||
"ideType": "ANTIGRAVITY",
|
||||
}
|
||||
}
|
||||
|
||||
func antigravityControlPlaneMetadata(userAgent string) map[string]string {
|
||||
return map[string]string{
|
||||
"ide_type": "ANTIGRAVITY",
|
||||
"ide_version": misc.AntigravityVersionFromUserAgent(userAgent),
|
||||
"ide_name": "antigravity",
|
||||
}
|
||||
}
|
||||
|
||||
func extractCloudaicompanionProject(data map[string]any) string {
|
||||
if data == nil {
|
||||
return ""
|
||||
}
|
||||
for _, key := range []string{"cloudaicompanionProject", "projectId", "project"} {
|
||||
switch value := data[key].(type) {
|
||||
case string:
|
||||
if trimmed := strings.TrimSpace(value); trimmed != "" {
|
||||
return trimmed
|
||||
}
|
||||
case map[string]any:
|
||||
if id, ok := value["id"].(string); ok {
|
||||
if trimmed := strings.TrimSpace(id); trimmed != "" {
|
||||
return trimmed
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func defaultAntigravityTierID(loadResp map[string]any) string {
|
||||
if tiers, okTiers := loadResp["allowedTiers"].([]any); okTiers {
|
||||
for _, rawTier := range tiers {
|
||||
tier, okTier := rawTier.(map[string]any)
|
||||
if !okTier {
|
||||
continue
|
||||
}
|
||||
if isDefault, okDefault := tier["isDefault"].(bool); !okDefault || !isDefault {
|
||||
continue
|
||||
}
|
||||
if id, okID := tier["id"].(string); okID {
|
||||
if trimmed := strings.TrimSpace(id); trimmed != "" {
|
||||
return trimmed
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if currentTier, okTier := loadResp["currentTier"].(map[string]any); okTier {
|
||||
if id, okID := currentTier["id"].(string); okID {
|
||||
if trimmed := strings.TrimSpace(id); trimmed != "" {
|
||||
return trimmed
|
||||
}
|
||||
}
|
||||
}
|
||||
return "free-tier"
|
||||
}
|
||||
|
||||
// BuildAuthURL generates the OAuth authorization URL.
|
||||
func (o *AntigravityAuth) BuildAuthURL(state, redirectURI string) string {
|
||||
if strings.TrimSpace(redirectURI) == "" {
|
||||
@@ -123,7 +189,7 @@ func (o *AntigravityAuth) FetchUserInfo(ctx context.Context, accessToken string)
|
||||
return "", fmt.Errorf("antigravity userinfo: create request: %w", err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
req.Header.Set("User-Agent", o.loadCodeAssistUserAgent())
|
||||
req.Header.Set("User-Agent", o.shortUserAgent())
|
||||
|
||||
resp, errDo := o.httpClient.Do(req)
|
||||
if errDo != nil {
|
||||
@@ -159,13 +225,9 @@ func (o *AntigravityAuth) FetchUserInfo(ctx context.Context, accessToken string)
|
||||
|
||||
// FetchProjectID retrieves the project ID for the authenticated user via loadCodeAssist
|
||||
func (o *AntigravityAuth) FetchProjectID(ctx context.Context, accessToken string) (string, error) {
|
||||
userAgent := o.loadCodeAssistUserAgent()
|
||||
userAgent := o.shortUserAgent()
|
||||
loadReqBody := map[string]any{
|
||||
"metadata": map[string]string{
|
||||
"ide_type": "ANTIGRAVITY",
|
||||
"ide_version": misc.AntigravityVersionFromUserAgent(userAgent),
|
||||
"ide_name": "antigravity",
|
||||
},
|
||||
"metadata": antigravityLoadCodeAssistMetadata(),
|
||||
}
|
||||
|
||||
rawBody, errMarshal := json.Marshal(loadReqBody)
|
||||
@@ -179,9 +241,9 @@ func (o *AntigravityAuth) FetchProjectID(ctx context.Context, accessToken string
|
||||
return "", fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
req.Header.Set("Accept", "*/*")
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
req.Header.Set("X-Goog-Api-Client", misc.AntigravityGoogAPIClientUA)
|
||||
|
||||
resp, errDo := o.httpClient.Do(req)
|
||||
if errDo != nil {
|
||||
@@ -207,40 +269,16 @@ func (o *AntigravityAuth) FetchProjectID(ctx context.Context, accessToken string
|
||||
return "", fmt.Errorf("decode response: %w", errDecode)
|
||||
}
|
||||
|
||||
// Extract projectID from response
|
||||
projectID := ""
|
||||
if id, ok := loadResp["cloudaicompanionProject"].(string); ok {
|
||||
projectID = strings.TrimSpace(id)
|
||||
}
|
||||
if projectID == "" {
|
||||
if projectMap, ok := loadResp["cloudaicompanionProject"].(map[string]any); ok {
|
||||
if id, okID := projectMap["id"].(string); okID {
|
||||
projectID = strings.TrimSpace(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
projectID := extractCloudaicompanionProject(loadResp)
|
||||
|
||||
if projectID == "" {
|
||||
tierID := "legacy-tier"
|
||||
if tiers, okTiers := loadResp["allowedTiers"].([]any); okTiers {
|
||||
for _, rawTier := range tiers {
|
||||
tier, okTier := rawTier.(map[string]any)
|
||||
if !okTier {
|
||||
continue
|
||||
}
|
||||
if isDefault, okDefault := tier["isDefault"].(bool); okDefault && isDefault {
|
||||
if id, okID := tier["id"].(string); okID && strings.TrimSpace(id) != "" {
|
||||
tierID = strings.TrimSpace(id)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
projectID, err = o.OnboardUser(ctx, accessToken, tierID)
|
||||
projectID, err = o.OnboardUser(ctx, accessToken, defaultAntigravityTierID(loadResp))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if projectID == "" {
|
||||
return "", fmt.Errorf("project id not found in loadCodeAssist or onboardUser response")
|
||||
}
|
||||
return projectID, nil
|
||||
}
|
||||
|
||||
@@ -250,14 +288,10 @@ func (o *AntigravityAuth) FetchProjectID(ctx context.Context, accessToken string
|
||||
// OnboardUser attempts to fetch the project ID via onboardUser by polling for completion
|
||||
func (o *AntigravityAuth) OnboardUser(ctx context.Context, accessToken, tierID string) (string, error) {
|
||||
log.Infof("Antigravity: onboarding user with tier: %s", tierID)
|
||||
userAgent := o.loadCodeAssistUserAgent()
|
||||
userAgent := o.nodeUserAgent()
|
||||
requestBody := map[string]any{
|
||||
"tierId": tierID,
|
||||
"metadata": map[string]string{
|
||||
"ide_type": "ANTIGRAVITY",
|
||||
"ide_version": misc.AntigravityVersionFromUserAgent(userAgent),
|
||||
"ide_name": "antigravity",
|
||||
},
|
||||
"tier_id": tierID,
|
||||
"metadata": antigravityControlPlaneMetadata(userAgent),
|
||||
}
|
||||
|
||||
rawBody, errMarshal := json.Marshal(requestBody)
|
||||
@@ -276,13 +310,14 @@ func (o *AntigravityAuth) OnboardUser(ctx context.Context, accessToken, tierID s
|
||||
}
|
||||
reqCtx, cancel = context.WithTimeout(reqCtx, 30*time.Second)
|
||||
|
||||
endpointURL := fmt.Sprintf("%s/%s:onboardUser", APIEndpoint, APIVersion)
|
||||
endpointURL := fmt.Sprintf("%s/%s:onboardUser", DailyAPIEndpoint, APIVersion)
|
||||
req, errRequest := http.NewRequestWithContext(reqCtx, http.MethodPost, endpointURL, strings.NewReader(string(rawBody)))
|
||||
if errRequest != nil {
|
||||
cancel()
|
||||
return "", fmt.Errorf("create request: %w", errRequest)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
req.Header.Set("Accept", "*/*")
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
req.Header.Set("X-Goog-Api-Client", misc.AntigravityGoogAPIClientUA)
|
||||
@@ -312,18 +347,11 @@ func (o *AntigravityAuth) OnboardUser(ctx context.Context, accessToken, tierID s
|
||||
if done, okDone := data["done"].(bool); okDone && done {
|
||||
projectID := ""
|
||||
if responseData, okResp := data["response"].(map[string]any); okResp {
|
||||
switch projectValue := responseData["cloudaicompanionProject"].(type) {
|
||||
case map[string]any:
|
||||
if id, okID := projectValue["id"].(string); okID {
|
||||
projectID = strings.TrimSpace(id)
|
||||
}
|
||||
case string:
|
||||
projectID = strings.TrimSpace(projectValue)
|
||||
}
|
||||
projectID = extractCloudaicompanionProject(responseData)
|
||||
}
|
||||
|
||||
if projectID != "" {
|
||||
log.Infof("Successfully fetched project_id: %s", projectID)
|
||||
log.Infof("Successfully fetched project_id: %s", util.HideAPIKey(projectID))
|
||||
return projectID, nil
|
||||
}
|
||||
|
||||
@@ -346,5 +374,5 @@ func (o *AntigravityAuth) OnboardUser(ctx context.Context, accessToken, tierID s
|
||||
return "", fmt.Errorf("http %d: %s", resp.StatusCode, responseErr)
|
||||
}
|
||||
|
||||
return "", nil
|
||||
return "", fmt.Errorf("onboard user did not complete after %d attempts", maxAttempts)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package antigravity
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type roundTripperFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return f(req)
|
||||
}
|
||||
|
||||
func TestFetchProjectIDFromLoadCodeAssist(t *testing.T) {
|
||||
auth := NewAntigravityAuth(nil, &http.Client{Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
if req.URL.String() != "https://cloudcode-pa.googleapis.com/v1internal:loadCodeAssist" {
|
||||
t.Fatalf("unexpected request URL: %s", req.URL.String())
|
||||
}
|
||||
assertLoadCodeAssistHeaders(t, req)
|
||||
assertJSONContains(t, req, `"ideType":"ANTIGRAVITY"`)
|
||||
return jsonResponse(`{"cloudaicompanionProject":"cogent-snow-4mnnp"}`), nil
|
||||
})})
|
||||
|
||||
projectID, err := auth.FetchProjectID(context.Background(), "access-token")
|
||||
if err != nil {
|
||||
t.Fatalf("FetchProjectID error: %v", err)
|
||||
}
|
||||
if projectID != "cogent-snow-4mnnp" {
|
||||
t.Fatalf("projectID = %q", projectID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchProjectIDFallsBackToDailyOnboardUser(t *testing.T) {
|
||||
var sawOnboard bool
|
||||
auth := NewAntigravityAuth(nil, &http.Client{Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
switch req.URL.String() {
|
||||
case "https://cloudcode-pa.googleapis.com/v1internal:loadCodeAssist":
|
||||
assertLoadCodeAssistHeaders(t, req)
|
||||
return jsonResponse(`{"allowedTiers":[{"id":"free-tier","isDefault":true}]}`), nil
|
||||
case "https://daily-cloudcode-pa.googleapis.com/v1internal:onboardUser":
|
||||
sawOnboard = true
|
||||
assertOnboardUserHeaders(t, req)
|
||||
assertJSONContains(t, req, `"tier_id":"free-tier"`)
|
||||
assertJSONContains(t, req, `"ide_type":"ANTIGRAVITY"`)
|
||||
return jsonResponse(`{
|
||||
"done": true,
|
||||
"response": {
|
||||
"cloudaicompanionProject": {
|
||||
"id": "cogent-snow-4mnnp",
|
||||
"name": "cogent-snow-4mnnp",
|
||||
"projectNumber": "22597072101"
|
||||
}
|
||||
}
|
||||
}`), nil
|
||||
default:
|
||||
t.Fatalf("unexpected request URL: %s", req.URL.String())
|
||||
return nil, nil
|
||||
}
|
||||
})})
|
||||
|
||||
projectID, err := auth.FetchProjectID(context.Background(), "access-token")
|
||||
if err != nil {
|
||||
t.Fatalf("FetchProjectID error: %v", err)
|
||||
}
|
||||
if !sawOnboard {
|
||||
t.Fatalf("expected onboardUser fallback")
|
||||
}
|
||||
if projectID != "cogent-snow-4mnnp" {
|
||||
t.Fatalf("projectID = %q", projectID)
|
||||
}
|
||||
}
|
||||
|
||||
func assertLoadCodeAssistHeaders(t *testing.T, req *http.Request) {
|
||||
t.Helper()
|
||||
if got := req.Header.Get("Authorization"); got != "Bearer access-token" {
|
||||
t.Fatalf("Authorization = %q", got)
|
||||
}
|
||||
if got := req.Header.Get("Accept"); got != "*/*" {
|
||||
t.Fatalf("Accept = %q", got)
|
||||
}
|
||||
if got := req.Header.Get("X-Goog-Api-Client"); got != "" {
|
||||
t.Fatalf("X-Goog-Api-Client = %q, want empty", got)
|
||||
}
|
||||
if got := req.Header.Get("User-Agent"); strings.Contains(got, "google-api-nodejs-client/") {
|
||||
t.Fatalf("User-Agent = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func assertOnboardUserHeaders(t *testing.T, req *http.Request) {
|
||||
t.Helper()
|
||||
if got := req.Header.Get("Authorization"); got != "Bearer access-token" {
|
||||
t.Fatalf("Authorization = %q", got)
|
||||
}
|
||||
if got := req.Header.Get("Accept"); got != "*/*" {
|
||||
t.Fatalf("Accept = %q", got)
|
||||
}
|
||||
if got := req.Header.Get("X-Goog-Api-Client"); got != "gl-node/22.21.1" {
|
||||
t.Fatalf("X-Goog-Api-Client = %q", got)
|
||||
}
|
||||
if got := req.Header.Get("User-Agent"); !strings.Contains(got, "google-api-nodejs-client/10.3.0") {
|
||||
t.Fatalf("User-Agent = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func assertJSONContains(t *testing.T, req *http.Request, want string) {
|
||||
t.Helper()
|
||||
body, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("read body: %v", err)
|
||||
}
|
||||
bodyText := string(body)
|
||||
req.Body = io.NopCloser(strings.NewReader(bodyText))
|
||||
if !strings.Contains(bodyText, want) {
|
||||
t.Fatalf("body missing %s: %s", want, bodyText)
|
||||
}
|
||||
}
|
||||
|
||||
func jsonResponse(body string) *http.Response {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Header: make(http.Header),
|
||||
Body: io.NopCloser(strings.NewReader(body)),
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ const (
|
||||
|
||||
// Antigravity API configuration
|
||||
const (
|
||||
APIEndpoint = "https://cloudcode-pa.googleapis.com"
|
||||
APIVersion = "v1internal"
|
||||
APIEndpoint = "https://cloudcode-pa.googleapis.com"
|
||||
DailyAPIEndpoint = "https://daily-cloudcode-pa.googleapis.com"
|
||||
APIVersion = "v1internal"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user