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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import os
import json
import pickle
import base64
from pathlib import Path
from typing import Optional, Dict, Any
from cryptography.fernet import Fernet
import google.auth.transport.requests
import google_auth_oauthlib.flow
import googleapiclient.discovery
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
import webbrowser
import threading
import time
from http.server import HTTPServer, BaseHTTPRequestHandler
import urllib.parse as urlparse
from logger import logger

class OAuthCallbackHandler(BaseHTTPRequestHandler):
    """HTTP request handler for OAuth callback"""
    
    def do_GET(self):
        """Handle GET request (OAuth callback)"""
        # Parse the callback URL to extract authorization code
        parsed_path = urlparse.urlparse(self.path)
        query_params = urlparse.parse_qs(parsed_path.query)
        
        if 'code' in query_params:
            # Store the authorization code
            self.server.auth_code = query_params['code'][0]
            
            # Send success response
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            
            success_html = """
            <html>
            <head><title>Authentication Successful</title></head>
            <body style="font-family: Arial, sans-serif; text-align: center; padding: 50px;">
                <h1 style="color: #4CAF50;">✅ Authentication Successful!</h1>
                <p>You have successfully authenticated with Gmail.</p>
                <p>You can now close this window and return to Claude Desktop.</p>
                <script>
                    setTimeout(function() {
                        window.close();
                    }, 3000);
                </script>
            </body>
            </html>
            """
            self.wfile.write(success_html.encode())
        else:
            # Send error response
            self.send_response(400)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            
            error_html = """
            <html>
            <head><title>Authentication Error</title></head>
            <body style="font-family: Arial, sans-serif; text-align: center; padding: 50px;">
                <h1 style="color: #f44336;">❌ Authentication Failed</h1>
                <p>There was an error during authentication.</p>
                <p>Please try again.</p>
            </body>
            </html>
            """
            self.wfile.write(error_html.encode())
    
    def log_message(self, format, *args):
        """Suppress server log messages"""
        pass

