From c883a52dd2495a625b349f48844a35798d934b88 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 22 Jun 2024 18:29:41 +0800 Subject: [PATCH] :bug: Bug fixes on getting user --- pkg/internal/server/api/calls_api.go | 16 ++++++++-- .../server/api/channel_members_api.go | 31 +++++++++++++++---- pkg/internal/server/api/channels_api.go | 31 +++++++++++++++---- .../server/api/direct_channels_api.go | 6 +++- pkg/internal/server/api/index.go | 8 ++++- pkg/internal/server/api/messages_api.go | 21 ++++++++++--- pkg/internal/server/api/messages_ws.go | 2 +- pkg/internal/server/api/users_api.go | 6 +++- 8 files changed, 98 insertions(+), 23 deletions(-) diff --git a/pkg/internal/server/api/calls_api.go b/pkg/internal/server/api/calls_api.go index 793565a..5f9d0d8 100644 --- a/pkg/internal/server/api/calls_api.go +++ b/pkg/internal/server/api/calls_api.go @@ -2,6 +2,7 @@ package api import ( "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" "git.solsynth.dev/hydrogen/messaging/pkg/internal/services" "github.com/gofiber/fiber/v2" @@ -45,7 +46,10 @@ func getOngoingCall(c *fiber.Ctx) error { } func startCall(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } alias := c.Params("channel") var channel models.Channel @@ -72,7 +76,10 @@ func startCall(c *fiber.Ctx) error { } func endCall(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } alias := c.Params("channel") var channel models.Channel @@ -105,7 +112,10 @@ func endCall(c *fiber.Ctx) error { } func exchangeCallToken(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } alias := c.Params("channel") var channel models.Channel diff --git a/pkg/internal/server/api/channel_members_api.go b/pkg/internal/server/api/channel_members_api.go index 03fe070..3761035 100644 --- a/pkg/internal/server/api/channel_members_api.go +++ b/pkg/internal/server/api/channel_members_api.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" "git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" @@ -33,7 +34,10 @@ func listChannelMembers(c *fiber.Ctx) error { func getMyChannelMembership(c *fiber.Ctx) error { alias := c.Params("channel") - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } var err error var channel models.Channel @@ -54,7 +58,10 @@ func getMyChannelMembership(c *fiber.Ctx) error { } func addChannelMember(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } alias := c.Params("channel") var data struct { @@ -95,7 +102,10 @@ func addChannelMember(c *fiber.Ctx) error { } func removeChannelMember(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } alias := c.Params("channel") var data struct { @@ -137,7 +147,10 @@ func removeChannelMember(c *fiber.Ctx) error { } func editMyChannelMembership(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } alias := c.Params("channel") var data struct { @@ -183,7 +196,10 @@ func editMyChannelMembership(c *fiber.Ctx) error { } func joinChannel(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } alias := c.Params("channel") var channel models.Channel @@ -211,7 +227,10 @@ func joinChannel(c *fiber.Ctx) error { } func leaveChannel(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } alias := c.Params("channel") var channel models.Channel diff --git a/pkg/internal/server/api/channels_api.go b/pkg/internal/server/api/channels_api.go index 2fbf1bf..ec9024c 100644 --- a/pkg/internal/server/api/channels_api.go +++ b/pkg/internal/server/api/channels_api.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" "git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" @@ -28,7 +29,10 @@ func getChannel(c *fiber.Ctx) error { } func getChannelIdentity(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } alias := c.Params("channel") var err error @@ -61,7 +65,10 @@ func listChannel(c *fiber.Ctx) error { } func listOwnedChannel(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } var err error var channels []models.Channel @@ -78,7 +85,10 @@ func listOwnedChannel(c *fiber.Ctx) error { } func listAvailableChannel(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } var err error var channels []models.Channel @@ -95,7 +105,10 @@ func listAvailableChannel(c *fiber.Ctx) error { } func createChannel(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } var data struct { Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"` @@ -146,7 +159,10 @@ func createChannel(c *fiber.Ctx) error { } func editChannel(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } id, _ := c.ParamsInt("channelId", 0) var data struct { @@ -196,7 +212,10 @@ func editChannel(c *fiber.Ctx) error { } func deleteChannel(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } id, _ := c.ParamsInt("channelId", 0) tx := database.C.Where(&models.Channel{BaseModel: models.BaseModel{ID: uint(id)}}) diff --git a/pkg/internal/server/api/direct_channels_api.go b/pkg/internal/server/api/direct_channels_api.go index 230d376..5a8a45c 100644 --- a/pkg/internal/server/api/direct_channels_api.go +++ b/pkg/internal/server/api/direct_channels_api.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" "git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" @@ -11,7 +12,10 @@ import ( ) func createDirectChannel(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } var data struct { Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"` diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index dd4da17..fba6996 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -1,6 +1,7 @@ package api import ( + "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" "github.com/gofiber/contrib/websocket" "github.com/gofiber/fiber/v2" ) @@ -44,6 +45,11 @@ func MapAPIs(app *fiber.App) { channels.Post("/:channel/calls/ongoing/token", exchangeCallToken) } - api.Get("/ws", websocket.New(messageGateway)) + api.Use(func(c *fiber.Ctx) error { + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } + return c.Next() + }).Get("/ws", websocket.New(messageGateway)) } } diff --git a/pkg/internal/server/api/messages_api.go b/pkg/internal/server/api/messages_api.go index b6dc6dc..3fccb98 100644 --- a/pkg/internal/server/api/messages_api.go +++ b/pkg/internal/server/api/messages_api.go @@ -3,6 +3,7 @@ package api import ( "fmt" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" "git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts" "git.solsynth.dev/hydrogen/messaging/pkg/internal/services" @@ -11,7 +12,10 @@ import ( ) func listMessage(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } take := c.QueryInt("take", 0) offset := c.QueryInt("offset", 0) alias := c.Params("channel") @@ -42,7 +46,10 @@ func listMessage(c *fiber.Ctx) error { } func newMessage(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } alias := c.Params("channel") var data struct { @@ -121,7 +128,10 @@ func newMessage(c *fiber.Ctx) error { } func editMessage(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } alias := c.Params("channel") messageId, _ := c.ParamsInt("messageId", 0) @@ -175,7 +185,10 @@ func editMessage(c *fiber.Ctx) error { } func deleteMessage(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } alias := c.Params("channel") messageId, _ := c.ParamsInt("messageId", 0) diff --git a/pkg/internal/server/api/messages_ws.go b/pkg/internal/server/api/messages_ws.go index fcc2870..afbc714 100644 --- a/pkg/internal/server/api/messages_ws.go +++ b/pkg/internal/server/api/messages_ws.go @@ -9,7 +9,7 @@ import ( ) func messageGateway(c *websocket.Conn) { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) // Push connection services.ClientRegister(user, c) diff --git a/pkg/internal/server/api/users_api.go b/pkg/internal/server/api/users_api.go index 4d3df2c..9e1a330 100644 --- a/pkg/internal/server/api/users_api.go +++ b/pkg/internal/server/api/users_api.go @@ -2,12 +2,16 @@ package api import ( "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" "github.com/gofiber/fiber/v2" ) func getUserinfo(c *fiber.Ctx) error { - user := c.Locals("principal").(models.Account) + user := c.Locals("user").(models.Account) + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } var data models.Account if err := database.C.