|
| 1 | +# SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 2 | +# SPDX-License-Identifier: AGPL-3.0-or-later |
| 3 | +"""Minimal test ExApp for AppAPI integration tests. |
| 4 | +
|
| 5 | +Replaces the nc_py_api-based `tests/_install.py`. Implements only the |
| 6 | +callbacks AppAPI invokes (`/init`, `/enabled`, `/heartbeat`) plus a few |
| 7 | +introspection endpoints used by the integration tests. |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +from base64 import b64decode |
| 12 | + |
| 13 | +from fastapi import FastAPI, HTTPException, Request as FastAPIRequest |
| 14 | +from fastapi.responses import JSONResponse |
| 15 | + |
| 16 | +APP_ID = os.environ["APP_ID"] |
| 17 | +APP_SECRET = os.environ["APP_SECRET"] |
| 18 | +APP_VERSION = os.environ.get("APP_VERSION", "1.0.0") |
| 19 | + |
| 20 | +APP = FastAPI() |
| 21 | + |
| 22 | + |
| 23 | +def verify_auth(request: FastAPIRequest) -> str: |
| 24 | + """Validate AppAPI auth headers. Returns the username on success.""" |
| 25 | + ex_app_id = request.headers.get("EX-APP-ID", "") |
| 26 | + ex_app_version = request.headers.get("EX-APP-VERSION", "") |
| 27 | + auth_app_api = request.headers.get("AUTHORIZATION-APP-API", "") |
| 28 | + |
| 29 | + if not ex_app_id or not ex_app_version or not auth_app_api: |
| 30 | + raise HTTPException(status_code=401, detail="Missing AppAPI headers") |
| 31 | + if ex_app_id != APP_ID: |
| 32 | + raise HTTPException(status_code=401, detail=f"Invalid EX-APP-ID: {ex_app_id}") |
| 33 | + try: |
| 34 | + decoded = b64decode(auth_app_api).decode("UTF-8") |
| 35 | + username, secret = decoded.split(":", maxsplit=1) |
| 36 | + except Exception: |
| 37 | + raise HTTPException(status_code=401, detail="Malformed AUTHORIZATION-APP-API") |
| 38 | + if secret != APP_SECRET: |
| 39 | + raise HTTPException(status_code=401, detail="Invalid app secret") |
| 40 | + return username |
| 41 | + |
| 42 | + |
| 43 | +@APP.post("/init") |
| 44 | +async def init_callback(request: FastAPIRequest): |
| 45 | + verify_auth(request) |
| 46 | + return JSONResponse(content={}, status_code=200) |
| 47 | + |
| 48 | + |
| 49 | +@APP.put("/enabled") |
| 50 | +async def enabled_callback(enabled: bool, request: FastAPIRequest): |
| 51 | + verify_auth(request) |
| 52 | + return JSONResponse(content={"error": ""}, status_code=200) |
| 53 | + |
| 54 | + |
| 55 | +@APP.get("/heartbeat") |
| 56 | +async def heartbeat_callback(): |
| 57 | + return JSONResponse(content={"status": "ok"}, status_code=200) |
| 58 | + |
| 59 | + |
| 60 | +@APP.get("/cfg-echo") |
| 61 | +async def cfg_echo(request: FastAPIRequest): |
| 62 | + """Echo the env contract back. Used by test_app_cfg.py.""" |
| 63 | + verify_auth(request) |
| 64 | + return { |
| 65 | + "app_id": APP_ID, |
| 66 | + "app_version": APP_VERSION, |
| 67 | + "app_secret_prefix": APP_SECRET[:6], |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + import uvicorn |
| 73 | + uvicorn.run( |
| 74 | + "_test_app:APP", |
| 75 | + host=os.environ.get("APP_HOST", "0.0.0.0"), |
| 76 | + port=int(os.environ.get("APP_PORT", "9009")), |
| 77 | + log_level="info", |
| 78 | + ) |
0 commit comments