File size: 31,432 Bytes
d613ffd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
"""

╔══════════════════════════════════════════════════════════════════════════════╗

β•‘                                                                              β•‘

β•‘                    AUTHENTICATION PLUGIN for NoahsKI                         β•‘

β•‘                         Email Verification System                            β•‘

β•‘                                                                              β•‘

β•‘  Features:                                                                   β•‘

β•‘  βœ“ Email Registration & Verification                                        β•‘

β•‘  βœ“ Verification Code via Email                                              β•‘

β•‘  βœ“ Session Management                                                        β•‘

β•‘  βœ“ User Data Storage (JSON)                                                 β•‘

β•‘  βœ“ Password Hashing (bcrypt)                                                β•‘

β•‘  βœ“ Token-based Authentication                                               β•‘

β•‘                                                                              β•‘

β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

"""

import os
import json
import time
import secrets
import hashlib
import smtplib
import tempfile
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from pathlib import Path
from typing import Dict, Optional, Tuple
from dataclasses import dataclass, asdict
from datetime import datetime, timedelta
import logging

logger = logging.getLogger(__name__)


# ═══════════════════════════════════════════════════════════════════════════════
# CONFIGURATION
# ═══════════════════════════════════════════════════════════════════════════════

class AuthConfig:
    """Authentication plugin configuration"""
    
    # Detect Hugging Face Spaces
    IS_HF_SPACE = os.getenv('SPACE_ID') is not None
    
    # Paths (HF Spaces compatible)
    if IS_HF_SPACE:
        BASE_DATA_DIR = Path(tempfile.gettempdir()) / 'noahski_data'
    else:
        BASE_DATA_DIR = Path('noahski_data')
    
    DATA_DIR = BASE_DATA_DIR / 'auth'
    USERS_FILE = DATA_DIR / 'users.json'
    SESSIONS_FILE = DATA_DIR / 'sessions.json'
    VERIFICATION_FILE = DATA_DIR / 'verifications.json'
    
    # Email Settings (SMTP)
    SMTP_SERVER = os.getenv('SMTP_SERVER', 'smtp.gmail.com')
    SMTP_PORT = int(os.getenv('SMTP_PORT', 587))
    SMTP_USER = os.getenv('SMTP_USER', 'your-email@gmail.com')
    SMTP_PASSWORD = os.getenv('SMTP_PASSWORD', 'your-app-password')
    FROM_EMAIL = os.getenv('FROM_EMAIL', 'noreply@noahski.ai')
    FROM_NAME = os.getenv('FROM_NAME', 'NoahsKI')
    
    # Security
    VERIFICATION_CODE_LENGTH = 6
    VERIFICATION_EXPIRY_MINUTES = 15
    SESSION_EXPIRY_HOURS = 24
    MAX_LOGIN_ATTEMPTS = 5
    LOCKOUT_DURATION_MINUTES = 30
    
    # Token
    TOKEN_LENGTH = 32


# ═══════════════════════════════════════════════════════════════════════════════
# DATA CLASSES
# ═══════════════════════════════════════════════════════════════════════════════

@dataclass
class User:
    """User data structure"""
    email: str
    password_hash: str
    created_at: float
    verified: bool = False
    last_login: Optional[float] = None
    login_attempts: int = 0
    locked_until: Optional[float] = None
    username: Optional[str] = None
    
    def to_dict(self) -> Dict:
        return asdict(self)
    
    @staticmethod
    def from_dict(data: Dict) -> 'User':
        return User(**data)


@dataclass
class Session:
    """Session data structure"""
    token: str
    email: str
    created_at: float
    expires_at: float
    ip_address: Optional[str] = None
    user_agent: Optional[str] = None
    
    def is_valid(self) -> bool:
        return time.time() < self.expires_at
    
    def to_dict(self) -> Dict:
        return asdict(self)
    
    @staticmethod
    def from_dict(data: Dict) -> 'Session':
        return Session(**data)


