diff --git a/groupTasks/groups/production1/index.json b/groupTasks/groups/production1/index.json index 0b8fc83..08a733e 100644 --- a/groupTasks/groups/production1/index.json +++ b/groupTasks/groups/production1/index.json @@ -11,6 +11,11 @@ "parameterName": "kiste", "type": "number", "displayName": "Nummer der Kiste" + }, + { + "parameterName": "kiste2", + "type": "textarea", + "displayName": "Nummer der zweiten Kiste yooo" } ], "tasks": [ @@ -31,6 +36,12 @@ "type": "number", "displayName": "Nummer der Kiste", "global": true + }, + { + "parameterName": "kiste2", + "type": "textarea", + "displayName": "Nummer der zweiten Kiste lul", + "global": true } ] }, diff --git a/modules/utils/globals.go b/modules/utils/globals.go index a22b25e..73bc74b 100644 --- a/modules/utils/globals.go +++ b/modules/utils/globals.go @@ -18,7 +18,8 @@ const ( MaxAvatarSize = 5 * 1024 * 1024 // 5 MB - GroupTaskLockedTime = 3 // seconds - need to be equal with web + GroupTaskLockedTime = 3 // seconds - need to be equal with web + SessionExpiresAtTime = 7 * 24 * 60 * 60 // 1 week ) // commands sent to web clients diff --git a/modules/utils/utils.go b/modules/utils/utils.go index 1c513ab..0162b6d 100644 --- a/modules/utils/utils.go +++ b/modules/utils/utils.go @@ -2,6 +2,7 @@ package utils import ( "encoding/json" + "time" "github.com/gofiber/fiber/v2" "github.com/rs/zerolog/log" @@ -21,3 +22,7 @@ func MarshalJson(v any) string { return string(json) } + +func GetSessionExpiresAtTime() time.Time { + return time.Now().Add(time.Second * SessionExpiresAtTime) +} diff --git a/routers/router/api/v1/user/auth.go b/routers/router/api/v1/user/auth.go index 337b6f8..0a3d70d 100644 --- a/routers/router/api/v1/user/auth.go +++ b/routers/router/api/v1/user/auth.go @@ -24,7 +24,7 @@ func UserLogin(c *fiber.Ctx) error { } if err := utils.ValidateStruct(body); err != nil { - log.Error().Msgf("Failed to validate body, err: %s", err) + log.Error().Msgf("Failed to validate body, err: %v", err) return c.SendStatus(fiber.StatusBadRequest) } @@ -63,7 +63,8 @@ func UserLogin(c *fiber.Ctx) error { Id: session, IdForDeletion: uuid.V4(), UserId: user.Id, - UserAgent: string(c.Context().UserAgent())}) + UserAgent: string(c.Context().UserAgent()), + ExpiresAt: utils.GetSessionExpiresAtTime()}) return c.JSON(structs.UserLoginResponse{Session: session}) } diff --git a/socketserver/hub.go b/socketserver/hub.go index 674ae45..0a00be6 100644 --- a/socketserver/hub.go +++ b/socketserver/hub.go @@ -41,6 +41,24 @@ func RunHub() { log.Debug().Msgf("clients: %d", len(cache.GetSocketClients())) log.Debug().Msgf("REGISTER CLIENT: %s", sessionId) + // check that user session is not expired + var userSession structs.UserSession + + database.DB.First(&userSession, "user_id = ?", userId) + + if time.Now().After(userSession.ExpiresAt) { + newSocketClient.SendUnauthorizedCloseMessage() + database.DB.Delete(&structs.UserSession{}, "id = ?", sessionId) + continue + } + + // update session last used time + database.DB.Model(&structs.UserSession{}).Where("id = ?", sessionId).Updates(structs.UserSession{ + LastUsed: time.Now(), + ExpiresAt: utils.GetSessionExpiresAtTime(), + }) + + // init data message var user structs.User database.DB.First(&user, "id = ?", userId) @@ -184,9 +202,14 @@ func RunHub() { case connection := <-unregister: cache.DeleteClientByConn(connection) - socketclients.UpdateUserSessionsForUser(connection.Locals("userId").(string), connection.Locals("sessionId").(string)) + userId := connection.Locals("userId").(string) + sessionid := connection.Locals("sessionId").(string) - socketclients.UpdateConnectedUsers(connection.Locals("userId").(string)) + if userId != "" && sessionid != "" { + socketclients.UpdateUserSessionsForUser(connection.Locals("userId").(string), connection.Locals("sessionId").(string)) + + socketclients.UpdateConnectedUsers(connection.Locals("userId").(string)) + } } } }