From ccda8d607e28d1c2c4043eb917b9a4cd0ebde072 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 15 May 2023 22:03:52 +0200 Subject: [PATCH] session sign out --- modules/structs/socket.go | 1 + modules/structs/user.go | 15 ++++++++++----- modules/utils/globals.go | 6 ++++-- modules/utils/validator.go | 2 +- routers/router/api/v1/user/auth.go | 8 +++++--- routers/router/api/v1/user/session.go | 24 ++++++++++++++++++++++++ routers/router/router.go | 1 + socketclients/socketclients.go | 1 + 8 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 routers/router/api/v1/user/session.go diff --git a/modules/structs/socket.go b/modules/structs/socket.go index 6ffc4bf..50cc9f6 100644 --- a/modules/structs/socket.go +++ b/modules/structs/socket.go @@ -97,6 +97,7 @@ type UserData struct { } type UserSessionSocket struct { + IdForDeletion string UserAgent string ConnectionStatus uint8 LastUsed time.Time diff --git a/modules/structs/user.go b/modules/structs/user.go index f676e5c..11f5a2d 100644 --- a/modules/structs/user.go +++ b/modules/structs/user.go @@ -13,11 +13,12 @@ type User struct { } type UserSession struct { - Id string - UserId string - UserAgent string - LastUsed time.Time - ExpiresAt time.Time + Id string // user session which he use to connect to the websocket and api server + IdForDeletion string // this id is needed to sign out a session from website + UserId string + UserAgent string + LastUsed time.Time + ExpiresAt time.Time } type UserLoginRequest struct { @@ -33,3 +34,7 @@ type UserResponse struct { Username string Email string } + +type UserSignOutSessionRequest struct { + SessionId string +} diff --git a/modules/utils/globals.go b/modules/utils/globals.go index 445f23a..bef8c8c 100644 --- a/modules/utils/globals.go +++ b/modules/utils/globals.go @@ -9,6 +9,7 @@ const ( MaxPassword = 64 LenHeaderXAuthorization = 36 + lenHeaderXAuthorization = "36" LenUserId = 36 HeaderXAuthorization = "X-Authorization" @@ -37,7 +38,8 @@ const ( var ( generalRules = map[string]string{ - "Username": "required,min=" + minUsername + ",max=" + maxUsername, - "Password": "required", // length is checked later because sent in base64 + "Username": "required,min=" + minUsername + ",max=" + maxUsername, + "Password": "required", // length is checked later because sent in base64 + "SessionId": "required,len" + lenHeaderXAuthorization, } ) diff --git a/modules/utils/validator.go b/modules/utils/validator.go index 402cef3..cc4361e 100644 --- a/modules/utils/validator.go +++ b/modules/utils/validator.go @@ -30,5 +30,5 @@ func ValidateStruct(event interface{}) []*ErrorResponse { } func ValidatorInit() { - Validate.RegisterStructValidationMapRules(generalRules, structs.UserLoginRequest{}) + Validate.RegisterStructValidationMapRules(generalRules, structs.UserLoginRequest{}, structs.UserSignOutSessionRequest{}) } diff --git a/routers/router/api/v1/user/auth.go b/routers/router/api/v1/user/auth.go index bc6e451..978ccda 100644 --- a/routers/router/api/v1/user/auth.go +++ b/routers/router/api/v1/user/auth.go @@ -11,6 +11,7 @@ import ( "github.com/gofiber/fiber/v2" "github.com/rs/zerolog/log" + "github.com/savsgio/gotils/uuid" "golang.org/x/crypto/bcrypt" ) @@ -59,9 +60,10 @@ func UserLogin(c *fiber.Ctx) error { } database.DB.Create(&structs.UserSession{ - Id: session, - UserId: user.Id, - UserAgent: string(c.Context().UserAgent())}) + Id: session, + IdForDeletion: uuid.V4(), + UserId: user.Id, + UserAgent: string(c.Context().UserAgent())}) log.Info().Msg("Login user: " + user.Email) diff --git a/routers/router/api/v1/user/session.go b/routers/router/api/v1/user/session.go new file mode 100644 index 0000000..ad06035 --- /dev/null +++ b/routers/router/api/v1/user/session.go @@ -0,0 +1,24 @@ +package user + +import ( + "janex/admin-dashboard-backend/modules/structs" + "janex/admin-dashboard-backend/modules/utils" + + "github.com/gofiber/fiber/v2" + "github.com/rs/zerolog/log" +) + +func SignOutSession(c *fiber.Ctx) error { + var params structs.UserSignOutSessionRequest + + if err := c.ParamsParser(¶ms); err != nil { + log.Error().Msg("Failed to parse params, err: " + err.Error()) + return c.SendStatus(fiber.StatusBadRequest) + } + + log.Debug().Msgf("params %s", params.SessionId) + + log.Debug().Msgf("userId %s", utils.GetXAuhorizationHeader(c)) + + return c.SendStatus(fiber.StatusOK) +} diff --git a/routers/router/router.go b/routers/router/router.go index 1454530..522e2f4 100644 --- a/routers/router/router.go +++ b/routers/router/router.go @@ -15,6 +15,7 @@ func SetupRoutes(app *fiber.App) { u := v1.Group("/user") u.Post("/auth/login", user.UserLogin) u.Delete("/auth/logout", user.UserLogout) + u.Delete("/session/:sessionId", user.SignOutSession) } func userSessionValidation(c *fiber.Ctx) error { diff --git a/socketclients/socketclients.go b/socketclients/socketclients.go index c3c36f7..7307cf6 100644 --- a/socketclients/socketclients.go +++ b/socketclients/socketclients.go @@ -49,6 +49,7 @@ func GetUserSessions(userId string) []structs.UserSessionSocket { for _, userSession := range userSessions { userSessionsSocket = append(userSessionsSocket, structs.UserSessionSocket{ + IdForDeletion: userSession.IdForDeletion, UserAgent: userSession.UserAgent, ConnectionStatus: isUserSessionConnected(userSession.Id, socketClients), LastUsed: userSession.LastUsed,