From 885aefc96c7d8244d6afe2a40549167f1a51cc49 Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Fri, 27 Jun 2025 14:51:32 +0200 Subject: [PATCH] first key exchange thing --- .gitignore | 5 +- internal/game/user.go | 14 ++---- internal/rest_handler/create_user.go | 8 ++- main.go | 1 + python_frontend/frontend.py | 75 ++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 python_frontend/frontend.py diff --git a/.gitignore b/.gitignore index f7a48f4..9932e1f 100644 --- a/.gitignore +++ b/.gitignore @@ -29,4 +29,7 @@ go.work.sum # Editor/IDE # .idea/ -# .vscode/ \ No newline at end of file +# .vscode/ + +.venv +__pycache__ diff --git a/internal/game/user.go b/internal/game/user.go index 2ff18bc..31cc34b 100644 --- a/internal/game/user.go +++ b/internal/game/user.go @@ -2,22 +2,18 @@ package game import ( "crypto/ed25519" - "crypto/rand" ) type User struct { - Name string - PublicKey ed25519.PublicKey - PrivateKey ed25519.PrivateKey + Name string + PublicKey ed25519.PublicKey } -func NewUser(name string) User { +func NewUser(name string, publicKey ed25519.PublicKey) User { // ed25519 - public, private, _ := ed25519.GenerateKey(rand.Reader) return User{ - Name: name, - PublicKey: public, - PrivateKey: private, + Name: name, + PublicKey: publicKey, } } diff --git a/internal/rest_handler/create_user.go b/internal/rest_handler/create_user.go index 7754238..20c0a3f 100644 --- a/internal/rest_handler/create_user.go +++ b/internal/rest_handler/create_user.go @@ -1,6 +1,7 @@ package rest_handler import ( + "encoding/base64" "net/http" "gitea.elara.ws/Hazel/hangman/internal/game" @@ -11,7 +12,8 @@ func CreateUser(c echo.Context) error { session := game.GetSession(c.Param("session")) type BodyContent struct { - Name string + Name string + PublicKey string } var bodyContent BodyContent @@ -20,7 +22,9 @@ func CreateUser(c echo.Context) error { return c.String(http.StatusBadRequest, err.Error()) } - user := game.NewUser(bodyContent.Name) + pub, _ := base64.StdEncoding.DecodeString(bodyContent.PublicKey) + user := game.NewUser(bodyContent.Name, pub) session.AddUser(user) + return c.JSON(http.StatusOK, user) } diff --git a/main.go b/main.go index f634b47..2a4d0de 100644 --- a/main.go +++ b/main.go @@ -39,6 +39,7 @@ func main() { e.POST("/api/session", rest_handler.CreateSession) e.POST("/api/:session/user", rest_handler.CreateUser) + e.POST("/api/:session/test-auth", rest_handler.CreateSession) e.GET("/", view_handler.CreateSession) e.GET("/:name", view_handler.CreateUser) diff --git a/python_frontend/frontend.py b/python_frontend/frontend.py new file mode 100644 index 0000000..22a18a5 --- /dev/null +++ b/python_frontend/frontend.py @@ -0,0 +1,75 @@ +from __future__ import annotations + +from typing import List +import requests +from nacl.signing import SigningKey, VerifyKey +import base64 + +import json + + +BASE_URL = "http://localhost:1323/api" + + +class User: + def __init__(self, session: Session, name: str) -> None: + self.session = session + self.name = name + + self.signing_key = SigningKey.generate() + + r = requests.post(BASE_URL + f"/{self.session.name}/user", json={ + "Name": name, + "PublicKey": base64.b64encode(self.signing_key.verify_key.__bytes__()).decode("ascii") + }) + + data = r.json() + print(json.dumps(data, indent=4)) + + def __repr__(self) -> str: + return f"User({self.name})" + + + def test_auth(self): + r = self.signed_request("/test-auth", { + "foo": "bar" + }) + print(r.content) + + def signed_request(self, endpoint: str, body: dict) -> requests.Response: + payload = json.dumps(body).encode("utf-8") + + signature = self.signing_key.sign(payload) + + return requests.post( + url=BASE_URL+f"/{self.session.name}"+endpoint, + data=payload, + headers={ + "Content-Type": "application/json", + "signature": base64.b64encode(signature.signature).decode("ascii") + } + ) + + +class Session: + def __init__(self) -> None: + data = requests.post(BASE_URL + "/session").json() + self.name = data["Name"] + self.users: List[User] = [] + + def __repr__(self) -> str: + return f"Session({self.name})" + + def add_user(self, name: str) -> User: + u = User(session=self, name=name) + self.users.append(u) + return u + + +if __name__ == "__main__": + s = Session() + print(s) + u = s.add_user(name="Hazel") + print(u) + + u.test_auth()