Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 25s
feat(admin): 新增工会管理功能 feat(activity): 添加活动管理相关服务 feat(user): 实现用户道具卡和积分管理 feat(guild): 新增工会成员管理功能 fix: 修复数据库连接配置 fix: 修正jwtoken导入路径 fix: 解决端口冲突问题 style: 统一代码格式和注释风格 style: 更新项目常量命名 docs: 添加项目框架和开发规范文档 docs: 更新接口文档注释 chore: 移除无用代码和文件 chore: 更新Makefile和配置文件 chore: 清理日志文件 test: 添加道具卡测试脚本
81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
package interceptor
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"bindbox-game/configs"
|
|
"bindbox-game/internal/code"
|
|
"bindbox-game/internal/pkg/core"
|
|
"bindbox-game/internal/pkg/jwtoken"
|
|
"bindbox-game/internal/pkg/utils"
|
|
"bindbox-game/internal/proposal"
|
|
"bindbox-game/internal/repository/mysql/dao"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func (i *interceptor) AdminTokenAuthVerify(ctx core.Context) (sessionUserInfo proposal.SessionUserInfo, err core.BusinessError) {
|
|
headerAuthorizationString := ctx.GetHeader("Authorization")
|
|
if headerAuthorizationString == "" {
|
|
err = core.Error(
|
|
http.StatusUnauthorized,
|
|
code.JWTAuthVerifyError,
|
|
"无法确认您的身份,请进行登录。")
|
|
|
|
return
|
|
}
|
|
|
|
// 验证 JWT 是否合法
|
|
jwtClaims, jwtErr := jwtoken.New(configs.Get().JWT.AdminSecret).Parse(headerAuthorizationString)
|
|
if jwtErr != nil {
|
|
err = core.Error(
|
|
http.StatusUnauthorized,
|
|
code.JWTAuthVerifyError,
|
|
"您的账号登录过期,请重新登录。")
|
|
|
|
return
|
|
}
|
|
|
|
// 验证用户状态
|
|
info, dbErr := dao.Use(i.db.GetDbR()).Admin.WithContext(ctx.RequestContext()).Where(dao.Use(i.db.GetDbR()).Admin.ID.Eq(jwtClaims.Id)).First()
|
|
if dbErr != nil && dbErr != gorm.ErrRecordNotFound {
|
|
err = core.Error(
|
|
http.StatusUnauthorized,
|
|
code.ServerError,
|
|
"身份验证失败,如需帮助请联系我们。")
|
|
|
|
return
|
|
}
|
|
|
|
if dbErr == gorm.ErrRecordNotFound {
|
|
err = core.Error(
|
|
http.StatusUnauthorized,
|
|
code.ServerError,
|
|
"无法确认您的身份,请进行登录。")
|
|
|
|
return
|
|
}
|
|
|
|
if utils.MD5(headerAuthorizationString) != info.LastLoginHash {
|
|
err = core.Error(
|
|
http.StatusUnauthorized,
|
|
code.JWTAuthVerifyError,
|
|
"您的账号已在别处登录,为了保护您的账户安全,请重新登录。")
|
|
return
|
|
}
|
|
|
|
// 验证登录状态
|
|
if info.LoginStatus != 1 {
|
|
err = core.Error(
|
|
http.StatusUnauthorized,
|
|
code.ServerError,
|
|
"您的账号已被禁用,如需帮助请联系我们。").WithAlert()
|
|
|
|
return
|
|
}
|
|
|
|
sessionUserInfo = jwtClaims.SessionUserInfo
|
|
|
|
return
|
|
}
|