feat: openid login
This commit is contained in:
+48
-59
@@ -27,78 +27,58 @@ type UserInfo struct {
|
||||
GroupId int64 // 分组ID
|
||||
GroupName string // 分组名称
|
||||
Type uint8 // 用户类型
|
||||
ClientCode string
|
||||
Valid bool
|
||||
}
|
||||
|
||||
func firstMD(md metadata.MD, keys ...string) string {
|
||||
for _, key := range keys {
|
||||
vals := md.Get(key)
|
||||
if len(vals) > NumberZero && vals[NumberZero] != StringEmpty {
|
||||
return vals[NumberZero]
|
||||
}
|
||||
}
|
||||
return StringEmpty
|
||||
}
|
||||
|
||||
func parseInt64MD(md metadata.MD, keys ...string) int64 {
|
||||
raw := firstMD(md, keys...)
|
||||
if raw == StringEmpty {
|
||||
return NumberZero
|
||||
}
|
||||
v, _ := strconv.ParseInt(raw, NumberTen, NumberSixtyFourth)
|
||||
return v
|
||||
}
|
||||
|
||||
func GetUserFromCtx(ctx context.Context) UserInfo {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
return UserInfo{Valid: false}
|
||||
}
|
||||
uidList := md.Get("x-user-id")
|
||||
nameList := md.Get("x-user-name")
|
||||
refreshList := md.Get("x-refresh")
|
||||
ipList := md.Get("x-client-ip")
|
||||
userAgentList := md.Get("x-user-agent")
|
||||
|
||||
storeName := md.Get("x-store-name")
|
||||
storeId := md.Get("x-store-id")
|
||||
saleName := md.Get("x-sale-name")
|
||||
saleId := md.Get("x-sale-id")
|
||||
groupId := md.Get("x-group-id")
|
||||
groupName := md.Get("x-group-name")
|
||||
Type := md.Get("x-user-type")
|
||||
province := md.Get("x-sale-province")
|
||||
mobile := md.Get("x-sale-mobile")
|
||||
|
||||
if len(refreshList) == NumberZero {
|
||||
refreshList = md.Get("X-Refresh")
|
||||
}
|
||||
var userInfo UserInfo
|
||||
if len(uidList) > NumberZero {
|
||||
userInfo.RawUID = uidList[NumberZero]
|
||||
}
|
||||
if len(nameList) > NumberZero {
|
||||
userInfo.Name = nameList[NumberZero]
|
||||
}
|
||||
if len(storeName) > NumberZero {
|
||||
userInfo.StoreName = storeName[NumberZero]
|
||||
userInfo := UserInfo{
|
||||
RawUID: firstMD(md, "x-user-id"),
|
||||
Name: firstMD(md, "x-user-name"),
|
||||
Refresh: firstMD(md, "x-refresh", "X-Refresh"),
|
||||
ClientIP: firstMD(md, "x-client-ip"),
|
||||
UserAgent: firstMD(md, "x-user-agent"),
|
||||
StoreName: firstMD(md, "x-store-name"),
|
||||
StoreId: parseInt64MD(md, "x-store-id"),
|
||||
SaleName: firstMD(md, "x-sale-name"),
|
||||
SaleId: parseInt64MD(md, "x-sale-id"),
|
||||
SaleMobile: firstMD(md, "x-sale-mobile"),
|
||||
SaleProvince: parseInt64MD(md, "x-sale-province"),
|
||||
GroupId: parseInt64MD(md, "x-group-id"),
|
||||
GroupName: firstMD(md, "x-group-name"),
|
||||
ClientCode: firstMD(md, "x-client-code"),
|
||||
}
|
||||
|
||||
if len(groupName) > NumberZero {
|
||||
userInfo.GroupName = groupName[NumberZero]
|
||||
}
|
||||
if len(Type) > NumberZero {
|
||||
TypeInt, _ := strconv.ParseInt(Type[NumberZero], NumberTen, NumberSixtyFourth)
|
||||
userInfo.Type = uint8(TypeInt)
|
||||
if typeRaw := firstMD(md, "x-user-type"); typeRaw != StringEmpty {
|
||||
typeInt, _ := strconv.ParseInt(typeRaw, NumberTen, NumberSixtyFourth)
|
||||
userInfo.Type = uint8(typeInt)
|
||||
}
|
||||
|
||||
if len(storeId) > NumberZero {
|
||||
userInfo.StoreId, _ = strconv.ParseInt(storeId[NumberZero], NumberTen, NumberSixtyFourth)
|
||||
}
|
||||
if len(saleName) > NumberZero {
|
||||
userInfo.SaleName = saleName[NumberZero]
|
||||
}
|
||||
if len(province) > NumberZero {
|
||||
userInfo.SaleProvince, _ = strconv.ParseInt(province[NumberZero], NumberTen, NumberSixtyFourth)
|
||||
}
|
||||
if len(mobile) > NumberZero {
|
||||
userInfo.SaleMobile = mobile[NumberZero]
|
||||
}
|
||||
if len(saleId) > NumberZero {
|
||||
userInfo.SaleId, _ = strconv.ParseInt(saleId[NumberZero], NumberTen, NumberSixtyFourth)
|
||||
}
|
||||
if len(groupId) > NumberZero {
|
||||
userInfo.GroupId, _ = strconv.ParseInt(groupId[NumberZero], NumberTen, NumberSixtyFourth)
|
||||
}
|
||||
if len(refreshList) > NumberZero {
|
||||
userInfo.Refresh = refreshList[NumberZero]
|
||||
}
|
||||
if len(ipList) > NumberZero {
|
||||
userInfo.ClientIP = ipList[NumberZero]
|
||||
}
|
||||
if len(userAgentList) > NumberZero {
|
||||
userInfo.UserAgent = userAgentList[NumberZero]
|
||||
if userInfo.UserAgent != StringEmpty {
|
||||
userInfo.UserAgent, _ = url.QueryUnescape(userInfo.UserAgent)
|
||||
ua := user_agent.New(userInfo.UserAgent)
|
||||
userInfo.BrowserName, userInfo.BrowserVer = ua.Browser()
|
||||
@@ -116,6 +96,15 @@ func GetUserFromCtx(ctx context.Context) UserInfo {
|
||||
userName, _ := url.QueryUnescape(userInfo.Name)
|
||||
userInfo.ID = uid
|
||||
userInfo.Name = userName
|
||||
if userInfo.SaleName != StringEmpty {
|
||||
userInfo.SaleName, _ = url.QueryUnescape(userInfo.SaleName)
|
||||
}
|
||||
if userInfo.StoreName != StringEmpty {
|
||||
userInfo.StoreName, _ = url.QueryUnescape(userInfo.StoreName)
|
||||
}
|
||||
if userInfo.GroupName != StringEmpty {
|
||||
userInfo.GroupName, _ = url.QueryUnescape(userInfo.GroupName)
|
||||
}
|
||||
userInfo.Valid = true
|
||||
|
||||
return userInfo
|
||||
|
||||
Reference in New Issue
Block a user