fixed changing of pointers
This commit is contained in:
		| @@ -9,7 +9,10 @@ import ( | ||||
| ) | ||||
|  | ||||
| func CreateUser(c echo.Context) error { | ||||
| 	session := game.GetSession(c.Param("session")) | ||||
| 	session, err := game.GetSession(c.Param("session")) | ||||
| 	if err != nil { | ||||
| 		return c.String(http.StatusBadRequest, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	type BodyContent struct { | ||||
| 		Name      string | ||||
| @@ -17,12 +20,15 @@ func CreateUser(c echo.Context) error { | ||||
| 	} | ||||
| 	var bodyContent BodyContent | ||||
|  | ||||
| 	err := c.Bind(&bodyContent) | ||||
| 	err = c.Bind(&bodyContent) | ||||
| 	if err != nil { | ||||
| 		return c.String(http.StatusBadRequest, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	pub, _ := base64.StdEncoding.DecodeString(bodyContent.PublicKey) | ||||
| 	pub, err := base64.StdEncoding.DecodeString(bodyContent.PublicKey) | ||||
| 	if err != nil { | ||||
| 		return c.String(http.StatusBadRequest, err.Error()) | ||||
| 	} | ||||
| 	user := game.NewUser(bodyContent.Name, pub) | ||||
| 	session.AddUser(user) | ||||
|  | ||||
|   | ||||
| @@ -1,40 +1,29 @@ | ||||
| package rest_handler | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"net/http" | ||||
|  | ||||
| 	"gitea.elara.ws/Hazel/hangman/internal/game" | ||||
| 	"github.com/labstack/echo/v4" | ||||
| ) | ||||
|  | ||||
| func GuessLetter(c echo.Context) error { | ||||
| 	session := game.GetSession(c.Param("session")) | ||||
| 	session, user, err := GetData(c) | ||||
|  | ||||
| 	sig := c.Request().Header.Get("signature") | ||||
| 	body, _ := io.ReadAll(c.Request().Body) | ||||
|  | ||||
| 	u := session.VerifySignature(sig, body) | ||||
| 	if u == nil { | ||||
| 		return c.String(http.StatusBadRequest, "This user was not fount in the current session.") | ||||
| 	if err != nil { | ||||
| 		return c.String(http.StatusBadRequest, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	if !session.CurrentUser.PublicKey.Equal(u.PublicKey) { | ||||
| 		return c.String(http.StatusBadRequest, "It's not the turn of user "+u.Name+". It's the turn of "+session.CurrentUser.Name+".") | ||||
| 	if session.CurrentUser != user { | ||||
| 		return c.String(http.StatusBadRequest, "It's not the turn of user "+user.Name+". It's the turn of "+session.CurrentUser.Name+".") | ||||
| 	} | ||||
|  | ||||
| 	c.Request().Body = io.NopCloser(bytes.NewBuffer(body)) | ||||
|  | ||||
| 	type BodyContent struct { | ||||
| 		Guess string | ||||
| 	} | ||||
| 	var bodyContent BodyContent | ||||
|  | ||||
| 	err := c.Bind(&bodyContent) | ||||
| 	err = c.Bind(&bodyContent) | ||||
| 	if err != nil { | ||||
| 		fmt.Println(err) | ||||
| 		return c.String(http.StatusBadRequest, err.Error()) | ||||
| 	} | ||||
|  | ||||
|   | ||||
							
								
								
									
										30
									
								
								internal/rest_handler/helper.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								internal/rest_handler/helper.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| package rest_handler | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"io" | ||||
|  | ||||
| 	"gitea.elara.ws/Hazel/hangman/internal/game" | ||||
| 	"github.com/labstack/echo/v4" | ||||
| ) | ||||
|  | ||||
| func GetData(c echo.Context) (*game.Session, *game.User, error) { | ||||
| 	session, err := game.GetSession(c.Param("session")) | ||||
| 	if err != nil { | ||||
| 		return session, nil, err | ||||
| 	} | ||||
|  | ||||
| 	sig := c.Request().Header.Get("signature") | ||||
| 	body, _ := io.ReadAll(c.Request().Body) | ||||
|  | ||||
| 	user, err := session.VerifySignature(sig, body) | ||||
| 	if err != nil { | ||||
| 		return session, user, err | ||||
| 	} | ||||
|  | ||||
| 	c.Request().Body = io.NopCloser(bytes.NewBuffer(body)) | ||||
|  | ||||
| 	// fmt.Printf("user %v:\t%p\n", user.Name, user) | ||||
|  | ||||
| 	return session, user, nil | ||||
| } | ||||
| @@ -1,8 +1,6 @@ | ||||
| package rest_handler | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"net/http" | ||||
|  | ||||
| 	"gitea.elara.ws/Hazel/hangman/internal/game" | ||||
| @@ -10,30 +8,17 @@ import ( | ||||
| ) | ||||
|  | ||||
| func TestAuth(c echo.Context) error { | ||||
| 	session := game.GetSession(c.Param("session")) | ||||
|  | ||||
| 	type TestResults struct { | ||||
| 		SignatureValid bool | ||||
| 		User           *string | ||||
| 		Session *game.Session | ||||
| 		User    *game.User | ||||
| 		Error   error | ||||
| 	} | ||||
|  | ||||
| 	sig := c.Request().Header.Get("signature") | ||||
| 	body, _ := io.ReadAll(c.Request().Body) | ||||
| 	fmt.Println(sig) | ||||
| 	session, user, err := GetData(c) | ||||
|  | ||||
| 	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) | ||||
| 	return c.JSON(http.StatusOK, TestResults{ | ||||
| 		Session: session, | ||||
| 		User:    user, | ||||
| 		Error:   err, | ||||
| 	}) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user