Commit 2aab245c authored by 贾浩@五瓣科技's avatar 贾浩@五瓣科技

update telegram userId

parent b495f58e
package server
import (
"fmt"
"sdk_api/constant"
apiModel "sdk_api/model/api"
"sdk_api/util"
......@@ -23,8 +24,9 @@ func checkUser(c *gin.Context) {
case constant.PlatformTelegram:
var ok bool
var userId string
var botId string
for _, token := range conf.TGBot.Tokens {
ok, userId = util.VerifyInitData(req.InitData, token)
ok, botId, userId = util.VerifyInitData(req.InitData, token)
if ok {
break
}
......@@ -33,14 +35,14 @@ func checkUser(c *gin.Context) {
c.JSON(200, withError("invalid initData"))
return
}
ok, uid, keystore, err := srv.CheckUser(constant.PlatformTelegram, userId)
dbId := fmt.Sprintf("%s:%s", botId, userId)
ok, uid, keystore, err := srv.CheckUser(constant.PlatformTelegram, dbId)
if err != nil {
c.JSON(200, withError(constant.InternalError))
return
}
token := util.GenerateJWT(uid, constant.PlatformTelegram, userId)
token := util.GenerateJWT(uid, constant.PlatformTelegram, dbId)
resp := &apiModel.CheckUserResponse{
IsNewUser: !ok,
......
......@@ -13,7 +13,7 @@ import (
"github.com/tidwall/gjson"
)
func VerifyInitData(initData, botToken string) (ok bool, userId string) {
func VerifyInitData(initData, botToken string) (ok bool, botId, userId string) {
h := hmac.New(sha256.New, []byte("WebAppData"))
h.Write([]byte(botToken))
secret := h.Sum(nil)
......@@ -34,7 +34,7 @@ func VerifyInitData(initData, botToken string) (ok bool, userId string) {
authDate, _ := strconv.Atoi(params.Get(key))
if int64(authDate) < time.Now().Unix()-3600 || int64(authDate) > time.Now().Unix()+300 {
// todo 可以限制超时时间
return false, ""
return false, "", ""
}
}
......@@ -53,6 +53,11 @@ func VerifyInitData(initData, botToken string) (ok bool, userId string) {
payload := strings.Join(payloads, "\n")
h2.Write([]byte(payload))
h2sum := h2.Sum(nil)
items := strings.Split(botToken, ":")
if len(items) != 2 {
return
}
ok = fmt.Sprintf("%x", h2sum) == hashval
botId = items[0]
return
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment