Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
sdk-api
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Odysseus
sdk-api
Commits
2aab245c
Commit
2aab245c
authored
Jun 26, 2024
by
贾浩@五瓣科技
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update telegram userId
parent
b495f58e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
6 deletions
+13
-6
user.go
server/user.go
+6
-4
telegram.go
util/telegram.go
+7
-2
No files found.
server/user.go
View file @
2aab245c
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
,
user
Id
)
ok
,
uid
,
keystore
,
err
:=
srv
.
CheckUser
(
constant
.
PlatformTelegram
,
db
Id
)
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
,
user
Id
)
token
:=
util
.
GenerateJWT
(
uid
,
constant
.
PlatformTelegram
,
db
Id
)
resp
:=
&
apiModel
.
CheckUserResponse
{
resp
:=
&
apiModel
.
CheckUserResponse
{
IsNewUser
:
!
ok
,
IsNewUser
:
!
ok
,
...
...
util/telegram.go
View file @
2aab245c
...
@@ -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
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment