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
56231eea
authored
Apr 26, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: prevent common user from specifying channel id (#12)
parent
3616adfc
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
1 deletions
+26
-1
README.md
+1
-0
middleware/auth.go
+12
-1
model/user.go
+13
-0
No files found.
README.md
View file @
56231eea
...
...
@@ -138,6 +138,7 @@ sudo service nginx restart
之后就可以使用你的令牌访问 One API 了,使用方式与 [OpenAI API](https://platform.openai.com/docs/api-reference/introduction) 一致。
可以通过在令牌后面添加渠道 ID 的方式指定使用哪一个渠道处理本次请求,例如:`
Authorization: Bearer ONE_API_KEY-CHANNEL_ID
`。
注意,需要是管理员用户创建的令牌才能指定渠道 ID。
不加的话将会使用负载均衡的方式使用多个渠道。
...
...
middleware/auth.go
View file @
56231eea
...
...
@@ -83,7 +83,18 @@ func TokenAuth() func(c *gin.Context) {
c
.
Set
(
"token_id"
,
token
.
Id
)
c
.
Set
(
"unlimited_times"
,
token
.
UnlimitedTimes
)
if
len
(
parts
)
>
1
{
c
.
Set
(
"channelId"
,
parts
[
1
])
if
model
.
IsAdmin
(
token
.
UserId
)
{
c
.
Set
(
"channelId"
,
parts
[
1
])
}
else
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"error"
:
gin
.
H
{
"message"
:
"普通用户不支持指定渠道"
,
"type"
:
"one_api_error"
,
},
})
c
.
Abort
()
return
}
}
c
.
Next
()
}
...
...
model/user.go
View file @
56231eea
...
...
@@ -175,3 +175,16 @@ func ResetUserPasswordByEmail(email string, password string) error {
err
=
DB
.
Model
(
&
User
{})
.
Where
(
"email = ?"
,
email
)
.
Update
(
"password"
,
hashedPassword
)
.
Error
return
err
}
func
IsAdmin
(
userId
int
)
bool
{
if
userId
==
0
{
return
false
}
var
user
User
err
:=
DB
.
Where
(
"id = ?"
,
userId
)
.
Select
(
"role"
)
.
Find
(
&
user
)
.
Error
if
err
!=
nil
{
common
.
SysError
(
"No such user "
+
err
.
Error
())
return
false
}
return
user
.
Role
>=
common
.
RoleAdminUser
}
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