@dataclass
class VerificationCode:
    """Email verification code"""
    email: str
    code: str
    created_at: float
    expires_at: float
    attempts: int = 0
    
    def is_valid(self) -> bool:
        return time.time() < self.expires_at and self.attempts < 3
    
    def to_dict(self) -> Dict:
        return asdict(self)
    
    @staticmethod
    def from_dict(data: Dict) -> 'VerificationCode':
        return VerificationCode(**data)


# ═══════════════════════════════════════════════════════════════════════════════
# EMAIL SERVICE
# ═══════════════════════════════════════════════════════════════════════════════

class EmailService:
    """Handle email sending"""
    
    def __init__(self):
        self.smtp_server = AuthConfig.SMTP_SERVER
        self.smtp_port = AuthConfig.SMTP_PORT
        self.smtp_user = AuthConfig.SMTP_USER
        self.smtp_password = AuthConfig.SMTP_PASSWORD
        self.from_email = AuthConfig.FROM_EMAIL
        self.from_name = AuthConfig.FROM_NAME
    
    def send_verification_email(self, to_email: str, code: str) -> bool:
        """Send verification code email"""
        try:
            subject = f"πŸ” Dein NoahsKI Verifizierungscode"
            
            html_body = f"""

            <!DOCTYPE html>

            <html>

            <head>

                <style>

                    body {{ font-family: Arial, sans-serif; background-color: #f4f4f4; padding: 20px; }}

                    .container {{ background-color: white; padding: 30px; border-radius: 10px; max-width: 600px; margin: 0 auto; }}

                    .code {{ font-size: 32px; font-weight: bold; color: #4CAF50; text-align: center; padding: 20px; background-color: #f0f0f0; border-radius: 5px; letter-spacing: 5px; }}

                    .header {{ color: #333; text-align: center; }}

                    .footer {{ color: #666; font-size: 12px; text-align: center; margin-top: 30px; }}

                </style>

            </head>

            <body>

                <div class="container">

                    <h1 class="header">πŸš€ Willkommen bei NoahsKI!</h1>

                    <p>Danke fΓΌr deine Registrierung. Hier ist dein Verifizierungscode:</p>

                    

                    <div class="code">{code}</div>

                    

                    <p>Dieser Code ist <strong>15 Minuten</strong> gΓΌltig.</p>

                    

                    <p><strong>⚠️ Wichtig:</strong> Falls du diese Email nicht angefordert hast, ignoriere sie einfach.</p>

                    

                    <div class="footer">

                        <p>Β© 2026 NoahsKI - Dein KI-Assistent</p>

                        <p>Diese Email wurde automatisch generiert.</p>

                    </div>

                </div>

            </body>

            </html>

            """
            
            plain_body = f"""

            πŸš€ Willkommen bei NoahsKI!

            

            Danke fΓΌr deine Registrierung. Hier ist dein Verifizierungscode:

            

            {code}

            

            Dieser Code ist 15 Minuten gΓΌltig.

            

            ⚠️ Falls du diese Email nicht angefordert hast, ignoriere sie einfach.

            

            Β© 2026 NoahsKI

            """
            
            return self._send_email(to_email, subject, html_body, plain_body)
        
        except Exception as e:
            logger.error(f"Failed to send verification email: {e}")
            return False
    
    def send_welcome_email(self, to_email: str, username: str = None) -> bool:
        """Send welcome email after successful verification"""
        try:
            subject = "πŸŽ‰ Account erfolgreich verifiziert!"
            name = username or to_email.split('@')[0]
            
            html_body = f"""

            <!DOCTYPE html>

            <html>

            <head>

                <style>

                    body {{ font-family: Arial, sans-serif; background-color: #f4f4f4; padding: 20px; }}

                    .container {{ background-color: white; padding: 30px; border-radius: 10px; max-width: 600px; margin: 0 auto; }}

                    .header {{ color: #4CAF50; text-align: center; }}

                    .feature {{ padding: 10px 0; }}

                    .footer {{ color: #666; font-size: 12px; text-align: center; margin-top: 30px; }}

                </style>

            </head>

            <body>

                <div class="container">

                    <h1 class="header">πŸŽ‰ Willkommen bei NoahsKI, {name}!</h1>

                    

                    <p>Dein Account wurde erfolgreich verifiziert und ist jetzt aktiv!</p>

                    

                    <h3>Was du jetzt tun kannst:</h3>

                    <div class="feature">βœ… Mit NoahsKI chatten</div>

                    <div class="feature">🎨 Bilder generieren</div>

                    <div class="feature">🌐 Web-Recherche nutzen</div>

                    <div class="feature">πŸ“š Autonomes Learning System</div>

                    <div class="feature">πŸ’‘ Code-Generierung</div>

                    

                    <p style="margin-top: 30px;">Viel Spaß mit NoahsKI!</p>

                    

                    <div class="footer">

                        <p>Β© 2026 NoahsKI - Dein KI-Assistent</p>

                    </div>

                </div>

            </body>

            </html>

            """
            
            plain_body = f"""

            πŸŽ‰ Willkommen bei NoahsKI, {name}!

            

            Dein Account wurde erfolgreich verifiziert und ist jetzt aktiv!

            

            Was du jetzt tun kannst:

            βœ… Mit NoahsKI chatten

            🎨 Bilder generieren

            🌐 Web-Recherche nutzen

            πŸ“š Autonomes Learning System

            πŸ’‘ Code-Generierung

            

            Viel Spaß mit NoahsKI!

            

            Β© 2026 NoahsKI

            """
            
            return self._send_email(to_email, subject, html_body, plain_body)
        
        except Exception as e:
            logger.error(f"Failed to send welcome email: {e}")
            return False
    
    def _send_email(self, to_email: str, subject: str, html_body: str, plain_body: str) -> bool:
        """Internal method to send email via SMTP"""
        try:
            # Create message
            msg = MIMEMultipart('alternative')
            msg['Subject'] = subject
            msg['From'] = f"{self.from_name} <{self.from_email}>"
            msg['To'] = to_email
            
            # Attach both plain and HTML versions
            part1 = MIMEText(plain_body, 'plain', 'utf-8')
            part2 = MIMEText(html_body, 'html', 'utf-8')
            msg.attach(part1)
            msg.attach(part2)
            
            # Send email
            with smtplib.SMTP(self.smtp_server, self.smtp_port) as server:
                server.starttls()
                server.login(self.smtp_user, self.smtp_password)
                server.send_message(msg)
            
            logger.info(f"βœ“ Email sent to {to_email}")
            return True
        
        except Exception as e:
            logger.error(f"SMTP error: {e}")
            return False


