shadowbrain / shadow_brain_core /brain /copilot_auth.py
taemin1980's picture
๐Ÿ”ฑ Imperial Deployment: Shadow Brain Core ignition
d50a68d verified
Raw
History Blame Contribute Delete
3.61 kB
import os
import time
import requests
import re
import sys
CLIENT_ID = "Iv1.b507a08c87ecfe98" # Standard VSCode/Copilot Client ID
def initiate_device_flow():
print("\n๐Ÿ” [Jarvis Auth] Initiating GitHub Device Flow...")
try:
res = requests.post("https://github.com/login/device/code", data={
"client_id": CLIENT_ID,
"scope": "read:user"
}, headers={"Accept": "application/json"}, timeout=10)
res.raise_for_status()
return res.json()
except Exception as e:
print(f"โŒ Failed to initiate auth: {e}")
sys.exit(1)
def poll_for_token(device_code, interval, expires_in):
print(f"\nโณ Polling for authorization (expires in {expires_in}s)...")
start_time = time.time()
while time.time() - start_time < expires_in:
try:
res = requests.post("https://github.com/login/oauth/access_token", data={
"client_id": CLIENT_ID,
"device_code": device_code,
"grant_type": "urn:ietf:params:oauth:grant-type:device_code"
}, headers={"Accept": "application/json"}, timeout=10)
res.raise_for_status()
data = res.json()
if "access_token" in data:
return data["access_token"]
error = data.get("error")
if error == "authorization_pending":
time.sleep(interval)
elif error == "slow_down":
interval += 5
time.sleep(interval)
else:
print(f"โŒ Auth error: {error}")
return None
except KeyboardInterrupt:
raise
except Exception as e:
print(f"โš ๏ธ Polling error: {e}")
time.sleep(interval)
return None
def update_env(token):
# Find project root (assumed to be 2 levels up from this script in brain/)
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
env_path = os.path.join(project_root, ".env")
content = ""
if os.path.exists(env_path):
with open(env_path, 'r', encoding='utf-8') as f:
content = f.read()
if "GITHUB_TOKEN=" in content:
new_content = re.sub(r'GITHUB_TOKEN=.*', f'GITHUB_TOKEN={token}', content)
else:
new_content = content.rstrip() + f"\nGITHUB_TOKEN={token}\n"
with open(env_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"\nโœ… .env file updated with new GITHUB_TOKEN.")
def main():
data = initiate_device_flow()
user_code = data.get("user_code")
verification_uri = data.get("verification_uri")
print("\n" + "!"*50, flush=True)
print(f" ์ธ์ฆ ๋งํฌ: {verification_uri}", flush=True)
print(f" ์ธ์ฆ ์ฝ”๋“œ: {user_code}", flush=True)
print("!"*50, flush=True)
print("\n์œ„ ๋งํฌ๋ฅผ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์—ด๊ณ  ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์Šน์ธํ•ด ์ฃผ์‹ญ์‹œ์˜ค.", flush=True)
# Optional: try to open the browser automatically
try:
import webbrowser
webbrowser.open(verification_uri)
except:
pass
token = poll_for_token(data["device_code"], data.get("interval", 5), data.get("expires_in", 900))
if token:
print("\nโœจ Authentication Successful!")
update_env(token)
else:
print("\nโŒ Authentication failed or timed out.")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\nโš ๏ธ [Jarvis Auth] ์ธ์ฆ์ด ์‚ฌ์šฉ์ž์— ์˜ํ•ด ์ค‘๋‹จ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")
sys.exit(0)