SisiTheFox commited on
Commit
97018c0
ยท
1 Parent(s): 7893cd8

๐Ÿ”’ Add API Key authentication to prevent abuse

Browse files

- Add API_KEY environment variable check
- Protect /generate-pdf endpoint with API key
- Protect /generate-pdf-base64 endpoint with API key
- Keep /health endpoint public for monitoring
- Return 403 error for unauthorized requests

This protects the PDF generation API from being abused.

๐ŸฆŠ Created by SisiTheFox

Files changed (1) hide show
  1. app.py +20 -3
app.py CHANGED
@@ -2,9 +2,10 @@
2
  ๐ŸฆŠ Sisi PDF API - Universal HTML to PDF Converter
3
  Perfect support for Chinese (Simplified & Traditional), Emoji, SVG, and modern CSS
4
  Powered by Playwright + Chromium
 
5
  """
6
 
7
- from fastapi import FastAPI, HTTPException
8
  from fastapi.middleware.cors import CORSMiddleware
9
  from fastapi.responses import Response
10
  from pydantic import BaseModel
@@ -13,10 +14,24 @@ from typing import Optional
13
  import logging
14
  import base64
15
  import asyncio
 
16
 
17
  logging.basicConfig(level=logging.INFO)
18
  logger = logging.getLogger(__name__)
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  app = FastAPI(
21
  title="Sisi PDF API",
22
  description="Universal HTML to PDF converter with perfect Chinese, Emoji, and SVG support",
@@ -75,9 +90,10 @@ def health_check():
75
 
76
 
77
  @app.post("/generate-pdf")
78
- async def generate_pdf(request: PDFRequest):
79
  """
80
  Generate PDF from HTML content
 
81
 
82
  Perfect support for:
83
  - Chinese characters (็ฎ€ไฝ“ไธญๆ–‡ใ€็น้ซ”ไธญๆ–‡)
@@ -137,9 +153,10 @@ async def generate_pdf(request: PDFRequest):
137
 
138
 
139
  @app.post("/generate-pdf-base64")
140
- async def generate_pdf_base64(request: PDFRequest):
141
  """
142
  Generate PDF and return as base64 string
 
143
  Useful for embedding in JSON responses
144
  """
145
  try:
 
2
  ๐ŸฆŠ Sisi PDF API - Universal HTML to PDF Converter
3
  Perfect support for Chinese (Simplified & Traditional), Emoji, SVG, and modern CSS
4
  Powered by Playwright + Chromium
5
+ ๐Ÿ”’ Protected with API Key Authentication
6
  """
7
 
8
+ from fastapi import FastAPI, HTTPException, Header, Depends
9
  from fastapi.middleware.cors import CORSMiddleware
10
  from fastapi.responses import Response
11
  from pydantic import BaseModel
 
14
  import logging
15
  import base64
16
  import asyncio
17
+ import os
18
 
19
  logging.basicConfig(level=logging.INFO)
20
  logger = logging.getLogger(__name__)
21
 
22
+ # ๐Ÿ” API Key Configuration
23
+ API_KEY = os.environ.get("API_KEY", "sisi_default_key_change_me")
24
+
25
+
26
+ def verify_api_key(x_api_key: Optional[str] = Header(None)):
27
+ """Verify API key from request header"""
28
+ if x_api_key != API_KEY:
29
+ raise HTTPException(
30
+ status_code=403,
31
+ detail="๐Ÿ”’ Invalid or missing API key. Please contact SisiTheFox for access."
32
+ )
33
+ return x_api_key
34
+
35
  app = FastAPI(
36
  title="Sisi PDF API",
37
  description="Universal HTML to PDF converter with perfect Chinese, Emoji, and SVG support",
 
90
 
91
 
92
  @app.post("/generate-pdf")
93
+ async def generate_pdf(request: PDFRequest, api_key: str = Depends(verify_api_key)):
94
  """
95
  Generate PDF from HTML content
96
+ ๐Ÿ”’ Requires API Key authentication
97
 
98
  Perfect support for:
99
  - Chinese characters (็ฎ€ไฝ“ไธญๆ–‡ใ€็น้ซ”ไธญๆ–‡)
 
153
 
154
 
155
  @app.post("/generate-pdf-base64")
156
+ async def generate_pdf_base64(request: PDFRequest, api_key: str = Depends(verify_api_key)):
157
  """
158
  Generate PDF and return as base64 string
159
+ ๐Ÿ”’ Requires API Key authentication
160
  Useful for embedding in JSON responses
161
  """
162
  try: