移除自定义请求头,使用标准请求头获取客户端信息

This commit is contained in:
2025-12-02 09:53:52 +08:00
parent e59c7c96f6
commit 55bd894668
3 changed files with 14 additions and 17 deletions

View File

@@ -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),

View File

@@ -1,8 +1,5 @@
package core
const HeaderUserIP = "X-Data-IP"
const HeaderUserUA = "X-Data-UA"
// PageReq 分页请求参数
type PageReq struct {
RawPage int `json:"page"`

View File

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