From d0e045974fe6e6e5d0d33f420841a5e90609695e Mon Sep 17 00:00:00 2001 From: tijl Date: Mon, 26 Aug 2024 16:57:28 +0200 Subject: [PATCH] flags implementation --- internal/apps/flags/{game.go => handlers.go} | 24 +++++++++--- internal/apps/flags/main.go | 39 -------------------- internal/apps/flags/util.go | 30 +++++++++++++++ internal/handlers/auth.go | 3 +- 4 files changed, 51 insertions(+), 45 deletions(-) rename internal/apps/flags/{game.go => handlers.go} (88%) diff --git a/internal/apps/flags/game.go b/internal/apps/flags/handlers.go similarity index 88% rename from internal/apps/flags/game.go rename to internal/apps/flags/handlers.go index 6739b7c..0c88d36 100644 --- a/internal/apps/flags/game.go +++ b/internal/apps/flags/handlers.go @@ -16,9 +16,11 @@ import ( "github.com/google/uuid" ) +const flagSessionCookie = "app_flags_game_session" + func answerHandler(c *fiber.Ctx) error { - gameId, err := uuid.Parse(c.Cookies("app_flags_game_session")) + gameId, err := uuid.Parse(c.Cookies(flagSessionCookie)) if err != nil { return err } @@ -77,7 +79,7 @@ func questionHandler(c *fiber.Ctx, newGame NewGameUUID) error { if newGame.used { gameId = newGame.UUID } else { - gameId, err = uuid.Parse(c.Cookies("app_flags_game_session")) + gameId, err = uuid.Parse(c.Cookies(flagSessionCookie)) if err != nil { return err } @@ -88,6 +90,11 @@ func questionHandler(c *fiber.Ctx, newGame NewGameUUID) error { return err } + uid, err := user.GetSession(c) + if uid != gameSession.Uid.String { + utils.ClearCookie(c, flagSessionCookie) + } + if (gameSession.QuestionAmount != 0) && (gameSession.QuestionAmount+1 == gameSession.QuestionCurrent) { return gameEndHandler(c) } @@ -158,7 +165,7 @@ func startNewGameHandler(c *fiber.Ctx) error { } c.Cookie(&fiber.Cookie{ - Name: "app_flags_game_session", + Name: flagSessionCookie, Value: row.GameID.String(), //Secure: true, }) @@ -167,7 +174,7 @@ func startNewGameHandler(c *fiber.Ctx) error { } func gameEndHandler(c *fiber.Ctx) error { - gameId, err := uuid.Parse(c.Cookies("app_flags_game_session")) + gameId, err := uuid.Parse(c.Cookies(flagSessionCookie)) if err != nil { return err } @@ -183,6 +190,13 @@ func gameEndHandler(c *fiber.Ctx) error { } func stopGameHandler(c *fiber.Ctx) error { // exit game - utils.ClearCookie(c, "app_flags_game_session") + utils.ClearCookie(c, flagSessionCookie) return EntryPageHandler(c) } + +func EntryPageHandler(c *fiber.Ctx) error { + data := *web.Common(c) + data["Title"] = "tmp" + data["SupportedTags"] = supportedTags + return c.Render("apps/flags/start", data, "layouts/base") +} diff --git a/internal/apps/flags/main.go b/internal/apps/flags/main.go index a2137ca..1c9abd6 100644 --- a/internal/apps/flags/main.go +++ b/internal/apps/flags/main.go @@ -1,12 +1,8 @@ package flags -// WIP - import ( "embed" "encoding/json" - "errors" - "fmt" "git.tijl.dev/tijl/tijl.dev-core/modules/i18n" log "git.tijl.dev/tijl/tijl.dev-core/modules/logger" @@ -22,34 +18,6 @@ type CountryCode struct { Tags []string `json:"tags"` } -func filterCountriesByTags(tags []string) (error, []string) { - var result []string - - for _, tag := range tags { - isSupported := false - for _, supportedTag := range supportedTags { - if tag == supportedTag { - isSupported = true - } - } - if isSupported == false { - return errors.New(fmt.Sprintf("unsupported tag %v", tag)), result - } - } - - for _, country := range countryCodes { - for _, tag := range tags { - for _, cTag := range country.Tags { - if cTag == tag { - result = append(result, country.Code) - } - } - } - } - - return nil, result -} - //go:embed locales/* data/* var Embed embed.FS @@ -115,10 +83,3 @@ func loadData() error { return nil } - -func EntryPageHandler(c *fiber.Ctx) error { - data := *web.Common(c) - data["Title"] = "tmp" - data["SupportedTags"] = supportedTags - return c.Render("apps/flags/start", data, "layouts/base") -} diff --git a/internal/apps/flags/util.go b/internal/apps/flags/util.go index 0272234..2594474 100644 --- a/internal/apps/flags/util.go +++ b/internal/apps/flags/util.go @@ -3,6 +3,8 @@ package flags import ( "crypto/sha256" "encoding/binary" + "errors" + "fmt" "math/rand" ) @@ -36,3 +38,31 @@ func shuffleSlice(slice []string, seed string) []string { return shuffled } + +func filterCountriesByTags(tags []string) (error, []string) { + var result []string + + for _, tag := range tags { + isSupported := false + for _, supportedTag := range supportedTags { + if tag == supportedTag { + isSupported = true + } + } + if isSupported == false { + return errors.New(fmt.Sprintf("unsupported tag %v", tag)), result + } + } + + for _, country := range countryCodes { + for _, tag := range tags { + for _, cTag := range country.Tags { + if cTag == tag { + result = append(result, country.Code) + } + } + } + } + + return nil, result +} diff --git a/internal/handlers/auth.go b/internal/handlers/auth.go index 5fef704..0c8c5d7 100644 --- a/internal/handlers/auth.go +++ b/internal/handlers/auth.go @@ -5,6 +5,7 @@ import ( "git.tijl.dev/tijl/tijl.dev-core/internal/oidc" "git.tijl.dev/tijl/tijl.dev-core/internal/user" + "git.tijl.dev/tijl/tijl.dev-core/internal/utils" "git.tijl.dev/tijl/tijl.dev-core/modules/i18n" "git.tijl.dev/tijl/tijl.dev-core/modules/web" "github.com/gofiber/fiber/v2" @@ -44,7 +45,7 @@ func authHandler(c *fiber.Ctx) error { func logoutHandler(c *fiber.Ctx) error { _, err := user.GetSession(c) if err == nil { - c.ClearCookie("session", "state") + utils.ClearCookie(c, "session") return c.Redirect("/login", http.StatusOK) } else { return c.Redirect("/login", http.StatusUnauthorized)