From ee81c95b32198dea7767c1981ac0c01aadbf9cee Mon Sep 17 00:00:00 2001 From: Alberto Lerda Date: Thu, 9 Feb 2023 18:27:12 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=A5=B3=20initial=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 4 ++ .gitmodules | 6 +++ example/new_session.sh | 3 ++ example/session.json | 6 +++ main.py | 93 ++++++++++++++++++++++++++++++++++++++++++ pyfabapi | 1 + requirements.txt | 8 ++++ zenflows-crypto | 1 + 8 files changed, 122 insertions(+) create mode 100644 .env.example create mode 100644 .gitmodules create mode 100755 example/new_session.sh create mode 100644 example/session.json create mode 100644 main.py create mode 160000 pyfabapi create mode 100644 requirements.txt create mode 160000 zenflows-crypto diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..3000097 --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +FAB_HOST=localhost +FAB_PORT=59661 +FAB_USER=Testuser +FAB_PASS=secret diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..cf3ce07 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "pyfabapi"] + path = pyfabapi + url = https://gitlab.com/fabinfra/fabaccess/pyfabapi.git +[submodule "zenflows-crypto"] + path = zenflows-crypto + url = git@github.com:interfacerproject/zenflows-crypto.git diff --git a/example/new_session.sh b/example/new_session.sh new file mode 100755 index 0000000..7df665b --- /dev/null +++ b/example/new_session.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +curl -X POST -H 'Content-Type:application/json' -d "@session.json" http://localhost:8000/new-session diff --git a/example/session.json b/example/session.json new file mode 100644 index 0000000..59d3e1c --- /dev/null +++ b/example/session.json @@ -0,0 +1,6 @@ +{ + "command": "OPEN", + "eddsa_public_key": "EdDja2UdyPPEduFhXLEzzRHuW9TdaG7g16oVFAXWYvHt", + "eddsa_signature": "4YApLBq9KMytJZmcRUdU2Ltn6QqLiDCPWshziBJymeP88vRg63VNWL19PM8TxZjcQvkBU6g7ABmwXdCyPnzWsNjM", + "timestamp": "1234567" +} diff --git a/main.py b/main.py new file mode 100644 index 0000000..c1d2186 --- /dev/null +++ b/main.py @@ -0,0 +1,93 @@ +import asyncio +from pyfabapi import fabapi +import pyfabapi.fabapi.user_system + +from typing import Union + +from fastapi import FastAPI +from pydantic import BaseModel + +from zenroom import zencode_exec + +import json + +import os +from dotenv import load_dotenv + +load_dotenv() + +class Config: + fab_host: string + fab_port: int + fab_user: string + fab_pass: string + + def __init__(self): + self.fab_host = os.getenv("FAB_HOST") + self.fab_port = int(os.getenv("FAB_PORT")) + self.fab_user = os.getenv("FAB_USER") + self.fab_pass = os.getenv("FAB_PASS") + +with open('zenflows-crypto/src/verify_fabaccess_open.zen','r') as file: + zen_verify_open = file.read() + +with open('zenflows-crypto/src/verify_fabaccess_cmd.zen','r') as file: + zen_verify_cmd = file.read() + +class NewSession(BaseModel): + timestamp: str + command: str + eddsa_public_key: str + eddsa_signature: str + +class Command(BaseModel): + timestamp: str + token: str + service: str + command: str + eddsa_public_key: str + eddsa_signature: str + +app = FastAPI() +conf = Config() + +# Maybe the session is useless +@app.post("/new-session") +async def new_session(cmd: NewSession): + zen_result = zencode_exec(zen_verify_open, keys=cmd.json()) + + if zen_result.output == '': + raise HTTPException(status_code=500, detail="Invalid signature") + + res = json.loads(zen_result.output) + + if res["output"][0] != 'ok': + raise HTTPException(status_code=500, detail="Invalid signature") + + return {"token": "todo"} + +@app.get("/command") +async def read_root(cmd: Command): + zen_result = zencode_exec(zen_verify_cmd, keys=cmd.json()) + + if zen_result.output == '': + raise HTTPException(status_code=500, detail="Invalid signature") + + res = json.loads(zen_result.output) + + if res["output"][0] != 'ok': + raise HTTPException(status_code=500, detail="Invalid signature") + + + # a service "urn:fabaccess:resource:Another" + + session = await fabapi.connect(conf.fab_host, conf.fab_port, conf.fab_user, conf.fab_pass) + info = session.machineSystem.info + ma = await info.getMachineURN(cmd.service).a_wait() + + if ma.just: + print(ma) + print(ma.just) + await ma.just.use.use().a_wait() + else: + raise HTTPException(status_code=500, detail="No such resource") diff --git a/pyfabapi b/pyfabapi new file mode 160000 index 0000000..2383a6c --- /dev/null +++ b/pyfabapi @@ -0,0 +1 @@ +Subproject commit 2383a6cc0dd3c49a677e05e5cedd6854d89f4237 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..392b3db --- /dev/null +++ b/requirements.txt @@ -0,0 +1,8 @@ +fastapi==0.90.0 +meson==1.0.0 +ninja==1.11.1 +pycapnp==1.3.0 +pydantic==1.10.4 +python-dotenv==0.21.1 +uvicorn==0.20.0 +zenroom==2.16.4 diff --git a/zenflows-crypto b/zenflows-crypto new file mode 160000 index 0000000..49c4f42 --- /dev/null +++ b/zenflows-crypto @@ -0,0 +1 @@ +Subproject commit 49c4f42c667d53101efaec52617b19b0abc96089