扩展令牌内省函数以支持多账号池

This commit is contained in:
2026-01-09 17:22:02 +08:00
parent b7a9682552
commit 19fa8b381c

View File

@@ -490,12 +490,23 @@ type RevokeReq struct {
// Introspect 令牌检查端点
func Introspect(ctx *fiber.Ctx) error {
// 验证权限
authCtx, err := GetAuthCtx(ctx).PermitUser()
if err != nil {
return err
authCtx := GetAuthCtx(ctx)
// 尝试验证用户权限
if _, err := authCtx.PermitUser(); err == nil {
return introspectUser(ctx, authCtx)
}
// 尝试验证管理员权限
if _, err := authCtx.PermitAdmin(); err == nil {
return introspectAdmin(ctx, authCtx)
}
return ErrAuthenticateForbidden
}
// introspectUser 获取并返回用户信息
func introspectUser(ctx *fiber.Ctx, authCtx *AuthCtx) error {
// 获取用户信息
profile, err := q.User.
Where(q.User.ID.Eq(authCtx.User.ID)).
@@ -519,12 +530,33 @@ func Introspect(ctx *fiber.Ctx) error {
if profile.IDNo != nil && *profile.IDNo != "" {
profile.IDNo = u.P(maskIdNo(*profile.IDNo))
}
return ctx.JSON(IntrospectResp{*profile, hasPassword})
return ctx.JSON(struct {
m.User
HasPassword bool `json:"has_password"` // 是否设置了密码
}{*profile, hasPassword})
}
type IntrospectResp struct {
m.User
HasPassword bool `json:"has_password"` // 是否设置了密码
// introspectAdmin 获取并返回管理员信息
func introspectAdmin(ctx *fiber.Ctx, authCtx *AuthCtx) error {
// 获取管理员信息
profile, err := q.Admin.
Where(q.Admin.ID.Eq(authCtx.Admin.ID)).
Omit(q.Admin.DeletedAt).
Take()
if err != nil {
return err
}
// 不返回密码
profile.Password = ""
// 掩码敏感信息
if profile.Phone != nil && *profile.Phone != "" {
profile.Phone = u.P(maskPhone(*profile.Phone))
}
return ctx.JSON(profile)
}
func maskPhone(phone string) string {