You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/api/auth/auth.go

127 lines
5.2 KiB

3 years ago
package apiAuth
import (
api "Open_IM/pkg/base_info"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/log"
3 years ago
"Open_IM/pkg/common/token_verify"
3 years ago
"Open_IM/pkg/grpc-etcdv3/getcdv3"
rpc "Open_IM/pkg/proto/auth"
3 years ago
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
3 years ago
"Open_IM/pkg/utils"
3 years ago
"context"
3 years ago
"github.com/fatih/structs"
3 years ago
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func UserRegister(c *gin.Context) {
params := api.UserRegisterReq{}
if err := c.BindJSON(&params); err != nil {
3 years ago
errMsg := " BindJSON failed " + err.Error()
log.NewError("0", errMsg)
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": errMsg})
3 years ago
return
}
if params.Secret != config.Config.Secret {
3 years ago
errMsg := " params.Secret != config.Config.Secret "
log.NewError(params.OperationID, errMsg, params.Secret, config.Config.Secret)
c.JSON(http.StatusBadRequest, gin.H{"errCode": 401, "errMsg": errMsg})
3 years ago
return
}
3 years ago
req := &rpc.UserRegisterReq{UserInfo: &open_im_sdk.UserInfo{}}
3 years ago
utils.CopyStructFields(req.UserInfo, &params)
3 years ago
//copier.Copy(req.UserInfo, &params)
3 years ago
req.OperationID = params.OperationID
3 years ago
log.NewInfo(req.OperationID, "UserRegister args ", req.String())
3 years ago
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImAuthName)
client := rpc.NewAuthClient(etcdConn)
reply, err := client.UserRegister(context.Background(), req)
3 years ago
if err != nil {
3 years ago
errMsg := req.OperationID + " " + "UserRegister failed " + err.Error() + req.String()
3 years ago
log.NewError(req.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
3 years ago
return
}
if reply.CommonResp.ErrCode != 0 {
3 years ago
errMsg := req.OperationID + " " + " UserRegister failed " + reply.CommonResp.ErrMsg + req.String()
3 years ago
log.NewError(req.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
3 years ago
return
}
pbDataToken := &rpc.UserTokenReq{Platform: params.Platform, FromUserID: params.UserID, OperationID: params.OperationID}
replyToken, err := client.UserToken(context.Background(), pbDataToken)
if err != nil {
3 years ago
errMsg := req.OperationID + " " + " client.UserToken failed " + err.Error() + pbDataToken.String()
log.NewError(req.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
3 years ago
return
}
resp := api.UserRegisterResp{CommResp: api.CommResp{ErrCode: replyToken.CommonResp.ErrCode, ErrMsg: replyToken.CommonResp.ErrMsg},
UserToken: api.UserTokenInfo{UserID: req.UserInfo.UserID, Token: replyToken.Token, ExpiredTime: replyToken.ExpiredTime}}
log.NewInfo(req.OperationID, "UserRegister return ", resp)
3 years ago
c.JSON(http.StatusOK, resp)
3 years ago
}
func UserToken(c *gin.Context) {
params := api.UserTokenReq{}
if err := c.BindJSON(&params); err != nil {
3 years ago
errMsg := " BindJSON failed " + err.Error()
log.NewError("0", errMsg)
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": errMsg})
3 years ago
return
}
if params.Secret != config.Config.Secret {
3 years ago
errMsg := params.OperationID + " params.Secret != config.Config.Secret "
3 years ago
log.NewError(params.OperationID, "params.Secret != config.Config.Secret", params.Secret, config.Config.Secret)
3 years ago
c.JSON(http.StatusBadRequest, gin.H{"errCode": 401, "errMsg": errMsg})
3 years ago
return
}
req := &rpc.UserTokenReq{Platform: params.Platform, FromUserID: params.UserID, OperationID: params.OperationID}
3 years ago
log.NewInfo(req.OperationID, "UserToken args ", req.String())
3 years ago
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImAuthName)
client := rpc.NewAuthClient(etcdConn)
reply, err := client.UserToken(context.Background(), req)
if err != nil {
3 years ago
errMsg := req.OperationID + " UserToken failed " + err.Error() + req.String()
log.NewError(req.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
3 years ago
return
}
resp := api.UserTokenResp{CommResp: api.CommResp{ErrCode: reply.CommonResp.ErrCode, ErrMsg: reply.CommonResp.ErrMsg},
UserToken: api.UserTokenInfo{UserID: req.FromUserID, Token: reply.Token, ExpiredTime: reply.ExpiredTime}}
log.NewInfo(req.OperationID, "UserToken return ", resp)
3 years ago
c.JSON(http.StatusOK, resp)
3 years ago
}
3 years ago
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
}
3 years ago
resp := api.ParseTokenResp{CommResp: api.CommResp{ErrCode: 0, ErrMsg: ""}, ExpireTime: api.ExpireTime{ExpireTimeSeconds: uint32(expireTime)}}
3 years ago
resp.Data = structs.Map(&resp.ExpireTime)
3 years ago
log.NewInfo(params.OperationID, "ParseToken return ", resp)
c.JSON(http.StatusOK, resp)
}