File size: 9,837 Bytes
494c89b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
275
276
277
278
279
280
281
282
"""
OAuth клиент - Device Authorization Flow
Альтернативный метод авторизации без локального сервера.

Flow:
1. Register client → получаем clientId, clientSecret
2. POST /device_authorization → получаем device_code, user_code, verification_uri
3. Открываем браузер с verification_uri
4. Пользователь вводит user_code и авторизуется
5. Polling POST /token пока не получим токены
"""

import json
import hashlib
import requests
import time
import webbrowser
from datetime import datetime, timedelta
from pathlib import Path
from typing import Optional, Dict, Tuple

import sys
sys.path.insert(0, str(Path(__file__).parent.parent))

from core.paths import get_paths
from core.kiro_config import (
    get_kiro_user_agent,
    get_kiro_scopes,
    get_client_id_hash,
)

_paths = get_paths()
TOKENS_DIR = _paths.tokens_dir

# AWS SSO OIDC Configuration
OIDC_REGION = "us-east-1"
OIDC_BASE = f"https://oidc.{OIDC_REGION}.amazonaws.com"
START_URL = "https://view.awsapps.com/start"

KIRO_SCOPES = get_kiro_scopes()


class OAuthDevice:
    """OAuth клиент с Device Authorization Flow"""
    
    def __init__(self):
        self.tokens_dir = TOKENS_DIR
        self.output_lines = []
        self.token_filename = None
        self.account_name = None
        
        # Client registration
        self.client_id = None
        self.client_secret = None
        
        # Device auth
        self.device_code = None
        self.user_code = None
        self.verification_uri = None
        self.interval = 5
        
        # Auth URL (verification URI)
        self.auth_url = None
    
    def _register_client(self) -> Tuple[str, str]:
        """Register OIDC client for device flow"""
        print("[OAuth-Device] Registering OIDC client...")
        
        headers = {
            "Content-Type": "application/json",
            "User-Agent": get_kiro_user_agent(),
            "Accept": "application/json",
        }
        
        resp = requests.post(
            f"{OIDC_BASE}/client/register",
            json={
                "clientName": "Kiro Account Switcher",
                "clientType": "public",
                "scopes": KIRO_SCOPES,
                "grantTypes": ["urn:ietf:params:oauth:grant-type:device_code", "refresh_token"],
                "issuerUrl": START_URL
            },
            headers=headers,
            timeout=30
        )
        
        if resp.status_code != 200:
            raise Exception(f"Client registration failed: {resp.text}")
        
        data = resp.json()
        print(f"[OAuth-Device] ✓ Client registered: {data['clientId'][:20]}...")
        return data["clientId"], data.get("clientSecret", "")
    
    def _start_device_auth(self) -> Dict:
        """Start device authorization flow"""
        print("[OAuth-Device] Starting device authorization...")
        
        headers = {
            "Content-Type": "application/json",
            "User-Agent": get_kiro_user_agent(),
            "Accept": "application/json",
        }
        
        resp = requests.post(
            f"{OIDC_BASE}/device_authorization",
            json={
                "clientId": self.client_id,
                "clientSecret": self.client_secret,
                "startUrl": START_URL
            },
            headers=headers,
            timeout=30
        )
        
        if resp.status_code != 200:
            raise Exception(f"Device authorization failed: {resp.text}")
        
        data = resp.json()
        self.device_code = data["deviceCode"]
        self.user_code = data["userCode"]
        self.verification_uri = data.get("verificationUriComplete") or data.get("verificationUri")
        self.interval = data.get("interval", 5)
        
        print(f"[OAuth-Device] ✓ Device code obtained")
        print(f"[OAuth-Device] User code: {self.user_code}")
        return data
    
    def _poll_for_token(self, timeout: int = 300) -> Dict:
        """Poll for token after user authorizes"""
        print("[OAuth-Device] Waiting for authorization...")
        
        headers = {
            "Content-Type": "application/json",
            "User-Agent": get_kiro_user_agent(),
            "Accept": "application/json",
        }
        
        start_time = time.time()
        while time.time() - start_time < timeout:
            time.sleep(self.interval)
            
            resp = requests.post(
                f"{OIDC_BASE}/token",
                json={
                    "clientId": self.client_id,
                    "clientSecret": self.client_secret,
                    "grantType": "urn:ietf:params:oauth:grant-type:device_code",
                    "deviceCode": self.device_code
                },
                headers=headers,
                timeout=30
            )
            
            if resp.status_code == 200:
                print("[OAuth-Device] ✓ Token obtained!")
                return resp.json()
            
            data = resp.json()
            error = data.get("error", "")
            
            if error == "authorization_pending":
                continue
            elif error == "slow_down":
                self.interval += 1
                continue
            elif error == "expired_token":
                raise Exception("Device code expired")
            elif error == "access_denied":
                raise Exception("Access denied by user")
            else:
                raise Exception(f"Token error: {resp.text}")
        
        raise Exception("Authorization timeout")
    
    def _save_token(self, token_data: Dict, account_name: str) -> str:
        """Save token to file"""
        import re
        from core.kiro_config import get_machine_id
        
        timestamp = int(datetime.now().timestamp() * 1000)
        safe_name = re.sub(r'[^a-zA-Z0-9_-]', '_', account_name)
        filename = f"token-BuilderId-IdC-{safe_name}-{timestamp}.json"
        filepath = self.tokens_dir / filename
        
        expires_in = token_data.get('expiresIn', token_data.get('expires_in', 3600))
        expires_at = (datetime.utcnow() + timedelta(seconds=expires_in)).isoformat() + 'Z'
        
        client_id_hash = get_client_id_hash(START_URL)
        
        # ВАЖНО: Сохраняем machine ID который использовался при регистрации!
        current_machine_id = get_machine_id()
        
        token_file = {
            "accessToken": token_data.get('accessToken', token_data.get('access_token')),
            "refreshToken": token_data.get('refreshToken', token_data.get('refresh_token')),
            "expiresAt": expires_at,
            "tokenType": token_data.get('tokenType', token_data.get('token_type', 'Bearer')),
            "clientIdHash": client_id_hash,
            "accountName": account_name,
            "provider": "BuilderId",
            "authMethod": "DeviceFlow",
            "region": OIDC_REGION,
            "createdAt": datetime.now().isoformat(),
            "_clientId": self.client_id,
            "_clientSecret": self.client_secret,
            "_machineId": current_machine_id  # ANTI-BAN: сохраняем machine ID!
        }
        
        filepath.write_text(json.dumps(token_file, indent=2))
        print(f"[OAuth-Device] ✓ Token saved to: {filepath}")
        self.output_lines.append(f"Token saved to: {filepath}")
        
        return filename
    
    def start(self, account_name: str = 'auto') -> Optional[str]:
        """
        Start OAuth device flow and return verification URL
        
        Returns:
            Verification URL for user to open in browser
        """
        try:
            self.account_name = account_name
            
            # 1. Register client
            self.client_id, self.client_secret = self._register_client()
            
            # 2. Start device authorization
            self._start_device_auth()
            
            # 3. Build verification URL
            self.auth_url = self.verification_uri or f"{START_URL}?user_code={self.user_code}"
            
            print(f"[OAuth-Device] Verification URL: {self.auth_url}")
            self.output_lines.append(f"Verification URL:\n{self.auth_url}")
            self.output_lines.append(f"User Code: {self.user_code}")
            
            return self.auth_url
            
        except Exception as e:
            print(f"[OAuth-Device] Error: {e}")
            self.output_lines.append(f"Error: {e}")
            return None
    
    def wait_for_callback(self, timeout: int = 300) -> bool:
        """
        Poll for token after user authorizes in browser
        
        Returns:
            True if successful, False on error or timeout
        """
        try:
            # Poll for token
            token_data = self._poll_for_token(timeout)
            
            # Save token
            self.token_filename = self._save_token(token_data, self.account_name)
            self.output_lines.append("Authentication successful!")
            return True
            
        except Exception as e:
            print(f"[OAuth-Device] Error: {e}")
            self.output_lines.append(f"Error: {e}")
            return False
    
    def get_auth_url(self) -> Optional[str]:
        """Get the verification URL"""
        return self.auth_url
    
    def get_user_code(self) -> Optional[str]:
        """Get the user code to display"""
        return self.user_code
    
    def get_token_filename(self) -> Optional[str]:
        """Get saved token filename"""
        return self.token_filename
    
    def close(self):
        """Cleanup (nothing to do for device flow)"""
        pass