events auth repair
This commit is contained in:
@@ -15,7 +15,7 @@ from create_routes import get_all_routers
|
||||
from prometheus_fastapi_instrumentator import Instrumentator
|
||||
from app_handler import setup_middleware, get_uvicorn_config
|
||||
from create_file import setup_security_schema, configure_route_security
|
||||
from fastapi.openapi.utils import get_openapi
|
||||
from open_api_creator import OpenAPISchemaCreator, create_openapi_schema
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
@@ -63,12 +63,8 @@ def create_app() -> FastAPI:
|
||||
if app.openapi_schema:
|
||||
return app.openapi_schema
|
||||
|
||||
openapi_schema = get_openapi(
|
||||
title="WAG Management API",
|
||||
version="4.0.0",
|
||||
description="WAG Management API Service",
|
||||
routes=app.routes,
|
||||
)
|
||||
# Create OpenAPI schema using our custom creator
|
||||
openapi_schema = create_openapi_schema(app)
|
||||
|
||||
# Add security scheme
|
||||
openapi_schema.update(setup_security_schema())
|
||||
|
||||
@@ -79,7 +79,10 @@ class MiddlewareModule:
|
||||
token_context = TokenService.get_object_via_access_key(access_token=redis_token)
|
||||
if not token_context:
|
||||
raise HTTPExceptionApi(
|
||||
error_code="USER_NOT_FOUND", lang="tr", loc=get_line_number_for_error()
|
||||
error_code="USER_NOT_FOUND",
|
||||
lang="tr",
|
||||
loc=get_line_number_for_error(),
|
||||
sys_msg="TokenService: Token Context couldnt retrieved from redis",
|
||||
)
|
||||
|
||||
return AuthContext(token_context=token_context)
|
||||
|
||||
@@ -11,8 +11,10 @@ This module provides functionality to create and customize OpenAPI documentation
|
||||
|
||||
from typing import Any, Dict, List, Optional, Set
|
||||
from fastapi import FastAPI, APIRouter
|
||||
from fastapi.routing import APIRoute
|
||||
from fastapi.openapi.utils import get_openapi
|
||||
from AllConfigs.main import MainConfig as Config
|
||||
from create_routes import get_all_routers
|
||||
|
||||
|
||||
class OpenAPISchemaCreator:
|
||||
@@ -28,7 +30,7 @@ class OpenAPISchemaCreator:
|
||||
app: FastAPI application instance
|
||||
"""
|
||||
self.app = app
|
||||
self.protected_paths: Set[str] = set()
|
||||
_, self.protected_routes = get_all_routers()
|
||||
self.tags_metadata = self._create_tags_metadata()
|
||||
|
||||
@staticmethod
|
||||
@@ -60,16 +62,16 @@ class OpenAPISchemaCreator:
|
||||
"""
|
||||
return {
|
||||
"Bearer Auth": {
|
||||
"type": "apiKey",
|
||||
"in": "header",
|
||||
"name": "evyos-session-key",
|
||||
"description": "Enter: **'Bearer <JWT>'**, where JWT is the access token",
|
||||
"type": "http",
|
||||
"scheme": "bearer",
|
||||
"bearerFormat": "JWT",
|
||||
"description": "Enter the token with the `Bearer: ` prefix",
|
||||
},
|
||||
"API Key": {
|
||||
"type": "apiKey",
|
||||
"in": "header",
|
||||
"name": "X-API-Key",
|
||||
"description": "API key for service authentication",
|
||||
"description": "Optional API key for service authentication",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -82,20 +84,10 @@ class OpenAPISchemaCreator:
|
||||
"""
|
||||
return {
|
||||
"401": {
|
||||
"description": "Unauthorized - Authentication failed or not provided",
|
||||
"description": "Unauthorized - Invalid or missing credentials",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"detail": {"type": "string"},
|
||||
"error_code": {"type": "string"},
|
||||
},
|
||||
},
|
||||
"example": {
|
||||
"detail": "Invalid authentication credentials",
|
||||
"error_code": "INVALID_CREDENTIALS",
|
||||
},
|
||||
"schema": {"$ref": "#/components/schemas/HTTPValidationError"}
|
||||
}
|
||||
},
|
||||
},
|
||||
@@ -135,6 +127,62 @@ class OpenAPISchemaCreator:
|
||||
},
|
||||
}
|
||||
|
||||
def _process_request_body(self, path: str, method: str, schema: Dict[str, Any]) -> None:
|
||||
"""
|
||||
Process request body to include examples from model config.
|
||||
|
||||
Args:
|
||||
path: Route path
|
||||
method: HTTP method
|
||||
schema: OpenAPI schema to modify
|
||||
"""
|
||||
try:
|
||||
route_schema = schema["paths"][path][method]
|
||||
if "requestBody" in route_schema:
|
||||
request_body = route_schema["requestBody"]
|
||||
if "content" in request_body:
|
||||
content = request_body["content"]
|
||||
if "application/json" in content:
|
||||
json_content = content["application/json"]
|
||||
if "schema" in json_content and "$ref" in json_content["schema"]:
|
||||
ref = json_content["schema"]["$ref"]
|
||||
model_name = ref.split("/")[-1]
|
||||
if model_name in schema["components"]["schemas"]:
|
||||
model_schema = schema["components"]["schemas"][model_name]
|
||||
if "example" in model_schema:
|
||||
json_content["example"] = model_schema["example"]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
def _process_response_examples(self, path: str, method: str, schema: Dict[str, Any]) -> None:
|
||||
"""
|
||||
Process response body to include examples from model config.
|
||||
|
||||
Args:
|
||||
path: Route path
|
||||
method: HTTP method
|
||||
schema: OpenAPI schema to modify
|
||||
"""
|
||||
try:
|
||||
route_schema = schema["paths"][path][method]
|
||||
if "responses" in route_schema:
|
||||
responses = route_schema["responses"]
|
||||
if "200" in responses:
|
||||
response = responses["200"]
|
||||
if "content" in response:
|
||||
content = response["content"]
|
||||
if "application/json" in content:
|
||||
json_content = content["application/json"]
|
||||
if "schema" in json_content and "$ref" in json_content["schema"]:
|
||||
ref = json_content["schema"]["$ref"]
|
||||
model_name = ref.split("/")[-1]
|
||||
if model_name in schema["components"]["schemas"]:
|
||||
model_schema = schema["components"]["schemas"][model_name]
|
||||
if "example" in model_schema:
|
||||
json_content["example"] = model_schema["example"]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
def configure_route_security(
|
||||
self, path: str, method: str, schema: Dict[str, Any]
|
||||
) -> None:
|
||||
@@ -146,7 +194,8 @@ class OpenAPISchemaCreator:
|
||||
method: HTTP method
|
||||
schema: OpenAPI schema to modify
|
||||
"""
|
||||
if path not in Config.INSECURE_PATHS:
|
||||
# Check if route is protected based on dynamic routing info
|
||||
if path in self.protected_routes and method in self.protected_routes[path]:
|
||||
schema["paths"][path][method]["security"] = [
|
||||
{"Bearer Auth": []},
|
||||
{"API Key": []},
|
||||
@@ -154,6 +203,11 @@ class OpenAPISchemaCreator:
|
||||
schema["paths"][path][method]["responses"].update(
|
||||
self._create_common_responses()
|
||||
)
|
||||
|
||||
# Process request body examples
|
||||
self._process_request_body(path, method, schema)
|
||||
# Process response examples
|
||||
self._process_response_examples(path, method, schema)
|
||||
|
||||
def create_schema(self) -> Dict[str, Any]:
|
||||
"""
|
||||
@@ -174,9 +228,7 @@ class OpenAPISchemaCreator:
|
||||
if "components" not in openapi_schema:
|
||||
openapi_schema["components"] = {}
|
||||
|
||||
openapi_schema["components"][
|
||||
"securitySchemes"
|
||||
] = self._create_security_schemes()
|
||||
openapi_schema["components"]["securitySchemes"] = self._create_security_schemes()
|
||||
|
||||
# Configure route security and responses
|
||||
for route in self.app.routes:
|
||||
|
||||
Reference in New Issue
Block a user