Skip to content

Commit 9fc0971

Browse files
committed
fix(gcp): remove redundant json.loads call and avoid double-wrapping short-lived token JSON
1 parent e0dfaff commit 9fc0971

1 file changed

Lines changed: 21 additions & 12 deletions

File tree

core/gcp/gcp_access.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,26 @@ def save_credential(self):
205205
else :
206206
data = []
207207

208+
# Prepare credential data for storage
209+
if hasattr(self, "encoded_credential"):
210+
credential_data = self.encoded_credential.decode('utf-8')
211+
elif self.credential_type == "short_lived_token":
212+
# For short-lived tokens, store token and scopes directly
213+
token_data = {
214+
"token": self.credential.token,
215+
"scopes": list(self.credential.scopes) if self.credential.scopes else ["https://www.googleapis.com/auth/cloud-platform"]
216+
}
217+
credential_data = base64.b64encode(json.dumps(token_data).encode('utf-8')).decode('utf-8')
218+
else:
219+
# For other credential types, to_json() returns a string that needs wrapping
220+
cred_json = self.credential.to_json() if hasattr(self.credential, "to_json") else {}
221+
credential_data = base64.b64encode(json.dumps(cred_json).encode('utf-8')).decode('utf-8')
222+
208223
credential_to_saved = {
209224
"name": getattr(self, "credential_name", None),
210225
"current": getattr(self, "credential_current", False),
211226
"type": getattr(self, "credential_type", None),
212-
"credential": (
213-
self.encoded_credential.decode('utf-8')
214-
if hasattr(self, "encoded_credential")
215-
else base64.b64encode(
216-
json.dumps(
217-
self.credential.to_json() if hasattr(self.credential, "to_json") else {}
218-
).encode('utf-8')
219-
).decode('utf-8')
220-
),
227+
"credential": credential_data,
221228
}
222229

223230

@@ -278,9 +285,11 @@ def get_current_access(self):
278285
scopes=["https://www.googleapis.com/auth/cloud-platform"]
279286
)
280287
elif credential["type"] == "short_lived_token":
281-
cred_dict = json.loads(cred_dict)
282-
token = cred_dict.get("token") if isinstance(cred_dict, dict) else None
283-
scopes = cred_dict.get("scopes") if isinstance(cred_dict, dict) else ["https://www.googleapis.com/auth/cloud-platform"]
288+
# Handle double-wrapped JSON (to_json() returns string, then json.dumps wraps it again)
289+
if isinstance(cred_dict, str):
290+
cred_dict = json.loads(cred_dict)
291+
token = cred_dict.get("token")
292+
scopes = cred_dict.get("scopes", ["https://www.googleapis.com/auth/cloud-platform"])
284293
self.credential = ShortLivedTokenCredentials(token=token, scopes=scopes)
285294
else:
286295
self.credential = cred_dict

0 commit comments

Comments
 (0)