File size: 29,323 Bytes
d15f07c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
OAuth authentication routes - HuggingFace, Google, Facebook, GitHub
"""
import os
import hmac
import hashlib
import httpx
from datetime import datetime, timedelta
from typing import Optional
from urllib.parse import urlencode
from dotenv import load_dotenv
from loguru import logger

from fastapi import APIRouter, Depends, HTTPException, Request, status
from fastapi.responses import RedirectResponse
from sqlalchemy.orm import Session
from pydantic import BaseModel

from api.database import get_db
from api.models import User, OAuthState
from api.auth import create_access_token, generate_state_token, is_admin_email
from api.origins import is_safe_redirect

# Load environment variables from .env file
load_dotenv()

router = APIRouter(prefix="/auth", tags=["authentication"])

# OAuth provider configurations
OAUTH_PROVIDERS = {
    'huggingface': {
        'authorize_url': 'https://huggingface.co/oauth/authorize',
        'token_url': 'https://huggingface.co/oauth/token',
        'userinfo_url': 'https://huggingface.co/api/whoami-v2',
        'scope': 'openid profile email',
        'client_id_env': 'HUGGINGFACE_CLIENT_ID',
        'client_secret_env': 'HUGGINGFACE_CLIENT_SECRET',
    },
    'google': {
        'authorize_url': 'https://accounts.google.com/o/oauth2/v2/auth',
        'token_url': 'https://oauth2.googleapis.com/token',
        'userinfo_url': 'https://www.googleapis.com/oauth2/v2/userinfo',
        'scope': 'openid email profile',
        'client_id_env': 'GOOGLE_CLIENT_ID',
        'client_secret_env': 'GOOGLE_CLIENT_SECRET',
    },
    'facebook': {
        'authorize_url': 'https://www.facebook.com/v18.0/dialog/oauth',
        'token_url': 'https://graph.facebook.com/v18.0/oauth/access_token',
        'userinfo_url': 'https://graph.facebook.com/me?fields=id,name,email,picture',
        'scope': 'email public_profile',
        'client_id_env': 'FACEBOOK_APP_ID',
        'client_secret_env': 'FACEBOOK_APP_SECRET',
    },
    'github': {
        'authorize_url': 'https://github.com/login/oauth/authorize',
        'token_url': 'https://github.com/login/oauth/access_token',
        'userinfo_url': 'https://api.github.com/user',
        'scope': 'user:email',
        'client_id_env': 'GITHUB_CLIENT_ID',
        'client_secret_env': 'GITHUB_CLIENT_SECRET',
    },
}


# Response models
class TokenResponse(BaseModel):
    access_token: str
    token_type: str = "bearer"
    user: dict


class UserResponse(BaseModel):
    user_id: int
    email: str
    username: Optional[str]
    full_name: Optional[str]
    avatar_url: Optional[str]
    oauth_provider: Optional[str]
    state: Optional[str]
    county: Optional[str]
    city: Optional[str]
    school_board: Optional[str]
    profile_completed: Optional[bool]
    is_admin: Optional[bool] = False


# Helper functions
def get_base_url(request: Request) -> str:
    """
    Get the base URL from the request, handling proxy headers
    
    In production (HuggingFace Spaces with nginx reverse proxy):
    - Returns: https://www.communityone.com
    
    In local development:
    - Returns: http://localhost:8001
    """
    # Check for explicit API_BASE_URL override first
    if base_url := os.getenv('API_BASE_URL'):
        # Only use if it's not the default localhost value
        if 'localhost' not in base_url and '127.0.0.1' not in base_url:
            return base_url
    
    # Detect from request headers (handles nginx reverse proxy)
    scheme = request.headers.get('x-forwarded-proto', request.url.scheme)
    host = request.headers.get('x-forwarded-host', request.headers.get('host', request.url.netloc))
    
    # Clean up host (remove port if it's standard)
    if ':' in host:
        host_parts = host.split(':')
        port = host_parts[1]
        # Remove standard ports
        if (scheme == 'https' and port == '443') or (scheme == 'http' and port == '80'):
            host = host_parts[0]
    
    return f"{scheme}://{host}"


def get_or_create_user(
    db: Session,
    email: str,
    provider: str,
    oauth_id: str,
    full_name: Optional[str] = None,
    avatar_url: Optional[str] = None,
    username: Optional[str] = None,
    email_verified: bool = False,
) -> User:
    """Get existing user or create new one from OAuth data"""
    
    # Try to find existing user by OAuth ID first
    user = db.query(User).filter(
        User.oauth_provider == provider,
        User.oauth_id == oauth_id
    ).first()
    
    # Admin status is derived from the ADMIN_EMAILS allowlist and re-evaluated on
    # every login, so promoting/demoting an admin is a config change, not a DB edit.
    admin = is_admin_email(email)

    if user:
        # Update user info if changed. Keep email in sync with the provider: it
        # is the identity `is_admin`/`require_admin` key off, so letting it drift
        # would silently lock out an admin whose provider email changed.
        user.email = email
        user.full_name = full_name or user.full_name
        user.avatar_url = avatar_url or user.avatar_url
        user.username = username or user.username
        user.last_login = datetime.utcnow()
        user.is_admin = admin
        db.commit()
        return user

    # Try to find by email
    user = db.query(User).filter(User.email == email).first()

    if user:
        # Merging a new OAuth identity into a pre-existing account (matched only
        # by email, not by this provider's own oauth_id) is an account-takeover
        # vector unless the provider attests the email is verified. Refuse
        # otherwise β€” email is unique, so a separate account isn't possible.
        if not email_verified:
            raise HTTPException(
                status_code=status.HTTP_409_CONFLICT,
                detail=(
                    "An account with this email already exists. Sign in with the "
                    "method you used originally, then link this provider from "
                    "settings."
                ),
            )
        # Link OAuth account to existing user
        user.oauth_provider = provider
        user.oauth_id = oauth_id
        user.full_name = full_name or user.full_name
        user.avatar_url = avatar_url or user.avatar_url
        user.username = username or user.username
        user.last_login = datetime.utcnow()
        user.is_verified = True  # OAuth emails are verified
        user.is_admin = admin
        db.commit()
        return user

    # Create new user
    user = User(
        email=email,
        username=username,
        full_name=full_name,
        avatar_url=avatar_url,
        oauth_provider=provider,
        oauth_id=oauth_id,
        is_verified=True,
        is_active=True,
        is_admin=admin,
        last_login=datetime.utcnow(),
    )
    db.add(user)
    db.commit()
    db.refresh(user)
    return user


# OAuth routes
@router.get("/login/{provider}")
async def oauth_login(
    provider: str,
    request: Request,
    db: Session = Depends(get_db),
    redirect_uri: Optional[str] = None
):
    """
    Initiate OAuth login flow
    
    Supported providers: huggingface, google, facebook, github
    """
    if provider not in OAUTH_PROVIDERS:
        raise HTTPException(status_code=400, detail=f"Unsupported provider: {provider}")

    # The callback appends the freshly minted JWT to redirect_uri, so an
    # attacker-controlled target would exfiltrate the token. Drop anything not
    # provably safe (an allowlisted origin or a same-origin relative path) and
    # fall back to the trusted default β€” never hard-fail login over it.
    if redirect_uri and not is_safe_redirect(redirect_uri):
        logger.warning("Dropping unsafe OAuth redirect_uri: {}", redirect_uri)
        redirect_uri = None

    config = OAUTH_PROVIDERS[provider]
    client_id = os.getenv(config['client_id_env'])
    client_secret = os.getenv(config['client_secret_env'])

    # Missing OAuth credentials is a configuration problem, not a server crash.
    # Return a clear 503 so the cause is visible instead of a swallowed 500.
    if not client_id or not client_secret:
        missing = config['client_id_env'] if not client_id else config['client_secret_env']
        logger.error("OAuth not configured for {}: missing env var {}", provider, missing)
        raise HTTPException(
            status_code=503,
            detail=(
                f"{provider.title()} sign-in is temporarily unavailable "
                f"(server is missing {missing}). Please try another sign-in method "
                "or contact johnbowyer@communityone.com."
            ),
        )

    # Generate state token for CSRF protection and persist it.
    # A failure here is almost always a missing/unmigrated oauth_states table β€”
    # surface that explicitly rather than letting it fall through as a generic 500.
    state = generate_state_token()
    try:
        oauth_state = OAuthState(
            state_token=state,
            provider=provider,
            redirect_uri=redirect_uri,
            expires_at=datetime.utcnow() + timedelta(minutes=10),
        )
        db.add(oauth_state)
        db.commit()
    except Exception as e:
        db.rollback()
        logger.exception("Failed to persist OAuth state for {}: {}", provider, e)
        raise HTTPException(
            status_code=503,
            detail=(
                f"{provider.title()} sign-in is temporarily unavailable "
                "(could not start the login session). Please try again shortly "
                "or contact johnbowyer@communityone.com."
            ),
        )
    
    # Build callback URL dynamically from request (handles both local and production)
    base_url = get_base_url(request)
    callback_url = f"{base_url}/api/auth/callback/{provider}"
    
    # Build authorization URL
    params = {
        'client_id': client_id,
        'redirect_uri': callback_url,
        'scope': config['scope'],
        'state': state,
        'response_type': 'code',
    }
    
    auth_url = f"{config['authorize_url']}?{urlencode(params)}"
    return RedirectResponse(url=auth_url)


def _safe_error_redirect_base(oauth_state) -> str:
    """Base URL for an OAuth *error* redirect, re-validated against the allowlist.

    ``redirect_uri`` is validated at ``/login`` before it is stored, but a stale
    ``OAuthState`` row (one written before a deploy, or by any future code path
    that skips that check) could still carry an unsafe target. An error redirect
    echoes attacker-controllable text in the query string, so never bounce it to
    anything not provably safe β€” fall back to a same-origin ``/``.
    """
    frontend_url = os.getenv('FRONTEND_URL', '')
    redirect_url = oauth_state.redirect_uri or (
        frontend_url if frontend_url and 'localhost' not in frontend_url else '/'
    )
    return redirect_url if is_safe_redirect(redirect_url) else '/'


@router.get("/callback/{provider}", name="oauth_callback")
async def oauth_callback(
    provider: str,
    request: Request,
    code: Optional[str] = None,
    state: Optional[str] = None,
    error: Optional[str] = None,
    db: Session = Depends(get_db)
):
    """OAuth callback handler"""
    
    if error:
        raise HTTPException(status_code=400, detail=f"OAuth error: {error}")
    
    if not code or not state:
        raise HTTPException(status_code=400, detail="Missing code or state parameter")
    
    if provider not in OAUTH_PROVIDERS:
        raise HTTPException(status_code=400, detail=f"Unsupported provider: {provider}")
    
    # Verify state token
    oauth_state = db.query(OAuthState).filter(
        OAuthState.state_token == state,
        OAuthState.provider == provider
    ).first()
    
    if not oauth_state or oauth_state.expires_at < datetime.utcnow():
        raise HTTPException(status_code=400, detail="Invalid or expired state token")
    
    config = OAUTH_PROVIDERS[provider]
    client_id = os.getenv(config['client_id_env'])
    client_secret = os.getenv(config['client_secret_env'])
    
    # Build callback URL dynamically from request (must match the one sent to authorize)
    base_url = get_base_url(request)
    callback_url = f"{base_url}/api/auth/callback/{provider}"
    
    # Exchange code for access token
    try:
        logger.info(f"πŸ” [{provider.upper()}] Starting token exchange")
        logger.info(f"πŸ” [{provider.upper()}] Token URL: {config['token_url']}")
        logger.info(f"πŸ” [{provider.upper()}] Callback URL: {callback_url}")
        logger.info(f"πŸ” [{provider.upper()}] Client ID configured: {bool(client_id)}")
        logger.info(f"πŸ” [{provider.upper()}] Client Secret configured: {bool(client_secret)}")
        
        async with httpx.AsyncClient() as client:
            token_response = await client.post(
                config['token_url'],
                data={
                    'client_id': client_id,
                    'client_secret': client_secret,
                    'code': code,
                    'redirect_uri': callback_url,
                    'grant_type': 'authorization_code',
                },
                headers={'Accept': 'application/json'}
            )
            
            logger.info(f"πŸ“Š [{provider.upper()}] Token response status: {token_response.status_code}")
            
            if token_response.status_code != 200:
                logger.error(f"❌ [{provider.upper()}] Token exchange failed!")
                logger.error(f"❌ [{provider.upper()}] Response: {token_response.text}")
                
                # Parse error response for user-friendly message
                error_msg = "Authentication failed"
                try:
                    error_data = token_response.json()
                    # Facebook error format: {"error": {"message": "...", "type": "..."}}
                    if 'error' in error_data:
                        if isinstance(error_data['error'], dict):
                            error_msg = error_data['error'].get('message', error_msg)
                            # Special handling for Facebook's "error validating web secret"
                            if provider == 'facebook' and 'validating' in error_msg.lower() and 'secret' in error_msg.lower():
                                error_msg = "Invalid App Secret. Please update FACEBOOK_APP_SECRET in your .env file with the correct secret from Facebook Developer Console."
                        else:
                            error_msg = str(error_data['error'])
                    # Google/other providers: {"error": "...", "error_description": "..."}
                    elif 'error_description' in error_data:
                        error_msg = error_data['error_description']
                    elif 'error' in error_data:
                        error_msg = error_data['error']
                except:
                    error_msg = f"Authentication failed (HTTP {token_response.status_code})"
                
                # Log full error for debugging
                logger.error(f"OAuth token exchange failed for {provider}: {token_response.text}")
                
                # Redirect to frontend with error
                redirect_url = _safe_error_redirect_base(oauth_state)
                params = urlencode({'error': f'{provider.title()} login failed: {error_msg}'})
                return RedirectResponse(url=f"{redirect_url}?{params}")
            
            token_data = token_response.json()
            logger.info(f"βœ… [{provider.upper()}] Token response keys: {list(token_data.keys())}")
            access_token = token_data.get('access_token')
            
            if not access_token:
                logger.error(f"❌ [{provider.upper()}] No access token in response!")
                logger.error(f"No access token in response from {provider}: {token_data}")
                redirect_url = _safe_error_redirect_base(oauth_state)
                params = urlencode({'error': f'{provider.title()} login failed: No access token received'})
                return RedirectResponse(url=f"{redirect_url}?{params}")
            
            logger.info(f"βœ… [{provider.upper()}] Got access token (first 20 chars): {access_token[:20]}...")
            
            # Get user info from provider
            logger.info(f"πŸ” [{provider.upper()}] Fetching user info...")
            user_info = await get_user_info(provider, access_token, config)
            logger.info(f"πŸ“Š [{provider.upper()}] User info result: {user_info is not None}")
        
        # Validate we got user info (email is now always set, even if placeholder for Facebook)
        if not user_info:
            logger.error(f"❌ [{provider.upper()}] Could not retrieve user info! Check API response logs above.")
            logger.error(f"Could not retrieve user info from {provider}. Check API response logs above.")
            redirect_url = _safe_error_redirect_base(oauth_state)
            params = urlencode({'error': f'{provider.title()} login failed: Could not retrieve user information'})
            return RedirectResponse(url=f"{redirect_url}?{params}")
    
    except Exception as e:
        logger.error(f"Unexpected error during {provider} OAuth: {str(e)}")
        redirect_url = _safe_error_redirect_base(oauth_state)
        params = urlencode({'error': f'{provider.title()} login failed: {str(e)}'})
        return RedirectResponse(url=f"{redirect_url}?{params}")
    
    # Get or create user. A verified-email conflict (an existing account owns
    # this email under a different identity) surfaces as a friendly error
    # redirect rather than a raw 409.
    try:
        user = get_or_create_user(
            db=db,
            email=user_info['email'],
            provider=provider,
            oauth_id=user_info['oauth_id'],
            full_name=user_info.get('full_name'),
            avatar_url=user_info.get('avatar_url'),
            username=user_info.get('username'),
            email_verified=bool(user_info.get('email_verified')),
        )
    except HTTPException as exc:
        redirect_url = _safe_error_redirect_base(oauth_state)
        params = urlencode({'error': exc.detail})
        return RedirectResponse(url=f"{redirect_url}?{params}")

    # Clean up state token
    db.delete(oauth_state)
    db.commit()
    
    # Create JWT token (sub must be string, not int)
    jwt_token = create_access_token(data={"sub": str(user.user_id)})
    
    # Redirect to frontend with token
    # On HuggingFace/production, frontend and backend are same domain - use relative path
    # On local dev, frontend is separate server - use FRONTEND_URL
    frontend_url = os.getenv('FRONTEND_URL', '')
    
    # If FRONTEND_URL is localhost or not set, assume same-domain deployment (HuggingFace)
    if not frontend_url or 'localhost' in frontend_url:
        # Use relative redirect (works on HuggingFace where both are same domain)
        redirect_url = oauth_state.redirect_uri or '/'
    else:
        # Use absolute URL for separate frontend server
        redirect_url = oauth_state.redirect_uri or frontend_url
    
    # Defense-in-depth: redirect_uri was validated at /login, but never append
    # the token to a target that isn't provably safe.
    if not is_safe_redirect(redirect_url):
        logger.warning("Refusing token redirect to unsafe target: {}", redirect_url)
        redirect_url = '/'

    # Append token as URL parameter
    params = urlencode({'token': jwt_token})
    full_redirect_url = f"{redirect_url}?{params}" if '?' not in redirect_url else f"{redirect_url}&{params}"
    
    return RedirectResponse(url=full_redirect_url)


async def get_user_info(provider: str, access_token: str, config: dict) -> dict:
    """Get user information from OAuth provider"""
    
    async with httpx.AsyncClient() as client:
        headers = {'Authorization': f'Bearer {access_token}'}
        
        user_info = {}
        
        if provider == 'huggingface':
            resp = await client.get(config['userinfo_url'], headers=headers)
            data = resp.json()
            user_info = {
                'email': data.get('email'),
                # whoami-v2 returns camelCase `emailVerified`; the OIDC
                # /oauth/userinfo endpoint uses snake_case. Accept either.
                'email_verified': bool(data.get('emailVerified') or data.get('email_verified')),
                'oauth_id': str(data.get('id')),
                'full_name': data.get('fullname') or data.get('name'),
                'avatar_url': data.get('avatarUrl'),
                'username': data.get('name'),
            }
        
        elif provider == 'google':
            resp = await client.get(config['userinfo_url'], headers=headers)
            data = resp.json()
            user_info = {
                'email': data.get('email'),
                # Google v2 userinfo returns `verified_email`; OIDC returns
                # `email_verified`. Accept either.
                'email_verified': bool(
                    data.get('verified_email') or data.get('email_verified')
                ),
                'oauth_id': data.get('id'),
                'full_name': data.get('name'),
                'avatar_url': data.get('picture'),
                'username': data.get('email', '').split('@')[0],
            }
        
        elif provider == 'facebook':
            # Facebook requires appsecret_proof for server-side API calls
            # Generate HMAC-SHA256 hash of access token using app secret
            app_secret = os.getenv('FACEBOOK_APP_SECRET')
            if not app_secret:
                logger.error("❌ [FACEBOOK] FACEBOOK_APP_SECRET not configured!")
                return None
            
            # Create appsecret_proof
            appsecret_proof = hmac.new(
                app_secret.encode('utf-8'),
                access_token.encode('utf-8'),
                hashlib.sha256
            ).hexdigest()
            
            # Facebook uses access token as query parameter, not Bearer header
            # Add appsecret_proof for security
            userinfo_url_with_token = f"{config['userinfo_url']}&access_token={access_token}&appsecret_proof={appsecret_proof}"
            
            logger.info(f"πŸ” [FACEBOOK] Requesting user info from: {config['userinfo_url']}")
            logger.info(f"πŸ” [FACEBOOK] Access token (first 20 chars): {access_token[:20]}...")
            logger.info(f"πŸ” [FACEBOOK] Generated appsecret_proof: {appsecret_proof[:20]}...")
            
            try:
                resp = await client.get(userinfo_url_with_token)
                
                # Enhanced logging for debugging
                logger.info(f"πŸ“Š [FACEBOOK] API response status: {resp.status_code}")
                logger.info(f"πŸ“Š [FACEBOOK] API response headers: {dict(resp.headers)}")
                logger.info(f"πŸ“Š [FACEBOOK] API response body: {resp.text[:1000]}")
                
                if resp.status_code != 200:
                    logger.error(f"❌ [FACEBOOK] Userinfo request failed!")
                    logger.error(f"❌ [FACEBOOK] Status: {resp.status_code}")
                    logger.error(f"❌ [FACEBOOK] Full response: {resp.text}")
                    return None
                
                data = resp.json()
                logger.info(f"βœ… [FACEBOOK] Successfully parsed JSON response")
                logger.info(f"βœ… [FACEBOOK] Data keys: {list(data.keys())}")
                
                # Validate we got required data
                if not data.get('id'):
                    logger.error(f"❌ [FACEBOOK] Missing user ID in response!")
                    logger.error(f"❌ [FACEBOOK] Full response data: {data}")
                    return None
                
                logger.info(f"βœ… [FACEBOOK] Got user ID: {data.get('id')}")
                
                # Facebook may not return email if permission not approved in App Review
                # Generate a placeholder email using Facebook ID if email not available
                email = data.get('email')
                if not email:
                    fb_id = data.get('id')
                    email = f"facebook_{fb_id}@communityone.placeholder"
                    logger.warning(f"⚠️  [FACEBOOK] No email returned for user {fb_id}. Using placeholder: {email}")
                else:
                    logger.info(f"βœ… [FACEBOOK] Got email: {email}")
                
                user_info = {
                    'email': email,
                    # Graph API exposes no email-verification flag, and the email
                    # may be a synthetic placeholder β€” never treat it as verified
                    # for the purpose of merging into an existing account.
                    'email_verified': False,
                    'oauth_id': str(data.get('id')),
                    'full_name': data.get('name'),
                    'avatar_url': data.get('picture', {}).get('data', {}).get('url') if isinstance(data.get('picture'), dict) else None,
                    'username': data.get('name', '').replace(' ', '_').lower(),
                }
                logger.info(f"βœ… [FACEBOOK] Created user_info: {user_info}")
                
            except Exception as e:
                logger.error(f"❌ [FACEBOOK] Exception during user info fetch: {str(e)}")
                logger.error(f"❌ [FACEBOOK] Exception type: {type(e).__name__}")
                import traceback
                logger.error(f"❌ [FACEBOOK] Traceback: {traceback.format_exc()}")
                return None
        
        elif provider == 'github':
            # Get user profile
            resp = await client.get(config['userinfo_url'], headers=headers)
            data = resp.json()
            
            # Resolve the primary email and its verified status from
            # /user/emails β€” the /user profile email alone carries no
            # verification signal.
            email = None
            email_verified = False
            resp_emails = await client.get('https://api.github.com/user/emails', headers=headers)
            if resp_emails.status_code == 200:
                emails = resp_emails.json()
                primary = next(
                    (e for e in emails if e.get('primary')),
                    emails[0] if emails else None,
                )
                if primary:
                    email = primary.get('email')
                    email_verified = bool(primary.get('verified'))
            if not email:
                # Fallback to the profile email; verification stays unknown.
                email = data.get('email')

            user_info = {
                'email': email,
                'email_verified': email_verified,
                'oauth_id': str(data.get('id')),
                'full_name': data.get('name'),
                'avatar_url': data.get('avatar_url'),
                'username': data.get('login'),
            }
        
        return user_info


@router.get("/me", response_model=UserResponse)
def get_current_user_info(
    request: Request,
    db: Session = Depends(get_db)
):
    """Get current authenticated user info"""
    
    # Get token from Authorization header
    auth_header = request.headers.get('Authorization')
    if not auth_header or not auth_header.startswith('Bearer '):
        raise HTTPException(status_code=401, detail="Not authenticated")
    
    token = auth_header.split(' ')[1]
    
    # Decode token and get user
    from api.auth import decode_access_token
    payload = decode_access_token(token)
    user_id = int(payload.get('sub'))  # Convert back to int for DB query
    
    user = db.query(User).filter(User.user_id == user_id).first()
    if not user:
        raise HTTPException(status_code=404, detail="User not found")
    
    return user


@router.patch("/profile", response_model=UserResponse)
def update_user_profile(
    profile_data: dict,
    request: Request,
    db: Session = Depends(get_db)
):
    """Update user profile (location preferences)"""
    
    # Get token from Authorization header
    auth_header = request.headers.get('Authorization')
    if not auth_header or not auth_header.startswith('Bearer '):
        raise HTTPException(status_code=401, detail="Not authenticated")
    
    token = auth_header.split(' ')[1]
    
    # Decode token and get user
    from api.auth import decode_access_token
    payload = decode_access_token(token)
    user_id = int(payload.get('sub'))
    
    user = db.query(User).filter(User.user_id == user_id).first()
    if not user:
        raise HTTPException(status_code=404, detail="User not found")
    
    # Update allowed fields
    allowed_fields = ['state', 'county', 'city', 'school_board', 'profile_completed']
    for field, value in profile_data.items():
        if field in allowed_fields and hasattr(user, field):
            setattr(user, field, value)
    
    user.updated_at = datetime.utcnow()
    db.commit()
    db.refresh(user)
    
    return user


@router.post("/logout")
def logout():
    """Logout endpoint (client-side token removal)"""
    return {"message": "Logged out successfully. Please remove the token from client."}