User Log and Persistent Faction Information
This commit is contained in:
@@ -20,7 +20,7 @@ class LoginRequest(BaseModel):
|
||||
|
||||
|
||||
def get_client_ip(request: Request) -> str:
|
||||
"""Get client IP address from request"""
|
||||
#Get client IP address from request
|
||||
# Check X-Forwarded-For header first (for proxy/load balancer)
|
||||
forwarded = request.headers.get("X-Forwarded-For")
|
||||
if forwarded:
|
||||
@@ -29,7 +29,7 @@ def get_client_ip(request: Request) -> str:
|
||||
|
||||
|
||||
def is_locked_out(ip: str) -> bool:
|
||||
"""Check if IP is currently locked out"""
|
||||
#Check if IP is currently locked out
|
||||
if ip not in failed_attempts:
|
||||
return False
|
||||
|
||||
@@ -49,7 +49,7 @@ def is_locked_out(ip: str) -> bool:
|
||||
|
||||
|
||||
def record_failed_attempt(ip: str):
|
||||
"""Record a failed login attempt"""
|
||||
#Record a failed login attempt
|
||||
now = datetime.now()
|
||||
|
||||
if ip not in failed_attempts:
|
||||
@@ -64,13 +64,13 @@ def record_failed_attempt(ip: str):
|
||||
|
||||
|
||||
def clear_failed_attempts(ip: str):
|
||||
"""Clear failed attempts for an IP after successful login"""
|
||||
#Clear failed attempts for an IP after successful login
|
||||
if ip in failed_attempts:
|
||||
del failed_attempts[ip]
|
||||
|
||||
|
||||
def create_jwt_token(username: str) -> str:
|
||||
"""Create a JWT token for the user"""
|
||||
#Create a JWT token for the user
|
||||
expiration = datetime.utcnow() + timedelta(days=7) # Token valid for 7 days
|
||||
payload = {
|
||||
"username": username,
|
||||
@@ -81,7 +81,7 @@ def create_jwt_token(username: str) -> str:
|
||||
|
||||
|
||||
def verify_jwt_token(token: str) -> dict:
|
||||
"""Verify and decode a JWT token"""
|
||||
#Verify and decode a JWT token
|
||||
try:
|
||||
payload = jwt.decode(token, config_module.JWT_SECRET, algorithms=["HS256"])
|
||||
return payload
|
||||
@@ -93,7 +93,7 @@ def verify_jwt_token(token: str) -> dict:
|
||||
|
||||
@router.post("/login")
|
||||
async def login(request: Request, response: Response, req: LoginRequest):
|
||||
"""Login endpoint with rate limiting"""
|
||||
#Login endpoint with rate limiting
|
||||
client_ip = get_client_ip(request)
|
||||
|
||||
# Check if IP is locked out
|
||||
@@ -144,14 +144,14 @@ async def login(request: Request, response: Response, req: LoginRequest):
|
||||
|
||||
@router.post("/logout")
|
||||
async def logout(response: Response):
|
||||
"""Logout endpoint"""
|
||||
#Logout endpoint
|
||||
response.delete_cookie("auth_token")
|
||||
return {"status": "success"}
|
||||
|
||||
|
||||
@router.get("/status")
|
||||
async def auth_status(request: Request):
|
||||
"""Check authentication status"""
|
||||
#Check authentication status
|
||||
token = request.cookies.get("auth_token")
|
||||
|
||||
if not token:
|
||||
|
||||
Reference in New Issue
Block a user