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

update v2 api

parent a94f0866
...@@ -17,8 +17,10 @@ func initRouter(e *gin.Engine) { ...@@ -17,8 +17,10 @@ func initRouter(e *gin.Engine) {
user.POST("/serverLogin", login) user.POST("/serverLogin", login)
user.POST("/create", middleware.JWTMiddleware, createUser) user.POST("/create", middleware.JWTMiddleware, createUser)
} }
v2 := e.Group("/api/v2")
{ {
v1.GET("/task/status", getTaskStatus) user := v2.Group("/user")
user.POST("/serverLogin", loginV2)
} }
} }
...@@ -203,3 +203,91 @@ func login(c *gin.Context) { ...@@ -203,3 +203,91 @@ func login(c *gin.Context) {
c.JSON(200, withSuccess(resp)) c.JSON(200, withSuccess(resp))
return return
} }
func loginV2(c *gin.Context) {
req := &apiModel.LoginRequest{}
if err := c.ShouldBindJSON(req); err != nil {
c.JSON(200, withError(constant.InvalidParam))
return
}
var platformId string
switch req.Platform {
case constant.PlatformTelegram:
var ok bool
var userId string
var botId string
for _, token := range conf.TGBot.Tokens {
ok, botId, userId = util.VerifyInitData(req.InitData, token)
if ok {
break
}
}
if !ok {
c.JSON(200, withError("invalid initData"))
return
}
_ = botId
// platformId = fmt.Sprintf("%s:%s", botId, userId)
platformId = fmt.Sprintf("v2:%s", userId)
case constant.PlatformFingerprint:
if len(req.VisitorID) <= 10 {
c.JSON(200, withError(constant.InvalidParam))
return
}
platformId = req.VisitorID
default:
c.JSON(200, withError(constant.UnsupportedPlatform))
return
}
// 检查签名是否为keystore中的地址
address := gjson.Get(req.Keystore, "address").String()
binSignature, err := hexutil.Decode(req.Signature)
if err != nil || len(binSignature) < 65 {
c.JSON(200, withError("invalid signature"))
return
}
binSignature[64] -= 27
ecdsaPub, err := crypto.SigToPub(accounts.TextHash([]byte(req.Keystore)), binSignature)
if err != nil {
c.JSON(200, withError("invalid signature"))
return
}
addr := crypto.PubkeyToAddress(*ecdsaPub)
if strings.ToLower(addr.Hex()[2:]) != address {
c.JSON(200, withError("invalid signature"))
return
}
isExistKeystore, uid, keystore, err := srv.CheckUser(req.Platform, platformId)
if err != nil {
c.JSON(200, withError(constant.InternalError))
return
}
token := util.GenerateJWT(uid, req.Platform, platformId)
resp := &apiModel.CheckUserResponse{
IsNewUser: !isExistKeystore,
Keystore: keystore,
Token: token,
Uid: uid,
}
if !isExistKeystore {
_, err = srv.SetKeystore(uid, address, req.Keystore)
if err != nil {
c.JSON(200, withError(constant.InternalError))
return
}
resp.Keystore = req.Keystore
}
taskId, err := srv.AONServerLogin(address, req.UserId, req.InviterId)
if err != nil {
c.JSON(200, withError(constant.InternalError))
return
}
resp.TaskId = taskId
c.JSON(200, withSuccess(resp))
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