From 2fcc784bc4b435522f19d44f6c9aad82f63fd084 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 31 Oct 2024 00:17:53 +0800 Subject: [PATCH] :rewind: Revert ":recycle: Move models.Account to sec.UserInfo" for a better solution This reverts commit 8fbb7960 --- go.sum | 10 ++++ pkg/internal/gap/server.go | 11 ++-- pkg/internal/grpc/realms.go | 4 +- pkg/internal/http/admin/notify_api.go | 9 ++-- pkg/internal/http/admin/permissions_api.go | 7 ++- pkg/internal/http/admin/users_api.go | 5 +- pkg/internal/http/api/accounts_api.go | 22 +++----- pkg/internal/http/api/avatar_api.go | 34 +++++------- pkg/internal/http/api/bot_token_api.go | 21 +++----- pkg/internal/http/api/bots_api.go | 21 +++----- pkg/internal/http/api/index.go | 3 ++ pkg/internal/http/api/notifications_api.go | 23 +++------ pkg/internal/http/api/notify_api.go | 5 +- pkg/internal/http/api/oauth_api.go | 17 ++---- pkg/internal/http/api/preferences_api.go | 17 +++--- pkg/internal/http/api/realm_members_api.go | 15 +++--- pkg/internal/http/api/realms_api.go | 17 +++--- pkg/internal/http/api/relationships_api.go | 60 ++++++---------------- pkg/internal/http/api/reports_api.go | 10 ++-- pkg/internal/http/api/security_api.go | 7 ++- pkg/internal/http/api/sign_api.go | 11 ++-- pkg/internal/http/api/statuses_api.go | 26 ++++------ pkg/internal/http/exts/auth.go | 4 +- pkg/internal/http/server.go | 2 - pkg/internal/services/bots.go | 4 +- pkg/internal/services/events.go | 4 +- pkg/internal/services/preferences.go | 18 +++---- pkg/internal/services/realms.go | 24 ++++----- pkg/internal/services/relationships.go | 8 +-- pkg/internal/services/reports.go | 10 ++-- pkg/internal/services/sign.go | 14 ++--- pkg/internal/services/statuses.go | 14 ++--- pkg/internal/services/ticket.go | 2 +- pkg/main.go | 12 ----- settings.toml | 7 +-- 35 files changed, 192 insertions(+), 286 deletions(-) diff --git a/go.sum b/go.sum index b148a32..2424d37 100644 --- a/go.sum +++ b/go.sum @@ -33,6 +33,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +git.solsynth.dev/hypernet/nexus v0.0.0-20241024155238-421834ae5c4a h1:yl2cnfFvvgOGwr9qoWu4QQHVpVPCI9f1idyaKt2gXxo= +git.solsynth.dev/hypernet/nexus v0.0.0-20241024155238-421834ae5c4a/go.mod h1:68l4+CkuG3sz9aiUhbN5tq3YHrt6KUNK9FIWaGuWCdc= git.solsynth.dev/hypernet/nexus v0.0.0-20241026162044-3d487fa48baf h1:Fil1ASTvxmqFySD3OoptC7uRXEYpWLwLfjlkWZNNwVQ= git.solsynth.dev/hypernet/nexus v0.0.0-20241026162044-3d487fa48baf/go.mod h1:fXQsHXGio+7/0U95IitKF07wS4yTdCMp5ms8wpFBwVI= git.solsynth.dev/hypernet/nexus v0.0.0-20241030153633-427c83e38fe4 h1:UTptbxd2b6RFKu0tr2RmQjNnqde/M0JVO4nZk7ntUYo= @@ -258,10 +260,16 @@ github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfE github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA= +github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw= +github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A= github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs= github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= @@ -793,6 +801,8 @@ gorm.io/datatypes v1.2.4 h1:uZmGAcK/QZ0uyfCuVg0VQY1ZmV9h1fuG0tMwKByO1z4= gorm.io/datatypes v1.2.4/go.mod h1:f4BsLcFAX67szSv8svwLRjklArSHAvHLeE3pXAS5DZI= gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo= gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM= +gorm.io/driver/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo= +gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0= gorm.io/driver/postgres v1.5.9 h1:DkegyItji119OlcaLjqN11kHoUgZ/j13E0jkJZgD6A8= gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI= gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU= diff --git a/pkg/internal/gap/server.go b/pkg/internal/gap/server.go index 9996e32..ce830f2 100644 --- a/pkg/internal/gap/server.go +++ b/pkg/internal/gap/server.go @@ -2,21 +2,18 @@ package gap import ( "fmt" - "strings" - "git.solsynth.dev/hypernet/nexus/pkg/nex" "git.solsynth.dev/hypernet/nexus/pkg/proto" "git.solsynth.dev/hypernet/pusher/pkg/pushkit/pushcon" "github.com/rs/zerolog/log" "github.com/samber/lo" + "strings" "github.com/spf13/viper" ) -var ( - Nx *nex.Conn - Px *pushcon.Conn -) +var Nx *nex.Conn +var Px *pushcon.Conn func InitializeToNexus() error { grpcBind := strings.SplitN(viper.GetString("grpc_bind"), ":", 2) @@ -33,7 +30,7 @@ func InitializeToNexus() error { Type: nex.ServiceTypeAuth, Label: "Passport", GrpcAddr: grpcOutbound, - HttpAddr: lo.ToPtr("http://" + httpOutbound + "/api"), + HttpAddr: lo.ToPtr("http://" + httpOutbound), }) if err == nil { go func() { diff --git a/pkg/internal/grpc/realms.go b/pkg/internal/grpc/realms.go index 331bf2d..fb7490d 100644 --- a/pkg/internal/grpc/realms.go +++ b/pkg/internal/grpc/realms.go @@ -45,7 +45,7 @@ func (v *App) ListAvailableRealm(ctx context.Context, request *proto.LookupUserR if err != nil { return nil, fmt.Errorf("unable to find target account: %v", err) } - realms, err := services.ListAvailableRealm(account.ID) + realms, err := services.ListAvailableRealm(account) if err != nil { return nil, err } @@ -77,7 +77,7 @@ func (v *App) ListOwnedRealm(ctx context.Context, request *proto.LookupUserRealm if err != nil { return nil, fmt.Errorf("unable to find target account: %v", err) } - realms, err := services.ListOwnedRealm(account.ID) + realms, err := services.ListOwnedRealm(account) if err != nil { return nil, err } diff --git a/pkg/internal/http/admin/notify_api.go b/pkg/internal/http/admin/notify_api.go index 6232452..02fb0b1 100644 --- a/pkg/internal/http/admin/notify_api.go +++ b/pkg/internal/http/admin/notify_api.go @@ -5,7 +5,6 @@ import ( "git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "github.com/gofiber/fiber/v2" "github.com/rs/zerolog/log" ) @@ -28,13 +27,13 @@ func notifyAllUser(c *fiber.Ctx) error { if err := exts.EnsureGrantedPerm(c, "AdminNotifyAll", true); err != nil { return err } - operator := c.Locals("user").(*sec.UserInfo) + operator := c.Locals("user").(models.Account) var users []models.Account if err := database.C.Find(&users).Error; err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } else { - services.AddAuditRecord(operator.ID, "notify.all", c.IP(), c.Get(fiber.HeaderUserAgent), map[string]any{ + services.AddAuditRecord(operator, "notify.all", c.IP(), c.Get(fiber.HeaderUserAgent), map[string]any{ "payload": data, }) } @@ -86,13 +85,13 @@ func notifyOneUser(c *fiber.Ctx) error { if err := exts.EnsureGrantedPerm(c, "AdminNotifyAll", true); err != nil { return err } - operator := c.Locals("user").(*sec.UserInfo) + operator := c.Locals("user").(models.Account) var user models.Account if err := database.C.Where("id = ?", data.UserID).First(&user).Error; err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } else { - services.AddAuditRecord(operator.ID, "notify.one", c.IP(), c.Get(fiber.HeaderUserAgent), map[string]any{ + services.AddAuditRecord(operator, "notify.one", c.IP(), c.Get(fiber.HeaderUserAgent), map[string]any{ "user_id": user.ID, "payload": data, }) diff --git a/pkg/internal/http/admin/permissions_api.go b/pkg/internal/http/admin/permissions_api.go index c0db9ab..927edb4 100644 --- a/pkg/internal/http/admin/permissions_api.go +++ b/pkg/internal/http/admin/permissions_api.go @@ -2,11 +2,10 @@ package admin import ( "fmt" - "git.solsynth.dev/hydrogen/passport/pkg/authkit/models" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" + "git.solsynth.dev/hydrogen/passport/pkg/internal/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" "github.com/gofiber/fiber/v2" ) @@ -17,7 +16,7 @@ func editUserPermission(c *fiber.Ctx) error { if err := exts.EnsureGrantedPerm(c, "AdminUserPermission", true); err != nil { return err } - operator := c.Locals("user").(*sec.UserInfo) + operator := c.Locals("user").(models.Account) var data struct { PermNodes map[string]any `json:"perm_nodes" validate:"required"` @@ -40,7 +39,7 @@ func editUserPermission(c *fiber.Ctx) error { if err := database.C.Save(&user).Error; err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } else { - services.AddAuditRecord(operator.ID, "user.permissions.edit", c.IP(), c.Get(fiber.HeaderUserAgent), map[string]any{ + services.AddAuditRecord(operator, "user.permissions.edit", c.IP(), c.Get(fiber.HeaderUserAgent), map[string]any{ "user_id": user.ID, "previous_permissions": prev, "new_permissions": data.PermNodes, diff --git a/pkg/internal/http/admin/users_api.go b/pkg/internal/http/admin/users_api.go index 70d4807..2f58ece 100644 --- a/pkg/internal/http/admin/users_api.go +++ b/pkg/internal/http/admin/users_api.go @@ -6,7 +6,6 @@ import ( "git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "github.com/gofiber/fiber/v2" ) @@ -54,7 +53,7 @@ func forceConfirmAccount(c *fiber.Ctx) error { if err := exts.EnsureGrantedPerm(c, "AdminUserConfirmation", true); err != nil { return err } - operator := c.Locals("user").(*sec.UserInfo) + operator := c.Locals("user").(models.Account) var user models.Account if err := database.C.Where("id = ?", userId).First(&user).Error; err != nil { @@ -64,7 +63,7 @@ func forceConfirmAccount(c *fiber.Ctx) error { if err := services.ForceConfirmAccount(user); err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } else { - services.AddAuditRecord(operator.ID, "user.confirm", c.IP(), c.Get(fiber.HeaderUserAgent), map[string]any{ + services.AddAuditRecord(operator, "user.confirm", c.IP(), c.Get(fiber.HeaderUserAgent), map[string]any{ "user_id": user.ID, }) } diff --git a/pkg/internal/http/api/accounts_api.go b/pkg/internal/http/api/accounts_api.go index ca0d54d..fcae350 100644 --- a/pkg/internal/http/api/accounts_api.go +++ b/pkg/internal/http/api/accounts_api.go @@ -2,8 +2,6 @@ package api import ( "fmt" - "git.solsynth.dev/hydrogen/passport/pkg/authkit/models" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "strconv" "strings" "time" @@ -11,6 +9,7 @@ import ( "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" "git.solsynth.dev/hydrogen/passport/pkg/internal/database" + "git.solsynth.dev/hydrogen/passport/pkg/internal/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" "github.com/gofiber/fiber/v2" jsoniter "github.com/json-iterator/go" @@ -49,7 +48,7 @@ func getUserinfo(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var data models.Account if err := database.C. @@ -60,7 +59,7 @@ func getUserinfo(c *fiber.Ctx) error { First(&data).Error; err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } else { - data.PermNodes = user.PermNodes + data.PermNodes = c.Locals("permissions").(map[string]any) } var resp fiber.Map @@ -85,7 +84,7 @@ func getEvents(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) take := c.QueryInt("take", 0) offset := c.QueryInt("offset", 0) @@ -117,7 +116,7 @@ func editUserinfo(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var data struct { Nick string `json:"nick" validate:"required"` @@ -223,16 +222,11 @@ func requestDeleteAccount(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) - var account models.Account - if err := database.C.Where("id = ?", user.ID).First(&account).Error; err != nil { - return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("account was not found: %v", err)) - } - - if err := services.CheckAbleToDeleteAccount(account); err != nil { + if err := services.CheckAbleToDeleteAccount(user); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) - } else if err = services.RequestDeleteAccount(account); err != nil { + } else if err = services.RequestDeleteAccount(user); err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } diff --git a/pkg/internal/http/api/avatar_api.go b/pkg/internal/http/api/avatar_api.go index 2aecaff..7be6833 100644 --- a/pkg/internal/http/api/avatar_api.go +++ b/pkg/internal/http/api/avatar_api.go @@ -1,12 +1,10 @@ package api import ( - "fmt" - "git.solsynth.dev/hydrogen/passport/pkg/authkit/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" + "git.solsynth.dev/hydrogen/passport/pkg/internal/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "github.com/gofiber/fiber/v2" "strconv" ) @@ -15,7 +13,7 @@ func setAvatar(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var data struct { AttachmentID string `json:"attachment" validate:"required"` @@ -25,7 +23,9 @@ func setAvatar(c *fiber.Ctx) error { return err } - if err := database.C.Where("id = ?", user.ID).Updates(&models.Account{Avatar: &data.AttachmentID}).Error; err != nil { + user.Avatar = &data.AttachmentID + + if err := database.C.Save(&user).Error; err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } else { services.AddEvent(user.ID, "profile.edit.avatar", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent)) @@ -39,7 +39,7 @@ func setBanner(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var data struct { AttachmentID string `json:"attachment" validate:"required"` @@ -49,7 +49,9 @@ func setBanner(c *fiber.Ctx) error { return err } - if err := database.C.Where("id = ?", user.ID).Updates(&models.Account{Banner: &data.AttachmentID}).Error; err != nil { + user.Banner = &data.AttachmentID + + if err := database.C.Save(&user).Error; err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } else { services.AddEvent(user.ID, "profile.edit.banner", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent)) @@ -63,14 +65,9 @@ func getAvatar(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) - var account models.Account - if err := database.C.Where("id = ?", user.ID).First(&account).Error; err != nil { - return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("account was not found: %v", err)) - } - - if content := account.GetAvatar(); content == nil { + if content := user.GetAvatar(); content == nil { return c.SendStatus(fiber.StatusNotFound) } else { return c.Redirect(*content, fiber.StatusFound) @@ -81,14 +78,9 @@ func getBanner(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) - var account models.Account - if err := database.C.Where("id = ?", user.ID).First(&account).Error; err != nil { - return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("account was not found: %v", err)) - } - - if content := account.GetBanner(); content == nil { + if content := user.GetBanner(); content == nil { return c.SendStatus(fiber.StatusNotFound) } else { return c.Redirect(*content, fiber.StatusFound) diff --git a/pkg/internal/http/api/bot_token_api.go b/pkg/internal/http/api/bot_token_api.go index 3d280aa..84e35e0 100644 --- a/pkg/internal/http/api/bot_token_api.go +++ b/pkg/internal/http/api/bot_token_api.go @@ -6,7 +6,6 @@ import ( "git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "github.com/gofiber/fiber/v2" "gorm.io/gorm" ) @@ -15,7 +14,7 @@ func listBotKeys(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var tx *gorm.DB @@ -51,7 +50,7 @@ func getBotKey(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) id, _ := c.ParamsInt("id", 0) @@ -70,7 +69,7 @@ func createBotKey(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var data struct { Name string `json:"name" validate:"required"` @@ -83,7 +82,7 @@ func createBotKey(c *fiber.Ctx) error { return err } - var target models.Account + target := user botId, _ := c.ParamsInt("botId", 0) if botId > 0 { @@ -92,12 +91,6 @@ func createBotKey(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("bot not found: %v", err)) } target = bot - } else { - var account models.Account - if err := database.C.Where("id = ?", user.ID).First(&account).Error; err != nil { - return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("account not found: %v", err)) - } - target = account } key, err := services.NewApiKey(target, models.ApiKey{ @@ -116,7 +109,7 @@ func editBotKey(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var data struct { Name string `json:"name" validate:"required"` @@ -163,7 +156,7 @@ func rollBotKey(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) id, _ := c.ParamsInt("id", 0) @@ -196,7 +189,7 @@ func revokeBotKey(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) id, _ := c.ParamsInt("id", 0) diff --git a/pkg/internal/http/api/bots_api.go b/pkg/internal/http/api/bots_api.go index a33f7ac..ea6be10 100644 --- a/pkg/internal/http/api/bots_api.go +++ b/pkg/internal/http/api/bots_api.go @@ -1,12 +1,10 @@ package api import ( - "fmt" - "git.solsynth.dev/hydrogen/passport/pkg/authkit/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" + "git.solsynth.dev/hydrogen/passport/pkg/internal/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "github.com/gofiber/fiber/v2" "github.com/samber/lo" "gorm.io/datatypes" @@ -18,7 +16,7 @@ func listBots(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) tx := database.C.Where("automated_id = ?", user.ID) @@ -43,9 +41,9 @@ func createBot(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) - cnt, _ := services.GetBotCount(user.ID) + cnt, _ := services.GetBotCount(user) if err := exts.EnsureGrantedPerm(c, "CreateBots", cnt+1); err != nil { return err } @@ -67,14 +65,7 @@ func createBot(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, "invalid bot nick, length requires 4 to 24") } - var account models.Account - if err := database.C.Where(&models.Account{ - Name: data.Name, - }).First(&account).Error; err != nil { - return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("account was not found: %v", err)) - } - - bot, err := services.NewBot(account, models.Account{ + bot, err := services.NewBot(user, models.Account{ Name: data.Name, Nick: data.Nick, Description: data.Description, @@ -93,7 +84,7 @@ func deleteBot(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) id, _ := c.ParamsInt("botId", 0) diff --git a/pkg/internal/http/api/index.go b/pkg/internal/http/api/index.go index e10e8f5..78b0365 100644 --- a/pkg/internal/http/api/index.go +++ b/pkg/internal/http/api/index.go @@ -18,6 +18,9 @@ func MapAPIs(app *fiber.App, baseURL string) { notify := api.Group("/notifications").Name("Notifications API") { + // Deprecated, use /subscription instead, will be removed in the future + notify.Post("/subscribe", addNotifySubscriber) + notify.Get("/", getNotifications) notify.Get("/subscription", getNotifySubscriber) notify.Post("/subscription", addNotifySubscriber) diff --git a/pkg/internal/http/api/notifications_api.go b/pkg/internal/http/api/notifications_api.go index 1657dac..b6ddb1c 100644 --- a/pkg/internal/http/api/notifications_api.go +++ b/pkg/internal/http/api/notifications_api.go @@ -1,12 +1,10 @@ package api import ( - "fmt" - "git.solsynth.dev/hydrogen/passport/pkg/authkit/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" + "git.solsynth.dev/hydrogen/passport/pkg/internal/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "github.com/gofiber/fiber/v2" "github.com/samber/lo" "strconv" @@ -20,7 +18,7 @@ func getNotifications(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) tx := database.C.Where(&models.Notification{AccountID: user.ID}).Model(&models.Notification{}) @@ -49,7 +47,7 @@ func markNotificationRead(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) id, _ := c.ParamsInt("notificationId", 0) if err := exts.EnsureAuthenticated(c); err != nil { @@ -78,7 +76,7 @@ func markNotificationReadBatch(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var data struct { MessageIDs []uint `json:"messages"` @@ -102,7 +100,7 @@ func getNotifySubscriber(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var subscribers []models.NotificationSubscriber if err := database.C.Where(&models.NotificationSubscriber{ @@ -118,7 +116,7 @@ func addNotifySubscriber(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var data struct { Provider string `json:"provider" validate:"required"` @@ -139,13 +137,8 @@ func addNotifySubscriber(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusOK) } - var account models.Account - if err := database.C.Where("id = ?", user.ID).First(&account).Error; err != nil { - return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("account was not found: %v", err)) - } - subscriber, err := services.AddNotifySubscriber( - account, + user, data.Provider, data.DeviceID, data.DeviceToken, @@ -163,7 +156,7 @@ func removeNotifySubscriber(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) device := c.Params("deviceId") diff --git a/pkg/internal/http/api/notify_api.go b/pkg/internal/http/api/notify_api.go index 49a715f..03b73c9 100644 --- a/pkg/internal/http/api/notify_api.go +++ b/pkg/internal/http/api/notify_api.go @@ -2,10 +2,9 @@ package api import ( "fmt" - "git.solsynth.dev/hydrogen/passport/pkg/authkit/models" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" + "git.solsynth.dev/hydrogen/passport/pkg/internal/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" "github.com/gofiber/fiber/v2" ) @@ -14,7 +13,7 @@ func notifyUser(c *fiber.Ctx) error { if err := exts.EnsureGrantedPerm(c, "DevNotifyUser", true); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var data struct { ClientID string `json:"client_id" validate:"required"` diff --git a/pkg/internal/http/api/oauth_api.go b/pkg/internal/http/api/oauth_api.go index 330a0d9..04cd938 100755 --- a/pkg/internal/http/api/oauth_api.go +++ b/pkg/internal/http/api/oauth_api.go @@ -1,15 +1,13 @@ package api import ( - "fmt" - "git.solsynth.dev/hydrogen/passport/pkg/authkit/models" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "strings" "time" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" "git.solsynth.dev/hydrogen/passport/pkg/internal/database" + "git.solsynth.dev/hydrogen/passport/pkg/internal/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" "github.com/gofiber/fiber/v2" "github.com/samber/lo" @@ -33,7 +31,7 @@ func tryAuthorizeThirdClient(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var ticket models.AuthTicket if err := database.C.Where(&models.AuthTicket{ @@ -74,23 +72,18 @@ func authorizeThirdClient(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var client models.ThirdClient if err := database.C.Where(&models.ThirdClient{Alias: id}).First(&client).Error; err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) } - var account models.Account - if err := database.C.Where("id = ?", user.ID).First(&account).Error; err != nil { - return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("account was not found: %v", err)) - } - switch response { case "code": // OAuth Authorization Mode ticket, err := services.NewOauthTicket( - account, + user, client, strings.Split(scope, " "), []string{services.InternalTokenAudience, client.Alias}, @@ -111,7 +104,7 @@ func authorizeThirdClient(c *fiber.Ctx) error { case "token": // OAuth Implicit Mode ticket, err := services.NewOauthTicket( - account, + user, client, strings.Split(scope, " "), []string{services.InternalTokenAudience, client.Alias}, diff --git a/pkg/internal/http/api/preferences_api.go b/pkg/internal/http/api/preferences_api.go index 5ed62b5..1c62be7 100644 --- a/pkg/internal/http/api/preferences_api.go +++ b/pkg/internal/http/api/preferences_api.go @@ -4,7 +4,6 @@ import ( "git.solsynth.dev/hydrogen/passport/pkg/authkit/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "github.com/gofiber/fiber/v2" ) @@ -12,9 +11,9 @@ func getAuthPreference(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) - cfg, err := services.GetAuthPreference(user.ID) + cfg, err := services.GetAuthPreference(user) if err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) } @@ -26,14 +25,14 @@ func updateAuthPreference(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var data models.AuthConfig if err := exts.BindAndValidate(c, &data); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } - cfg, err := services.UpdateAuthPreference(user.ID, data) + cfg, err := services.UpdateAuthPreference(user, data) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { @@ -47,8 +46,8 @@ func getNotificationPreference(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) - notification, err := services.GetNotificationPreference(user.ID) + user := c.Locals("user").(models.Account) + notification, err := services.GetNotificationPreference(user) if err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) } @@ -60,7 +59,7 @@ func updateNotificationPreference(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var data struct { Config map[string]bool `json:"config"` @@ -70,7 +69,7 @@ func updateNotificationPreference(c *fiber.Ctx) error { return err } - notification, err := services.UpdateNotificationPreference(user.ID, data.Config) + notification, err := services.UpdateNotificationPreference(user, data.Config) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { diff --git a/pkg/internal/http/api/realm_members_api.go b/pkg/internal/http/api/realm_members_api.go index 0ebe6c2..57903c6 100644 --- a/pkg/internal/http/api/realm_members_api.go +++ b/pkg/internal/http/api/realm_members_api.go @@ -5,7 +5,6 @@ import ( "git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "github.com/gofiber/fiber/v2" ) @@ -26,7 +25,7 @@ func getMyRealmMember(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) if realm, err := services.GetRealmWithAlias(alias); err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) @@ -41,7 +40,7 @@ func addRealmMember(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) alias := c.Params("realm") var data struct { @@ -64,7 +63,7 @@ func addRealmMember(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusNotFound, err.Error()) } - if err := services.AddRealmMember(user.ID, account, realm); err != nil { + if err := services.AddRealmMember(user, account, realm); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { return c.SendStatus(fiber.StatusOK) @@ -75,7 +74,7 @@ func removeRealmMember(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) alias := c.Params("realm") var data struct { @@ -98,7 +97,7 @@ func removeRealmMember(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusNotFound, err.Error()) } - if err := services.RemoveRealmMember(user.ID, account, realm); err != nil { + if err := services.RemoveRealmMember(user, account, realm); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { return c.SendStatus(fiber.StatusOK) @@ -109,7 +108,7 @@ func leaveRealm(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) alias := c.Params("realm") realm, err := services.GetRealmWithAlias(alias) @@ -126,7 +125,7 @@ func leaveRealm(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusNotFound, err.Error()) } - if err := services.RemoveRealmMember(user.ID, account, realm); err != nil { + if err := services.RemoveRealmMember(user, account, realm); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { return c.SendStatus(fiber.StatusOK) diff --git a/pkg/internal/http/api/realms_api.go b/pkg/internal/http/api/realms_api.go index 1a3168e..6d47382 100644 --- a/pkg/internal/http/api/realms_api.go +++ b/pkg/internal/http/api/realms_api.go @@ -5,7 +5,6 @@ import ( "git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "github.com/gofiber/fiber/v2" "strconv" ) @@ -32,8 +31,8 @@ func listOwnedRealm(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) - if realms, err := services.ListOwnedRealm(user.ID); err != nil { + user := c.Locals("user").(models.Account) + if realms, err := services.ListOwnedRealm(user); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { return c.JSON(realms) @@ -44,8 +43,8 @@ func listAvailableRealm(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) - if realms, err := services.ListAvailableRealm(user.ID); err != nil { + user := c.Locals("user").(models.Account) + if realms, err := services.ListAvailableRealm(user); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { return c.JSON(realms) @@ -56,7 +55,7 @@ func createRealm(c *fiber.Ctx) error { if err := exts.EnsureGrantedPerm(c, "CreateRealms", true); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var data struct { Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"` @@ -83,7 +82,7 @@ func createRealm(c *fiber.Ctx) error { IsPublic: data.IsPublic, IsCommunity: data.IsCommunity, AccountID: user.ID, - }, user.ID) + }, user) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) @@ -98,7 +97,7 @@ func editRealm(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) id, _ := c.ParamsInt("realmId", 0) var data struct { @@ -147,7 +146,7 @@ func deleteRealm(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) id, _ := c.ParamsInt("realmId", 0) var realm models.Realm diff --git a/pkg/internal/http/api/relationships_api.go b/pkg/internal/http/api/relationships_api.go index 9dc873f..bc5be5c 100644 --- a/pkg/internal/http/api/relationships_api.go +++ b/pkg/internal/http/api/relationships_api.go @@ -1,11 +1,9 @@ package api import ( - "git.solsynth.dev/hydrogen/passport/pkg/authkit/models" - "git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" + "git.solsynth.dev/hydrogen/passport/pkg/internal/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "github.com/gofiber/fiber/v2" "strconv" ) @@ -14,17 +12,17 @@ func listRelationship(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) status := c.QueryInt("status", -1) var err error var friends []models.AccountRelationship if status < 0 { - if friends, err = services.ListAllRelationship(user.ID); err != nil { + if friends, err = services.ListAllRelationship(user); err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } } else { - if friends, err = services.ListRelationshipWithFilter(user.ID, models.RelationshipStatus(status)); err != nil { + if friends, err = services.ListRelationshipWithFilter(user, models.RelationshipStatus(status)); err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } } @@ -36,7 +34,7 @@ func getRelationship(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) relatedId, _ := c.ParamsInt("relatedId", 0) related, err := services.GetAccount(uint(relatedId)) @@ -55,7 +53,7 @@ func editRelationship(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) relatedId, _ := c.ParamsInt("relatedId", 0) var data struct { @@ -87,7 +85,7 @@ func deleteRelationship(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) relatedId, _ := c.ParamsInt("relatedId", 0) related, err := services.GetAccount(uint(relatedId)) @@ -113,7 +111,7 @@ func makeFriendship(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) relatedName := c.Query("related") relatedId, _ := c.ParamsInt("relatedId", 0) @@ -133,14 +131,7 @@ func makeFriendship(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, "must one of username or user id") } - var account models.Account - if err := database.C.Where(&models.Account{ - BaseModel: models.BaseModel{ID: user.ID}, - }).First(&account).Error; err != nil { - return fiber.NewError(fiber.StatusNotFound, err.Error()) - } - - friend, err := services.NewFriend(account, related) + friend, err := services.NewFriend(user, related) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { @@ -153,7 +144,7 @@ func makeBlockship(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) relatedName := c.Query("related") relatedId, _ := c.ParamsInt("relatedId", 0) @@ -173,14 +164,7 @@ func makeBlockship(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, "must one of username or user id") } - var account models.Account - if err := database.C.Where(&models.Account{ - BaseModel: models.BaseModel{ID: user.ID}, - }).First(&account).Error; err != nil { - return fiber.NewError(fiber.StatusNotFound, err.Error()) - } - - friend, err := services.NewBlockship(account, related) + friend, err := services.NewBlockship(user, related) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { @@ -193,22 +177,15 @@ func acceptFriend(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) relatedId, _ := c.ParamsInt("relatedId", 0) - var account models.Account - if err := database.C.Where(&models.Account{ - BaseModel: models.BaseModel{ID: user.ID}, - }).First(&account).Error; err != nil { - return fiber.NewError(fiber.StatusNotFound, err.Error()) - } - related, err := services.GetAccount(uint(relatedId)) if err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) } - if err := services.HandleFriend(account, related, true); err != nil { + if err := services.HandleFriend(user, related, true); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { services.AddEvent(user.ID, "relationships.friends.accept", strconv.Itoa(relatedId), c.IP(), c.Get(fiber.HeaderUserAgent)) @@ -220,7 +197,7 @@ func declineFriend(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) relatedId, _ := c.ParamsInt("relatedId", 0) related, err := services.GetAccount(uint(relatedId)) @@ -228,14 +205,7 @@ func declineFriend(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusNotFound, err.Error()) } - var account models.Account - if err := database.C.Where(&models.Account{ - BaseModel: models.BaseModel{ID: user.ID}, - }).First(&account).Error; err != nil { - return fiber.NewError(fiber.StatusNotFound, err.Error()) - } - - if err := services.HandleFriend(account, related, false); err != nil { + if err := services.HandleFriend(user, related, false); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { services.AddEvent(user.ID, "relationships.friends.decline", strconv.Itoa(relatedId), c.IP(), c.Get(fiber.HeaderUserAgent)) diff --git a/pkg/internal/http/api/reports_api.go b/pkg/internal/http/api/reports_api.go index 133ac40..1cae138 100644 --- a/pkg/internal/http/api/reports_api.go +++ b/pkg/internal/http/api/reports_api.go @@ -2,8 +2,8 @@ package api import ( "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" + "git.solsynth.dev/hydrogen/passport/pkg/internal/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "github.com/gofiber/fiber/v2" ) @@ -11,9 +11,9 @@ func listAbuseReports(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) - reports, err := services.ListAbuseReport(user.ID) + reports, err := services.ListAbuseReport(user) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } @@ -58,7 +58,7 @@ func createAbuseReport(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var data struct { Resource string `json:"resource" validate:"required"` @@ -69,7 +69,7 @@ func createAbuseReport(c *fiber.Ctx) error { return err } - report, err := services.NewAbuseReport(data.Resource, data.Reason, user.ID) + report, err := services.NewAbuseReport(data.Resource, data.Reason, user) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } diff --git a/pkg/internal/http/api/security_api.go b/pkg/internal/http/api/security_api.go index fd65746..fdb624b 100644 --- a/pkg/internal/http/api/security_api.go +++ b/pkg/internal/http/api/security_api.go @@ -1,10 +1,9 @@ package api import ( - "git.solsynth.dev/hydrogen/passport/pkg/authkit/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" + "git.solsynth.dev/hydrogen/passport/pkg/internal/models" "github.com/gofiber/fiber/v2" ) @@ -12,7 +11,7 @@ func getTickets(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) take := c.QueryInt("take", 0) offset := c.QueryInt("offset", 0) @@ -44,7 +43,7 @@ func killTicket(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) id, _ := c.ParamsInt("ticketId", 0) if err := database.C.Delete(&models.AuthTicket{}, &models.AuthTicket{ diff --git a/pkg/internal/http/api/sign_api.go b/pkg/internal/http/api/sign_api.go index 6197f3e..4641b96 100644 --- a/pkg/internal/http/api/sign_api.go +++ b/pkg/internal/http/api/sign_api.go @@ -5,7 +5,6 @@ import ( "git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "github.com/gofiber/fiber/v2" "strconv" ) @@ -17,7 +16,7 @@ func listDailySignRecord(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var count int64 if err := database.C. @@ -82,9 +81,9 @@ func getTodayDailySign(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) - if record, err := services.GetTodayDailySign(user.ID); err != nil { + if record, err := services.GetTodayDailySign(user); err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) } else { return c.JSON(record) @@ -95,9 +94,9 @@ func doDailySign(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) - if record, err := services.DailySign(user.ID); err != nil { + if record, err := services.DailySign(user); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { services.AddEvent(user.ID, "dailySign", strconv.Itoa(int(record.ID)), c.IP(), c.Get(fiber.HeaderUserAgent)) diff --git a/pkg/internal/http/api/statuses_api.go b/pkg/internal/http/api/statuses_api.go index 1ebd1c8..2002131 100644 --- a/pkg/internal/http/api/statuses_api.go +++ b/pkg/internal/http/api/statuses_api.go @@ -2,13 +2,12 @@ package api import ( "fmt" - "git.solsynth.dev/hydrogen/passport/pkg/authkit/models" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "strconv" "time" "git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts" + "git.solsynth.dev/hydrogen/passport/pkg/internal/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" "github.com/gofiber/fiber/v2" "github.com/samber/lo" @@ -40,22 +39,15 @@ func getMyselfStatus(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) status, err := services.GetStatus(user.ID) disturbable := services.GetStatusDisturbable(user.ID) == nil online := services.GetStatusOnline(user.ID) == nil - var account models.Account - if err := database.C.Where(&models.Account{ - BaseModel: models.BaseModel{ID: user.ID}, - }).Preload("Profile").First(&account).Error; err != nil { - return fiber.NewError(fiber.StatusNotFound, err.Error()) - } - return c.JSON(fiber.Map{ "status": lo.Ternary(err == nil, &status, nil), - "last_seen_at": account.Profile.LastSeenAt, + "last_seen_at": user.Profile.LastSeenAt, "is_disturbable": disturbable, "is_online": online, }) @@ -65,7 +57,7 @@ func setStatus(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var req struct { Type string `json:"type" validate:"required"` @@ -96,7 +88,7 @@ func setStatus(c *fiber.Ctx) error { AccountID: user.ID, } - if status, err := services.NewStatus(user.ID, status); err != nil { + if status, err := services.NewStatus(user, status); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { services.AddEvent(user.ID, "statuses.set", strconv.Itoa(int(status.ID)), c.IP(), c.Get(fiber.HeaderUserAgent)) @@ -108,7 +100,7 @@ func editStatus(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) var req struct { Type string `json:"type" validate:"required"` @@ -135,7 +127,7 @@ func editStatus(c *fiber.Ctx) error { status.IsInvisible = req.IsInvisible status.ClearAt = req.ClearAt - if status, err := services.EditStatus(user.ID, status); err != nil { + if status, err := services.EditStatus(user, status); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { services.AddEvent(user.ID, "statuses.edit", strconv.Itoa(int(status.ID)), c.IP(), c.Get(fiber.HeaderUserAgent)) @@ -147,9 +139,9 @@ func clearStatus(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err } - user := c.Locals("user").(*sec.UserInfo) + user := c.Locals("user").(models.Account) - if err := services.ClearStatus(user.ID); err != nil { + if err := services.ClearStatus(user); err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } else { services.AddEvent(user.ID, "statuses.clear", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent)) diff --git a/pkg/internal/http/exts/auth.go b/pkg/internal/http/exts/auth.go index a2b0e15..26c6c41 100644 --- a/pkg/internal/http/exts/auth.go +++ b/pkg/internal/http/exts/auth.go @@ -2,13 +2,13 @@ package exts import ( "fmt" + "git.solsynth.dev/hydrogen/passport/pkg/internal/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/services" - "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "github.com/gofiber/fiber/v2" ) func EnsureAuthenticated(c *fiber.Ctx) error { - if _, ok := c.Locals("user").(*sec.UserInfo); !ok { + if _, ok := c.Locals("user").(models.Account); !ok { return fiber.NewError(fiber.StatusUnauthorized) } diff --git a/pkg/internal/http/server.go b/pkg/internal/http/server.go index 37efba1..ea53d08 100644 --- a/pkg/internal/http/server.go +++ b/pkg/internal/http/server.go @@ -1,7 +1,6 @@ package http import ( - "git.solsynth.dev/hydrogen/passport/pkg/authkit" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "strings" @@ -57,7 +56,6 @@ func NewServer() *App { })) app.Use(sec.ContextMiddleware(IReader)) - app.Use(authkit.ParseAccountMiddleware) admin.MapAdminAPIs(app, "/api/admin") api.MapAPIs(app, "/api") diff --git a/pkg/internal/services/bots.go b/pkg/internal/services/bots.go index 972c6fe..9ed4ec9 100644 --- a/pkg/internal/services/bots.go +++ b/pkg/internal/services/bots.go @@ -5,9 +5,9 @@ import ( "git.solsynth.dev/hydrogen/passport/pkg/internal/database" ) -func GetBotCount(user uint) (int64, error) { +func GetBotCount(user models.Account) (int64, error) { var count int64 - if err := database.C.Where("automated_id = ?", user).Count(&count).Error; err != nil { + if err := database.C.Where("automated_id = ?", user.ID).Count(&count).Error; err != nil { return 0, err } return count, nil diff --git a/pkg/internal/services/events.go b/pkg/internal/services/events.go index b3b40bc..34a87da 100644 --- a/pkg/internal/services/events.go +++ b/pkg/internal/services/events.go @@ -21,13 +21,13 @@ func AddEvent(user uint, event, target, ip, ua string) { } // AddAuditRecord to keep logs to make administrators' operations clear to query -func AddAuditRecord(operator uint, act, ip, ua string, metadata map[string]any) { +func AddAuditRecord(operator models.Account, act, ip, ua string, metadata map[string]any) { writeAuditQueue = append(writeAuditQueue, models.AuditRecord{ Action: act, Metadata: metadata, IpAddress: ip, UserAgent: ua, - AccountID: operator, + AccountID: operator.ID, }) } diff --git a/pkg/internal/services/preferences.go b/pkg/internal/services/preferences.go index bf40cee..474b672 100644 --- a/pkg/internal/services/preferences.go +++ b/pkg/internal/services/preferences.go @@ -15,21 +15,21 @@ import ( "gorm.io/datatypes" ) -func GetAuthPreference(account uint) (models.PreferenceAuth, error) { +func GetAuthPreference(account models.Account) (models.PreferenceAuth, error) { var auth models.PreferenceAuth - if err := database.C.Where("account_id = ?", account).First(&auth).Error; err != nil { + if err := database.C.Where("account_id = ?", account.ID).First(&auth).Error; err != nil { return auth, err } return auth, nil } -func UpdateAuthPreference(account uint, config models.AuthConfig) (models.PreferenceAuth, error) { +func UpdateAuthPreference(account models.Account, config models.AuthConfig) (models.PreferenceAuth, error) { var auth models.PreferenceAuth var err error if auth, err = GetAuthPreference(account); err != nil { auth = models.PreferenceAuth{ - AccountID: account, + AccountID: account.ID, Config: datatypes.NewJSONType(config), } } else { @@ -44,16 +44,16 @@ func GetNotificationPreferenceCacheKey(accountId uint) string { return fmt.Sprintf("notification-preference#%d", accountId) } -func GetNotificationPreference(account uint) (models.PreferenceNotification, error) { +func GetNotificationPreference(account models.Account) (models.PreferenceNotification, error) { var notification models.PreferenceNotification cacheManager := cache.New[any](localCache.S) marshal := marshaler.New(cacheManager) contx := context.Background() - if val, err := marshal.Get(contx, GetNotificationPreferenceCacheKey(account), new(models.PreferenceNotification)); err == nil { + if val, err := marshal.Get(contx, GetNotificationPreferenceCacheKey(account.ID), new(models.PreferenceNotification)); err == nil { notification = val.(models.PreferenceNotification) } else { - if err := database.C.Where("account_id = ?", account).First(¬ification).Error; err != nil { + if err := database.C.Where("account_id = ?", account.ID).First(¬ification).Error; err != nil { return notification, err } CacheNotificationPreference(notification) @@ -76,12 +76,12 @@ func CacheNotificationPreference(prefs models.PreferenceNotification) { ) } -func UpdateNotificationPreference(account uint, config map[string]bool) (models.PreferenceNotification, error) { +func UpdateNotificationPreference(account models.Account, config map[string]bool) (models.PreferenceNotification, error) { var notification models.PreferenceNotification var err error if notification, err = GetNotificationPreference(account); err != nil { notification = models.PreferenceNotification{ - AccountID: account, + AccountID: account.ID, Config: lo.MapValues(config, func(v bool, k string) any { return v }), } } else { diff --git a/pkg/internal/services/realms.go b/pkg/internal/services/realms.go index e7068a2..afc7484 100644 --- a/pkg/internal/services/realms.go +++ b/pkg/internal/services/realms.go @@ -18,20 +18,20 @@ func ListCommunityRealm() ([]models.Realm, error) { return realms, nil } -func ListOwnedRealm(user uint) ([]models.Realm, error) { +func ListOwnedRealm(user models.Account) ([]models.Realm, error) { var realms []models.Realm - if err := database.C.Where(&models.Realm{AccountID: user}).Find(&realms).Error; err != nil { + if err := database.C.Where(&models.Realm{AccountID: user.ID}).Find(&realms).Error; err != nil { return realms, err } return realms, nil } -func ListAvailableRealm(user uint) ([]models.Realm, error) { +func ListAvailableRealm(user models.Account) ([]models.Realm, error) { var realms []models.Realm var members []models.RealmMember if err := database.C.Where(&models.RealmMember{ - AccountID: user, + AccountID: user.ID, }).Find(&members).Error; err != nil { return realms, err } @@ -57,9 +57,9 @@ func GetRealmWithAlias(alias string) (models.Realm, error) { return realm, nil } -func NewRealm(realm models.Realm, user uint) (models.Realm, error) { +func NewRealm(realm models.Realm, user models.Account) (models.Realm, error) { realm.Members = []models.RealmMember{ - {AccountID: user, PowerLevel: 100}, + {AccountID: user.ID, PowerLevel: 100}, } err := database.C.Save(&realm).Error @@ -90,14 +90,14 @@ func GetRealmMember(userId uint, realmId uint) (models.RealmMember, error) { return member, nil } -func AddRealmMember(user uint, affected models.Account, target models.Realm) error { +func AddRealmMember(user models.Account, affected models.Account, target models.Realm) error { if !target.IsPublic && !target.IsCommunity { - if member, err := GetRealmMember(user, target.ID); err != nil { + if member, err := GetRealmMember(user.ID, target.ID); err != nil { return fmt.Errorf("only realm member can add people: %v", err) } else if member.PowerLevel < 50 { return fmt.Errorf("only realm moderator can add people") } - rel, err := GetRelationWithTwoNode(affected.ID, user) + rel, err := GetRelationWithTwoNode(affected.ID, user.ID) if err != nil || HasPermNodeWithDefault( rel.PermNodes, "RealmAdd", @@ -116,9 +116,9 @@ func AddRealmMember(user uint, affected models.Account, target models.Realm) err return err } -func RemoveRealmMember(user uint, affected models.Account, target models.Realm) error { - if user != affected.ID { - if member, err := GetRealmMember(user, target.ID); err != nil { +func RemoveRealmMember(user models.Account, affected models.Account, target models.Realm) error { + if user.ID != affected.ID { + if member, err := GetRealmMember(user.ID, target.ID); err != nil { return fmt.Errorf("only realm member can remove other member: %v", err) } else if member.PowerLevel < 50 { return fmt.Errorf("only realm moderator can invite people") diff --git a/pkg/internal/services/relationships.go b/pkg/internal/services/relationships.go index 72d4987..51bd814 100644 --- a/pkg/internal/services/relationships.go +++ b/pkg/internal/services/relationships.go @@ -9,10 +9,10 @@ import ( "gorm.io/gorm" ) -func ListAllRelationship(user uint) ([]models.AccountRelationship, error) { +func ListAllRelationship(user models.Account) ([]models.AccountRelationship, error) { var relationships []models.AccountRelationship if err := database.C. - Where("account_id = ?", user). + Where("account_id = ?", user.ID). Preload("Account"). Preload("Related"). Find(&relationships).Error; err != nil { @@ -22,10 +22,10 @@ func ListAllRelationship(user uint) ([]models.AccountRelationship, error) { return relationships, nil } -func ListRelationshipWithFilter(user uint, status models.RelationshipStatus) ([]models.AccountRelationship, error) { +func ListRelationshipWithFilter(user models.Account, status models.RelationshipStatus) ([]models.AccountRelationship, error) { var relationships []models.AccountRelationship if err := database.C. - Where("account_id = ? AND status = ?", user, status). + Where("account_id = ? AND status = ?", user.ID, status). Preload("Account"). Preload("Related"). Find(&relationships).Error; err != nil { diff --git a/pkg/internal/services/reports.go b/pkg/internal/services/reports.go index 8cda01b..e1de7d6 100644 --- a/pkg/internal/services/reports.go +++ b/pkg/internal/services/reports.go @@ -5,10 +5,10 @@ import ( "git.solsynth.dev/hydrogen/passport/pkg/internal/database" ) -func ListAbuseReport(account uint) ([]models.AbuseReport, error) { +func ListAbuseReport(account models.Account) ([]models.AbuseReport, error) { var reports []models.AbuseReport err := database.C. - Where("account_id = ?", account). + Where("account_id = ?", account.ID). Find(&reports).Error return reports, err } @@ -51,13 +51,13 @@ func UpdateAbuseReportStatus(id uint, status, message string) error { return nil } -func NewAbuseReport(resource string, reason string, account uint) (models.AbuseReport, error) { +func NewAbuseReport(resource string, reason string, account models.Account) (models.AbuseReport, error) { var report models.AbuseReport if err := database.C. Where( "resource = ? AND account_id = ? AND status IN ?", resource, - account, + account.ID, []string{models.ReportStatusPending, models.ReportStatusReviewing}, ).First(&report).Error; err == nil { return report, fmt.Errorf("you already reported this resource and it still in process") @@ -66,7 +66,7 @@ func NewAbuseReport(resource string, reason string, account uint) (models.AbuseR report = models.AbuseReport{ Resource: resource, Reason: reason, - AccountID: account, + AccountID: account.ID, } err := database.C.Create(&report).Error diff --git a/pkg/internal/services/sign.go b/pkg/internal/services/sign.go index 3572b91..a268c1a 100644 --- a/pkg/internal/services/sign.go +++ b/pkg/internal/services/sign.go @@ -11,11 +11,11 @@ import ( "time" ) -func CheckDailyCanSign(user uint) error { +func CheckDailyCanSign(user models.Account) error { probe := time.Now().Format("2006-01-02") var record models.SignRecord - if err := database.C.Where("account_id = ? AND created_at::date = ?", user, probe).First(&record).Error; err != nil { + if err := database.C.Where("account_id = ? AND created_at::date = ?", user.ID, probe).First(&record).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil } @@ -24,22 +24,22 @@ func CheckDailyCanSign(user uint) error { return fmt.Errorf("daliy sign record exists") } -func GetTodayDailySign(user uint) (models.SignRecord, error) { +func GetTodayDailySign(user models.Account) (models.SignRecord, error) { probe := time.Now().Format("2006-01-02") var record models.SignRecord - if err := database.C.Where("account_id = ? AND created_at::date = ?", user, probe).First(&record).Error; err != nil { + if err := database.C.Where("account_id = ? AND created_at::date = ?", user.ID, probe).First(&record).Error; err != nil { return record, fmt.Errorf("unable get daliy sign record: %v", err) } return record, nil } -func DailySign(user uint) (models.SignRecord, error) { +func DailySign(user models.Account) (models.SignRecord, error) { tier := rand.Intn(5) record := models.SignRecord{ ResultTier: tier, ResultExperience: rand.Intn(int(math.Max(float64(tier), 1)*100)+1-100) + 100, - AccountID: user, + AccountID: user.ID, } if err := CheckDailyCanSign(user); err != nil { @@ -47,7 +47,7 @@ func DailySign(user uint) (models.SignRecord, error) { } var profile models.AccountProfile - if err := database.C.Where("account_id = ?", user).First(&profile).Error; err != nil { + if err := database.C.Where("account_id = ?", user.ID).First(&profile).Error; err != nil { return record, fmt.Errorf("unable get account profile: %v", err) } else { profile.Experience += uint64(record.ResultExperience) diff --git a/pkg/internal/services/statuses.go b/pkg/internal/services/statuses.go index 7bdf2b4..4773277 100644 --- a/pkg/internal/services/statuses.go +++ b/pkg/internal/services/statuses.go @@ -98,27 +98,27 @@ func GetStatusOnline(uid uint) error { } } -func NewStatus(user uint, status models.Status) (models.Status, error) { +func NewStatus(user models.Account, status models.Status) (models.Status, error) { if err := database.C.Save(&status).Error; err != nil { return status, err } else { - CacheUserStatus(user, status) + CacheUserStatus(user.ID, status) } return status, nil } -func EditStatus(user uint, status models.Status) (models.Status, error) { +func EditStatus(user models.Account, status models.Status) (models.Status, error) { if err := database.C.Save(&status).Error; err != nil { return status, err } else { - CacheUserStatus(user, status) + CacheUserStatus(user.ID, status) } return status, nil } -func ClearStatus(user uint) error { +func ClearStatus(user models.Account) error { if err := database.C. - Where("account_id = ?", user). + Where("account_id = ?", user.ID). Where("clear_at > ?", time.Now()). Updates(models.Status{ClearAt: lo.ToPtr(time.Now())}).Error; err != nil { return err @@ -127,7 +127,7 @@ func ClearStatus(user uint) error { marshal := marshaler.New(cacheManager) contx := context.Background() - marshal.Delete(contx, GetStatusCacheKey(user)) + marshal.Delete(contx, GetStatusCacheKey(user.ID)) } return nil diff --git a/pkg/internal/services/ticket.go b/pkg/internal/services/ticket.go index a62ea0c..332e118 100644 --- a/pkg/internal/services/ticket.go +++ b/pkg/internal/services/ticket.go @@ -57,7 +57,7 @@ func NewTicket(user models.Account, ip, ua string) (models.AuthTicket, error) { } else { steps = min(steps, int(count)) - cfg, err := GetAuthPreference(user.ID) + cfg, err := GetAuthPreference(user) if err == nil && cfg.Config.Data().MaximumAuthSteps >= 1 { steps = min(steps, cfg.Config.Data().MaximumAuthSteps) } diff --git a/pkg/main.go b/pkg/main.go index 7534429..c246613 100644 --- a/pkg/main.go +++ b/pkg/main.go @@ -58,18 +58,6 @@ func main() { http.IReader = reader log.Info().Msg("Internal jwt public key loaded.") } - if reader, err := sec.NewJwtReader(viper.GetString("security.public_key")); err != nil { - log.Error().Err(err).Msg("An error occurred when reading public key for jwt. Signing token may not work.") - } else { - services.EReader = reader - log.Info().Msg("Jwt public key loaded.") - } - if writer, err := sec.NewJwtWriter(viper.GetString("security.private_key")); err != nil { - log.Error().Err(err).Msg("An error occurred when reading private key for jwt. Signing token may not work.") - } else { - services.EWriter = writer - log.Info().Msg("Jwt private key loaded.") - } // Connect to database if err := database.NewGorm(); err != nil { diff --git a/settings.toml b/settings.toml index 5dae74c..b561e64 100644 --- a/settings.toml +++ b/settings.toml @@ -20,8 +20,9 @@ database = false print_routes = false [security] -access_token_duration = 3000 +issuer = "https://solsynth.dev" +cookie_domain = "localhost" +cookie_samesite = "Lax" +access_token_duration = 300 refresh_token_duration = 2592000 -public_key = "keys/public_key.pem" -private_key = "keys/private_key.pem" internal_public_key = "keys/internal_public_key.pem"