Просмотр исходного кода

fix: The callback URL is based on the configured API SERVER because the project might be behind an Nginx reverse proxy. If the Origin/Host is forgotten to configure the reverse proxy, it will be incorrect

lejianwen месяцев назад: 5
Родитель
Сommit
c14c4d478b
5 измененных файлов с 14 добавлено и 39 удалено
  1. 3 3
      http/controller/admin/login.go
  2. 1 1
      http/controller/admin/oauth.go
  3. 2 2
      http/controller/api/ouath.go
  4. 1 0
      model/oauth.go
  5. 7 33
      service/oauth.go

+ 3 - 3
http/controller/admin/login.go

@@ -169,8 +169,8 @@ func (ct *Login) LoginOptions(c *gin.Context) {
169
 		"ops":          ops,
169
 		"ops":          ops,
170
 		"register":     global.Config.App.Register,
170
 		"register":     global.Config.App.Register,
171
 		"need_captcha": needCaptcha,
171
 		"need_captcha": needCaptcha,
172
-		"disable_pwd": 	global.Config.App.DisablePwdLogin,
173
-		"auto_oidc":  	global.Config.App.DisablePwdLogin && len(ops) == 1,
172
+		"disable_pwd":  global.Config.App.DisablePwdLogin,
173
+		"auto_oidc":    global.Config.App.DisablePwdLogin && len(ops) == 1,
174
 	})
174
 	})
175
 }
175
 }
176
 
176
 
@@ -191,7 +191,7 @@ func (ct *Login) OidcAuth(c *gin.Context) {
191
 		return
191
 		return
192
 	}
192
 	}
193
 
193
 
