diff --git a/tubearchivist/api/README.md b/tubearchivist/api/README.md index 0316b3e..6b703a9 100644 --- a/tubearchivist/api/README.md +++ b/tubearchivist/api/README.md @@ -216,7 +216,7 @@ Returns: } ``` -Start a background task +Start a background task POST /api/task/ ```json { @@ -245,3 +245,25 @@ Send empty post request to validate cookie. "cookie_validated": true } ``` + +PUT /api/cookie/ +Send put request containing the cookie as a string: +```json +{ + "cookie": "your-cookie-as-string" +} +``` +Imports and validates cookie, returns on success: +```json +{ + "cookie_import": "done", + "cookie_validated": true +} +``` +Or returns status code 400 on failure: +```json +{ + "cookie_import": "fail", + "cookie_validated": false +} +``` diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index be9c571..9604ca5 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -480,6 +480,7 @@ class CookieView(ApiBaseView): """resolves to /api/cookie/ GET: check if cookie is enabled POST: verify validity of cookie + PUT: import cookie """ @staticmethod @@ -499,3 +500,27 @@ class CookieView(ApiBaseView): validated = CookieHandler(config).validate() return Response({"cookie_validated": validated}) + + @staticmethod + def put(request): + """handle put request""" + # pylint: disable=unused-argument + config = AppConfig().config + cookie = request.data.get("cookie") + if not cookie: + message = "missing cookie key in request data" + print(message) + return Response({"message": message}, status=400) + + print(f"cookie preview:\n\n{cookie[:300]}") + handler = CookieHandler(config) + handler.set_cookie(cookie) + validated = handler.validate() + if not validated: + handler.revoke() + message = {"cookie_import": "fail", "cookie_validated": validated} + print(f"cookie: {message}") + return Response({"message": message}, status=400) + + message = {"cookie_import": "done", "cookie_validated": validated} + return Response(message)