Browse Source

fix(casdoor): use certificate path instead to address env issue #584

Jacky 8 months ago
parent
commit
20e55e715d

+ 14 - 4
api/user/casdoor.go

@@ -11,6 +11,7 @@ import (
 	"gorm.io/gorm"
 	"gorm.io/gorm"
 	"net/http"
 	"net/http"
 	"net/url"
 	"net/url"
+	"os"
 )
 )
 
 
 type CasdoorLoginUser struct {
 type CasdoorLoginUser struct {
@@ -29,17 +30,24 @@ func CasdoorCallback(c *gin.Context) {
 	endpoint := settings.CasdoorSettings.Endpoint
 	endpoint := settings.CasdoorSettings.Endpoint
 	clientId := settings.CasdoorSettings.ClientId
 	clientId := settings.CasdoorSettings.ClientId
 	clientSecret := settings.CasdoorSettings.ClientSecret
 	clientSecret := settings.CasdoorSettings.ClientSecret
-	certificate := settings.CasdoorSettings.Certificate
+	certificatePath := settings.CasdoorSettings.CertificatePath
 	organization := settings.CasdoorSettings.Organization
 	organization := settings.CasdoorSettings.Organization
 	application := settings.CasdoorSettings.Application
 	application := settings.CasdoorSettings.Application
-	if endpoint == "" || clientId == "" || clientSecret == "" || certificate == "" || organization == "" || application == "" {
+	if endpoint == "" || clientId == "" || clientSecret == "" || certificatePath == "" ||
+		organization == "" || application == "" {
 		c.JSON(http.StatusInternalServerError, gin.H{
 		c.JSON(http.StatusInternalServerError, gin.H{
 			"message": "Casdoor is not configured",
 			"message": "Casdoor is not configured",
 		})
 		})
 		return
 		return
 	}
 	}
 
 
-	casdoorsdk.InitConfig(endpoint, clientId, clientSecret, certificate, organization, application)
+	certBytes, err := os.ReadFile(certificatePath)
+	if err != nil {
+		api.ErrHandler(c, err)
+		return
+	}
+
+	casdoorsdk.InitConfig(endpoint, clientId, clientSecret, string(certBytes), organization, application)
 
 
 	token, err := casdoorsdk.GetOAuthToken(loginUser.Code, loginUser.State)
 	token, err := casdoorsdk.GetOAuthToken(loginUser.Code, loginUser.State)
 	if err != nil {
 	if err != nil {
@@ -93,6 +101,8 @@ func GetCasdoorUri(c *gin.Context) {
 	encodedRedirectUri := url.QueryEscape(redirectUri)
 	encodedRedirectUri := url.QueryEscape(redirectUri)
 
 
 	c.JSON(http.StatusOK, gin.H{
 	c.JSON(http.StatusOK, gin.H{
-		"uri": fmt.Sprintf("%s/login/oauth/authorize?client_id=%s&response_type=code&redirect_uri=%s&state=%s&scope=read", endpoint, clientId, encodedRedirectUri, state),
+		"uri": fmt.Sprintf(
+			"%s/login/oauth/authorize?client_id=%s&response_type=code&redirect_uri=%s&state=%s&scope=read",
+			endpoint, clientId, encodedRedirectUri, state),
 	})
 	})
 }
 }

+ 2 - 2
docs/guide/config-casdoor.md

@@ -21,10 +21,10 @@ It is used to identify your application during the authentication process.
 This is the Client Secret generated by Casdoor for your application.
 This is the Client Secret generated by Casdoor for your application.
 It is necessary to keep your application secure.
 It is necessary to keep your application secure.
 
 
-## Certificate
+## CertificatePath
 - Type: `string`
 - Type: `string`
 
 
-This is the certificate used during the authentication process.
+This is the path of the certificate used during the authentication process.
 Make sure it is valid and trusted.
 Make sure it is valid and trusted.
 
 
 ## Organization
 ## Organization

+ 9 - 9
docs/guide/env.md

@@ -47,15 +47,15 @@ Applicable for version v2.0.0-beta.23 and above.
 
 
 ## Casdoor
 ## Casdoor
 
 
-| Configuration Setting | Environment Variable           |
-|-----------------------|--------------------------------|
-| Endpoint              | NGINX_UI_CASDOOR_ENDPOINT      |
-| ClientId              | NGINX_UI_CASDOOR_CLIENT_ID     |
-| ClientSecret          | NGINX_UI_CASDOOR_CLIENT_SECRET |
-| Certificate           | NGINX_UI_CASDOOR_CERTIFICATE   |
-| Organization          | NGINX_UI_CASDOOR_ORGANIZATION  |
-| Application           | NGINX_UI_CASDOOR_APPLICATION   |
-| RedirectUri           | NGINX_UI_CASDOOR_REDIRECT_URI  |
+| Configuration Setting | Environment Variable              |
+|-----------------------|-----------------------------------|
+| Endpoint              | NGINX_UI_CASDOOR_ENDPOINT         |
+| ClientId              | NGINX_UI_CASDOOR_CLIENT_ID        |
+| ClientSecret          | NGINX_UI_CASDOOR_CLIENT_SECRET    |
+| CertificatePath       | NGINX_UI_CASDOOR_CERTIFICATE_PATH |
+| Organization          | NGINX_UI_CASDOOR_ORGANIZATION     |
+| Application           | NGINX_UI_CASDOOR_APPLICATION      |
+| RedirectUri           | NGINX_UI_CASDOOR_REDIRECT_URI     |
 
 
 ## Logrotate
 ## Logrotate
 
 

+ 2 - 2
docs/zh_CN/guide/config-casdoor.md

@@ -18,10 +18,10 @@ Casdoor 是一个强大的、全面的身份认证解决方案,支持 OAuth 2.
 
 
 这是 Casdoor 为您的应用生成的客户端密钥。它是保持您的应用安全所必需的。
 这是 Casdoor 为您的应用生成的客户端密钥。它是保持您的应用安全所必需的。
 
 
-## Certificate
+## CertificatePath
 - 类型:`string`
 - 类型:`string`
 
 
-这是用于身份验证过程中的证书。确保它是有效和可信的。
+这是用于身份验证过程中的证书的路径。确保它是有效和可信的。
 
 
 ## Organization
 ## Organization
 - 类型:`string`
 - 类型:`string`

+ 9 - 9
docs/zh_CN/guide/env.md

@@ -46,15 +46,15 @@
 
 
 ## Casdoor
 ## Casdoor
 
 
-| Configuration Setting         | Environment Variable                  |
-| ----------------------------- | ------------------------------------- |
-| Endpoint                      | NGINX_UI_CASDOOR_ENDPOINT             |
-| ClientId                      | NGINX_UI_CASDOOR_CLIENT_ID            |
-| ClientSecret                  | NGINX_UI_CASDOOR_CLIENT_SECRET        |
-| Certificate                   | NGINX_UI_CASDOOR_CERTIFICATE          |
-| Organization                  | NGINX_UI_CASDOOR_ORGANIZATION         |
-| Application                   | NGINX_UI_CASDOOR_APPLICATION          |
-| RedirectUri                   | NGINX_UI_CASDOOR_REDIRECT_URI         |
+| Configuration Setting | Environment Variable              |
+|-----------------------|-----------------------------------|
+| Endpoint              | NGINX_UI_CASDOOR_ENDPOINT         |
+| ClientId              | NGINX_UI_CASDOOR_CLIENT_ID        |
+| ClientSecret          | NGINX_UI_CASDOOR_CLIENT_SECRET    |
+| CertificatePath       | NGINX_UI_CASDOOR_CERTIFICATE_PATH |
+| Organization          | NGINX_UI_CASDOOR_ORGANIZATION     |
+| Application           | NGINX_UI_CASDOOR_APPLICATION      |
+| RedirectUri           | NGINX_UI_CASDOOR_REDIRECT_URI     |
 
 
 ## Logrotate
 ## Logrotate
 
 

+ 1 - 1
docs/zh_TW/guide/config-casdoor.md

@@ -21,7 +21,7 @@ Casdoor 是一個強大的、全面的身份認證解決方案,支持 OAuth 2.
 ## Certificate
 ## Certificate
 - 類型:`string`
 - 類型:`string`
 
 
-這是用於身份驗證過程中的證書。確保它是有效和可信的。
+這是用於身份驗證過程中的證書的路徑。確保它是有效和可信的。
 
 
 ## Organization
 ## Organization
 - 類型:`string`
 - 類型:`string`

+ 9 - 9
docs/zh_TW/guide/env.md

@@ -46,15 +46,15 @@
 
 
 ## Casdoor
 ## Casdoor
 
 
-| Configuration Setting         | Environment Variable                  |
-| ----------------------------- | ------------------------------------- |
-| Endpoint                      | NGINX_UI_CASDOOR_ENDPOINT             |
-| ClientId                      | NGINX_UI_CASDOOR_CLIENT_ID            |
-| ClientSecret                  | NGINX_UI_CASDOOR_CLIENT_SECRET        |
-| Certificate                   | NGINX_UI_CASDOOR_CERTIFICATE          |
-| Organization                  | NGINX_UI_CASDOOR_ORGANIZATION         |
-| Application                   | NGINX_UI_CASDOOR_APPLICATION          |
-| RedirectUri                   | NGINX_UI_CASDOOR_REDIRECT_URI         |
+| Configuration Setting | Environment Variable              |
+|-----------------------|-----------------------------------|
+| Endpoint              | NGINX_UI_CASDOOR_ENDPOINT         |
+| ClientId              | NGINX_UI_CASDOOR_CLIENT_ID        |
+| ClientSecret          | NGINX_UI_CASDOOR_CLIENT_SECRET    |
+| CertificatePath       | NGINX_UI_CASDOOR_CERTIFICATE_PATH |
+| Organization          | NGINX_UI_CASDOOR_ORGANIZATION     |
+| Application           | NGINX_UI_CASDOOR_APPLICATION      |
+| RedirectUri           | NGINX_UI_CASDOOR_REDIRECT_URI     |
 
 
 ## Logrotate
 ## Logrotate
 
 

+ 14 - 14
settings/casdoor.go

@@ -1,21 +1,21 @@
 package settings
 package settings
 
 
 type Casdoor struct {
 type Casdoor struct {
-	Endpoint     string `json:"endpoint" protected:"true"`
-	ClientId     string `json:"client_id" protected:"true"`
-	ClientSecret string `json:"client_secret" protected:"true"`
-	Certificate  string `json:"certificate" protected:"true"`
-	Organization string `json:"organization" protected:"true"`
-	Application  string `json:"application" protected:"true"`
-	RedirectUri  string `json:"redirect_uri" protected:"true"`
+	Endpoint        string `json:"endpoint" protected:"true"`
+	ClientId        string `json:"client_id" protected:"true"`
+	ClientSecret    string `json:"client_secret" protected:"true"`
+	CertificatePath string `json:"certificate_path" protected:"true"`
+	Organization    string `json:"organization" protected:"true"`
+	Application     string `json:"application" protected:"true"`
+	RedirectUri     string `json:"redirect_uri" protected:"true"`
 }
 }
 
 
 var CasdoorSettings = Casdoor{
 var CasdoorSettings = Casdoor{
-	Endpoint:     "",
-	ClientId:     "",
-	ClientSecret: "",
-	Certificate:  "",
-	Organization: "",
-	Application:  "",
-	RedirectUri:  "",
+	Endpoint:        "",
+	ClientId:        "",
+	ClientSecret:    "",
+	CertificatePath: "",
+	Organization:    "",
+	Application:     "",
+	RedirectUri:     "",
 }
 }

+ 2 - 2
settings/settings_test.go

@@ -44,7 +44,7 @@ func TestSetup(t *testing.T) {
 	_ = os.Setenv("NGINX_UI_CASDOOR_ENDPOINT", "https://casdoor.example.com")
 	_ = os.Setenv("NGINX_UI_CASDOOR_ENDPOINT", "https://casdoor.example.com")
 	_ = os.Setenv("NGINX_UI_CASDOOR_CLIENT_ID", "clientId")
 	_ = os.Setenv("NGINX_UI_CASDOOR_CLIENT_ID", "clientId")
 	_ = os.Setenv("NGINX_UI_CASDOOR_CLIENT_SECRET", "clientSecret")
 	_ = os.Setenv("NGINX_UI_CASDOOR_CLIENT_SECRET", "clientSecret")
-	_ = os.Setenv("NGINX_UI_CASDOOR_CERTIFICATE", "cert.pem")
+	_ = os.Setenv("NGINX_UI_CASDOOR_CERTIFICATE_PATH", "cert.pem")
 	_ = os.Setenv("NGINX_UI_CASDOOR_ORGANIZATION", "org1")
 	_ = os.Setenv("NGINX_UI_CASDOOR_ORGANIZATION", "org1")
 	_ = os.Setenv("NGINX_UI_CASDOOR_APPLICATION", "app1")
 	_ = os.Setenv("NGINX_UI_CASDOOR_APPLICATION", "app1")
 	_ = os.Setenv("NGINX_UI_CASDOOR_REDIRECT_URI", "https://redirect.example.com")
 	_ = os.Setenv("NGINX_UI_CASDOOR_REDIRECT_URI", "https://redirect.example.com")
@@ -93,7 +93,7 @@ func TestSetup(t *testing.T) {
 	assert.Equal(t, "https://casdoor.example.com", CasdoorSettings.Endpoint)
 	assert.Equal(t, "https://casdoor.example.com", CasdoorSettings.Endpoint)
 	assert.Equal(t, "clientId", CasdoorSettings.ClientId)
 	assert.Equal(t, "clientId", CasdoorSettings.ClientId)
 	assert.Equal(t, "clientSecret", CasdoorSettings.ClientSecret)
 	assert.Equal(t, "clientSecret", CasdoorSettings.ClientSecret)
-	assert.Equal(t, "cert.pem", CasdoorSettings.Certificate)
+	assert.Equal(t, "cert.pem", CasdoorSettings.CertificatePath)
 	assert.Equal(t, "org1", CasdoorSettings.Organization)
 	assert.Equal(t, "org1", CasdoorSettings.Organization)
 	assert.Equal(t, "app1", CasdoorSettings.Application)
 	assert.Equal(t, "app1", CasdoorSettings.Application)
 	assert.Equal(t, "https://redirect.example.com", CasdoorSettings.RedirectUri)
 	assert.Equal(t, "https://redirect.example.com", CasdoorSettings.RedirectUri)