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
530e846a
authored
Jan 05, 2025
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: access_token auth
parent
cb5a1392
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
58 additions
and
5 deletions
+58
-5
.dockerignore
+2
-0
docs/api/api_auth.md
+53
-0
docs/api/user.md
+0
-0
middleware/auth.go
+3
-5
No files found.
.dockerignore
View file @
530e846a
...
@@ -4,3 +4,4 @@
...
@@ -4,3 +4,4 @@
.vscode
.vscode
.gitignore
.gitignore
Makefile
Makefile
docs
\ No newline at end of file
docs/api/api_auth.md
0 → 100644
View file @
530e846a
# API 鉴权文档
## 认证方式
### Access Token
对于需要鉴权的 API 接口,必须同时提供以下两个请求头来进行 Access Token 认证:
1.
**请求头中的 `Authorization` 字段**
将 Access Token 放置于 HTTP 请求头部的 `Authorization` 字段中,格式如下:
```
Authorization: <your_access_token>
```
其中 `<your_access_token>` 需要替换为实际的 Access Token 值。
2.
**请求头中的 `New-Api-User` 字段**
将用户 ID 放置于 HTTP 请求头部的 `New-Api-User` 字段中,格式如下:
```
New-Api-User: <your_user_id>
```
其中 `<your_user_id>` 需要替换为实际的用户 ID。
**注意:**
*
**必须同时提供 `Authorization` 和 `New-Api-User` 两个请求头才能通过鉴权。**
*
如果只提供其中一个请求头,或者两个请求头都未提供,则会返回
`401 Unauthorized`
错误。
*
如果
`Authorization`
中的 Access Token 无效,则会返回
`401 Unauthorized`
错误,并提示“无权进行此操作,access token 无效”。
*
如果
`New-Api-User`
中的用户 ID 与 Access Token 不匹配,则会返回
`401 Unauthorized`
错误,并提示“无权进行此操作,与登录用户不匹配,请重新登录”。
*
如果没有提供
`New-Api-User`
请求头,则会返回
`401 Unauthorized`
错误,并提示“无权进行此操作,未提供 New-Api-User”。
*
如果
`New-Api-User`
请求头格式错误,则会返回
`401 Unauthorized`
错误,并提示“无权进行此操作,New-Api-User 格式错误”。
*
如果用户已被禁用,则会返回
`403 Forbidden`
错误,并提示“用户已被封禁”。
*
如果用户权限不足,则会返回
`403 Forbidden`
错误,并提示“无权进行此操作,权限不足”。
*
如果用户信息无效,则会返回
`403 Forbidden`
错误,并提示“无权进行此操作,用户信息无效”。
## Curl 示例
假设您的 Access Token 为
`access_token`
,用户 ID 为
`123`
,要访问的 API 接口为
`/api/user/self`
,则可以使用以下 curl 命令:
```
bash
curl
-X
GET
\
-H
"Authorization: access_token"
\
-H
"New-Api-User: 123"
\
https://your-domain.com/api/user/self
```
请将
`access_token`
、
`123`
和
`https://your-domain.com`
替换为实际的值。
docs/api/user.md
0 → 100644
View file @
530e846a
middleware/auth.go
View file @
530e846a
...
@@ -64,13 +64,12 @@ func authHelper(c *gin.Context, minRole int) {
...
@@ -64,13 +64,12 @@ func authHelper(c *gin.Context, minRole int) {
return
return
}
}
}
}
if
!
useAccessToken
{
// get header New-Api-User
// get header New-Api-User
apiUserIdStr
:=
c
.
Request
.
Header
.
Get
(
"New-Api-User"
)
apiUserIdStr
:=
c
.
Request
.
Header
.
Get
(
"New-Api-User"
)
if
apiUserIdStr
==
""
{
if
apiUserIdStr
==
""
{
c
.
JSON
(
http
.
StatusUnauthorized
,
gin
.
H
{
c
.
JSON
(
http
.
StatusUnauthorized
,
gin
.
H
{
"success"
:
false
,
"success"
:
false
,
"message"
:
"无权进行此操作,请刷新页面或清空缓存后重试
"
,
"message"
:
"无权进行此操作,未提供 New-Api-User
"
,
})
})
c
.
Abort
()
c
.
Abort
()
return
return
...
@@ -79,7 +78,7 @@ func authHelper(c *gin.Context, minRole int) {
...
@@ -79,7 +78,7 @@ func authHelper(c *gin.Context, minRole int) {
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusUnauthorized
,
gin
.
H
{
c
.
JSON
(
http
.
StatusUnauthorized
,
gin
.
H
{
"success"
:
false
,
"success"
:
false
,
"message"
:
"无权进行此操作,登录信息无效,请重新登录
"
,
"message"
:
"无权进行此操作,New-Api-User 格式错误
"
,
})
})
c
.
Abort
()
c
.
Abort
()
return
return
...
@@ -88,12 +87,11 @@ func authHelper(c *gin.Context, minRole int) {
...
@@ -88,12 +87,11 @@ func authHelper(c *gin.Context, minRole int) {
if
id
!=
apiUserId
{
if
id
!=
apiUserId
{
c
.
JSON
(
http
.
StatusUnauthorized
,
gin
.
H
{
c
.
JSON
(
http
.
StatusUnauthorized
,
gin
.
H
{
"success"
:
false
,
"success"
:
false
,
"message"
:
"无权进行此操作,与登录用户不匹配,请重新登录
"
,
"message"
:
"无权进行此操作,New-Api-User 与登录用户不匹配
"
,
})
})
c
.
Abort
()
c
.
Abort
()
return
return
}
}
}
if
status
.
(
int
)
==
common
.
UserStatusDisabled
{
if
status
.
(
int
)
==
common
.
UserStatusDisabled
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"success"
:
false
,
...
...
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