SisiTheFox commited on
Commit
b408cad
Β·
1 Parent(s): 7143d5d

πŸ”’ Add API Key authentication to prevent abuse

Browse files

- Add API_KEY environment variable check
- Protect /api/calculate-birth-chart endpoint
- Protect /api/generate-chart-svg endpoint
- Keep /health endpoint public for monitoring
- Return 403 error for unauthorized requests

This protects the Astrology API from being abused.

🌟 Created by SisiTheFox

Files changed (1) hide show
  1. app.py +26 -4
app.py CHANGED
@@ -1,9 +1,10 @@
1
  """
2
  🌟 Astrology API - Ultra Simple Version
3
  Just returns raw Kerykeion calculations
 
4
  """
5
 
6
- from fastapi import FastAPI, HTTPException
7
  from fastapi.middleware.cors import CORSMiddleware
8
  from fastapi.responses import Response
9
  from pydantic import BaseModel
@@ -12,10 +13,24 @@ from typing import Optional, Any, Dict
12
  import logging
13
  import io
14
  import re
 
15
 
16
  logging.basicConfig(level=logging.INFO)
17
  logger = logging.getLogger(__name__)
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  app = FastAPI(title="Sisi Astrology API", version="1.0.0")
20
 
21
  def to_ascii_safe(name: str) -> str:
@@ -97,7 +112,11 @@ def house_to_dict(house_model: Any) -> Dict:
97
  }
98
 
99
  @app.post("/api/calculate-birth-chart")
100
- async def calculate_chart(req: BirthRequest):
 
 
 
 
101
  try:
102
  logger.info(f"πŸ“Š Calculating for: {req.name}")
103
 
@@ -161,8 +180,11 @@ async def calculate_chart(req: BirthRequest):
161
 
162
 
163
  @app.post("/api/generate-chart-svg")
164
- async def generate_chart_svg(req: BirthRequest):
165
- """Generate professional birth chart SVG with aspects (Kerykeion 4.x)"""
 
 
 
166
  try:
167
  logger.info(f"🎨 Generating SVG chart for: {req.name}")
168
 
 
1
  """
2
  🌟 Astrology API - Ultra Simple Version
3
  Just returns raw Kerykeion calculations
4
+ πŸ”’ Protected with API Key Authentication
5
  """
6
 
7
+ from fastapi import FastAPI, HTTPException, Header, Depends
8
  from fastapi.middleware.cors import CORSMiddleware
9
  from fastapi.responses import Response
10
  from pydantic import BaseModel
 
13
  import logging
14
  import io
15
  import re
16
+ import os
17
 
18
  logging.basicConfig(level=logging.INFO)
19
  logger = logging.getLogger(__name__)
20
 
21
+ # πŸ” API Key Configuration
22
+ API_KEY = os.environ.get("API_KEY", "sisi_default_key_change_me")
23
+
24
+
25
+ def verify_api_key(x_api_key: Optional[str] = Header(None)):
26
+ """Verify API key from request header"""
27
+ if x_api_key != API_KEY:
28
+ raise HTTPException(
29
+ status_code=403,
30
+ detail="πŸ”’ Invalid or missing API key. Please contact SisiTheFox for access."
31
+ )
32
+ return x_api_key
33
+
34
  app = FastAPI(title="Sisi Astrology API", version="1.0.0")
35
 
36
  def to_ascii_safe(name: str) -> str:
 
112
  }
113
 
114
  @app.post("/api/calculate-birth-chart")
115
+ async def calculate_chart(req: BirthRequest, api_key: str = Depends(verify_api_key)):
116
+ """
117
+ Calculate birth chart
118
+ πŸ”’ Requires API Key authentication
119
+ """
120
  try:
121
  logger.info(f"πŸ“Š Calculating for: {req.name}")
122
 
 
180
 
181
 
182
  @app.post("/api/generate-chart-svg")
183
+ async def generate_chart_svg(req: BirthRequest, api_key: str = Depends(verify_api_key)):
184
+ """
185
+ Generate professional birth chart SVG with aspects (Kerykeion 4.x)
186
+ πŸ”’ Requires API Key authentication
187
+ """
188
  try:
189
  logger.info(f"🎨 Generating SVG chart for: {req.name}")
190