Skip to content

Commit 67e4885

Browse files
committed
fix: add authentication mechanism to management interface
1 parent 73fd87f commit 67e4885

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

helper/config.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ func init() {
5656

5757
// 获取配置目录
5858
func getConfigPath() string {
59-
appPath, err := os.Executable()
60-
if err == nil {
61-
appPath = filepath.Dir(appPath)
62-
if appPath != "" {
63-
return appPath + "/app.conf"
59+
if IsRelease() {
60+
appPath, err := os.Executable()
61+
if err == nil {
62+
appPath = filepath.Dir(appPath)
63+
if appPath != "" {
64+
return appPath + "/app.conf"
65+
}
6466
}
6567
}
6668
return "./app.conf"
@@ -109,7 +111,10 @@ func ReadConfig() (*DefaultConfig, error) {
109111
// 重新加载配置文件
110112
func LoadConfig(configStr string) (*DefaultConfig, error) {
111113
var config = &DefaultConfig{}
112-
yaml.Unmarshal([]byte(configStr), config)
114+
err := yaml.Unmarshal([]byte(configStr), config)
115+
if err != nil {
116+
return nil, err
117+
}
113118
return config, nil
114119
}
115120

helper/default.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import (
99
"math/rand"
1010
"os"
1111
"reflect"
12+
"strings"
1213
"time"
1314
)
1415

15-
var AppName = "server"
16+
var AppName = "gpt-zmide-server"
1617

1718
func init() {
1819
type obj struct{}
@@ -21,7 +22,8 @@ func init() {
2122
}
2223

2324
func IsRelease() bool {
24-
return os.Getenv("DEBUG") == ""
25+
arg1 := strings.ToLower(os.Args[0])
26+
return (!strings.Contains(arg1, "go-build") && os.Getenv("DEBUG") == "")
2527
}
2628

2729
// 生成随机字符串

middleware/admin.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* @Author: Bin
3+
* @Date: 2023-03-10
4+
* @FilePath: /gpt-zmide-server/middleware/admin.go
5+
*/
6+
package middleware
7+
8+
import (
9+
"crypto/md5"
10+
"encoding/base64"
11+
"fmt"
12+
"gpt-zmide-server/controllers/apis"
13+
"gpt-zmide-server/helper"
14+
"net/http"
15+
"strings"
16+
17+
"github.com/gin-gonic/gin"
18+
)
19+
20+
func adminCredential(token string) bool {
21+
if token == "" {
22+
return false
23+
}
24+
25+
if decoded, err := base64.StdEncoding.DecodeString(token); err == nil && decoded != nil {
26+
if input := strings.Split(string(decoded), ":"); len(input) == 2 {
27+
userObj := helper.Config.AdminUser
28+
user, password := input[0], fmt.Sprintf("%x", md5.Sum([]byte(input[1])))
29+
if user == userObj.User && password == userObj.Password {
30+
return true
31+
}
32+
}
33+
}
34+
35+
return false
36+
}
37+
38+
func BasicAuthAdmin() gin.HandlerFunc {
39+
return func(c *gin.Context) {
40+
// Search user in the slice of allowed credentials
41+
auth := strings.Replace(c.Request.Header.Get("Authorization"), "Basic ", "", -1)
42+
if auth == "" {
43+
auth = c.Query("token")
44+
}
45+
46+
if !adminCredential(auth) {
47+
// Credentials doesn't match, we return 401 and abort handlers chain.
48+
apis.APIDefaultController.Fail(c, "请登录管理员账号")
49+
c.AbortWithStatus(http.StatusUnauthorized)
50+
return
51+
}
52+
53+
}
54+
}

routers/routers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func BuildRouter(r *gin.Engine) *gin.Engine {
4141
openApis.POST("/", apisCtlOpen.Index)
4242
openApis.POST("/query", apisCtlOpen.Query)
4343

44-
adminApis := api.Group("/admin")
44+
adminApis := api.Group("/admin", middleware.BasicAuthAdmin())
4545

4646
// 后台管理应用接口
4747
adminApp := adminApis.Group("/application")

0 commit comments

Comments
 (0)