Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5686e7b7
authored
Sep 24, 2024
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 更新令牌生成算法
parent
05f35fd0
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
16 deletions
+50
-16
common/utils.go
+18
-13
controller/token.go
+10
-1
controller/user.go
+22
-2
No files found.
common/utils.go
View file @
5686e7b7
package
common
import
(
crand
"crypto/rand"
"fmt"
"github.com/google/uuid"
"html/template"
"log"
"math/big"
"math/rand"
"net"
"os/exec"
...
...
@@ -145,21 +147,24 @@ func init() {
rand
.
Seed
(
time
.
Now
()
.
UnixNano
())
}
func
GenerateKey
()
string
{
//rand.Seed(time.Now().UnixNano())
key
:=
make
([]
byte
,
48
)
for
i
:=
0
;
i
<
16
;
i
++
{
key
[
i
]
=
keyChars
[
rand
.
Intn
(
len
(
keyChars
))]
}
uuid_
:=
GetUUID
()
for
i
:=
0
;
i
<
32
;
i
++
{
c
:=
uuid_
[
i
]
if
i
%
2
==
0
&&
c
>=
'a'
&&
c
<=
'z'
{
c
=
c
-
'a'
+
'A'
func
GenerateRandomKey
(
length
int
)
(
string
,
error
)
{
b
:=
make
([]
byte
,
length
)
maxI
:=
big
.
NewInt
(
int64
(
len
(
keyChars
)))
for
i
:=
range
b
{
n
,
err
:=
crand
.
Int
(
crand
.
Reader
,
maxI
)
if
err
!=
nil
{
return
""
,
err
}
key
[
i
+
16
]
=
c
b
[
i
]
=
keyChars
[
n
.
Int64
()]
}
return
string
(
key
)
return
string
(
b
),
nil
}
func
GenerateKey
()
(
string
,
error
)
{
//rand.Seed(time.Now().UnixNano())
return
GenerateRandomKey
(
48
)
}
func
GetRandomInt
(
max
int
)
int
{
...
...
controller/token.go
View file @
5686e7b7
...
...
@@ -123,10 +123,19 @@ func AddToken(c *gin.Context) {
})
return
}
key
,
err
:=
common
.
GenerateKey
()
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
"生成令牌失败"
,
})
common
.
SysError
(
"failed to generate token key: "
+
err
.
Error
())
return
}
cleanToken
:=
model
.
Token
{
UserId
:
c
.
GetInt
(
"id"
),
Name
:
token
.
Name
,
Key
:
common
.
GenerateKey
()
,
Key
:
key
,
CreatedTime
:
common
.
GetTimestamp
(),
AccessedTime
:
common
.
GetTimestamp
(),
ExpiredTime
:
token
.
ExpiredTime
,
...
...
controller/user.go
View file @
5686e7b7
...
...
@@ -200,11 +200,20 @@ func Register(c *gin.Context) {
}
// 生成默认令牌
if
constant
.
GenerateDefaultToken
{
key
,
err
:=
common
.
GenerateKey
()
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
"生成默认令牌失败"
,
})
common
.
SysError
(
"failed to generate token key: "
+
err
.
Error
())
return
}
// 生成默认令牌
token
:=
model
.
Token
{
UserId
:
insertedUser
.
Id
,
// 使用插入后的用户ID
Name
:
cleanUser
.
Username
+
"的初始令牌"
,
Key
:
common
.
GenerateKey
()
,
Key
:
key
,
CreatedTime
:
common
.
GetTimestamp
(),
AccessedTime
:
common
.
GetTimestamp
(),
ExpiredTime
:
-
1
,
// 永不过期
...
...
@@ -311,7 +320,18 @@ func GenerateAccessToken(c *gin.Context) {
})
return
}
user
.
SetAccessToken
(
common
.
GetUUID
())
// get rand int 28-32
randI
:=
common
.
GetRandomInt
(
4
)
key
,
err
:=
common
.
GenerateRandomKey
(
29
+
randI
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
"生成失败"
,
})
common
.
SysError
(
"failed to generate key: "
+
err
.
Error
())
return
}
user
.
SetAccessToken
(
key
)
if
model
.
DB
.
Where
(
"access_token = ?"
,
user
.
AccessToken
)
.
First
(
user
)
.
RowsAffected
!=
0
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
...
...
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