From 55bd8946680d7aeafc4ba21172128e8d6a3f3a00 Mon Sep 17 00:00:00 2001 From: luorijun Date: Tue, 2 Dec 2025 09:53:52 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=A4=B4=EF=BC=8C=E4=BD=BF=E7=94=A8=E6=A0=87?= =?UTF-8?q?=E5=87=86=E8=AF=B7=E6=B1=82=E5=A4=B4=E8=8E=B7=E5=8F=96=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/auth/authorize.go | 26 +++++++++++++------------- web/core/http.go | 3 --- web/handlers/channel.go | 2 +- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/web/auth/authorize.go b/web/auth/authorize.go index 8358287..28e9c37 100644 --- a/web/auth/authorize.go +++ b/web/auth/authorize.go @@ -165,7 +165,7 @@ func Token(c *fiber.Ctx) error { }) } -func authAuthorizationCode(ctx *fiber.Ctx, auth *AuthCtx, req *TokenReq, now time.Time) (*m.Session, error) { +func authAuthorizationCode(c *fiber.Ctx, auth *AuthCtx, req *TokenReq, now time.Time) (*m.Session, error) { // 检查 code 获取用户授权信息 data, err := g.Redis.Get(context.Background(), req.Code).Result() @@ -210,11 +210,11 @@ func authAuthorizationCode(ctx *fiber.Ctx, auth *AuthCtx, req *TokenReq, now tim // todo 检查 scope // 生成会话 - ip, _ := orm.ParseInet(ctx.Get(core.HeaderUserIP)) - ua := ctx.Get(core.HeaderUserUA) + ip, _ := orm.ParseInet(c.IP()) // 可空字段,忽略异常 + ua := u.X(c.Get(fiber.HeaderUserAgent)) session := &m.Session{ IP: ip, - UA: u.X(ua), + UA: ua, UserID: &user.ID, ClientID: &auth.Client.ID, Scopes: u.P(strings.Join(codeCtx.Scopes, " ")), @@ -234,15 +234,15 @@ func authAuthorizationCode(ctx *fiber.Ctx, auth *AuthCtx, req *TokenReq, now tim return session, nil } -func authClientCredential(ctx *fiber.Ctx, auth *AuthCtx, _ *TokenReq, now time.Time) (*m.Session, error) { +func authClientCredential(c *fiber.Ctx, auth *AuthCtx, _ *TokenReq, now time.Time) (*m.Session, error) { // todo 检查 scope // 生成会话 - ip, _ := orm.ParseInet(ctx.Get(core.HeaderUserIP)) - ua := ctx.Get(core.HeaderUserUA) + ip, _ := orm.ParseInet(c.IP()) // 可空字段,忽略异常 + ua := u.X(c.Get(fiber.HeaderUserAgent)) session := &m.Session{ IP: ip, - UA: u.X(ua), + UA: ua, ClientID: &auth.Client.ID, AccessToken: uuid.NewString(), AccessTokenExpires: now.Add(time.Duration(env.SessionAccessExpire) * time.Second), @@ -257,9 +257,9 @@ func authClientCredential(ctx *fiber.Ctx, auth *AuthCtx, _ *TokenReq, now time.T return session, nil } -func authPassword(ctx *fiber.Ctx, auth *AuthCtx, req *TokenReq, now time.Time) (*m.Session, error) { - ip, _ := orm.ParseInet(ctx.Get(core.HeaderUserIP)) - ua := ctx.Get(core.HeaderUserUA) +func authPassword(c *fiber.Ctx, auth *AuthCtx, req *TokenReq, now time.Time) (*m.Session, error) { + ip, _ := orm.ParseInet(c.IP()) // 可空字段,忽略异常 + ua := u.X(c.Get(fiber.HeaderUserAgent)) var user *m.User err := q.Q.Transaction(func(tx *q.Query) (err error) { @@ -299,7 +299,7 @@ func authPassword(ctx *fiber.Ctx, auth *AuthCtx, req *TokenReq, now time.Time) ( // 更新用户的登录时间 user.LastLogin = u.P(time.Now()) user.LastLoginIP = ip - user.LastLoginUA = u.X(ua) + user.LastLoginUA = ua if err := tx.User.Save(user); err != nil { return err } @@ -313,7 +313,7 @@ func authPassword(ctx *fiber.Ctx, auth *AuthCtx, req *TokenReq, now time.Time) ( // 生成会话 session := &m.Session{ IP: ip, - UA: u.X(ua), + UA: ua, UserID: &user.ID, ClientID: &auth.Client.ID, Scopes: u.X(req.Scope), diff --git a/web/core/http.go b/web/core/http.go index a44b5ea..1dadb22 100644 --- a/web/core/http.go +++ b/web/core/http.go @@ -1,8 +1,5 @@ package core -const HeaderUserIP = "X-Data-IP" -const HeaderUserUA = "X-Data-UA" - // PageReq 分页请求参数 type PageReq struct { RawPage int `json:"page"` diff --git a/web/handlers/channel.go b/web/handlers/channel.go index 0db7cff..07798d9 100644 --- a/web/handlers/channel.go +++ b/web/handlers/channel.go @@ -117,7 +117,7 @@ func CreateChannel(c *fiber.Ctx) error { return core.NewBizErr("解析参数失败", err) } - ip, err := netip.ParseAddr(c.Get(core.HeaderUserIP)) + ip, err := netip.ParseAddr(c.IP()) if err != nil { return core.NewBizErr("获取客户端地址失败", err) }