-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: dao
- Loading branch information
Showing
14 changed files
with
96 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,4 +118,3 @@ $RECYCLE.BIN/ | |
|
||
# Air | ||
tmp | ||
conf/nuxbt.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,27 @@ | ||
# NuxBT-Backend | ||
|
||
### Dev | ||
|
||
lint | ||
|
||
```shell | ||
make lint | ||
``` | ||
|
||
test | ||
|
||
```shell | ||
make test | ||
``` | ||
|
||
gorm gen | ||
|
||
```shell | ||
make gen | ||
``` | ||
|
||
build | ||
|
||
```shell | ||
make build | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,47 @@ | ||
package jwt | ||
|
||
import ( | ||
"github.com/TensoRaws/NuxBT-Backend/module/cache" | ||
"github.com/TensoRaws/NuxBT-Backend/module/log" | ||
"github.com/TensoRaws/NuxBT-Backend/module/util" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// RequireAuth 鉴权中间件 | ||
// 如果用户携带的 token 验证通过,将 user_id 存入上下文中然后执行下一个 Handler | ||
func RequireAuth() gin.HandlerFunc { | ||
func RequireAuth(redisClient *cache.Client, addBlacklist bool) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
// 从输入的 url 中查询 token 值 | ||
token := c.Query("token") | ||
// auth = [[header][claims][signature]] | ||
// 解析 token | ||
// 从请求头中获取 token | ||
token := c.Request.Header.Get("Authorization") | ||
|
||
log.Logger.Info("Get token successfully") | ||
|
||
// 检查 Token 是否存在于 Redis 黑名单中 | ||
exists := redisClient.Exists(token).Val() | ||
if exists > 0 { | ||
log.Logger.Info("Token has been blacklisted") | ||
util.AbortWithMsg(c, "Token has been blacklisted") | ||
return | ||
} | ||
// 如果 Token 不在黑名单中,继续处理请求 | ||
claims, err := ParseToken(token) | ||
if err != nil { | ||
util.AbortWithMsg(c, "TOKEN IS INVALID, Please Log In") | ||
return | ||
} | ||
|
||
userID := claims.ID | ||
// 在上下文中存储 token 和 user_id | ||
c.Set("token", token) | ||
c.Set("user_id", userID) | ||
// 放行 | ||
c.Next() | ||
|
||
// 如果启用拉黑模式,处理请求拉黑 Token | ||
if addBlacklist { | ||
err := redisClient.Set(token, "", GetJWTTokenExpiredDuration()).Err() | ||
if err != nil { | ||
log.Logger.Error("Error adding token to blacklist: " + err.Error()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.