移除自定义请求头,使用标准请求头获取客户端信息
This commit is contained in:
@@ -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 获取用户授权信息
|
// 检查 code 获取用户授权信息
|
||||||
data, err := g.Redis.Get(context.Background(), req.Code).Result()
|
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
|
// todo 检查 scope
|
||||||
|
|
||||||
// 生成会话
|
// 生成会话
|
||||||
ip, _ := orm.ParseInet(ctx.Get(core.HeaderUserIP))
|
ip, _ := orm.ParseInet(c.IP()) // 可空字段,忽略异常
|
||||||
ua := ctx.Get(core.HeaderUserUA)
|
ua := u.X(c.Get(fiber.HeaderUserAgent))
|
||||||
session := &m.Session{
|
session := &m.Session{
|
||||||
IP: ip,
|
IP: ip,
|
||||||
UA: u.X(ua),
|
UA: ua,
|
||||||
UserID: &user.ID,
|
UserID: &user.ID,
|
||||||
ClientID: &auth.Client.ID,
|
ClientID: &auth.Client.ID,
|
||||||
Scopes: u.P(strings.Join(codeCtx.Scopes, " ")),
|
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
|
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
|
// todo 检查 scope
|
||||||
|
|
||||||
// 生成会话
|
// 生成会话
|
||||||
ip, _ := orm.ParseInet(ctx.Get(core.HeaderUserIP))
|
ip, _ := orm.ParseInet(c.IP()) // 可空字段,忽略异常
|
||||||
ua := ctx.Get(core.HeaderUserUA)
|
ua := u.X(c.Get(fiber.HeaderUserAgent))
|
||||||
session := &m.Session{
|
session := &m.Session{
|
||||||
IP: ip,
|
IP: ip,
|
||||||
UA: u.X(ua),
|
UA: ua,
|
||||||
ClientID: &auth.Client.ID,
|
ClientID: &auth.Client.ID,
|
||||||
AccessToken: uuid.NewString(),
|
AccessToken: uuid.NewString(),
|
||||||
AccessTokenExpires: now.Add(time.Duration(env.SessionAccessExpire) * time.Second),
|
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
|
return session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func authPassword(ctx *fiber.Ctx, auth *AuthCtx, req *TokenReq, now time.Time) (*m.Session, error) {
|
func authPassword(c *fiber.Ctx, auth *AuthCtx, req *TokenReq, now time.Time) (*m.Session, error) {
|
||||||
ip, _ := orm.ParseInet(ctx.Get(core.HeaderUserIP))
|
ip, _ := orm.ParseInet(c.IP()) // 可空字段,忽略异常
|
||||||
ua := ctx.Get(core.HeaderUserUA)
|
ua := u.X(c.Get(fiber.HeaderUserAgent))
|
||||||
|
|
||||||
var user *m.User
|
var user *m.User
|
||||||
err := q.Q.Transaction(func(tx *q.Query) (err error) {
|
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.LastLogin = u.P(time.Now())
|
||||||
user.LastLoginIP = ip
|
user.LastLoginIP = ip
|
||||||
user.LastLoginUA = u.X(ua)
|
user.LastLoginUA = ua
|
||||||
if err := tx.User.Save(user); err != nil {
|
if err := tx.User.Save(user); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -313,7 +313,7 @@ func authPassword(ctx *fiber.Ctx, auth *AuthCtx, req *TokenReq, now time.Time) (
|
|||||||
// 生成会话
|
// 生成会话
|
||||||
session := &m.Session{
|
session := &m.Session{
|
||||||
IP: ip,
|
IP: ip,
|
||||||
UA: u.X(ua),
|
UA: ua,
|
||||||
UserID: &user.ID,
|
UserID: &user.ID,
|
||||||
ClientID: &auth.Client.ID,
|
ClientID: &auth.Client.ID,
|
||||||
Scopes: u.X(req.Scope),
|
Scopes: u.X(req.Scope),
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
const HeaderUserIP = "X-Data-IP"
|
|
||||||
const HeaderUserUA = "X-Data-UA"
|
|
||||||
|
|
||||||
// PageReq 分页请求参数
|
// PageReq 分页请求参数
|
||||||
type PageReq struct {
|
type PageReq struct {
|
||||||
RawPage int `json:"page"`
|
RawPage int `json:"page"`
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ func CreateChannel(c *fiber.Ctx) error {
|
|||||||
return core.NewBizErr("解析参数失败", err)
|
return core.NewBizErr("解析参数失败", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, err := netip.ParseAddr(c.Get(core.HeaderUserIP))
|
ip, err := netip.ParseAddr(c.IP())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return core.NewBizErr("获取客户端地址失败", err)
|
return core.NewBizErr("获取客户端地址失败", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user