Chris4K commited on
Commit
33bb2af
·
verified ·
1 Parent(s): e904f34

Create auth/auth_handler.py

Browse files
Files changed (1) hide show
  1. auth/auth_handler.py +35 -0
auth/auth_handler.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # auth/auth_handler.py
2
+ from fastapi import Depends, HTTPException, Security
3
+ from fastapi.security import APIKeyHeader, APIKeyQuery
4
+ from datetime import datetime, timedelta
5
+ import jwt
6
+ from typing import Optional
7
+ import os
8
+ from pydantic import BaseModel
9
+
10
+ class AuthConfig:
11
+ SECRET_KEY = os.getenv("SECRET_KEY", "your-secret-key-here") # Change in production
12
+ API_KEY = os.getenv("API_KEY", "your-api-key-here") # Change in production
13
+ ALGORITHM = "HS256"
14
+ ACCESS_TOKEN_EXPIRE_MINUTES = 30
15
+
16
+ class Token(BaseModel):
17
+ access_token: str
18
+ token_type: str
19
+
20
+ api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
21
+ api_key_query = APIKeyQuery(name="api_key", auto_error=False)
22
+
23
+ async def get_api_key(
24
+ api_key_header: str = Security(api_key_header),
25
+ api_key_query: str = Security(api_key_query),
26
+ ) -> str:
27
+ if api_key_header == AuthConfig.API_KEY:
28
+ return api_key_header
29
+ if api_key_query == AuthConfig.API_KEY:
30
+ return api_key_query
31
+ raise HTTPException(
32
+ status_code=401,
33
+ detail="Invalid API Key"
34
+ )
35
+