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) }