-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
71 lines (60 loc) · 2.07 KB
/
Copy pathauth.py
File metadata and controls
71 lines (60 loc) · 2.07 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
"""
Google Cloud authentication helper for Chitty Workspace marketplace tools.
Uses the gcloud CLI credentials — no keys are stored by Chitty.
"""
import subprocess
import json
import sys
def get_access_token():
"""Get OAuth2 access token from gcloud CLI."""
try:
result = subprocess.run(
["gcloud", "auth", "print-access-token"],
capture_output=True, text=True, timeout=10
)
if result.returncode != 0:
return None, f"gcloud auth failed: {result.stderr.strip()}"
return result.stdout.strip(), None
except FileNotFoundError:
return None, "gcloud CLI not found. Install: https://cloud.google.com/sdk/docs/install"
except subprocess.TimeoutExpired:
return None, "gcloud auth timed out"
def get_project_id():
"""Get the default GCP project from gcloud config."""
try:
result = subprocess.run(
["gcloud", "config", "get-value", "project"],
capture_output=True, text=True, timeout=10
)
if result.returncode != 0 or not result.stdout.strip():
return None, "No default project set. Run: gcloud config set project PROJECT_ID"
return result.stdout.strip(), None
except FileNotFoundError:
return None, "gcloud CLI not found"
def check_auth():
"""Check if user is authenticated with gcloud."""
token, err = get_access_token()
if token:
project, proj_err = get_project_id()
return {
"authenticated": True,
"project": project,
"project_error": proj_err
}
return {
"authenticated": False,
"error": err
}
def auth_headers():
"""Get HTTP headers with Bearer token for GCP API calls."""
token, err = get_access_token()
if not token:
raise RuntimeError(f"Not authenticated: {err}")
return {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
if __name__ == "__main__":
# When run directly, check auth status
status = check_auth()
print(json.dumps(status, indent=2))