|
@@ -7,16 +7,19 @@ import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
+ "github.com/go-redis/redis/v7"
|
|
|
"github.com/mssola/user_agent"
|
|
|
"gogs.baozhida.cn/zoie/OAuth-core/api"
|
|
|
"gogs.baozhida.cn/zoie/OAuth-core/pkg"
|
|
|
jwt "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth"
|
|
|
"gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
|
|
|
"gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
|
|
|
+ "gogs.baozhida.cn/zoie/OAuth-core/pkg/utils"
|
|
|
"gogs.baozhida.cn/zoie/OAuth-core/sdk"
|
|
|
"gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
|
|
|
"gorm.io/gorm"
|
|
|
"net/http"
|
|
|
+ "strings"
|
|
|
)
|
|
|
|
|
|
func PayloadFunc(data interface{}) jwt.MapClaims {
|
|
@@ -36,6 +39,7 @@ func PayloadFunc(data interface{}) jwt.MapClaims {
|
|
|
jwt.SingleKey: single,
|
|
|
jwt.DeptIdKey: u.DeptId,
|
|
|
jwt.DeptNameKey: d.DeptName,
|
|
|
+ jwt.RandomKey: utils.GetRandString(8, "", 0),
|
|
|
}
|
|
|
}
|
|
|
return jwt.MapClaims{}
|
|
@@ -55,6 +59,7 @@ func IdentityHandler(c *gin.Context) interface{} {
|
|
|
"single": claims["single"],
|
|
|
"DeptId": claims["deptId"],
|
|
|
"DeptName": claims["deptName"],
|
|
|
+ "RandomKey": claims["randomKey"],
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -115,8 +120,6 @@ func Authenticator(c *gin.Context) (interface{}, error) {
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- err = sdk.Runtime.GetCacheAdapter().Del(model.GetEnterDeptCacheKey(u.Id))
|
|
|
- err = sdk.Runtime.GetCacheAdapter().Del(model.GetEnterDeptNameCacheKey(u.Id))
|
|
|
|
|
|
return map[string]interface{}{"user": u, "role": role, "dept": dept, "single": single}, nil
|
|
|
}
|
|
@@ -193,6 +196,7 @@ func Authorizator(data interface{}, c *gin.Context) bool {
|
|
|
c.Set("dataScope", r.DataScope)
|
|
|
c.Set("deptId", u.DeptId)
|
|
|
c.Set("deptName", d.DeptName)
|
|
|
+ c.Set("randomKey", utils.GetRandString(8, "", 0))
|
|
|
return true
|
|
|
}
|
|
|
return false
|
|
@@ -243,3 +247,27 @@ func GetSingleLogin(c *gin.Context) (bool, error) {
|
|
|
return false, nil
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+func SetEnterDeptId(c *gin.Context, newToken string, userId int64) error {
|
|
|
+ oldToken := ""
|
|
|
+ list := strings.Split(c.Request.Header.Get("Authorization"), ".")
|
|
|
+ if len(list) > 0 {
|
|
|
+ oldToken = list[len(list)-1]
|
|
|
+ }
|
|
|
+ list2 := strings.Split(newToken, ".")
|
|
|
+ newToken2 := list2[len(list2)-1]
|
|
|
+ deptIdStr, err := sdk.Runtime.GetCacheAdapter().Get(fmt.Sprintf("enter-dept-%s-%d", oldToken, userId))
|
|
|
+ if err == nil {
|
|
|
+ sdk.Runtime.GetCacheAdapter().Set(fmt.Sprintf("enter-dept-%s-%d", newToken2, userId), deptIdStr, int(config.JwtConfig.Timeout))
|
|
|
+ sdk.Runtime.GetCacheAdapter().Del(fmt.Sprintf("enter-dept-%s-%d", oldToken, userId))
|
|
|
+ }
|
|
|
+ deptName, err := sdk.Runtime.GetCacheAdapter().Get(fmt.Sprintf("enter-dept-name-%s-%d", oldToken, userId))
|
|
|
+ if err == nil {
|
|
|
+ sdk.Runtime.GetCacheAdapter().Set(fmt.Sprintf("enter-dept-name-%s-%d", newToken2, userId), deptName, int(config.JwtConfig.Timeout))
|
|
|
+ sdk.Runtime.GetCacheAdapter().Del(fmt.Sprintf("enter-dept-name-%s-%d", oldToken, userId))
|
|
|
+ }
|
|
|
+ if err == redis.Nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ return err
|
|
|
+}
|