# ═══════════════════════════════════════════════════════════════════════════════
# AUTH PLUGIN MAIN CLASS
# ═══════════════════════════════════════════════════════════════════════════════

class AuthPlugin:
    """Main authentication plugin"""
    
    def __init__(self):
        logger.info("πŸ” Initializing Authentication Plugin...")
        
        # Create directories
        AuthConfig.DATA_DIR.mkdir(parents=True, exist_ok=True)
        
        # Initialize services
        self.email_service = EmailService()
        
        # Load data
        self.users: Dict[str, User] = self._load_users()
        self.sessions: Dict[str, Session] = self._load_sessions()
        self.verifications: Dict[str, VerificationCode] = self._load_verifications()
        
        logger.info(f"βœ… Auth Plugin initialized ({len(self.users)} users)")
    
    # ─────────────────────────────────────────────────────────────────────────
    # DATA PERSISTENCE
    # ─────────────────────────────────────────────────────────────────────────
    
    def _load_users(self) -> Dict[str, User]:
        """Load users from JSON"""
        try:
            if AuthConfig.USERS_FILE.exists():
                with open(AuthConfig.USERS_FILE, 'r', encoding='utf-8') as f:
                    data = json.load(f)
                return {email: User.from_dict(u) for email, u in data.items()}
        except Exception as e:
            logger.error(f"Failed to load users: {e}")
        return {}
    
    def _save_users(self):
        """Save users to JSON"""
        try:
            # Ensure directory exists
            AuthConfig.DATA_DIR.mkdir(parents=True, exist_ok=True)
            data = {email: u.to_dict() for email, u in self.users.items()}
            with open(AuthConfig.USERS_FILE, 'w', encoding='utf-8') as f:
                json.dump(data, f, indent=2, ensure_ascii=False)
        except Exception as e:
            logger.warning(f"⚠️  Failed to save users: {e}")
    
    def _load_sessions(self) -> Dict[str, Session]:
        """Load sessions from JSON"""
        try:
            if AuthConfig.SESSIONS_FILE.exists():
                with open(AuthConfig.SESSIONS_FILE, 'r', encoding='utf-8') as f:
                    data = json.load(f)
                sessions = {token: Session.from_dict(s) for token, s in data.items()}
                # Clean expired sessions
                return {t: s for t, s in sessions.items() if s.is_valid()}
        except Exception as e:
            logger.error(f"Failed to load sessions: {e}")
        return {}
    
    def _save_sessions(self):
        """Save sessions to JSON"""
        try:
            # Ensure directory exists
            AuthConfig.DATA_DIR.mkdir(parents=True, exist_ok=True)
            # Only save valid sessions
            valid_sessions = {t: s for t, s in self.sessions.items() if s.is_valid()}
            data = {token: s.to_dict() for token, s in valid_sessions.items()}
            with open(AuthConfig.SESSIONS_FILE, 'w', encoding='utf-8') as f:
                json.dump(data, f, indent=2, ensure_ascii=False)
        except Exception as e:
            logger.warning(f"⚠️  Failed to save sessions: {e}")
    
    def _load_verifications(self) -> Dict[str, VerificationCode]:
        """Load verification codes from JSON"""
        try:
            if AuthConfig.VERIFICATION_FILE.exists():
                with open(AuthConfig.VERIFICATION_FILE, 'r', encoding='utf-8') as f:
                    data = json.load(f)
                codes = {email: VerificationCode.from_dict(v) for email, v in data.items()}
                # Clean expired codes
                return {e: v for e, v in codes.items() if v.is_valid()}
        except Exception as e:
            logger.error(f"Failed to load verifications: {e}")
        return {}
    
    def _save_verifications(self):
        """Save verification codes to JSON"""
        try:
            # Ensure directory exists
            AuthConfig.DATA_DIR.mkdir(parents=True, exist_ok=True)
            # Only save valid codes
            valid_codes = {e: v for e, v in self.verifications.items() if v.is_valid()}
            data = {email: v.to_dict() for email, v in valid_codes.items()}
            with open(AuthConfig.VERIFICATION_FILE, 'w', encoding='utf-8') as f:
                json.dump(data, f, indent=2, ensure_ascii=False)
        except Exception as e:
            logger.warning(f"⚠️  Failed to save verifications: {e}")
    
    # ─────────────────────────────────────────────────────────────────────────
    # PASSWORD HASHING
    # ─────────────────────────────────────────────────────────────────────────
    
    def _hash_password(self, password: str) -> str:
        """Hash password with salt"""
        salt = secrets.token_hex(16)
        pwd_hash = hashlib.pbkdf2_hmac('sha256', password.encode(), salt.encode(), 100000)
        return f"{salt}${pwd_hash.hex()}"
    
    def _verify_password(self, password: str, password_hash: str) -> bool:
        """Verify password against hash"""
        try:
            salt, pwd_hash = password_hash.split('$')
            computed_hash = hashlib.pbkdf2_hmac('sha256', password.encode(), salt.encode(), 100000)
            return computed_hash.hex() == pwd_hash
        except:
            return False
    
    # ─────────────────────────────────────────────────────────────────────────
    # VERIFICATION CODE GENERATION
    # ─────────────────────────────────────────────────────────────────────────
    
    def _generate_verification_code(self) -> str:
        """Generate 6-digit verification code"""
        return ''.join([str(secrets.randbelow(10)) for _ in range(AuthConfig.VERIFICATION_CODE_LENGTH)])
    
    # ─────────────────────────────────────────────────────────────────────────
    # PUBLIC API METHODS
    # ─────────────────────────────────────────────────────────────────────────
    
    def register(self, email: str, password: str, username: str = None) -> Tuple[bool, str]:
        """

        Register new user and send verification email

        

        Returns: (success, message)

        """
        try:
            # Validate email format
            if '@' not in email or '.' not in email.split('@')[1]:
                return False, "UngΓΌltige Email-Adresse"
            
            # Check if user exists
            if email in self.users:
                return False, "Email bereits registriert"
            
            # Validate password
            if len(password) < 8:
                return False, "Passwort muss mindestens 8 Zeichen lang sein"
            
            # Create user (unverified)
            user = User(
                email=email,
                password_hash=self._hash_password(password),
                created_at=time.time(),
                verified=False,
                username=username
            )
            
            self.users[email] = user
            self._save_users()
            
            # Generate verification code
            code = self._generate_verification_code()
            expires_at = time.time() + (AuthConfig.VERIFICATION_EXPIRY_MINUTES * 60)
            
            verification = VerificationCode(
                email=email,
                code=code,
                created_at=time.time(),
                expires_at=expires_at
            )
            
            self.verifications[email] = verification
            self._save_verifications()
            
            # Send verification email
            email_sent = self.email_service.send_verification_email(email, code)
            
            if email_sent:
                logger.info(f"βœ“ User registered: {email}")
                return True, f"Registrierung erfolgreich! Verifizierungscode wurde an {email} gesendet."
            else:
                return True, "Registrierung erfolgreich, aber Email konnte nicht gesendet werden. Bitte kontaktiere den Support."
        
        except Exception as e:
            logger.error(f"Registration error: {e}")
            return False, f"Fehler bei der Registrierung: {str(e)}"
    
    def verify_email(self, email: str, code: str) -> Tuple[bool, str]:
        """

        Verify email with code

        

        Returns: (success, message)

        """
        try:
            # Check if verification exists
            if email not in self.verifications:
                return False, "Kein Verifizierungscode gefunden"
            
            verification = self.verifications[email]
            
            # Check if expired
            if not verification.is_valid():
                return False, "Verifizierungscode abgelaufen oder zu viele Versuche"
            
            # Increment attempts
            verification.attempts += 1
            self._save_verifications()
            
            # Check code
            if verification.code != code:
                return False, f"Falscher Code (Versuch {verification.attempts}/3)"
            
            # Mark user as verified
            if email in self.users:
                self.users[email].verified = True
                self._save_users()
                
                # Remove verification code
                del self.verifications[email]
                self._save_verifications()
                
                # Send welcome email
                self.email_service.send_welcome_email(email, self.users[email].username)
                
                logger.info(f"βœ“ Email verified: {email}")
                return True, "Email erfolgreich verifiziert! Du kannst dich jetzt anmelden."
            else:
                return False, "User nicht gefunden"
        
        except Exception as e:
            logger.error(f"Verification error: {e}")
            return False, f"Fehler bei der Verifizierung: {str(e)}"
    
    def resend_verification(self, email: str) -> Tuple[bool, str]:
        """Resend verification code"""
        try:
            if email not in self.users:
                return False, "Email nicht registriert"
            
            if self.users[email].verified:
                return False, "Email bereits verifiziert"
            
            # Generate new code
            code = self._generate_verification_code()
            expires_at = time.time() + (AuthConfig.VERIFICATION_EXPIRY_MINUTES * 60)
            
            verification = VerificationCode(
                email=email,
                code=code,
                created_at=time.time(),
                expires_at=expires_at
            )
            
            self.verifications[email] = verification
            self._save_verifications()
            
            # Send email
            email_sent = self.email_service.send_verification_email(email, code)
            
            if email_sent:
                return True, "Neuer Verifizierungscode wurde gesendet"
            else:
                return False, "Email konnte nicht gesendet werden"
        
        except Exception as e:
            logger.error(f"Resend error: {e}")
            return False, f"Fehler: {str(e)}"
    
    def login(self, email: str, password: str, ip_address: str = None, user_agent: str = None) -> Tuple[bool, str, Optional[str]]:
        """

        Login user

        

        Returns: (success, message, token)

        """
        try:
            # Check if user exists
            if email not in self.users:
                return False, "Email oder Passwort falsch", None
            
            user = self.users[email]
            
            # Check if account is locked
            if user.locked_until and time.time() < user.locked_until:
                remaining = int((user.locked_until - time.time()) / 60)
                return False, f"Account gesperrt. Versuche es in {remaining} Minuten erneut.", None
            
            # Check if verified
            if not user.verified:
                return False, "Email noch nicht verifiziert", None
            
            # Verify password
            if not self._verify_password(password, user.password_hash):
                user.login_attempts += 1
                
                # Lock account after max attempts
                if user.login_attempts >= AuthConfig.MAX_LOGIN_ATTEMPTS:
                    user.locked_until = time.time() + (AuthConfig.LOCKOUT_DURATION_MINUTES * 60)
                    self._save_users()
                    return False, f"Zu viele fehlgeschlagene Versuche. Account fΓΌr {AuthConfig.LOCKOUT_DURATION_MINUTES} Minuten gesperrt.", None
                
                self._save_users()
                remaining = AuthConfig.MAX_LOGIN_ATTEMPTS - user.login_attempts
                return False, f"Email oder Passwort falsch ({remaining} Versuche ΓΌbrig)", None
            
            # Reset login attempts on successful login
            user.login_attempts = 0
            user.last_login = time.time()
            user.locked_until = None
            self._save_users()
            
            # Create session
            token = secrets.token_urlsafe(AuthConfig.TOKEN_LENGTH)
            expires_at = time.time() + (AuthConfig.SESSION_EXPIRY_HOURS * 3600)
            
            session = Session(
                token=token,
                email=email,
                created_at=time.time(),
                expires_at=expires_at,
                ip_address=ip_address,
                user_agent=user_agent
            )
            
            self.sessions[token] = session
            self._save_sessions()
            
            logger.info(f"βœ“ User logged in: {email}")
            return True, "Login erfolgreich", token
        
        except Exception as e:
            logger.error(f"Login error: {e}")
            return False, f"Fehler beim Login: {str(e)}", None
    
    def logout(self, token: str) -> Tuple[bool, str]:
        """Logout user"""
        try:
            if token in self.sessions:
                email = self.sessions[token].email
                del self.sessions[token]
                self._save_sessions()
                logger.info(f"βœ“ User logged out: {email}")
                return True, "Logout erfolgreich"
            else:
                return False, "Session nicht gefunden"
        except Exception as e:
            logger.error(f"Logout error: {e}")
            return False, f"Fehler beim Logout: {str(e)}"
    
    def validate_token(self, token: str) -> Tuple[bool, Optional[str]]:
        """

        Validate session token

        

        Returns: (valid, email)

        """
        if token in self.sessions:
            session = self.sessions[token]
            if session.is_valid():
                return True, session.email
            else:
                # Clean up expired session
                del self.sessions[token]
                self._save_sessions()
        
        return False, None
    
    def get_user_info(self, email: str) -> Optional[Dict]:
        """Get user information (without sensitive data)"""
        if email in self.users:
            user = self.users[email]
            return {
                'email': user.email,
                'username': user.username,
                'created_at': user.created_at,
                'verified': user.verified,
                'last_login': user.last_login
            }
        return None
    
    def get_stats(self) -> Dict:
        """Get authentication statistics"""
        return {
            'total_users': len(self.users),
            'verified_users': sum(1 for u in self.users.values() if u.verified),
            'active_sessions': len([s for s in self.sessions.values() if s.is_valid()]),
            'pending_verifications': len([v for v in self.verifications.values() if v.is_valid()])
        }


# ═══════════════════════════════════════════════════════════════════════════════
# PLUGIN METADATA
# ═══════════════════════════════════════════════════════════════════════════════

PLUGIN_INFO = {
    'name': 'auth_plugin',
    'version': '1.0.0',
    'author': 'NoahsKI Team',
    'description': 'Email-based authentication with verification',
    'endpoints': [
        '/auth/register',
        '/auth/verify',
        '/auth/resend',
        '/auth/login',
        '/auth/logout',
        '/auth/validate',
        '/auth/user',
        '/auth/stats'
    ]
}

__all__ = ['AuthPlugin', 'AuthConfig', 'PLUGIN_INFO']