-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.py
More file actions
1379 lines (1194 loc) · 58.9 KB
/
app.py
File metadata and controls
1379 lines (1194 loc) · 58.9 KB
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
import os
from pathlib import Path
from dotenv import load_dotenv
ENV = os.getenv("ENVIRONMENT", "development").lower()
LOG_LEVEL = logging.INFO if ENV == "production" else logging.DEBUG
logging.basicConfig(
level=LOG_LEVEL,
format='%(asctime)s %(levelname)s %(name)s %(message)s',
handlers=[logging.StreamHandler()]
)
load_dotenv(dotenv_path=Path(__file__).parent / '.env', override=True)
"""
FastAPI application to generate professional resumes from GitHub repositories using AI.
Optimized for production with modular design, robust logging, and efficient session management.
"""
import asyncio
import json
import os
import platform
import re
import time
import urllib.parse
import uuid
from collections import defaultdict
from contextlib import asynccontextmanager
from datetime import datetime, timezone
from pathlib import Path
from typing import Optional, Dict, Any
import httpx
import redis
import ipaddress
from api_analytics.fastapi import Analytics
from fastapi import FastAPI, Request, Form, WebSocket, WebSocketDisconnect, HTTPException, Query
from fastapi.responses import HTMLResponse, FileResponse, RedirectResponse, Response
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from github import Github, GithubException
from pydantic import BaseModel, field_validator
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.errors import RateLimitExceeded
from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.middleware.cors import CORSMiddleware
from starlette.middleware.gzip import GZipMiddleware
from starlette.middleware.sessions import SessionMiddleware
from starlette.middleware.trustedhost import TrustedHostMiddleware
from starlette.websockets import WebSocketState
from tools import gitingest_tool, clone_repo_tool, create_resume_tool
from tools.gitingest import IGNORE_DIRS, IGNORE_EXTENSIONS
from tools.utils import robust_rmtree
# --- Analytics Counters ---
ANALYTICS_TOTAL_USERS_KEY = "analytics:total_users"
ANALYTICS_TOTAL_REPOS_KEY = "analytics:total_repos_analyzed"
ANALYTICS_TOTAL_REPO_SIZE_KEY = "analytics:total_repo_size_mb"
ANALYTICS_TOTAL_FILES_KEY = "analytics:total_files_analyzed"
ANALYTICS_MAX_REPO_SIZE_KEY = "analytics:max_repo_size_mb"
ANALYTICS_MAX_FILES_KEY = "analytics:max_files_in_repo"
async def increment_analytics_counter(key: str, unique_value: str = None):
if not redis_client:
return
if unique_value:
# Use a Redis set for unique values (e.g., users)
await asyncio.to_thread(redis_client.sadd, key, unique_value)
else:
await asyncio.to_thread(redis_client.incr, key)
async def increment_repo_size_and_files(size_mb: float, file_count: int):
if not redis_client:
return
await asyncio.to_thread(redis_client.incrbyfloat, ANALYTICS_TOTAL_REPO_SIZE_KEY, size_mb)
await asyncio.to_thread(redis_client.incrby, ANALYTICS_TOTAL_FILES_KEY, file_count)
# Track max repo size
current_max_size = await asyncio.to_thread(redis_client.get, ANALYTICS_MAX_REPO_SIZE_KEY)
if not current_max_size or float(size_mb) > float(current_max_size):
await asyncio.to_thread(redis_client.set, ANALYTICS_MAX_REPO_SIZE_KEY, size_mb)
# Track max file count
current_max_files = await asyncio.to_thread(redis_client.get, ANALYTICS_MAX_FILES_KEY)
if not current_max_files or int(file_count) > int(current_max_files):
await asyncio.to_thread(redis_client.set, ANALYTICS_MAX_FILES_KEY, file_count)
async def get_analytics():
if not redis_client:
return {
"total_users": None, "total_repos_analyzed": None, "total_repo_size_mb": None, "total_files_analyzed": None,
"avg_repo_size_mb": None, "avg_files_per_repo": None, "max_repo_size_mb": None, "max_files_in_repo": None
}
total_users = await asyncio.to_thread(redis_client.scard, ANALYTICS_TOTAL_USERS_KEY)
total_repos = await asyncio.to_thread(redis_client.get, ANALYTICS_TOTAL_REPOS_KEY)
total_repo_size = await asyncio.to_thread(redis_client.get, ANALYTICS_TOTAL_REPO_SIZE_KEY)
total_files = await asyncio.to_thread(redis_client.get, ANALYTICS_TOTAL_FILES_KEY)
max_repo_size = await asyncio.to_thread(redis_client.get, ANALYTICS_MAX_REPO_SIZE_KEY)
max_files = await asyncio.to_thread(redis_client.get, ANALYTICS_MAX_FILES_KEY)
total_repos_val = int(total_repos) if total_repos else 0
total_repo_size_val = float(total_repo_size) if total_repo_size else 0.0
total_files_val = int(total_files) if total_files else 0
avg_repo_size = (total_repo_size_val / total_repos_val) if total_repos_val else 0.0
avg_files = (total_files_val / total_repos_val) if total_repos_val else 0.0
return {
"total_users": total_users,
"total_repos_analyzed": total_repos_val,
"total_repo_size_mb": total_repo_size_val,
"total_files_analyzed": total_files_val,
"avg_repo_size_mb": avg_repo_size,
"avg_files_per_repo": avg_files,
"max_repo_size_mb": float(max_repo_size) if max_repo_size else 0.0,
"max_files_in_repo": int(max_files) if max_files else 0
}
# Configuration
ENV = os.getenv("ENVIRONMENT", "development").lower()
LOG_LEVEL = logging.INFO if ENV == "production" else logging.DEBUG
SESSION_COOKIE_NAME = "gitresume_session"
SESSION_EXPIRY_SECONDS = 604800 # 7 days
CACHE_TTL = 300 # Cache time-to-live in seconds
DDoS_WINDOW = 10 # Seconds for DDoS tracking
DDoS_MAX_REQUESTS = 50 # Max requests in DDoS window
CLOUDFLARE_ONLY = os.getenv("CLOUDFLARE_ONLY", "false").lower() == "true"
# Cloudflare IP ranges for middleware
CLOUDFLARE_IP_RANGES = [
# IPv4
"173.245.48.0/20", "103.21.244.0/22", "103.22.200.0/22", "103.31.4.0/22",
"141.101.64.0/18", "108.162.192.0/18", "190.93.240.0/20", "188.114.96.0/20",
"197.234.240.0/22", "198.41.128.0/17", "162.158.0.0/15", "104.16.0.0/13",
"104.24.0.0/14", "172.64.0.0/13", "131.0.72.0/22",
# IPv6
"2400:cb00::/32", "2606:4700::/32", "2803:f800::/32", "2405:b500::/32",
"2405:8100::/32", "2a06:98c0::/29", "2c0f:f248::/32"
]
CLOUDFLARE_NETWORKS = [ipaddress.ip_network(cidr) for cidr in CLOUDFLARE_IP_RANGES]
# Global caches
repository_cache = {}
request_counts = defaultdict(list)
def get_redis_client() -> Optional[redis.Redis]:
"""Initialize and return a Redis client or None if connection fails."""
try:
client = redis.Redis(
host=os.getenv("REDIS_HOST"),
port=int(os.getenv("REDIS_PORT")),
decode_responses=True,
username=os.getenv("REDIS_USERNAME"),
password=os.getenv("REDIS_PASSWORD"),
socket_connect_timeout=5,
socket_timeout=5,
retry_on_timeout=True,
health_check_interval=30,
max_connections=20
)
client.ping()
logging.info({"message": "Redis connection established successfully"})
return client
except Exception as e:
logging.warning(
{"message": "Redis connection failed, falling back to memory-based rate limiting", "error": str(e)})
return None
redis_client = get_redis_client()
def validate_github_url(url: str) -> bool:
"""Validate if the provided URL is a valid GitHub repository URL."""
if not url or not isinstance(url, str) or re.search(r'[<>"\'`]', url):
return False
pattern = r'^https://github\.com/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/?$'
return bool(re.match(pattern, url))
def validate_github_token(token: str) -> bool:
"""Validate if the provided GitHub token is in a valid format."""
if not token or not isinstance(token, str):
return False
pattern = r'^[a-zA-Z0-9_]+$'
return bool(re.match(pattern, token)) and len(token) >= 20
def sanitize_input(text: str, max_length: int = 1000) -> str:
"""Sanitize input string by removing dangerous characters and enforcing length limit."""
if not text or not isinstance(text, str):
return ""
return re.sub(r'[<>"\'`]', '', text.strip())[:max_length]
# Set Windows event loop policy if applicable
if platform.system() == "Windows":
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
logging.info({"message": "Set WindowsProactorEventLoopPolicy for Windows compatibility"})
class GitHubSessionData(BaseModel):
"""Pydantic model for session data."""
user_id: str
access_token: Optional[str] = None
github_token: Optional[str] = None
redirect_url: Optional[str] = None
session_id: str
created_at: str
repo_url: Optional[str] = None
local_path: Optional[str] = None
job_description: Optional[str] = None
result: Optional[Dict[str, Any]] = None
status: Optional[str] = None
@field_validator('repo_url')
def validate_repo_url(cls, v):
if v and not validate_github_url(v):
raise ValueError('Invalid GitHub repository URL')
return v
@field_validator('github_token')
def validate_github_token_field(cls, v):
if v and not validate_github_token(v):
raise ValueError('Invalid GitHub token format')
return v
@field_validator('job_description')
def validate_job_description(cls, v):
if v:
return sanitize_input(v, max_length=5000)
return v
model_config = {"arbitrary_types_allowed": True}
async def save_session(session: GitHubSessionData):
"""Save session data to Redis with expiry."""
if not redis_client:
raise RuntimeError("Redis is required for session management.")
await asyncio.to_thread(
redis_client.setex,
f"session:{session.session_id}",
SESSION_EXPIRY_SECONDS,
session.model_dump_json()
)
async def get_session(session_id: str) -> Optional[GitHubSessionData]:
"""Retrieve session data from Redis."""
if not redis_client:
return None
data = await asyncio.to_thread(redis_client.get, f"session:{session_id}")
if not data:
return None
try:
return GitHubSessionData(**json.loads(data))
except Exception as e:
logging.warning({"message": "Invalid session data in Redis", "session_id": session_id, "error": str(e)})
return None
async def delete_session(session_id: str):
"""Delete session data from Redis."""
if redis_client:
await asyncio.to_thread(redis_client.delete, f"session:{session_id}")
def set_session_cookie(response, session_id: str):
"""Set session cookie on the response."""
response.set_cookie(
key=SESSION_COOKIE_NAME,
value=session_id,
max_age=SESSION_EXPIRY_SECONDS,
httponly=True,
secure=ENV == "production",
samesite="Lax"
)
async def get_cached_repository_validation(repo_url: str, github_token: str = None) -> Dict[str, Any]:
"""Retrieve or validate repository access, caching results."""
cache_key = f"{repo_url}_{hash(github_token) if github_token else 'public'}"
current_time = time.time()
if cache_key in repository_cache:
cached_result, timestamp = repository_cache[cache_key]
if current_time - timestamp < CACHE_TTL:
logging.debug({"message": "Using cached repository validation", "repo": repo_url})
return cached_result
result = await validate_repository_access(repo_url, github_token)
repository_cache[cache_key] = (result, current_time)
# Clean up stale cache entries
if len(repository_cache) > 1000:
cutoff_time = current_time - CACHE_TTL
stale_keys = [k for k, (_, timestamp) in repository_cache.items() if timestamp < cutoff_time]
for k in stale_keys:
repository_cache.pop(k, None)
logging.debug({"message": "Cleaned stale repository cache entries", "removed": len(stale_keys)})
return result
async def periodic_cleanup():
"""Periodically clean up stale cache entries."""
while True:
try:
await asyncio.sleep(300)
current_time = time.time()
cutoff_time = current_time - CACHE_TTL
old_keys = [k for k, (_, timestamp) in repository_cache.items() if timestamp < cutoff_time]
for key in old_keys:
repository_cache.pop(key, None)
if old_keys:
logging.info({"message": "Cleaned up repository cache", "removed_entries": len(old_keys)})
except Exception as e:
logging.error({"message": "Error in periodic cleanup", "error": str(e)})
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Manage application lifecycle, including startup and shutdown."""
logging.info({"message": "Application starting up"})
if redis_client:
try:
ping_result = await asyncio.to_thread(redis_client.ping)
logging.info({"message": "Redis connection validated on startup", "result": ping_result})
except Exception as e:
logging.warning({"message": "Redis validation failed on startup", "error": str(e)})
cleanup_task = asyncio.create_task(periodic_cleanup())
logging.info({"message": "Started periodic cleanup task"})
try:
yield
finally:
cleanup_task.cancel()
try:
await cleanup_task
except asyncio.CancelledError:
logging.info({"message": "Periodic cleanup task cancelled"})
logging.info({"message": "Application shutdown complete"})
# Initialize FastAPI app
app = FastAPI(
title="GitHub to Resume Generator",
description="Generate professional resumes from GitHub repositories using AI",
version="1.0.0",
lifespan=lifespan,
docs_url="/docs" if ENV != "production" else None,
redoc_url="/redoc" if ENV != "production" else None
)
# Rate limiting setup
def rate_limit_key_func(request: Request) -> str:
"""Generate rate limit key based on client IP."""
return (
request.headers.get("X-Forwarded-For", "").split(",")[0].strip() or
request.headers.get("X-Real-IP") or
request.client.host if request.client else "unknown"
)
limiter = Limiter(
key_func=rate_limit_key_func,
default_limits=["10/hour", "2/minute"],
storage_uri=f"redis://{os.getenv('REDIS_USERNAME', 'default')}:{os.getenv('REDIS_PASSWORD')}@{os.getenv('REDIS_HOST')}:{os.getenv('REDIS_PORT')}" if redis_client else None
)
app.state.limiter = limiter
# Remove the default SlowAPI JSON handler for RateLimitExceeded
# app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
# Add a custom handler to render the ratelimit.jinja template
@app.exception_handler(RateLimitExceeded)
async def custom_rate_limit_handler(request: Request, exc: RateLimitExceeded):
"""Render a user-friendly rate limit error page."""
logging.warning({
"message": "Rate limit exceeded",
"path": request.url.path,
"client_ip": rate_limit_key_func(request),
"detail": str(exc.detail) if hasattr(exc, 'detail') else str(exc)
})
return templates.TemplateResponse(
"ratelimit.jinja",
{"request": request, "error_code": 429,
"error_message": str(exc.detail) if hasattr(exc, 'detail') else str(exc)},
status_code=429
)
# Middleware setup
if os.getenv("API_ANALYTICS_KEY"):
app.add_middleware(Analytics, api_key=os.getenv("API_ANALYTICS_KEY"))
logging.info({"message": "API Analytics middleware enabled"})
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["GET", "POST", "OPTIONS"],
allow_headers=["*"],
max_age=3600
)
app.add_middleware(TrustedHostMiddleware, allowed_hosts=["*"])
app.add_middleware(GZipMiddleware, minimum_size=1000)
app.add_middleware(SessionMiddleware, secret_key=os.getenv("SESSION_SECRET_KEY"), session_cookie=SESSION_COOKIE_NAME,
max_age=SESSION_EXPIRY_SECONDS, same_site="lax", https_only=ENV == "production")
# Mount static files
static_dir = Path("static")
static_dir.mkdir(exist_ok=True)
app.mount("/static", StaticFiles(directory=static_dir), name="static")
templates = Jinja2Templates(directory="templates")
# Middleware for HTTPS scheme and security headers
@app.middleware("http")
async def force_https_and_security_headers(request: Request, call_next):
"""Force HTTPS scheme and add security headers."""
if "x-forwarded-proto" in request.headers:
request.scope["scheme"] = request.headers["x-forwarded-proto"]
if request.url.path == "/health":
return await call_next(request)
try:
response = await call_next(request)
response.headers.update({
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "DENY",
"X-XSS-Protection": "1; mode=block",
"Referrer-Policy": "strict-origin-when-cross-origin"
})
if ENV == "production":
response.headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains"
return response
except Exception as e:
logging.warning({"message": "Error in security headers middleware", "error": str(e)})
return Response("Bad Request", status_code=400)
# Middleware for request validation
@app.middleware("http")
async def validate_requests(request: Request, call_next):
"""Validate incoming HTTP requests for suspicious patterns."""
if request.url.path == "/health":
return await call_next(request)
if not hasattr(request, 'method') or not request.method:
logging.warning({"message": "Invalid request without method", "client_ip": rate_limit_key_func(request)})
return Response("Bad Request", status_code=400)
if request.url.path and any(
suspicious in request.url.path.lower() for suspicious in ['../', '.env', 'config', 'admin']):
logging.warning({"message": "Suspicious path access attempt", "path": request.url.path,
"client_ip": rate_limit_key_func(request)})
return Response("Not Found", status_code=404)
return await call_next(request)
# Middleware for DDoS protection
@app.middleware("http")
async def ddos_protection_middleware(request: Request, call_next):
"""Prevent DDoS attacks by limiting requests per IP."""
client_ip = rate_limit_key_func(request)
current_time = time.time()
request_counts[client_ip] = [t for t in request_counts[client_ip] if current_time - t < DDoS_WINDOW]
request_counts[client_ip].append(current_time)
if len(request_counts[client_ip]) > DDoS_MAX_REQUESTS:
logging.warning({"message": "DDoS protection triggered", "ip": client_ip})
return Response("Too many requests. Please try again later.", status_code=429)
return await call_next(request)
# Middleware for Cloudflare-only access
if CLOUDFLARE_ONLY:
@app.middleware("http")
async def cloudflare_only_middleware(request: Request, call_next):
"""Restrict access to Cloudflare IPs if enabled."""
client_ip = rate_limit_key_func(request)
try:
ip_obj = ipaddress.ip_address(client_ip)
if not any(ip_obj in net for net in CLOUDFLARE_NETWORKS):
logging.warning({"message": "Blocked non-Cloudflare IP", "ip": client_ip})
return Response("Access denied. Please use the public site.", status_code=403)
except Exception:
logging.warning({"message": "Invalid IP address", "ip": client_ip})
return Response("Access denied.", status_code=403)
return await call_next(request)
async def get_filtered_repo_stats(local_path: str) -> Dict[str, Any]:
"""Calculates repository stats, ignoring specified directories and extensions."""
def calculate_stats():
file_count = 0
total_size = 0
repo_root = Path(local_path)
for root, dirs, files in os.walk(repo_root):
dirs[:] = [d for d in dirs if d not in IGNORE_DIRS]
for file_name in files:
file_path = Path(root) / file_name
if file_path.suffix.lower() not in IGNORE_EXTENSIONS:
try:
total_size += file_path.stat().st_size
file_count += 1
except FileNotFoundError:
continue
return {"file_count": file_count, "repo_size_mb": total_size / (1024 * 1024)}
return await asyncio.to_thread(calculate_stats)
async def validate_repository_access(repo_url: str, github_token: str = None) -> Dict[str, Any]:
"""Validate access to a GitHub repository with clear error handling and messaging.
Args:
repo_url: URL of the GitHub repository (e.g., https://github.com/owner/repo)
github_token: Optional GitHub personal access token for authentication
Returns:
Dictionary containing validation result, repository status, and error details if any
"""
# Normalize and validate URL
if not repo_url.startswith("https://github.com/"):
return {
"success": False,
"is_public": False,
"error_message": "Invalid GitHub repository URL. Please use format: https://github.com/owner/repo",
"error_code": "invalid_url",
"owner": None,
"repo_name": None
}
# Extract owner and repo name
path_part = repo_url.replace("https://github.com/", "").strip("/")
segments = path_part.split("/")
if len(segments) < 2:
return {
"success": False,
"is_public": False,
"error_message": "Invalid repository path. Please provide both owner and repository name",
"error_code": "invalid_path",
"owner": None,
"repo_name": None
}
owner, repo_name = segments[0], segments[1]
repo_full_name = f"{owner}/{repo_name}"
try:
# Initialize GitHub client
github_client = Github(github_token) if github_token else Github()
repo_obj = github_client.get_repo(repo_full_name)
logging.info({
"message": "Successfully validated repository access",
"repo": repo_url,
"is_public": not repo_obj.private,
"owner": owner,
"repo_name": repo_name
})
return {
"success": True,
"is_public": not repo_obj.private,
"error_message": None,
"error_code": None,
"owner": owner,
"repo_name": repo_name
}
except GithubException as e:
error_details = {
"repo": repo_url,
"error": str(e),
"status_code": getattr(e, 'status', 'unknown')
}
error_code = "api_error"
error_message = f"Failed to access repository: {str(e)}"
if e.status == 404:
logging.warning({**error_details, "message": "Repository not found or inaccessible"})
error_code = "not_found_or_private"
error_message = "Repository not found or requires authentication. Please verify the URL and access permissions."
if not github_token:
error_message = "Repository is private or does not exist. For private repositories, a GitHub token is required."
elif e.status == 403:
logging.warning({**error_details, "message": "Access to repository denied"})
error_code = "access_denied"
error_message = "Access denied. You don't have permission to view this repository."
elif e.status == 401:
logging.warning({**error_details, "message": "Authentication failed"})
error_code = "auth_required"
error_message = "Authentication failed. Please provide a valid GitHub token."
else:
logging.error({**error_details, "message": "GitHub API error occurred"})
return {
"success": False,
"is_public": False,
"error_message": error_message,
"error_code": error_code,
"owner": owner,
"repo_name": repo_name
}
except Exception as e:
logging.error({
"message": "Unexpected error during repository validation",
"repo": repo_url,
"error": str(e),
"type": type(e).__name__
})
return {
"success": False,
"is_public": False,
"error_message": f"An unexpected error occurred: {str(e)}",
"error_code": "unexpected_error",
"owner": owner,
"repo_name": repo_name
}
async def enforce_private_repo_auth(validation_result: Dict[str, Any], session_data: Optional[GitHubSessionData],
github_token: Optional[str]) -> tuple[bool, Optional[str]]:
"""Ensure private repositories require authentication."""
if not validation_result["success"]:
return False, validation_result["error_message"]
if not validation_result["is_public"] and not (session_data and (session_data.access_token or github_token)):
return False, "Authentication required to access private repository. Please log in with GitHub."
return True, None
def build_index_context(request: Request, session_data: Optional[GitHubSessionData] = None, repo_url: str = "",
job_description: str = "", error: Optional[str] = None, path: str = "/") -> Dict[str, Any]:
"""Build context for rendering the index template."""
is_authenticated = bool(session_data and session_data.access_token)
return {
"request": request,
"repo_url": repo_url,
"job_description_hidden": job_description,
"job_description": job_description,
"loading": False,
"streaming": False,
"result": None,
"error": error,
"pre_filled": bool(repo_url),
"is_authenticated": is_authenticated,
"github_token": session_data.github_token if session_data else None,
"is_public": False,
"current_path": path,
"session_id": session_data.session_id if session_data else None
}
@app.get("/health")
async def health_check():
"""Return application health status."""
active_sessions = 0
if redis_client:
try:
cursor = 0
session_pattern = "session:*"
while True:
cursor, keys = await asyncio.to_thread(redis_client.scan, cursor, match=session_pattern, count=100)
active_sessions += len(keys)
if cursor == 0:
break
except Exception as e:
logging.error({"message": "Failed to count active sessions in Redis", "error": str(e)})
analytics = await get_analytics()
return {
"status": "healthy",
"environment": ENV,
"redis_connected": redis_client is not None,
"active_sessions": active_sessions,
"total_users": analytics["total_users"],
"total_repos_analyzed": analytics["total_repos_analyzed"],
"total_repo_size_mb": analytics["total_repo_size_mb"],
"total_files_analyzed": analytics["total_files_analyzed"],
"avg_repo_size_mb": analytics["avg_repo_size_mb"],
"avg_files_per_repo": analytics["avg_files_per_repo"],
"max_repo_size_mb": analytics["max_repo_size_mb"],
"max_files_in_repo": analytics["max_files_in_repo"],
"timestamp": datetime.now(timezone.utc).isoformat()
}
@app.get("/favicon.ico")
async def favicon():
"""Serve favicon.ico."""
favicon_path = static_dir / "favicon-32x32.png"
if favicon_path.exists():
response = FileResponse(favicon_path)
response.headers["Cache-Control"] = "public, max-age=86400"
return response
logging.warning({"message": "Favicon not found", "path": str(favicon_path)})
raise HTTPException(status_code=404, detail="Favicon not found")
@app.get("/favicon-16x16.png")
async def favicon_16():
"""Serve favicon-16x16.png."""
favicon_path = static_dir / "favicon-16x16.png"
if favicon_path.exists():
response = FileResponse(favicon_path)
response.headers["Cache-Control"] = "public, max-age=86400"
return response
raise HTTPException(status_code=404, detail="Favicon not found")
@app.get("/favicon-32x32.png")
async def favicon_32():
"""Serve favicon-32x32.png."""
favicon_path = static_dir / "favicon-32x32.png"
if favicon_path.exists():
response = FileResponse(favicon_path)
response.headers["Cache-Control"] = "public, max-age=86400"
return response
raise HTTPException(status_code=404, detail="Favicon not found")
@app.get("/apple-touch-icon.png")
async def apple_touch_icon():
"""Serve apple-touch-icon.png."""
favicon_path = static_dir / "apple-touch-icon.png"
if favicon_path.exists():
response = FileResponse(favicon_path)
response.headers["Cache-Control"] = "public, max-age=86400"
return response
raise HTTPException(status_code=404, detail="Apple touch icon not found")
@app.get("/login")
@limiter.limit("5/minute")
async def login(request: Request):
"""Initiate GitHub OAuth login flow."""
session_id = str(uuid.uuid4())
state = str(uuid.uuid4())
request.session["session_id"] = session_id
request.session["oauth_state"] = state
session = GitHubSessionData(
user_id=str(uuid.uuid4()),
session_id=session_id,
created_at=datetime.now(timezone.utc).isoformat()
)
await save_session(session)
# --- Analytics: increment unique users ---
await increment_analytics_counter(ANALYTICS_TOTAL_USERS_KEY, session_id)
logging.info({"message": "Created new session for login", "session_id": session_id})
github_client_id = os.getenv("GITHUB_CLIENT_ID")
if not github_client_id:
logging.error({"message": "GitHub Client ID not configured"})
raise HTTPException(status_code=500, detail="Authentication service temporarily unavailable")
params = {
"client_id": github_client_id,
"redirect_uri": os.getenv("CALLBACK_URL"),
"scope": "repo user",
"state": state,
"response_type": "code"
}
github_auth_url = f"https://github.com/login/oauth/authorize?{urllib.parse.urlencode(params)}"
logging.info({"message": "Redirecting to GitHub OAuth", "session_id": session_id})
response = RedirectResponse(github_auth_url)
set_session_cookie(response, session_id)
return response
@app.get("/callback")
@limiter.limit("5/minute")
async def callback(request: Request, code: str = None, state: str = None, error: str = None):
"""Handle GitHub OAuth callback."""
logging.info({"message": "OAuth callback received", "query_params": dict(request.query_params)})
if error:
logging.error({"message": "OAuth error from GitHub", "error": error})
return RedirectResponse(url="/?error=oauth_failed", status_code=302)
if not code or not state:
logging.error({"message": "Missing required OAuth parameters", "code": bool(code), "state": bool(state)})
return RedirectResponse(url="/?error=missing_params", status_code=302)
session_state = request.session.get("oauth_state")
if not session_state or state != session_state:
logging.error({"message": "Invalid OAuth state", "provided_state": state, "expected_state": session_state})
return RedirectResponse(url="/?error=invalid_state", status_code=302)
session_id = request.session.get("session_id")
session = await get_session(session_id)
if not session_id or not session:
logging.error({"message": "Invalid session during OAuth callback", "session_id": session_id})
return RedirectResponse(url="/?error=invalid_session", status_code=302)
github_client_id = os.getenv("GITHUB_CLIENT_ID")
github_client_secret = os.getenv("GITHUB_CLIENT_SECRET")
if not github_client_id or not github_client_secret:
logging.error({"message": "GitHub OAuth configuration incomplete"})
return RedirectResponse(url="/?error=config_error", status_code=302)
async with httpx.AsyncClient(timeout=30.0) as client:
try:
response = await client.post(
"https://github.com/login/oauth/access_token",
data={
"client_id": github_client_id,
"client_secret": github_client_secret,
"code": code,
"redirect_uri": os.getenv("CALLBACK_URL")
},
headers={"Accept": "application/json"}
)
response.raise_for_status()
data = response.json()
access_token = data.get("access_token")
if not access_token:
logging.error({"message": "No access token in OAuth response", "response": data})
raise HTTPException(status_code=400, detail="Authentication failed")
g = Github(access_token)
user = g.get_user()
session.access_token = access_token
session.user_id = str(user.id)
request.session["github_user"] = user.login
request.session["authenticated"] = True
await save_session(session)
logging.info({"message": "User authenticated successfully", "session_id": session_id, "user": user.login})
request.session.pop("oauth_state", None)
response = RedirectResponse(url="/", status_code=302)
set_session_cookie(response, session_id)
return response
except httpx.HTTPStatusError as e:
logging.error(
{"message": "OAuth token exchange failed", "status_code": e.response.status_code, "error": str(e),
"response_text": e.response.text})
return RedirectResponse(url="/?error=token_exchange_failed", status_code=302)
except GithubException as e:
logging.error({"message": "GitHub user validation failed", "error": str(e)})
return RedirectResponse(url="/?error=user_validation_failed", status_code=302)
except Exception as e:
logging.error({"message": "Unexpected OAuth error", "error": str(e)})
return RedirectResponse(url="/?error=unexpected_error", status_code=302)
@app.get("/logout")
async def logout(request: Request):
"""Log out the user and clear session."""
session_id = request.session.get("session_id")
if session_id:
session = await get_session(session_id)
if session and session.local_path:
try:
await asyncio.to_thread(robust_rmtree, session.local_path)
except Exception as e:
logging.warning({"message": "Failed to clean up session files", "error": str(e)})
await delete_session(session_id)
logging.info({"message": "User logged out successfully", "session_id": session_id})
request.session.clear()
response = RedirectResponse(url="/")
response.delete_cookie(SESSION_COOKIE_NAME)
return response
async def get_generation_data(session_id: str, generation_id: str) -> Optional[dict]:
"""Retrieve generation data from Redis."""
if not redis_client:
return None
data = await asyncio.to_thread(redis_client.get, f"generation:{session_id}:{generation_id}")
if not data:
return None
try:
return json.loads(data)
except Exception as e:
logging.warning(
{"message": "Invalid generation data in Redis", "session_id": session_id, "generation_id": generation_id,
"error": str(e)})
return None
async def save_generation_data(session_id: str, generation_id: str, data: dict, expiry: int = 3600):
"""Save generation data to Redis with expiry."""
if not redis_client:
raise RuntimeError("Redis is required for generation management.")
await asyncio.to_thread(
redis_client.setex,
f"generation:{session_id}:{generation_id}",
expiry,
json.dumps(data)
)
async def process_resume_generation(websocket: WebSocket, session_data: GitHubSessionData, generation_id: str):
"""Process resume generation with repository cloning, analysis, and AI generation."""
repo_url = session_data.repo_url
job_description = session_data.job_description
github_token = session_data.github_token
oauth_token = session_data.access_token
path = session_data.redirect_url or "/"
token_to_use = oauth_token or github_token or os.getenv("GITHUB_TOKEN")
validation_result = await get_cached_repository_validation(repo_url, token_to_use)
if not validation_result["success"]:
error_message = validation_result["error_message"]
if validation_result["error_code"] in ["auth_required", "access_denied"] and not token_to_use:
error_message += ". Please provide a GitHub token or log in with GitHub."
logging.error({"message": "Repository access validation failed", "repo": repo_url, "error": error_message})
await websocket.send_text(
json.dumps({"type": "error", "content": error_message, "generation_id": generation_id}))
await save_generation_data(session_data.session_id, generation_id, {"status": "error", "error": error_message})
return False
is_public = validation_result["is_public"]
owner, repo_name = validation_result["owner"], validation_result["repo_name"]
# --- Fast repo size check (avoid deep API calls) ---
try:
g = Github(token_to_use) if token_to_use else Github()
repo_obj = g.get_repo(f"{owner}/{repo_name}")
repo_size_mb = repo_obj.size / 1024 if hasattr(repo_obj, 'size') else 0
cache_key = f"large_repo:{owner}/{repo_name}"
# Check redis cache for large repo flag
if redis_client:
cached_large = await asyncio.to_thread(redis_client.get, cache_key)
if cached_large == '1':
await websocket.send_text(json.dumps({
"type": "error",
"content": f"This repository is too large to process. Large repositories are not supported. If you would like to help support the developer to cover expenses for large repo support, please reach out!",
"generation_id": generation_id
}))
await save_generation_data(session_data.session_id, generation_id,
{"status": "error", "error": "Large repository not supported."})
return False
if repo_size_mb > 150: # 150 MB limit for fast pre-check
if redis_client:
await asyncio.to_thread(redis_client.setex, cache_key, 86400, '1') # Cache for 1 day
await websocket.send_text(json.dumps({
"type": "error",
"content": f"This repository is too large to process (over 150 MB, {repo_size_mb:.2f} MB). Large repositories are not supported. If you would like to help support the developer to cover expenses for large repo support, please reach out!",
"generation_id": generation_id
}))
await save_generation_data(session_data.session_id, generation_id,
{"status": "error", "error": "Large repository not supported."})
return False
except Exception as e:
logging.warning({"message": "Could not estimate repository size for fast check", "error": str(e)})
await websocket.send_text(json.dumps(
{"type": "status", "content": f"🔄 Checking for existing repository clone: {repo_url}",
"generation_id": generation_id}))
await save_generation_data(session_data.session_id, generation_id, {"status": "cloning"})
local_path = session_data.local_path
clone_result = None
if local_path and Path(local_path).exists():
await websocket.send_text(json.dumps(
{"type": "status", "content": "🔄 Using existing repository clone", "generation_id": generation_id}))
stats = await get_filtered_repo_stats(local_path)
clone_result = {
"success": True,
"local_path": local_path,
"repo_name": f"{owner}/{repo_name}",
"repo_size_mb": stats["repo_size_mb"],
"file_count": stats["file_count"]
}
else:
await websocket.send_text(json.dumps(
{"type": "status", "content": f"🔄 Cloning repository: {repo_url}", "generation_id": generation_id}))
target_dir = Path(f"/tmp/{session_data.session_id}")
clone_result = await clone_repo_tool(repo_url, str(target_dir), token_to_use)
local_path = clone_result.get("local_path")
session_data.local_path = local_path
await save_session(session_data)
# --- Strict file count check after clone ---
if clone_result and clone_result.get("success") and local_path:
if "file_count" not in clone_result:
stats = await get_filtered_repo_stats(local_path)
clone_result["file_count"] = stats["file_count"]
clone_result["repo_size_mb"] = stats["repo_size_mb"]
file_count = clone_result["file_count"]
cache_key = f"large_repo:{owner}/{repo_name}"
if redis_client: