Compare commits
2 Commits
9ded6a4615
...
885aefc96c
Author | SHA1 | Date | |
---|---|---|---|
|
885aefc96c | ||
|
64fa44e45e |
3
.gitignore
vendored
3
.gitignore
vendored
@ -30,3 +30,6 @@ go.work.sum
|
|||||||
# Editor/IDE
|
# Editor/IDE
|
||||||
# .idea/
|
# .idea/
|
||||||
# .vscode/
|
# .vscode/
|
||||||
|
|
||||||
|
.venv
|
||||||
|
__pycache__
|
||||||
|
@ -1,17 +1,9 @@
|
|||||||
package session
|
package game
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
petname "github.com/dustinkirkland/golang-petname"
|
petname "github.com/dustinkirkland/golang-petname"
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
|
||||||
Name string
|
|
||||||
PublicKey string
|
|
||||||
PrivateKey string
|
|
||||||
}
|
|
||||||
|
|
||||||
var lastSessionId int = 0
|
var lastSessionId int = 0
|
||||||
var sessionStorage []Session = []Session{}
|
var sessionStorage []Session = []Session{}
|
||||||
var nameToSession map[string]*Session = make(map[string]*Session)
|
var nameToSession map[string]*Session = make(map[string]*Session)
|
||||||
@ -35,11 +27,13 @@ func NewSession() Session {
|
|||||||
nameToSession[sessionName] = &s
|
nameToSession[sessionName] = &s
|
||||||
lastSessionId++
|
lastSessionId++
|
||||||
|
|
||||||
fmt.Println(sessionName)
|
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSession(name string) *Session {
|
func GetSession(name string) *Session {
|
||||||
return nameToSession[name]
|
return nameToSession[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Session) AddUser(user User) {
|
||||||
|
s.Users = append(s.Users, user)
|
||||||
|
}
|
19
internal/game/user.go
Normal file
19
internal/game/user.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package game
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/ed25519"
|
||||||
|
)
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Name string
|
||||||
|
PublicKey ed25519.PublicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUser(name string, publicKey ed25519.PublicKey) User {
|
||||||
|
// ed25519
|
||||||
|
|
||||||
|
return User{
|
||||||
|
Name: name,
|
||||||
|
PublicKey: publicKey,
|
||||||
|
}
|
||||||
|
}
|
@ -3,11 +3,11 @@ package rest_handler
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"gitea.elara.ws/Hazel/hangman/internal/session"
|
"gitea.elara.ws/Hazel/hangman/internal/game"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateSession(c echo.Context) error {
|
func CreateSession(c echo.Context) error {
|
||||||
s := session.NewSession()
|
s := game.NewSession()
|
||||||
return c.JSON(http.StatusOK, s)
|
return c.JSON(http.StatusOK, s)
|
||||||
}
|
}
|
||||||
|
30
internal/rest_handler/create_user.go
Normal file
30
internal/rest_handler/create_user.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package rest_handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"gitea.elara.ws/Hazel/hangman/internal/game"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateUser(c echo.Context) error {
|
||||||
|
session := game.GetSession(c.Param("session"))
|
||||||
|
|
||||||
|
type BodyContent struct {
|
||||||
|
Name string
|
||||||
|
PublicKey string
|
||||||
|
}
|
||||||
|
var bodyContent BodyContent
|
||||||
|
|
||||||
|
err := c.Bind(&bodyContent)
|
||||||
|
if err != nil {
|
||||||
|
return c.String(http.StatusBadRequest, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub, _ := base64.StdEncoding.DecodeString(bodyContent.PublicKey)
|
||||||
|
user := game.NewUser(bodyContent.Name, pub)
|
||||||
|
session.AddUser(user)
|
||||||
|
|
||||||
|
return c.JSON(http.StatusOK, user)
|
||||||
|
}
|
6
main.go
6
main.go
@ -37,9 +37,11 @@ func main() {
|
|||||||
templates: templates,
|
templates: templates,
|
||||||
}
|
}
|
||||||
|
|
||||||
e.GET("/", view_handler.CreateSession)
|
e.POST("/api/session", rest_handler.CreateSession)
|
||||||
e.GET("/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)
|
e.GET("/:name", view_handler.CreateUser)
|
||||||
|
|
||||||
e.Logger.Fatal(e.Start(":1323"))
|
e.Logger.Fatal(e.Start(":1323"))
|
||||||
|
75
python_frontend/frontend.py
Normal file
75
python_frontend/frontend.py
Normal file
@ -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()
|
Loading…
x
Reference in New Issue
Block a user