Skip to content

Commit

Permalink
feat: added incoming requests verification
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Maximenyuk committed Feb 26, 2024
1 parent ba15c96 commit c66dce3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
35 changes: 31 additions & 4 deletions app/api/endpoints/botx.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
BotXMethodCallbackNotFoundError,
UnknownBotAccountError,
UnknownSystemEventError,
UnverifiedRequestError,
build_bot_disabled_response,
build_command_accepted_response,
build_unverified_request_response,
)

from app.api.dependencies.bot import bot_dependency
Expand All @@ -23,8 +25,11 @@
async def command_handler(request: Request, bot: Bot = bot_dependency) -> JSONResponse:
"""Receive commands from users. Max timeout - 5 seconds."""
logger.debug(f"Command headers: {request.headers}")
try:
bot.async_execute_raw_bot_command(await request.json())
try: # noqa: WPS225
bot.async_execute_raw_bot_command(
await request.json(),
request_headers=request.headers,
)
except UnknownSystemEventError as unknown_event_exc:
logger.warning(f"Received unknown system event `{unknown_event_exc.type_name}`")

Expand All @@ -48,6 +53,14 @@ async def command_handler(request: Request, bot: Bot = bot_dependency) -> JSONRe
build_bot_disabled_response(error_label),
status_code=HTTPStatus.SERVICE_UNAVAILABLE,
)
except UnverifiedRequestError as exc:
logger.warning(f"UnverifiedRequestError: {exc.args[0]}")
return JSONResponse(
content=build_unverified_request_response(
status_message=exc.args[0],
),
status_code=HTTPStatus.UNAUTHORIZED,
)

return JSONResponse(
build_command_accepted_response(), status_code=HTTPStatus.ACCEPTED
Expand All @@ -57,7 +70,10 @@ async def command_handler(request: Request, bot: Bot = bot_dependency) -> JSONRe
@router.get("/status")
async def status_handler(request: Request, bot: Bot = bot_dependency) -> JSONResponse:
try:
status = await bot.raw_get_status(dict(request.query_params))
status = await bot.raw_get_status(
dict(request.query_params),
request_headers=request.headers,
)
except UnknownBotAccountError as exc:
error_label = f"Unknown bot_id: {exc.bot_id}"
logger.warning(error_label)
Expand All @@ -66,14 +82,25 @@ async def status_handler(request: Request, bot: Bot = bot_dependency) -> JSONRes
build_bot_disabled_response(error_label),
status_code=HTTPStatus.SERVICE_UNAVAILABLE,
)
except UnverifiedRequestError as exc:
logger.warning(f"UnverifiedRequestError: {exc.args[0]}")
return JSONResponse(
content=build_unverified_request_response(
status_message=exc.args[0],
),
status_code=HTTPStatus.UNAUTHORIZED,
)

return JSONResponse(status)


@router.post("/notification/callback")
async def callback_handler(request: Request, bot: Bot = bot_dependency) -> JSONResponse:
try:
await bot.set_raw_botx_method_result(await request.json())
await bot.set_raw_botx_method_result(
await request.json(),
verify_request=False,
)
except BotXMethodCallbackNotFoundError as exc:
error_label = f"Unexpected callback with sync_id: {exc.sync_id}"
logger.warning(error_label)
Expand Down
2 changes: 1 addition & 1 deletion pybotx-submodule
Submodule pybotx-submodule updated 40 files
+12 −3 README.md
+268 −152 poetry.lock
+19 −3 pybotx/__init__.py
+1 −1 pybotx/bot/api/responses/bot_disabled.py
+39 −0 pybotx/bot/api/responses/unverified_request.py
+87 −9 pybotx/bot/bot.py
+10 −10 pybotx/bot/bot_accounts_storage.py
+11 −0 pybotx/bot/exceptions.py
+2 −0 pybotx/bot/handler.py
+11 −0 pybotx/bot/handler_collector.py
+10 −1 pybotx/models/attachments.py
+3 −0 pybotx/models/commands.py
+1 −0 pybotx/models/enums.py
+76 −0 pybotx/models/system_events/event_edit.py
+6 −1 pyproject.toml
+1 −1 setup.cfg
+12 −2 tests/client/notifications_api/test_direct_notification.py
+4 −0 tests/client/notifications_api/test_internal_bot_notification.py
+3 −0 tests/client/notifications_api/test_markup.py
+1 −0 tests/client/smartapps_api/test_smartapp_custom_notification.py
+11 −1 tests/client/test_botx_method_callback.py
+25 −0 tests/conftest.py
+1 −1 tests/system_events/test_added_to_chat.py
+1 −1 tests/system_events/test_chat_created.py
+1 −1 tests/system_events/test_cts_login.py
+1 −1 tests/system_events/test_cts_logout.py
+1 −1 tests/system_events/test_deleted_from_chat.py
+126 −0 tests/system_events/test_event_edit.py
+1 −1 tests/system_events/test_internal_bot_notification.py
+1 −1 tests/system_events/test_left_from_chat.py
+1 −1 tests/system_events/test_smartapp_event.py
+23 −6 tests/test_attachments.py
+56 −3 tests/test_base_command.py
+46 −3 tests/test_end_to_end.py
+2 −2 tests/test_files.py
+5 −5 tests/test_incoming_message.py
+1 −1 tests/test_logs.py
+8 −8 tests/test_status.py
+1 −1 tests/test_stickers.py
+437 −0 tests/test_verify_request.py

0 comments on commit c66dce3

Please sign in to comment.