class GmailOAuthManager:
    """Manages Gmail OAuth 2.0 authentication and token storage for multiple accounts"""
    
    # Gmail API scopes
    SCOPES = [
        'https://www.googleapis.com/auth/gmail.readonly',
        'https://www.googleapis.com/auth/gmail.modify'
    ]
    
    def __init__(self, credentials_dir: str = None):
        """Initialize OAuth manager
        
        Args:
            credentials_dir: Directory to store credentials (defaults to ~/.mailquery_oauth)
        """
        if credentials_dir is None:
            credentials_dir = os.path.expanduser("~/.mailquery_oauth")
        
        self.credentials_dir = Path(credentials_dir)
        self.credentials_dir.mkdir(exist_ok=True)
        
        # File paths
        self.client_secrets_file = self.credentials_dir / "client_secret.json"
        self.accounts_file = self.credentials_dir / "accounts.json"
        self.encryption_key_file = self.credentials_dir / "key.key"
        self.current_account_file = self.credentials_dir / "current_account.txt"
        
        # Initialize encryption
        self._init_encryption()
        
        # OAuth flow settings
        self.redirect_uri = "http://localhost:8080/oauth2callback"
        
        # Current account
        self.current_account_email = self._load_current_account()
        
    def _init_encryption(self):
        """Initialize encryption for secure credential storage"""
        if self.encryption_key_file.exists():
            with open(self.encryption_key_file, 'rb') as key_file:
                self.encryption_key = key_file.read()
        else:
            self.encryption_key = Fernet.generate_key()
            with open(self.encryption_key_file, 'wb') as key_file:
                key_file.write(self.encryption_key)
            # Make key file readable only by owner
            os.chmod(self.encryption_key_file, 0o600)
        
        self.cipher_suite = Fernet(self.encryption_key)
    
    def _load_current_account(self) -> Optional[str]:
        """Load the currently selected account"""
        if self.current_account_file.exists():
            try:
                with open(self.current_account_file, 'r') as f:
                    return f.read().strip()
            except Exception as e:
                logger.error(f"Failed to load current account: {e}")
        return None
    
    def _save_current_account(self, email: str):
        """Save the currently selected account"""
        try:
            with open(self.current_account_file, 'w') as f:
                f.write(email)
            self.current_account_email = email
            logger.info(f"Set current account to: {email}")
        except Exception as e:
            logger.error(f"Failed to save current account: {e}")
    
    def setup_client_secrets(self, client_id: str, client_secret: str):
        """Setup OAuth client secrets
        
        Args:
            client_id: Google OAuth 2.0 client ID
            client_secret: Google OAuth 2.0 client secret
        """
        client_config = {
            "web": {
                "client_id": client_id,
                "client_secret": client_secret,
                "auth_uri": "https://accounts.google.com/o/oauth2/auth",
                "token_uri": "https://oauth2.googleapis.com/token",
                "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
                "redirect_uris": [self.redirect_uri]
            }
        }
        
        with open(self.client_secrets_file, 'w') as f:
            json.dump(client_config, f, indent=2)
        
        logger.info("Client secrets saved successfully")
    
    def _encrypt_data(self, data: Any) -> bytes:
        """Encrypt data using Fernet encryption"""
        serialized_data = pickle.dumps(data)
        return self.cipher_suite.encrypt(serialized_data)
    
    def _decrypt_data(self, encrypted_data: bytes) -> Any:
        """Decrypt data using Fernet encryption"""
        decrypted_data = self.cipher_suite.decrypt(encrypted_data)
        return pickle.loads(decrypted_data)
    
    def get_authorization_url(self) -> str:
        """Get the authorization URL for OAuth flow
        
        Returns:
            Authorization URL that user should visit
        """
        if not self.client_secrets_file.exists():
            raise ValueError("Client secrets not found. Please run setup first.")
        
        flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
            str(self.client_secrets_file), 
            scopes=self.SCOPES
        )
        flow.redirect_uri = self.redirect_uri
        
        auth_url, _ = flow.authorization_url(
            access_type='offline',
            include_granted_scopes='true',
            prompt='consent'  # Force consent to get refresh token
        )
        
        return auth_url
    
    def authenticate_interactive(self) -> bool:
        """Interactive authentication flow that opens browser
        
        Returns:
            True if authentication successful, False otherwise
        """
        try:
            # Start local HTTP server for OAuth callback
            server = HTTPServer(('localhost', 8080), OAuthCallbackHandler)
            server.auth_code = None
            
            # Get authorization URL
            auth_url = self.get_authorization_url()
            
            logger.info("Opening browser for authentication...")
            logger.info(f"If browser doesn't open, visit: {auth_url}")
            
            # Open browser
            webbrowser.open(auth_url)
            
            # Start server in background thread
            server_thread = threading.Thread(target=server.handle_request)
            server_thread.daemon = True
            server_thread.start()
            
            # Wait for callback (max 5 minutes)
            timeout = 300  # 5 minutes
            start_time = time.time()
            
            while server.auth_code is None and (time.time() - start_time) < timeout:
                time.sleep(1)
            
            if server.auth_code is None:
                logger.error("Authentication timed out")
                return False
            
            # Exchange authorization code for credentials
            flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
                str(self.client_secrets_file), 
                scopes=self.SCOPES
            )
            flow.redirect_uri = self.redirect_uri
            
            flow.fetch_token(code=server.auth_code)
            credentials = flow.credentials
            
            # Get user email from credentials
            user_email = self._get_email_from_credentials(credentials)
            if not user_email:
                logger.error("Failed to get user email from credentials")
                return False
            
            # Save encrypted credentials for this account
            self._save_credentials(user_email, credentials)
            
            # Set as current account
            self._save_current_account(user_email)
            
            logger.info("Authentication successful!")
            return True
            
        except Exception as e:
            logger.error(f"Authentication failed: {e}")
            return False
    
    def _get_email_from_credentials(self, credentials: Credentials) -> Optional[str]:
        """Get email address from credentials"""
        try:
            service = googleapiclient.discovery.build(
                'gmail', 'v1', credentials=credentials
            )
            profile = service.users().getProfile(userId='me').execute()
            return profile.get('emailAddress')
        except Exception as e:
            logger.error(f"Failed to get email from credentials: {e}")
            return None
    
    def _save_credentials(self, email: str, credentials: Credentials):
        """Save encrypted credentials for a specific account"""
        try:
            # Load existing accounts
            accounts = self._load_accounts()
            
            # Encrypt and store credentials
            encrypted_credentials = self._encrypt_data(credentials)
            accounts[email] = base64.b64encode(encrypted_credentials).decode('utf-8')
            
            # Save accounts file
            with open(self.accounts_file, 'w') as f:
                json.dump(accounts, f, indent=2)
            
            # Make accounts file readable only by owner
            os.chmod(self.accounts_file, 0o600)
            
            logger.info(f"Credentials saved for account: {email}")
        except Exception as e:
            logger.error(f"Failed to save credentials for {email}: {e}")
            raise
    
    def _load_accounts(self) -> Dict[str, str]:
        """Load accounts data"""
        if not self.accounts_file.exists():
            return {}
        
        try:
            with open(self.accounts_file, 'r') as f:
                return json.load(f)
        except Exception as e:
            logger.error(f"Failed to load accounts: {e}")
            return {}
    
    def _load_credentials(self, email: str) -> Optional[Credentials]:
        """Load and decrypt credentials for a specific account"""
        accounts = self._load_accounts()
        
        if email not in accounts:
            return None
        
        try:
            encrypted_credentials = base64.b64decode(accounts[email])
            credentials = self._decrypt_data(encrypted_credentials)
            return credentials
        except Exception as e:
            logger.error(f"Failed to load credentials for {email}: {e}")
            return None
    
    def get_valid_credentials(self, email: str = None) -> Optional[Credentials]:
        """Get valid credentials for an account, refreshing if necessary
        
        Args:
            email: Email address of account (uses current account if None)
            
        Returns:
            Valid Credentials object or None if authentication required
        """
        if email is None:
            email = self.current_account_email
        
        if not email:
            logger.warning("No current account set")
            return None
        
        credentials = self._load_credentials(email)
        
        if not credentials:
            logger.warning(f"No stored credentials found for {email}")
            return None
        
        # Refresh if expired
        if credentials.expired and credentials.refresh_token:
            try:
                logger.info(f"Refreshing expired credentials for {email}...")
                credentials.refresh(Request())
                self._save_credentials(email, credentials)
                logger.info("Credentials refreshed successfully")
            except Exception as e:
                logger.error(f"Failed to refresh credentials for {email}: {e}")
                return None
        
        if not credentials.valid:
            logger.warning(f"Credentials are not valid for {email}")
            return None
        
        return credentials
    
    def is_authenticated(self, email: str = None) -> bool:
        """Check if user is authenticated
        
        Args:
            email: Email address to check (uses current account if None)
            
        Returns:
            True if valid credentials exist, False otherwise
        """
        return self.get_valid_credentials(email) is not None
    
    def switch_account(self, email: str) -> bool:
        """Switch to a different authenticated account
        
        Args:
            email: Email address to switch to
            
        Returns:
            True if switch successful, False if account not found or not authenticated
        """
        if self.is_authenticated(email):
            self._save_current_account(email)
            logger.info(f"Switched to account: {email}")
            return True
        else:
            logger.error(f"Account {email} is not authenticated")
            return False
    
    def list_accounts(self) -> Dict[str, bool]:
        """List all stored accounts and their authentication status
        
        Returns:
            Dictionary mapping email addresses to authentication status
        """
        accounts = self._load_accounts()
        result = {}
        
        for email in accounts.keys():
            result[email] = self.is_authenticated(email)
        
        return result
    
    def remove_account(self, email: str):
        """Remove an account and its credentials
        
        Args:
            email: Email address to remove
        """
        accounts = self._load_accounts()
        
        if email in accounts:
            del accounts[email]
            
            # Save updated accounts
            with open(self.accounts_file, 'w') as f:
                json.dump(accounts, f, indent=2)
            
            # If this was the current account, clear it
            if self.current_account_email == email:
                if self.current_account_file.exists():
                    self.current_account_file.unlink()
                self.current_account_email = None
            
            logger.info(f"Removed account: {email}")
        else:
            logger.warning(f"Account {email} not found")
    
    def clear_credentials(self):
        """Clear all stored credentials"""
        if self.accounts_file.exists():
            self.accounts_file.unlink()
        if self.current_account_file.exists():
            self.current_account_file.unlink()
        self.current_account_email = None
        logger.info("All credentials cleared")
    
    def get_gmail_service(self, email: str = None):
        """Get authenticated Gmail service object
        
        Args:
            email: Email address (uses current account if None)
            
        Returns:
            Gmail service object or None if not authenticated
        """
        credentials = self.get_valid_credentials(email)
        if not credentials:
            return None
        
        try:
            service = googleapiclient.discovery.build(
                'gmail', 'v1', credentials=credentials
            )
            return service
        except Exception as e:
            logger.error(f"Failed to build Gmail service: {e}")
            return None
    
    def get_user_email(self, email: str = None) -> Optional[str]:
        """Get the authenticated user's email address
        
        Args:
            email: Email address (uses current account if None)
            
        Returns:
            User's email address or None if not authenticated
        """
        if email is None:
            return self.current_account_email
        return email if self.is_authenticated(email) else None
    
    def get_current_account(self) -> Optional[str]:
        """Get the currently selected account
        
        Returns:
            Current account email or None if no account selected
        """
        return self.current_account_email

# Global OAuth manager instance
oauth_manager = GmailOAuthManager()