refactor: simplify ExternalInfoMiddleware and update GetExternalInfo to accept user token secret
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -11,11 +11,13 @@ import (
|
|||||||
|
|
||||||
func ExternalInfoMiddleware(ctx iris.Context) {
|
func ExternalInfoMiddleware(ctx iris.Context) {
|
||||||
channel := services.ChannelService.GetEnabledChannel(ctx)
|
channel := services.ChannelService.GetEnabledChannel(ctx)
|
||||||
var userTokenSecret string
|
if channel == nil {
|
||||||
if channel != nil {
|
ctx.StopExecution()
|
||||||
userTokenSecret = services.ChannelService.GetUserTokenSecret(channel)
|
_ = ctx.JSON(web.JsonErrorMsg("接入渠道异常"))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
ext, err := openidentity.GetExternalInfoWithUserTokenSecret(ctx, userTokenSecret)
|
secret := services.ChannelService.GetUserTokenSecret(channel)
|
||||||
|
ext, err := openidentity.GetExternalInfo(ctx, secret)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.StopExecution()
|
ctx.StopExecution()
|
||||||
_ = ctx.JSON(web.JsonError(err))
|
_ = ctx.JSON(web.JsonError(err))
|
||||||
|
|||||||
@@ -34,14 +34,9 @@ type userTokenJWTClaims struct {
|
|||||||
jwt.RegisteredClaims
|
jwt.RegisteredClaims
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetExternalInfo 从 Header(X-External-*)或 query(externalSource、externalId、externalName)解析身份。
|
func GetExternalInfo(ctx iris.Context, secret string) (*ExternalInfo, error) {
|
||||||
func GetExternalInfo(ctx iris.Context) (*ExternalInfo, error) {
|
if userToken := parseUserToken(ctx); strs.IsNotBlank(userToken) {
|
||||||
return GetExternalInfoWithUserTokenSecret(ctx, "")
|
claims, err := VerifyUserToken(userToken, secret)
|
||||||
}
|
|
||||||
|
|
||||||
func GetExternalInfoWithUserTokenSecret(ctx iris.Context, userTokenSecret string) (*ExternalInfo, error) {
|
|
||||||
if userToken := parseUserToken(ctx); userToken != "" {
|
|
||||||
claims, err := VerifyUserToken(userToken, userTokenSecret)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -67,19 +62,16 @@ func GetExternalInfoWithUserTokenSecret(ctx iris.Context, userTokenSecret string
|
|||||||
}
|
}
|
||||||
return &ExternalInfo{
|
return &ExternalInfo{
|
||||||
ExternalSource: externalSource,
|
ExternalSource: externalSource,
|
||||||
// TODO: 对接业务系统后,根据业务系统用户信息识别 user;未对接时统一按访客处理。
|
ExternalID: externalID,
|
||||||
ExternalID: externalID,
|
ExternalName: parseExternalName(ctx),
|
||||||
ExternalName: parseExternalName(ctx),
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func VerifyUserToken(userToken, secret string) (*UserTokenClaims, error) {
|
func VerifyUserToken(userToken, secret string) (*UserTokenClaims, error) {
|
||||||
userToken = strings.TrimSpace(userToken)
|
if strs.IsBlank(userToken) {
|
||||||
secret = strings.TrimSpace(secret)
|
|
||||||
if userToken == "" {
|
|
||||||
return nil, errorsx.Unauthorized("用户身份不能为空")
|
return nil, errorsx.Unauthorized("用户身份不能为空")
|
||||||
}
|
}
|
||||||
if secret == "" {
|
if strs.IsBlank(secret) {
|
||||||
return nil, errorsx.Unauthorized("用户身份校验未配置")
|
return nil, errorsx.Unauthorized("用户身份校验未配置")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,12 +96,10 @@ func VerifyUserToken(userToken, secret string) (*UserTokenClaims, error) {
|
|||||||
return nil, errorsx.Unauthorized("用户身份校验失败")
|
return nil, errorsx.Unauthorized("用户身份校验失败")
|
||||||
}
|
}
|
||||||
|
|
||||||
userID := strings.TrimSpace(claims.UserID)
|
if strs.IsBlank(claims.UserID) {
|
||||||
name := strings.TrimSpace(claims.Name)
|
|
||||||
if userID == "" {
|
|
||||||
return nil, errorsx.Unauthorized("用户标识不能为空")
|
return nil, errorsx.Unauthorized("用户标识不能为空")
|
||||||
}
|
}
|
||||||
if name == "" {
|
if strs.IsBlank(claims.Name) {
|
||||||
return nil, errorsx.Unauthorized("用户名称不能为空")
|
return nil, errorsx.Unauthorized("用户名称不能为空")
|
||||||
}
|
}
|
||||||
if claims.ExpiresAt == nil {
|
if claims.ExpiresAt == nil {
|
||||||
@@ -117,8 +107,8 @@ func VerifyUserToken(userToken, secret string) (*UserTokenClaims, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result := &UserTokenClaims{
|
result := &UserTokenClaims{
|
||||||
UserID: userID,
|
UserID: claims.UserID,
|
||||||
Name: name,
|
Name: claims.Name,
|
||||||
Exp: claims.ExpiresAt.Unix(),
|
Exp: claims.ExpiresAt.Unix(),
|
||||||
}
|
}
|
||||||
if claims.IssuedAt != nil {
|
if claims.IssuedAt != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user