Skip to content

Commit

Permalink
AppAPI 2.0 (#212)
Browse files Browse the repository at this point in the history
See: nextcloud/app_api#213

1. _All Talk Bots should be updated to use AppAPI auth for input
endpoints._
2. Updated Documentation reflecting last changes.
3. CI will fail, until merge in AppAPI will finished.

---------

Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
  • Loading branch information
bigcat88 authored Jan 25, 2024
1 parent a4f31b6 commit 533f231
Show file tree
Hide file tree
Showing 26 changed files with 100 additions and 126 deletions.
3 changes: 2 additions & 1 deletion .run/Skeleton (27).run.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Skeleton (27)" type="PythonConfigurationType" factoryName="Python">
<module name="nc_py_api" />
<option name="ENV_FILES" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="APP_HOST" value="0.0.0.0" />
<env name="APP_ID" value="skeleton" />
<env name="APP_PORT" value="9030" />
<env name="APP_SECRET" value="12345" />
<env name="APP_VERSION" value="1.0.0" />
<env name="NEXTCLOUD_URL" value="http://stable27.local/index.php" />
<env name="PYTHONUNBUFFERED" value="1" />
<env name="APP_HOST" value="0.0.0.0" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/examples/as_app/skeleton/lib" />
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

All notable changes to this project will be documented in this file.

## [0.8.1 - 2024-01-xx]
## [0.9.0 - 2024-01-25]

### Added

Expand All @@ -12,7 +12,9 @@ All notable changes to this project will be documented in this file.

### Changed

- **large amount of incompatible changes** for `AppAPI 2.0`, see PR for description. #212
- class `Share`.raw_data marked as deprecated and changed to `_raw_data`. #206
- `ex_app.talk_bot_app`/`ex_app.atalk_bot_app` renamed to `ex_app.talk_bot_msg`/`ex_app.atalk_bot_msg`.

## [0.8.0 - 2024-01-12]

Expand Down
7 changes: 3 additions & 4 deletions docs/NextcloudApp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ First register ``manual_install`` daemon:

.. code-block:: shell
php occ app_api:daemon:register manual_install "Manual Install" manual-install 0 0 0
php occ app_api:daemon:register manual_install "Manual Install" manual-install http host.docker.internal 0
Then, launch your application. Since this is a manual deployment, it's your responsibility to set minimum of the environment variables.
Here they are:
Expand All @@ -86,7 +86,7 @@ After launching your application, execute the following command in the Nextcloud
.. code-block:: shell
php occ app_api:app:register YOUR_APP_ID manual_install --json-info \
"{\"appid\":\"YOUR_APP_ID\",\"name\":\"YOUR_APP_DISPLAY_NAME\",\"daemon_config_name\":\"manual_install\",\"version\":\"YOU_APP_VERSION\",\"secret\":\"YOUR_APP_SECRET\",\"host\":\"host.docker.internal\",\"scopes\":{\"required\":[2, 10, 11],\"optional\":[30, 31, 32, 33]},\"port\":SELECTED_PORT,\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"YOUR_APP_ID\",\"name\":\"YOUR_APP_DISPLAY_NAME\",\"daemon_config_name\":\"manual_install\",\"version\":\"YOU_APP_VERSION\",\"secret\":\"YOUR_APP_SECRET\",\"scopes\":{\"required\":[\"ALL\"],\"optional\":[]},\"port\":SELECTED_PORT,\"system_app\":0}" \
--force-scopes --wait-finish
You can see how **nc_py_api** registers in ``scripts/dev_register.sh``.
Expand Down Expand Up @@ -226,8 +226,7 @@ and since this is not directly related to working with NextCloud, we will skip t
Using AppAPIAuthMiddleware
--------------------------

If your application does not implement `Talk Bot` functionality and you most often do not need
the ``NextcloudApp`` class returned after standard authentication with `Depends`:
If in your application in most cases you don't really need the ``NextcloudApp`` class returned after standard authentication using `Depends`:

.. code-block:: python
Expand Down
10 changes: 6 additions & 4 deletions docs/NextcloudTalkBot.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ Afterward, using FastAPI, you can define endpoints that will be invoked by Talk:
@APP.post("/currency_talk_bot")
async def currency_talk_bot(
message: Annotated[talk_bot.TalkBotMessage, Depends(talk_bot_app)],
_nc: Annotated[NextcloudApp, Depends(nc_app)],
message: Annotated[talk_bot.TalkBotMessage, Depends(atalk_bot_msg)],
background_tasks: BackgroundTasks,
):
return Response()
.. note::
You must include to each endpoint your bot provides the **Depends(talk_bot_app)**.
**message: Annotated[talk_bot.TalkBotMessage, Depends(talk_bot_app)]**
You must include to each endpoint your bot provides the **Depends(nc_app)**.

Depending on **talk_bot_app** serves as an automatic authentication handler for messages from the cloud, which returns the received message from Nextcloud upon successful authentication.
Depending on **nc_app** serves as an automatic authentication handler for messages from the cloud.

**message: Annotated[talk_bot.TalkBotMessage, Depends(talk_bot_app)]** - returns the received message from Nextcloud upon successful authentication.

Additionally, if your bot can provide quick and fixed execution times, you may not need to create background tasks.
However, in most cases, it's recommended to segregate functionality and perform operations in the background, while promptly returning an empty response to Nextcloud.
Expand Down
6 changes: 3 additions & 3 deletions examples/as_app/skeleton/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ run:
register27:
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:unregister skeleton --silent --force || true
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:register skeleton manual_install --json-info \
"{\"appid\":\"skeleton\",\"name\":\"App Skeleton\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"port\":9030,\"scopes\":{\"required\":[],\"optional\":[]},\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"skeleton\",\"name\":\"App Skeleton\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"port\":9030,\"scopes\":{\"required\":[],\"optional\":[]},\"system_app\":0}" \
--force-scopes --wait-finish

.PHONY: register28
register28:
docker exec master-stable28-1 sudo -u www-data php occ app_api:app:unregister skeleton --silent --force || true
docker exec master-stable28-1 sudo -u www-data php occ app_api:app:register skeleton manual_install --json-info \
"{\"appid\":\"skeleton\",\"name\":\"App Skeleton\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"port\":9030,\"scopes\":{\"required\":[],\"optional\":[]},\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"skeleton\",\"name\":\"App Skeleton\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"port\":9030,\"scopes\":{\"required\":[],\"optional\":[]},\"system_app\":0}" \
--force-scopes --wait-finish

.PHONY: register
register:
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:unregister skeleton --silent --force || true
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:register skeleton manual_install --json-info \
"{\"appid\":\"skeleton\",\"name\":\"App Skeleton\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"port\":9030,\"scopes\":{\"required\":[],\"optional\":[]},\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"skeleton\",\"name\":\"App Skeleton\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"port\":9030,\"scopes\":{\"required\":[],\"optional\":[]},\"system_app\":0}" \
--force-scopes --wait-finish
1 change: 0 additions & 1 deletion examples/as_app/skeleton/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
<optional>
</optional>
</scopes>
<protocol>http</protocol>
<system>false</system>
</external-app>
</info>
6 changes: 3 additions & 3 deletions examples/as_app/talk_bot/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ run27:
register:
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:unregister talk_bot --silent --force || true
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:register talk_bot manual_install --json-info \
"{\"appid\":\"talk_bot\",\"name\":\"TalkBot\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"port\":9032,\"scopes\":{\"required\":[\"TALK\", \"TALK_BOT\"],\"optional\":[]},\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"talk_bot\",\"name\":\"TalkBot\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"port\":9032,\"scopes\":{\"required\":[\"TALK\", \"TALK_BOT\"],\"optional\":[]},\"system_app\":0}" \
--force-scopes --wait-finish

.PHONY: register27
register27:
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:unregister talk_bot --silent --force || true
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:unregister talk_bot --force || true
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:register talk_bot manual_install --json-info \
"{\"appid\":\"talk_bot\",\"name\":\"TalkBot\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"port\":9032,\"scopes\":{\"required\":[\"TALK\", \"TALK_BOT\"],\"optional\":[]},\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"talk_bot\",\"name\":\"TalkBot\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"port\":9032,\"scopes\":{\"required\":[\"TALK\", \"TALK_BOT\"],\"optional\":[]},\"system_app\":0}" \
--force-scopes --wait-finish
1 change: 0 additions & 1 deletion examples/as_app/talk_bot/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<optional>
</optional>
</scopes>
<protocol>http</protocol>
<system>false</system>
</external-app>
</info>
5 changes: 3 additions & 2 deletions examples/as_app/talk_bot/lib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from fastapi import BackgroundTasks, Depends, FastAPI, Response

from nc_py_api import NextcloudApp, talk_bot
from nc_py_api.ex_app import run_app, set_handlers, talk_bot_app
from nc_py_api.ex_app import atalk_bot_msg, nc_app, run_app, set_handlers


# The same stuff as for usual External Applications
Expand Down Expand Up @@ -66,7 +66,8 @@ def currency_talk_bot_process_request(message: talk_bot.TalkBotMessage):

@APP.post("/currency_talk_bot")
async def currency_talk_bot(
message: Annotated[talk_bot.TalkBotMessage, Depends(talk_bot_app)],
_nc: Annotated[NextcloudApp, Depends(nc_app)],
message: Annotated[talk_bot.TalkBotMessage, Depends(atalk_bot_msg)],
background_tasks: BackgroundTasks,
):
# As during converting, we do not process converting locally, we perform this in background, in the background task.
Expand Down
4 changes: 2 additions & 2 deletions examples/as_app/talk_bot_ai/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ run27:
register:
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:unregister talk_bot_ai --silent --force || true
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:register talk_bot_ai manual_install --json-info \
"{\"appid\":\"talk_bot_ai\",\"name\":\"TalkBotAI\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"port\":9034,\"scopes\":{\"required\":[\"TALK\", \"TALK_BOT\"],\"optional\":[]},\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"talk_bot_ai\",\"name\":\"TalkBotAI\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"port\":9034,\"scopes\":{\"required\":[\"TALK\", \"TALK_BOT\"],\"optional\":[]},\"system_app\":0}" \
--force-scopes --wait-finish

.PHONY: register27
register27:
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:unregister talk_bot_ai --silent --force || true
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:register talk_bot_ai manual_install --json-info \
"{\"appid\":\"talk_bot_ai\",\"name\":\"TalkBotAI\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"port\":9034,\"scopes\":{\"required\":[\"TALK\", \"TALK_BOT\"],\"optional\":[]},\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"talk_bot_ai\",\"name\":\"TalkBotAI\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"port\":9034,\"scopes\":{\"required\":[\"TALK\", \"TALK_BOT\"],\"optional\":[]},\"system_app\":0}" \
--force-scopes --wait-finish
1 change: 0 additions & 1 deletion examples/as_app/talk_bot_ai/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<optional>
</optional>
</scopes>
<protocol>http</protocol>
<system>false</system>
</external-app>
</info>
11 changes: 9 additions & 2 deletions examples/as_app/talk_bot_ai/lib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
from transformers import pipeline

from nc_py_api import NextcloudApp, talk_bot
from nc_py_api.ex_app import get_model_path, run_app, set_handlers, talk_bot_app
from nc_py_api.ex_app import (
atalk_bot_msg,
get_model_path,
nc_app,
run_app,
set_handlers,
)


@asynccontextmanager
Expand All @@ -34,7 +40,8 @@ def ai_talk_bot_process_request(message: talk_bot.TalkBotMessage):

@APP.post("/ai_talk_bot")
async def ai_talk_bot(
message: Annotated[talk_bot.TalkBotMessage, Depends(talk_bot_app)],
_nc: Annotated[NextcloudApp, Depends(nc_app)],
message: Annotated[talk_bot.TalkBotMessage, Depends(atalk_bot_msg)],
background_tasks: BackgroundTasks,
):
if message.object_name == "message":
Expand Down
6 changes: 3 additions & 3 deletions examples/as_app/to_gif/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ run:
register27:
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:unregister to_gif --silent --force || true
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:register to_gif manual_install --json-info \
"{\"appid\":\"to_gif\",\"name\":\"to_gif\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"port\":9031,\"scopes\":{\"required\":[\"FILES\", \"NOTIFICATIONS\"],\"optional\":[]},\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"to_gif\",\"name\":\"to_gif\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"port\":9031,\"scopes\":{\"required\":[\"FILES\", \"NOTIFICATIONS\"],\"optional\":[]},\"system_app\":0}" \
--force-scopes --wait-finish

.PHONY: register28
register28:
docker exec master-stable28-1 sudo -u www-data php occ app_api:app:unregister to_gif --silent --force || true
docker exec master-stable28-1 sudo -u www-data php occ app_api:app:register to_gif manual_install --json-info \
"{\"appid\":\"to_gif\",\"name\":\"to_gif\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"port\":9031,\"scopes\":{\"required\":[\"FILES\", \"NOTIFICATIONS\"],\"optional\":[]},\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"to_gif\",\"name\":\"to_gif\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"port\":9031,\"scopes\":{\"required\":[\"FILES\", \"NOTIFICATIONS\"],\"optional\":[]},\"system_app\":0}" \
--force-scopes --wait-finish

.PHONY: register
register:
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:unregister to_gif --silent --force || true
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:register to_gif manual_install --json-info \
"{\"appid\":\"to_gif\",\"name\":\"to_gif\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"port\":9031,\"scopes\":{\"required\":[\"FILES\", \"NOTIFICATIONS\"],\"optional\":[]},\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"to_gif\",\"name\":\"to_gif\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"port\":9031,\"scopes\":{\"required\":[\"FILES\", \"NOTIFICATIONS\"],\"optional\":[]},\"system_app\":0}" \
--force-scopes --wait-finish
1 change: 0 additions & 1 deletion examples/as_app/to_gif/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<optional>
</optional>
</scopes>
<protocol>http</protocol>
<system>false</system>
</external-app>
</info>
6 changes: 3 additions & 3 deletions examples/as_app/ui_example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ run:
register27:
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:unregister ui_example --silent --force || true
docker exec master-stable27-1 sudo -u www-data php occ app_api:app:register ui_example manual_install --json-info \
"{\"appid\":\"ui_example\",\"name\":\"UI Example\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"port\":9035,\"scopes\":{\"required\":[],\"optional\":[]},\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"ui_example\",\"name\":\"UI Example\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"port\":9035,\"scopes\":{\"required\":[],\"optional\":[]},\"system_app\":0}" \
--force-scopes --wait-finish

.PHONY: register28
register28:
docker exec master-stable28-1 sudo -u www-data php occ app_api:app:unregister ui_example --silent --force || true
docker exec master-stable28-1 sudo -u www-data php occ app_api:app:register ui_example manual_install --json-info \
"{\"appid\":\"ui_example\",\"name\":\"UI Example\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"port\":9035,\"scopes\":{\"required\":[],\"optional\":[]},\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"ui_example\",\"name\":\"UI Example\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"port\":9035,\"scopes\":{\"required\":[],\"optional\":[]},\"system_app\":0}" \
--force-scopes --wait-finish

.PHONY: register
register:
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:unregister ui_example --silent --force || true
docker exec master-nextcloud-1 sudo -u www-data php occ app_api:app:register ui_example manual_install --json-info \
"{\"appid\":\"ui_example\",\"name\":\"UI Example\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"host\":\"host.docker.internal\",\"port\":9035,\"scopes\":{\"required\":[],\"optional\":[]},\"protocol\":\"http\",\"system_app\":0}" \
"{\"appid\":\"ui_example\",\"name\":\"UI Example\",\"daemon_config_name\":\"manual_install\",\"version\":\"1.0.0\",\"secret\":\"12345\",\"port\":9035,\"scopes\":{\"required\":[],\"optional\":[]},\"system_app\":0}" \
--force-scopes --wait-finish
1 change: 0 additions & 1 deletion examples/as_app/ui_example/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
<optional>
</optional>
</scopes>
<protocol>http</protocol>
<system>false</system>
</external-app>
</info>
2 changes: 1 addition & 1 deletion nc_py_api/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Version of nc_py_api."""

__version__ = "0.8.1.dev0"
__version__ = "0.9.0.dev0"
4 changes: 2 additions & 2 deletions nc_py_api/ex_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from .integration_fastapi import (
AppAPIAuthMiddleware,
anc_app,
atalk_bot_app,
atalk_bot_msg,
nc_app,
set_handlers,
talk_bot_app,
talk_bot_msg,
)
from .misc import get_model_path, persistent_storage, verify_version
from .ui.files_actions import UiActionFileInfo
Expand Down
24 changes: 5 additions & 19 deletions nc_py_api/ex_app/integration_fastapi.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""FastAPI directly related stuff."""

import asyncio
import hashlib
import hmac
import json
import os
import typing
Expand All @@ -21,7 +19,7 @@

from .._misc import get_username_secret_from_headers
from ..nextcloud import AsyncNextcloudApp, NextcloudApp
from ..talk_bot import TalkBotMessage, aget_bot_secret, get_bot_secret
from ..talk_bot import TalkBotMessage
from .misc import persistent_storage


Expand Down Expand Up @@ -49,26 +47,14 @@ def anc_app(request: HTTPConnection) -> AsyncNextcloudApp:
return nextcloud_app


def __talk_bot_app(secret: bytes | None, request: Request, body: bytes) -> TalkBotMessage:
if not secret:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
hmac_sign = hmac.new(
secret, request.headers.get("X-NEXTCLOUD-TALK-RANDOM", "").encode("UTF-8"), digestmod=hashlib.sha256
)
hmac_sign.update(body)
if request.headers["X-NEXTCLOUD-TALK-SIGNATURE"] != hmac_sign.hexdigest():
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
return TalkBotMessage(json.loads(body))


def talk_bot_app(request: Request) -> TalkBotMessage:
def talk_bot_msg(request: Request) -> TalkBotMessage:
"""Authentication handler for bot requests from Nextcloud Talk to the application."""
return __talk_bot_app(get_bot_secret(request.url.components.path), request, asyncio.run(request.body()))
return TalkBotMessage(json.loads(asyncio.run(request.body())))


async def atalk_bot_app(request: Request) -> TalkBotMessage:
async def atalk_bot_msg(request: Request) -> TalkBotMessage:
"""Async Authentication handler for bot requests from Nextcloud Talk to the application."""
return __talk_bot_app(await aget_bot_secret(request.url.components.path), request, await request.body())
return TalkBotMessage(json.loads(await request.body()))


def set_handlers(
Expand Down
Loading

0 comments on commit 533f231

Please sign in to comment.