Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 19fa8b381c |
@@ -490,12 +490,23 @@ type RevokeReq struct {
|
|||||||
|
|
||||||
// Introspect 令牌检查端点
|
// Introspect 令牌检查端点
|
||||||
func Introspect(ctx *fiber.Ctx) error {
|
func Introspect(ctx *fiber.Ctx) error {
|
||||||
// 验证权限
|
authCtx := GetAuthCtx(ctx)
|
||||||
authCtx, err := GetAuthCtx(ctx).PermitUser()
|
|
||||||
if err != nil {
|
// 尝试验证用户权限
|
||||||
return err
|
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.
|
profile, err := q.User.
|
||||||
Where(q.User.ID.Eq(authCtx.User.ID)).
|
Where(q.User.ID.Eq(authCtx.User.ID)).
|
||||||
@@ -519,12 +530,33 @@ func Introspect(ctx *fiber.Ctx) error {
|
|||||||
if profile.IDNo != nil && *profile.IDNo != "" {
|
if profile.IDNo != nil && *profile.IDNo != "" {
|
||||||
profile.IDNo = u.P(maskIdNo(*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 {
|
// introspectAdmin 获取并返回管理员信息
|
||||||
m.User
|
func introspectAdmin(ctx *fiber.Ctx, authCtx *AuthCtx) error {
|
||||||
HasPassword bool `json:"has_password"` // 是否设置了密码
|
// 获取管理员信息
|
||||||
|
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 {
|
func maskPhone(phone string) string {
|
||||||
|
|||||||
Reference in New Issue
Block a user