implemented getting user from signature
This commit is contained in:
parent
885aefc96c
commit
5f63d7385d
@ -1,6 +1,9 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/ed25519"
|
||||||
|
"encoding/base64"
|
||||||
|
|
||||||
petname "github.com/dustinkirkland/golang-petname"
|
petname "github.com/dustinkirkland/golang-petname"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -37,3 +40,15 @@ func GetSession(name string) *Session {
|
|||||||
func (s *Session) AddUser(user User) {
|
func (s *Session) AddUser(user User) {
|
||||||
s.Users = append(s.Users, user)
|
s.Users = append(s.Users, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s Session) VerifySignature(signature string, message []byte) *User {
|
||||||
|
for _, u := range s.Users {
|
||||||
|
sig, _ := base64.StdEncoding.DecodeString(signature)
|
||||||
|
|
||||||
|
if ed25519.Verify(u.PublicKey, message, sig) {
|
||||||
|
return &u
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
39
internal/rest_handler/test_auth.go
Normal file
39
internal/rest_handler/test_auth.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package rest_handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"gitea.elara.ws/Hazel/hangman/internal/game"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAuth(c echo.Context) error {
|
||||||
|
session := game.GetSession(c.Param("session"))
|
||||||
|
|
||||||
|
type TestResults struct {
|
||||||
|
SignatureValid bool
|
||||||
|
User *string
|
||||||
|
}
|
||||||
|
|
||||||
|
sig := c.Request().Header.Get("signature")
|
||||||
|
body, _ := io.ReadAll(c.Request().Body)
|
||||||
|
fmt.Println(sig)
|
||||||
|
|
||||||
|
u := session.VerifySignature(sig, body)
|
||||||
|
var resp TestResults
|
||||||
|
if u == nil {
|
||||||
|
resp = TestResults{
|
||||||
|
SignatureValid: false,
|
||||||
|
User: nil,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
resp = TestResults{
|
||||||
|
SignatureValid: true,
|
||||||
|
User: &u.Name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(http.StatusOK, resp)
|
||||||
|
}
|
2
main.go
2
main.go
@ -39,7 +39,7 @@ func main() {
|
|||||||
|
|
||||||
e.POST("/api/session", rest_handler.CreateSession)
|
e.POST("/api/session", rest_handler.CreateSession)
|
||||||
e.POST("/api/:session/user", rest_handler.CreateUser)
|
e.POST("/api/:session/user", rest_handler.CreateUser)
|
||||||
e.POST("/api/:session/test-auth", rest_handler.CreateSession)
|
e.POST("/api/:session/test-auth", rest_handler.TestAuth)
|
||||||
|
|
||||||
e.GET("/", view_handler.CreateSession)
|
e.GET("/", view_handler.CreateSession)
|
||||||
e.GET("/:name", view_handler.CreateUser)
|
e.GET("/:name", view_handler.CreateUser)
|
||||||
|
@ -69,7 +69,8 @@ class Session:
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
s = Session()
|
s = Session()
|
||||||
print(s)
|
print(s)
|
||||||
u = s.add_user(name="Hazel")
|
s.add_user(name="Hazel")
|
||||||
|
u = s.add_user(name="OtherHazel")
|
||||||
print(u)
|
print(u)
|
||||||
|
|
||||||
u.test_auth()
|
u.test_auth()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user