From f8105b0775b0e00cff28ee538412a881dba75168 Mon Sep 17 00:00:00 2001 From: Pamella Bezerra Date: Mon, 17 Jun 2024 17:40:16 -0300 Subject: [PATCH] Add django_auth to ninja API --- django_ai_assistant/api/views.py | 9 ++- example/assets/js/components/Chat/Chat.tsx | 7 +- frontend/openapi_schema.json | 77 +++++++++++++++++++--- 3 files changed, 76 insertions(+), 17 deletions(-) diff --git a/django_ai_assistant/api/views.py b/django_ai_assistant/api/views.py index 10eb0d6..5db9a52 100644 --- a/django_ai_assistant/api/views.py +++ b/django_ai_assistant/api/views.py @@ -5,6 +5,7 @@ from langchain_core.messages import message_to_dict from ninja import NinjaAPI from ninja.operation import Operation +from ninja.security import django_auth from django_ai_assistant import package_name, version from django_ai_assistant.api.schemas import ( @@ -26,7 +27,13 @@ def get_openapi_operation_id(self, operation: Operation) -> str: return (package_name + "_" + name).replace(".", "_") -api = API(title=package_name, version=version, urls_namespace="django_ai_assistant") +api = API( + title=package_name, + version=version, + urls_namespace="django_ai_assistant", + # Add auth to all endpoints + auth=django_auth, +) @api.exception_handler(AIUserNotAllowedError) diff --git a/example/assets/js/components/Chat/Chat.tsx b/example/assets/js/components/Chat/Chat.tsx index 60c30c7..2606c90 100644 --- a/example/assets/js/components/Chat/Chat.tsx +++ b/example/assets/js/components/Chat/Chat.tsx @@ -93,12 +93,7 @@ function ChatMessageList({ deleteMessage, }: { messages: ThreadMessagesSchemaOut[]; - deleteMessage: ({ - threadId, - messageId, - }: { - messageId: string; - }) => Promise; + deleteMessage: ({ messageId }: { messageId: string }) => Promise; }) { if (messages.length === 0) { return No messages.; diff --git a/frontend/openapi_schema.json b/frontend/openapi_schema.json index 75f5898..1adc83d 100644 --- a/frontend/openapi_schema.json +++ b/frontend/openapi_schema.json @@ -26,7 +26,12 @@ } } } - } + }, + "security": [ + { + "SessionAuth": [] + } + ] } }, "/assistants/{assistant_id}/": { @@ -55,7 +60,12 @@ } } } - } + }, + "security": [ + { + "SessionAuth": [] + } + ] } }, "/threads/": { @@ -78,7 +88,12 @@ } } } - } + }, + "security": [ + { + "SessionAuth": [] + } + ] }, "post": { "operationId": "django_ai_assistant_create_thread", @@ -105,7 +120,12 @@ } }, "required": true - } + }, + "security": [ + { + "SessionAuth": [] + } + ] } }, "/threads/{thread_id}/": { @@ -134,7 +154,12 @@ } } } - } + }, + "security": [ + { + "SessionAuth": [] + } + ] }, "patch": { "operationId": "django_ai_assistant_update_thread", @@ -171,7 +196,12 @@ } }, "required": true - } + }, + "security": [ + { + "SessionAuth": [] + } + ] }, "delete": { "operationId": "django_ai_assistant_delete_thread", @@ -191,7 +221,12 @@ "204": { "description": "No Content" } - } + }, + "security": [ + { + "SessionAuth": [] + } + ] } }, "/threads/{thread_id}/messages/": { @@ -224,7 +259,12 @@ } } } - } + }, + "security": [ + { + "SessionAuth": [] + } + ] }, "post": { "operationId": "django_ai_assistant_create_thread_message", @@ -254,7 +294,12 @@ } }, "required": true - } + }, + "security": [ + { + "SessionAuth": [] + } + ] } }, "/threads/{thread_id}/messages/{message_id}/": { @@ -285,7 +330,12 @@ "204": { "description": "No Content" } - } + }, + "security": [ + { + "SessionAuth": [] + } + ] } } }, @@ -414,6 +464,13 @@ "title": "ThreadMessagesSchemaIn", "type": "object" } + }, + "securitySchemes": { + "SessionAuth": { + "type": "apiKey", + "in": "cookie", + "name": "sessionid" + } } }, "servers": []