Browse Source

:bug: Authentication middleware compatible (#9720)

Yingyi / 颖逸 1 year ago
parent
commit
f38c52292b
2 changed files with 25 additions and 5 deletions
  1. 6 0
      kernel/model/session.go
  2. 19 5
      kernel/util/working.go

+ 6 - 0
kernel/model/session.go

@@ -162,6 +162,12 @@ func CheckAuth(c *gin.Context) {
 
 
 	// 未设置访问授权码
 	// 未设置访问授权码
 	if "" == Conf.AccessAuthCode {
 	if "" == Conf.AccessAuthCode {
+		// Skip the empty access authorization code check https://github.com/siyuan-note/siyuan/issues/9709
+		if util.SIYUAN_ACCESS_AUTH_CODE_BYPASS {
+			c.Next()
+			return
+		}
+
 		// Authenticate requests with the Origin header other than 127.0.0.1 https://github.com/siyuan-note/siyuan/issues/9180
 		// Authenticate requests with the Origin header other than 127.0.0.1 https://github.com/siyuan-note/siyuan/issues/9180
 		clientIP := c.ClientIP()
 		clientIP := c.ClientIP()
 		host := c.GetHeader("Host")
 		host := c.GetHeader("Host")

+ 19 - 5
kernel/util/working.go

@@ -46,6 +46,21 @@ const (
 	IsInsider = false
 	IsInsider = false
 )
 )
 
 
+var (
+	RUN_IN_CONTAINER               = false // 是否运行在容器中
+	SIYUAN_ACCESS_AUTH_CODE_BYPASS = false // 是否跳过空访问授权码检查
+)
+
+func initEnvVars() {
+	var err error
+
+	RUN_IN_CONTAINER = isRunningInDockerContainer()
+
+	if SIYUAN_ACCESS_AUTH_CODE_BYPASS, err = strconv.ParseBool(os.Getenv("SIYUAN_ACCESS_AUTH_CODE_BYPASS")); nil != err {
+		SIYUAN_ACCESS_AUTH_CODE_BYPASS = false
+	}
+}
+
 var (
 var (
 	bootProgress float64 // 启动进度,从 0 到 100
 	bootProgress float64 // 启动进度,从 0 到 100
 	bootDetails  string  // 启动细节描述
 	bootDetails  string  // 启动细节描述
@@ -53,6 +68,7 @@ var (
 )
 )
 
 
 func Boot() {
 func Boot() {
+	initEnvVars()
 	IncBootProgress(3, "Booting kernel...")
 	IncBootProgress(3, "Booting kernel...")
 	rand.Seed(time.Now().UTC().UnixNano())
 	rand.Seed(time.Now().UTC().UnixNano())
 	initMime()
 	initMime()
@@ -79,15 +95,13 @@ func Boot() {
 	ReadOnly, _ = strconv.ParseBool(*readOnly)
 	ReadOnly, _ = strconv.ParseBool(*readOnly)
 	AccessAuthCode = *accessAuthCode
 	AccessAuthCode = *accessAuthCode
 	Container = ContainerStd
 	Container = ContainerStd
-	if isRunningInDockerContainer() {
+	if RUN_IN_CONTAINER {
 		Container = ContainerDocker
 		Container = ContainerDocker
 		if "" == AccessAuthCode {
 		if "" == AccessAuthCode {
 			interruptBoot := true
 			interruptBoot := true
 
 
-			// Set the env `SIYUAN_ACCESS_AUTH_CODE_BYPASS=true` to skip  checking access auth code when deploying Docker https://github.com/siyuan-note/siyuan/issues/9709
-			byPassEnv := os.Getenv("SIYUAN_ACCESS_AUTH_CODE_BYPASS")
-			bypass, parseErr := strconv.ParseBool(byPassEnv)
-			if nil == parseErr && bypass {
+			// Set the env `SIYUAN_ACCESS_AUTH_CODE_BYPASS=true` to skip checking empty access auth code https://github.com/siyuan-note/siyuan/issues/9709
+			if SIYUAN_ACCESS_AUTH_CODE_BYPASS {
 				interruptBoot = false
 				interruptBoot = false
 				fmt.Println("bypass access auth code check since the env [SIYUAN_ACCESS_AUTH_CODE_BYPASS] is set to [true]")
 				fmt.Println("bypass access auth code check since the env [SIYUAN_ACCESS_AUTH_CODE_BYPASS] is set to [true]")
 			}
 			}