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

update telegram userId

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