mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-09-17 16:56:38 +08:00
feat: add authorization for local function call
This commit is contained in:
parent
243b5be31c
commit
cf4b04e047
@ -7,11 +7,14 @@ import (
|
|||||||
"chatplus/store/model"
|
"chatplus/store/model"
|
||||||
"chatplus/utils"
|
"chatplus/utils"
|
||||||
"chatplus/utils/resp"
|
"chatplus/utils/resp"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/golang-jwt/jwt/v5"
|
||||||
"github.com/imroc/req/v3"
|
"github.com/imroc/req/v3"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FunctionHandler struct {
|
type FunctionHandler struct {
|
||||||
@ -50,8 +53,41 @@ type dataItem struct {
|
|||||||
Remark string `json:"remark"`
|
Remark string `json:"remark"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check authorization
|
||||||
|
func (h *FunctionHandler) checkAuth(c *gin.Context) error {
|
||||||
|
tokenString := c.GetHeader(types.UserAuthHeader)
|
||||||
|
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||||
|
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||||
|
}
|
||||||
|
|
||||||
|
return []byte(h.App.Config.Session.SecretKey), nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error with parse auth token: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
claims, ok := token.Claims.(jwt.MapClaims)
|
||||||
|
if !ok || !token.Valid {
|
||||||
|
return errors.New("token is invalid")
|
||||||
|
}
|
||||||
|
|
||||||
|
expr := utils.IntValue(utils.InterfaceToString(claims["expired"]), 0)
|
||||||
|
if expr > 0 && int64(expr) < time.Now().Unix() {
|
||||||
|
return errors.New("token is expired")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// WeiBo 微博热搜
|
// WeiBo 微博热搜
|
||||||
func (h *FunctionHandler) WeiBo(c *gin.Context) {
|
func (h *FunctionHandler) WeiBo(c *gin.Context) {
|
||||||
|
if err := h.checkAuth(c); err != nil {
|
||||||
|
resp.ERROR(c, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if h.config.Token == "" {
|
if h.config.Token == "" {
|
||||||
resp.ERROR(c, "无效的 API Token")
|
resp.ERROR(c, "无效的 API Token")
|
||||||
return
|
return
|
||||||
@ -83,6 +119,11 @@ func (h *FunctionHandler) WeiBo(c *gin.Context) {
|
|||||||
|
|
||||||
// ZaoBao 今日早报
|
// ZaoBao 今日早报
|
||||||
func (h *FunctionHandler) ZaoBao(c *gin.Context) {
|
func (h *FunctionHandler) ZaoBao(c *gin.Context) {
|
||||||
|
if err := h.checkAuth(c); err != nil {
|
||||||
|
resp.ERROR(c, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if h.config.Token == "" {
|
if h.config.Token == "" {
|
||||||
resp.ERROR(c, "无效的 API Token")
|
resp.ERROR(c, "无效的 API Token")
|
||||||
return
|
return
|
||||||
@ -139,6 +180,11 @@ type ErrRes struct {
|
|||||||
|
|
||||||
// Dall3 DallE3 AI 绘图
|
// Dall3 DallE3 AI 绘图
|
||||||
func (h *FunctionHandler) Dall3(c *gin.Context) {
|
func (h *FunctionHandler) Dall3(c *gin.Context) {
|
||||||
|
if err := h.checkAuth(c); err != nil {
|
||||||
|
resp.ERROR(c, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var params map[string]interface{}
|
var params map[string]interface{}
|
||||||
if err := c.ShouldBindJSON(¶ms); err != nil {
|
if err := c.ShouldBindJSON(¶ms); err != nil {
|
||||||
resp.ERROR(c, types.InvalidArgs)
|
resp.ERROR(c, types.InvalidArgs)
|
||||||
|
@ -299,6 +299,7 @@
|
|||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
border-radius: 6px;
|
border-radius: 6px;
|
||||||
transition: all 0.3s ease; /* 添加过渡效果 */
|
transition: all 0.3s ease; /* 添加过渡效果 */
|
||||||
|
position: relative;
|
||||||
}
|
}
|
||||||
.page-mj .inner .task-list-box .finish-job-list .job-item .opt .opt-line {
|
.page-mj .inner .task-list-box .finish-job-list .job-item .opt .opt-line {
|
||||||
margin: 6px 0;
|
margin: 6px 0;
|
||||||
@ -327,6 +328,15 @@
|
|||||||
font-size: 20px;
|
font-size: 20px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
.page-mj .inner .task-list-box .finish-job-list .job-item .remove {
|
||||||
|
display: none;
|
||||||
|
position: absolute;
|
||||||
|
right: 10px;
|
||||||
|
top: 10px;
|
||||||
|
}
|
||||||
|
.page-mj .inner .task-list-box .finish-job-list .job-item:hover .remove {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
.page-mj .inner .task-list-box .finish-job-list .animate:hover {
|
.page-mj .inner .task-list-box .finish-job-list .animate:hover {
|
||||||
box-shadow: 0 0 10px rgba(71,255,241,0.6); /* 添加阴影效果 */
|
box-shadow: 0 0 10px rgba(71,255,241,0.6); /* 添加阴影效果 */
|
||||||
transform: translateY(-10px); /* 向上移动10像素 */
|
transform: translateY(-10px); /* 向上移动10像素 */
|
||||||
|
@ -184,6 +184,7 @@
|
|||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
border-radius: 6px;
|
border-radius: 6px;
|
||||||
transition: all 0.3s ease; /* 添加过渡效果 */
|
transition: all 0.3s ease; /* 添加过渡效果 */
|
||||||
|
position: relative;
|
||||||
}
|
}
|
||||||
.page-sd .inner .task-list-box .finish-job-list .job-item .opt .opt-line {
|
.page-sd .inner .task-list-box .finish-job-list .job-item .opt .opt-line {
|
||||||
margin: 6px 0;
|
margin: 6px 0;
|
||||||
@ -212,6 +213,15 @@
|
|||||||
font-size: 20px;
|
font-size: 20px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
.page-sd .inner .task-list-box .finish-job-list .job-item .remove {
|
||||||
|
display: none;
|
||||||
|
position: absolute;
|
||||||
|
right: 10px;
|
||||||
|
top: 10px;
|
||||||
|
}
|
||||||
|
.page-sd .inner .task-list-box .finish-job-list .job-item:hover .remove {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
.page-sd .inner .task-list-box .finish-job-list .animate:hover {
|
.page-sd .inner .task-list-box .finish-job-list .animate:hover {
|
||||||
box-shadow: 0 0 10px rgba(71,255,241,0.6); /* 添加阴影效果 */
|
box-shadow: 0 0 10px rgba(71,255,241,0.6); /* 添加阴影效果 */
|
||||||
transform: translateY(-10px); /* 向上移动10像素 */
|
transform: translateY(-10px); /* 向上移动10像素 */
|
||||||
|
@ -148,6 +148,7 @@
|
|||||||
overflow hidden
|
overflow hidden
|
||||||
border-radius 6px
|
border-radius 6px
|
||||||
transition: all 0.3s ease; /* 添加过渡效果 */
|
transition: all 0.3s ease; /* 添加过渡效果 */
|
||||||
|
position relative
|
||||||
|
|
||||||
.opt {
|
.opt {
|
||||||
.opt-line {
|
.opt-line {
|
||||||
@ -183,8 +184,22 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.remove {
|
||||||
|
display none
|
||||||
|
position absolute
|
||||||
|
right 10px
|
||||||
|
top 10px
|
||||||
|
}
|
||||||
|
|
||||||
|
&:hover{
|
||||||
|
.remove {
|
||||||
|
display block
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
.animate {
|
.animate {
|
||||||
&:hover {
|
&:hover {
|
||||||
box-shadow: 0 0 10px rgba(71, 255, 241, 0.6); /* 添加阴影效果 */
|
box-shadow: 0 0 10px rgba(71, 255, 241, 0.6); /* 添加阴影效果 */
|
||||||
|
Loading…
Reference in New Issue
Block a user