194
-	err, state, verifier, nonce, url := service.AllService.OauthService.BeginAuth(c, f.Op)
194
+	err, state, verifier, nonce, url := service.AllService.OauthService.BeginAuth(f.Op)
195
 	if err != nil {
195
 	if err != nil {
196
 		response.Error(c, response.TranslateMsg(c, err.Error()))
196
 		response.Error(c, response.TranslateMsg(c, err.Error()))
197
 		return
197
 		return

+ 1 - 1
http/controller/admin/oauth.go

@@ -44,7 +44,7 @@ func (o *Oauth) ToBind(c *gin.Context) {
44
 		return
44
 		return
45
 	}
45
 	}
46
 
46
 
47
-	err, state, verifier, nonce, url := service.AllService.OauthService.BeginAuth(c, f.Op)
47
+	err, state, verifier, nonce, url := service.AllService.OauthService.BeginAuth(f.Op)
48
 	if err != nil {
48
 	if err != nil {
49
 		response.Error(c, response.TranslateMsg(c, err.Error()))
49
 		response.Error(c, response.TranslateMsg(c, err.Error()))
50
 		return
50
 		return

+ 2 - 2
http/controller/api/ouath.go

@@ -36,7 +36,7 @@ func (o *Oauth) OidcAuth(c *gin.Context) {
36
 
36
 
37
 	oauthService := service.AllService.OauthService
37
 	oauthService := service.AllService.OauthService
38
 
38
 
39
-	err, state, verifier, nonce, url := oauthService.BeginAuth(c, f.Op)
39
+	err, state, verifier, nonce, url := oauthService.BeginAuth(f.Op)
40
 	if err != nil {
40
 	if err != nil {
41
 		response.Error(c, response.TranslateMsg(c, err.Error()))
41
 		response.Error(c, response.TranslateMsg(c, err.Error()))
42
 		return
42
 		return
@@ -170,7 +170,7 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
170
 	var user *model.User
170
 	var user *model.User
171
 	// 获取用户信息
171
 	// 获取用户信息
172
 	code := c.Query("code")
172
 	code := c.Query("code")
173
-	err, oauthUser := oauthService.Callback(c, code, verifier, op, nonce)
173
+	err, oauthUser := oauthService.Callback(code, verifier, op, nonce)
174
 	if err != nil {
174
 	if err != nil {
175
 		c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
175
 		c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
176
 			"message":     "OauthFailed",
176
 			"message":     "OauthFailed",

+ 1 - 0
model/oauth.go

@@ -41,6 +41,7 @@ type Oauth struct {
41
 	OauthType    string `json:"oauth_type"`
41
 	OauthType    string `json:"oauth_type"`
42
 	ClientId     string `json:"client_id"`
42
 	ClientId     string `json:"client_id"`
43
 	ClientSecret string `json:"client_secret"`
43
 	ClientSecret string `json:"client_secret"`
44
+	//RedirectUrl  string `json:"redirect_url"`
44
 	AutoRegister *bool  `json:"auto_register"`
45
 	AutoRegister *bool  `json:"auto_register"`
45
 	Scopes       string `json:"scopes"`
46
 	Scopes       string `json:"scopes"`
46
 	Issuer       string `json:"issuer"`
47
 	Issuer       string `json:"issuer"`

+ 7 - 33
service/oauth.go

@@ -6,7 +6,6 @@ import (
6
 	"errors"
6
 	"errors"
7
 
7
 
8
 	"github.com/coreos/go-oidc/v3/oidc"
8
 	"github.com/coreos/go-oidc/v3/oidc"
9
-	"github.com/gin-gonic/gin"
10
 	"github.com/lejianwen/rustdesk-api/v2/model"
9
 	"github.com/lejianwen/rustdesk-api/v2/model"
11
 	"github.com/lejianwen/rustdesk-api/v2/utils"
10
 	"github.com/lejianwen/rustdesk-api/v2/utils"
12
 	"golang.org/x/oauth2"
11
 	"golang.org/x/oauth2"
@@ -96,20 +95,16 @@ func (os *OauthService) DeleteOauthCache(key string) {
96
 	OauthCache.Delete(key)
95
 	OauthCache.Delete(key)
97
 }
96
 }
98
 
97
 
99
-func (os *OauthService) BeginAuth(c *gin.Context, op string) (error error, state, verifier, nonce, url string) {
98
+func (os *OauthService) BeginAuth(op string) (error error, state, verifier, nonce, url string) {
100
 	state = utils.RandomString(10) + strconv.FormatInt(time.Now().Unix(), 10)
99
 	state = utils.RandomString(10) + strconv.FormatInt(time.Now().Unix(), 10)
101
 	verifier = ""
100
 	verifier = ""
102
 	nonce = ""
101
 	nonce = ""
103
 	if op == model.OauthTypeWebauth {
102
 	if op == model.OauthTypeWebauth {
104
-		host := c.GetHeader("Origin")
105
-		if host == "" {
106
-			host = Config.Rustdesk.ApiServer
107
-		}
108
-		url = host + "/_admin/#/oauth/" + state
103
+		url = Config.Rustdesk.ApiServer + "/_admin/#/oauth/" + state
109
 		//url = "http://localhost:8888/_admin/#/oauth/" + code
104
 		//url = "http://localhost:8888/_admin/#/oauth/" + code
110
 		return nil, state, verifier, nonce, url
105
 		return nil, state, verifier, nonce, url
111
 	}
106
 	}
112
-	err, oauthInfo, oauthConfig, _ := os.GetOauthConfig(c, op)
107
+	err, oauthInfo, oauthConfig, _ := os.GetOauthConfig(op)
113
 	if err == nil {
108
 	if err == nil {
114
 		extras := make([]oauth2.AuthCodeOption, 0, 3)
109
 		extras := make([]oauth2.AuthCodeOption, 0, 3)
115
 
110
 
@@ -174,18 +169,16 @@ func (os *OauthService) LinuxdoProvider() *oidc.Provider {
174
 }
169
 }
175
 
170
 
176
 // GetOauthConfig retrieves the OAuth2 configuration based on the provider name
171
 // GetOauthConfig retrieves the OAuth2 configuration based on the provider name
177
-func (os *OauthService) GetOauthConfig(c *gin.Context, op string) (err error, oauthInfo *model.Oauth, oauthConfig *oauth2.Config, provider *oidc.Provider) {
172
+func (os *OauthService) GetOauthConfig(op string) (err error, oauthInfo *model.Oauth, oauthConfig *oauth2.Config, provider *oidc.Provider) {
178
 	//err, oauthInfo, oauthConfig = os.getOauthConfigGeneral(op)
173
 	//err, oauthInfo, oauthConfig = os.getOauthConfigGeneral(op)
179
 	oauthInfo = os.InfoByOp(op)
174
 	oauthInfo = os.InfoByOp(op)
180
 	if oauthInfo.Id == 0 || oauthInfo.ClientId == "" || oauthInfo.ClientSecret == "" {
175
 	if oauthInfo.Id == 0 || oauthInfo.ClientId == "" || oauthInfo.ClientSecret == "" {
181
 		return errors.New("ConfigNotFound"), nil, nil, nil
176
 		return errors.New("ConfigNotFound"), nil, nil, nil
182
 	}
177
 	}
183
-	redirectUrl := os.buildRedirectURL(c)
184
-	Logger.Debug("Redirect URL: ", redirectUrl)
185
 	oauthConfig = &oauth2.Config{
178
 	oauthConfig = &oauth2.Config{
186
 		ClientID:     oauthInfo.ClientId,
179
 		ClientID:     oauthInfo.ClientId,
187
 		ClientSecret: oauthInfo.ClientSecret,
180
 		ClientSecret: oauthInfo.ClientSecret,
188
-		RedirectURL:  redirectUrl,
181
+		RedirectURL:  Config.Rustdesk.ApiServer + "/api/oidc/callback",
189
 	}
182
 	}
190
 
183
 
191
 	// Maybe should validate the oauthConfig here
184
 	// Maybe should validate the oauthConfig here
@@ -340,8 +333,8 @@ func (os *OauthService) oidcCallback(oauthConfig *oauth2.Config, provider *oidc.
340
 }
333
 }
341
 
334
 
342
 // Callback: Get user information by code and op(Oauth provider)
335
 // Callback: Get user information by code and op(Oauth provider)
343
-func (os *OauthService) Callback(c *gin.Context, code, verifier, op, nonce string) (err error, oauthUser *model.OauthUser) {
344
-	err, oauthInfo, oauthConfig, provider := os.GetOauthConfig(c, op)
336
+func (os *OauthService) Callback(code, verifier, op, nonce string) (err error, oauthUser *model.OauthUser) {
337
+	err, oauthInfo, oauthConfig, provider := os.GetOauthConfig(op)
345
 	// oauthType is already validated in GetOauthConfig
338
 	// oauthType is already validated in GetOauthConfig
346
 	if err != nil {
339
 	if err != nil {
347
 		return err, nil
340
 		return err, nil
@@ -527,22 +520,3 @@ func (os *OauthService) getGithubPrimaryEmail(client *http.Client, githubUser *m
527
 
520
 
528
 	return fmt.Errorf("no primary verified email found")
521
 	return fmt.Errorf("no primary verified email found")
529
 }
522
 }
530
-
531
-func (os *OauthService) buildRedirectURL(c *gin.Context) string {
532
-	baseUrl := Config.Rustdesk.ApiServer
533
-	host := c.Request.Host
534
-
535
-	if host != "" {
536
-		scheme := c.GetHeader("X-Forwarded-Proto")
537
-		if scheme == "" {
538
-			if c.Request.TLS != nil {
539
-				scheme = "https"
540
-			} else {
541
-				scheme = "http"
542
-			}
543
-		}
544
-		baseUrl = fmt.Sprintf("%s://%s", scheme, host)
545
-	}
546
-
547
-	return fmt.Sprintf("%s/api/oidc/callback", baseUrl)
548
-}