setting last used and expires time for user sessions

main
alex 2023-06-11 15:55:10 +02:00
parent 98bf8197ed
commit 042bc9394a
5 changed files with 46 additions and 5 deletions

View File

@ -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
}
]
},

View File

@ -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

View File

@ -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)
}

View File

@ -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})
}

View File

@ -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))
}
}
}
}