summaryrefslogtreecommitdiffstats
path: root/packages/meshbay-cli/src/meshbay_cli/client/remove.py
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-cli/src/meshbay_cli/client/remove.py')
-rw-r--r--packages/meshbay-cli/src/meshbay_cli/client/remove.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/packages/meshbay-cli/src/meshbay_cli/client/remove.py b/packages/meshbay-cli/src/meshbay_cli/client/remove.py
new file mode 100644
index 0000000..370cf95
--- /dev/null
+++ b/packages/meshbay-cli/src/meshbay_cli/client/remove.py
@@ -0,0 +1,45 @@
+"""
+Deleting a file from a group (docs/MESHBAY_DESIGN.md ยง5.4).
+
+The node decides, and on two grounds only: a key paired with it as operator, or
+a live device of the account that uploaded the file โ€” any of them, so a file
+sent from a phone is deleted from the laptop. Being the group's owner on the hub
+is neither. The request is answered with a challenge the client signs with its
+key on the node; the node rebuilds what was signed itself.
+"""
+
+from meshbay_common import MNP_VERSION
+from meshbay_common.adminop import OP_FILE_DELETE, admin_transcript
+
+from ..errors import NODE, CliError
+from .keys import b64, unb64
+from .session import GroupSession
+
+
+def may_delete(session: GroupSession, entry: dict, user_id: str) -> bool:
+ """What the node will accept, known before asking: the operator, or the
+ file's uploader. The node checks again; this only avoids asking in vain."""
+ return session.role == "operator" or entry.get("uploader_id") == user_id
+
+
+async def delete_file(session: GroupSession, file_id: str) -> None:
+ """Delete one file. Raises `NodeRefusal` with the node's reason."""
+ reply = await session.ch.request({"type": "file_delete", "v": MNP_VERSION,
+ "file_id": file_id},
+ "admin_challenge", "file_delete_ack")
+ if reply["type"] == "file_delete_ack":
+ return
+ # What is signed is checked against what was asked, never taken from the
+ # node: a challenge for another operation, file, group or node is refused.
+ if (reply.get("op") != OP_FILE_DELETE or reply.get("subject") != file_id
+ or reply.get("group_id") != session.group_id
+ or reply.get("node_pk") != session.node_pk):
+ raise CliError(NODE, "The node asked to sign something other than this deletion; "
+ "nothing was signed.")
+ transcript = admin_transcript(op=OP_FILE_DELETE, node_pk_b64=session.node_pk,
+ group_id=session.group_id, subject=file_id,
+ nonce=unb64(reply["nonce"]), ts=int(reply["ts"]))
+ await session.ch.request({"type": "admin_response", "v": MNP_VERSION,
+ "op_id": reply["op_id"], "op": OP_FILE_DELETE,
+ "signature": b64(session.identity.sk_ed.sign(transcript))},
+ "file_delete_ack")