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
4172e9b6
authored
Jun 03, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: the format of key is now constant with that of OpenAI
parent
b28b49bb
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
29 additions
and
4 deletions
+29
-4
common/utils.go
+24
-0
controller/token.go
+1
-1
model/token.go
+1
-1
web/src/components/TokensTable.js
+3
-2
No files found.
common/utils.go
View file @
4172e9b6
...
@@ -5,6 +5,7 @@ import (
...
@@ -5,6 +5,7 @@ import (
"github.com/google/uuid"
"github.com/google/uuid"
"html/template"
"html/template"
"log"
"log"
"math/rand"
"net"
"net"
"os/exec"
"os/exec"
"runtime"
"runtime"
...
@@ -133,6 +134,29 @@ func GetUUID() string {
...
@@ -133,6 +134,29 @@ func GetUUID() string {
return
code
return
code
}
}
const
keyChars
=
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
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'
}
key
[
i
+
16
]
=
c
}
return
string
(
key
)
}
func
GetTimestamp
()
int64
{
func
GetTimestamp
()
int64
{
return
time
.
Now
()
.
Unix
()
return
time
.
Now
()
.
Unix
()
}
}
...
...
controller/token.go
View file @
4172e9b6
...
@@ -119,7 +119,7 @@ func AddToken(c *gin.Context) {
...
@@ -119,7 +119,7 @@ func AddToken(c *gin.Context) {
cleanToken
:=
model
.
Token
{
cleanToken
:=
model
.
Token
{
UserId
:
c
.
GetInt
(
"id"
),
UserId
:
c
.
GetInt
(
"id"
),
Name
:
token
.
Name
,
Name
:
token
.
Name
,
Key
:
common
.
Ge
tUUID
(),
Key
:
common
.
Ge
nerateKey
(),
CreatedTime
:
common
.
GetTimestamp
(),
CreatedTime
:
common
.
GetTimestamp
(),
AccessedTime
:
common
.
GetTimestamp
(),
AccessedTime
:
common
.
GetTimestamp
(),
ExpiredTime
:
token
.
ExpiredTime
,
ExpiredTime
:
token
.
ExpiredTime
,
...
...
model/token.go
View file @
4172e9b6
...
@@ -11,7 +11,7 @@ import (
...
@@ -11,7 +11,7 @@ import (
type
Token
struct
{
type
Token
struct
{
Id
int
`json:"id"`
Id
int
`json:"id"`
UserId
int
`json:"user_id"`
UserId
int
`json:"user_id"`
Key
string
`json:"key" gorm:"type:char(
32
);uniqueIndex"`
Key
string
`json:"key" gorm:"type:char(
48
);uniqueIndex"`
Status
int
`json:"status" gorm:"default:1"`
Status
int
`json:"status" gorm:"default:1"`
Name
string
`json:"name" gorm:"index" `
Name
string
`json:"name" gorm:"index" `
CreatedTime
int64
`json:"created_time" gorm:"bigint"`
CreatedTime
int64
`json:"created_time" gorm:"bigint"`
...
...
web/src/components/TokensTable.js
View file @
4172e9b6
...
@@ -238,11 +238,12 @@ const TokensTable = () => {
...
@@ -238,11 +238,12 @@ const TokensTable = () => {
size
=
{
'small'
}
size
=
{
'small'
}
positive
positive
onClick
=
{
async
()
=>
{
onClick
=
{
async
()
=>
{
if
(
await
copy
(
token
.
key
))
{
let
key
=
"sk-"
+
token
.
key
;
if
(
await
copy
(
key
))
{
showSuccess
(
'已复制到剪贴板!'
);
showSuccess
(
'已复制到剪贴板!'
);
}
else
{
}
else
{
showWarning
(
'无法复制到剪贴板,请手动复制,已将令牌填入搜索框。'
);
showWarning
(
'无法复制到剪贴板,请手动复制,已将令牌填入搜索框。'
);
setSearchKeyword
(
token
.
key
);
setSearchKeyword
(
key
);
}
}
}}
}}
>
>
...
...
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