|
|
|
|
@ -61,8 +61,9 @@ class JWTBearer(HTTPBearer):
|
|
|
|
|
|
|
|
|
|
def verify_jwt(self, jwtoken: str, method: str, endpoint: str):
|
|
|
|
|
try:
|
|
|
|
|
url_to_verify = f"{config.AUTH_SERVICE_API}/verify-token"
|
|
|
|
|
response = requests.get(
|
|
|
|
|
f"{config.AUTH_SERVICE_API}/verify-token",
|
|
|
|
|
url_to_verify,
|
|
|
|
|
headers={"Authorization": f"Bearer {jwtoken}"},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@ -127,8 +128,9 @@ async def internal_key(request: Request):
|
|
|
|
|
raise Exception(str(e))
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
verify_url = f"{config.AUTH_SERVICE_API}/verify-token"
|
|
|
|
|
response = requests.get(
|
|
|
|
|
f"{config.AUTH_SERVICE_API}/verify-token",
|
|
|
|
|
verify_url,
|
|
|
|
|
headers={"Authorization": f"{token}"},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@ -147,7 +149,7 @@ async def internal_key(request: Request):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import httpx
|
|
|
|
|
import asyncio
|
|
|
|
|
import logging
|
|
|
|
|
from typing import Dict, Any
|
|
|
|
|
import src.config as config
|
|
|
|
|
@ -162,6 +164,7 @@ AUTH_NOTIFY_ENDPOINT = f"{config.AUTH_SERVICE_API}/admin/notify-limit"
|
|
|
|
|
async def notify_admin_on_rate_limit(
|
|
|
|
|
endpoint_name: str,
|
|
|
|
|
ip_address: str,
|
|
|
|
|
request: Request,
|
|
|
|
|
method: str = "POST",
|
|
|
|
|
cooldown: int = 900,
|
|
|
|
|
timeout: int = 5
|
|
|
|
|
@ -172,21 +175,23 @@ async def notify_admin_on_rate_limit(
|
|
|
|
|
Async version - gunakan di async context.
|
|
|
|
|
"""
|
|
|
|
|
payload = {
|
|
|
|
|
"endpoint_name": endpoint_name,
|
|
|
|
|
"endpoint_name": f"oh/{endpoint_name}".replace("//", ""),
|
|
|
|
|
"ip_address": ip_address,
|
|
|
|
|
"method": method,
|
|
|
|
|
"cooldown": cooldown,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token = request.headers.get("Authorization")
|
|
|
|
|
headers = {"Authorization": token} if token else {}
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
async with httpx.AsyncClient(timeout=timeout) as client:
|
|
|
|
|
response = await client.post(AUTH_NOTIFY_ENDPOINT, json=payload)
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
result = response.json()
|
|
|
|
|
log.info(f"Notifikasi admin sent | Endpoint: {endpoint_name}")
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
response = await asyncio.to_thread(
|
|
|
|
|
requests.post, AUTH_NOTIFY_ENDPOINT,
|
|
|
|
|
json=payload, headers=headers, timeout=timeout
|
|
|
|
|
)
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
result = response.json()
|
|
|
|
|
log.info(f"Notifikasi admin sent | Endpoint: {endpoint_name}")
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
log.error(f"Error notifying admin: {str(e)}")
|
|
|
|
|
@ -198,6 +203,7 @@ async def notify_admin_on_rate_limit(
|
|
|
|
|
def notify_admin_on_rate_limit_sync(
|
|
|
|
|
endpoint_name: str,
|
|
|
|
|
ip_address: str,
|
|
|
|
|
request: Request,
|
|
|
|
|
method: str = "POST",
|
|
|
|
|
cooldown: int = 900,
|
|
|
|
|
timeout: int = 5
|
|
|
|
|
@ -209,21 +215,21 @@ def notify_admin_on_rate_limit_sync(
|
|
|
|
|
RECOMMENDED untuk use case ini.
|
|
|
|
|
"""
|
|
|
|
|
payload = {
|
|
|
|
|
"endpoint_name": endpoint_name,
|
|
|
|
|
"endpoint_name": f"oh/{endpoint_name}".replace("//", "/"),
|
|
|
|
|
"ip_address": ip_address,
|
|
|
|
|
"method": method,
|
|
|
|
|
"cooldown": cooldown,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token = request.headers.get("Authorization")
|
|
|
|
|
headers = {"Authorization": token} if token else {}
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
response = httpx.post(AUTH_NOTIFY_ENDPOINT, json=payload, timeout=timeout)
|
|
|
|
|
response = requests.post(AUTH_NOTIFY_ENDPOINT, json=payload, headers=headers, timeout=timeout)
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
result = response.json()
|
|
|
|
|
log.info(f"Notifikasi admin sent | Endpoint: {endpoint_name}")
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
log.error(f"Error notifying admin: {str(e)}")
|
|
|
|
|
return {"status": False, "message": str(e), "data": payload}
|
|
|
|
|
|