add ParseToken

pull/232/head
wenxu12345 3 years ago
parent deed6c7bbe
commit 7de29c29e9

@ -1 +1 @@
Subproject commit 856ac29b7c59a722c339a37d509035d94db8451f
Subproject commit 38bda4c08908a9f543925894ca50ee1802dd02e0

@ -4,6 +4,7 @@ import (
api "Open_IM/pkg/base_info"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/log"
"Open_IM/pkg/common/token_verify"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
rpc "Open_IM/pkg/proto/auth"
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
@ -96,3 +97,28 @@ func UserToken(c *gin.Context) {
log.NewInfo(req.OperationID, "UserToken return ", resp)
c.JSON(http.StatusOK, resp)
}
func ParseToken(c *gin.Context) {
params := api.ParseTokenReq{}
if err := c.BindJSON(&params); err != nil {
errMsg := " BindJSON failed " + err.Error()
log.NewError("0", errMsg)
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": errMsg})
return
}
var ok bool
var errInfo string
var expireTime int64
ok, _, errInfo, expireTime = token_verify.GetUserIDFromTokenExpireTime(c.Request.Header.Get("token"), params.OperationID)
if !ok {
errMsg := params.OperationID + " " + "GetUserIDFromTokenExpireTime failed " + errInfo + " token:" + c.Request.Header.Get("token")
log.NewError(params.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
resp := api.ParseTokenResp{CommResp: api.CommResp{ErrCode: 0, ErrMsg: ""}, ExpireTime: expireTime}
log.NewInfo(params.OperationID, "ParseToken return ", resp)
c.JSON(http.StatusOK, resp)
}

@ -37,3 +37,12 @@ type UserTokenResp struct {
CommResp
UserToken UserTokenInfo `json:"data"`
}
type ParseTokenReq struct {
OperationID string `json:"operationID" binding:"required"`
}
type ParseTokenResp struct {
CommResp
ExpireTime int64 `json:"token" binding:"required"`
}

@ -137,7 +137,16 @@ func GetUserIDFromToken(token string, operationID string) (bool, string, string)
}
log.Debug(operationID, "token claims.ExpiresAt.Second() ", claims.ExpiresAt.Unix())
return true, claims.UID, ""
claims.UID
}
func GetUserIDFromTokenExpireTime(token string, operationID string) (bool, string, string, int64) {
claims, err := ParseToken(token, operationID)
if err != nil {
log.Error(operationID, "ParseToken failed, ", err.Error(), token)
return false, "", err.Error(), 0
}
return true, claims.UID, "", claims.ExpiresAt.Unix()
}
func ParseTokenGetUserID(token string, operationID string) (error, string) {

Loading…
Cancel
Save