Compare commits
No commits in common. "885aefc96c7d8244d6afe2a40549167f1a51cc49" and "9ded6a46151fb0c36296e77dc8f5dcdab65c2802" have entirely different histories.
885aefc96c
...
9ded6a4615
3
.gitignore
vendored
3
.gitignore
vendored
@ -30,6 +30,3 @@ go.work.sum
|
|||||||
# Editor/IDE
|
# Editor/IDE
|
||||||
# .idea/
|
# .idea/
|
||||||
# .vscode/
|
# .vscode/
|
||||||
|
|
||||||
.venv
|
|
||||||
__pycache__
|
|
||||||
|
@ -1,19 +0,0 @@
|
|||||||
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/game"
|
"gitea.elara.ws/Hazel/hangman/internal/session"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateSession(c echo.Context) error {
|
func CreateSession(c echo.Context) error {
|
||||||
s := game.NewSession()
|
s := session.NewSession()
|
||||||
return c.JSON(http.StatusOK, s)
|
return c.JSON(http.StatusOK, s)
|
||||||
}
|
}
|
||||||
|
@ -1,30 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
@ -1,9 +1,17 @@
|
|||||||
package game
|
package session
|
||||||
|
|
||||||
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)
|
||||||
@ -27,13 +35,11 @@ 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)
|
|
||||||
}
|
|
6
main.go
6
main.go
@ -37,11 +37,9 @@ func main() {
|
|||||||
templates: templates,
|
templates: templates,
|
||||||
}
|
}
|
||||||
|
|
||||||
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("/", view_handler.CreateSession)
|
||||||
|
e.GET("/api/session", rest_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"))
|
||||||
|
@ -1,75 +0,0 @@
|
|||||||
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