🐛 Keep the nex_user in context always the pointer

This commit is contained in:
LittleSheep 2025-01-30 22:44:43 +08:00
parent 7cf9b80fe6
commit 2e3c663525
3 changed files with 9 additions and 8 deletions

View File

@ -3,14 +3,15 @@ package auth
import ( import (
"context" "context"
"fmt" "fmt"
"strconv"
"time"
"git.solsynth.dev/hypernet/nexus/pkg/internal/directory" "git.solsynth.dev/hypernet/nexus/pkg/internal/directory"
"git.solsynth.dev/hypernet/nexus/pkg/nex" "git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"git.solsynth.dev/hypernet/nexus/pkg/proto" "git.solsynth.dev/hypernet/nexus/pkg/proto"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"strconv"
"time"
) )
func userinfoFetch(c *fiber.Ctx) error { func userinfoFetch(c *fiber.Ctx) error {
@ -29,7 +30,7 @@ func userinfoFetch(c *fiber.Ctx) error {
defer cancel() defer cancel()
sed, err := strconv.Atoi(claims.Session) sed, err := strconv.Atoi(claims.Session)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusUnauthorized, fmt.Sprintf("invalid token payload")) return fiber.NewError(fiber.StatusUnauthorized, "invalid token payload")
} }
resp, err := proto.NewAuthServiceClient(conn).Authenticate(ctx, &proto.AuthRequest{ resp, err := proto.NewAuthServiceClient(conn).Authenticate(ctx, &proto.AuthRequest{
SessionId: uint64(sed), SessionId: uint64(sed),
@ -38,7 +39,7 @@ func userinfoFetch(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusUnauthorized, fmt.Sprintf("unable to load userinfo: %v", err)) return fiber.NewError(fiber.StatusUnauthorized, fmt.Sprintf("unable to load userinfo: %v", err))
} }
userinfo := sec.NewUserInfoFromProto(resp.Info.Info) userinfo := sec.NewUserInfoFromProto(resp.Info.Info)
c.Locals("nex_user", userinfo) c.Locals("nex_user", &userinfo)
tk, err := IWriter.WriteUserInfoJwt(userinfo) tk, err := IWriter.WriteUserInfoJwt(userinfo)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("unable to sign userinfo: %v", err)) return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("unable to sign userinfo: %v", err))

View File

@ -42,7 +42,7 @@ func MapAPIs(app *fiber.App) {
} }
// WatchTower administration APIs // WatchTower administration APIs
wt := app.Group("/watchtower").Name("WatchTower").Use(auth.ValidatorMiddleware) wt := app.Group("/wt").Name("WatchTower").Use(auth.ValidatorMiddleware)
{ {
wt.Post("/maintenance/database", wtRunDbMaintenance) wt.Post("/maintenance/database", wtRunDbMaintenance)
} }

View File

@ -15,14 +15,14 @@ import (
) )
func Listen(c *websocket.Conn) { func Listen(c *websocket.Conn) {
user, ok := c.Locals("nex_user").(sec.UserInfo) user, ok := c.Locals("nex_user").(*sec.UserInfo)
if !ok { if !ok {
c.Close() c.Close()
return return
} }
// Push connection // Push connection
clientId := ClientRegister(user, c) clientId := ClientRegister(*user, c)
// Event loop // Event loop
var mt int var mt int
@ -80,5 +80,5 @@ func Listen(c *websocket.Conn) {
} }
// Pop connection // Pop connection
ClientUnregister(user, clientId) ClientUnregister(*user, clientId)
} }