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

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 169
 		"ops":          ops,
170 170
 		"register":     global.Config.App.Register,
171 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 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 195
 	if err != nil {
196 196
 		response.Error(c, response.TranslateMsg(c, err.Error()))
197 197
 		return

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

@@ -44,7 +44,7 @@ func (o *Oauth) ToBind(c *gin.Context) {
44 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 48
 	if err != nil {
49 49
 		response.Error(c, response.TranslateMsg(c, err.Error()))
50 50
 		return

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

@@ -36,7 +36,7 @@ func (o *Oauth) OidcAuth(c *gin.Context) {
36 36
 
37 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 40
 	if err != nil {
41 41
 		response.Error(c, response.TranslateMsg(c, err.Error()))
42 42
 		return
@@ -170,7 +170,7 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
170 170
 	var user *model.User
171 171
 	// 获取用户信息
172 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 174
 	if err != nil {
175 175
 		c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
176 176
 			"message":     "OauthFailed",

+ 1 - 0
model/oauth.go

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

+ 7 - 33
service/oauth.go

@@ -6,7 +6,6 @@ import (
6 6
 	"errors"
7 7
 
8 8
 	"github.com/coreos/go-oidc/v3/oidc"
9
-	"github.com/gin-gonic/gin"
10 9
 	"github.com/lejianwen/rustdesk-api/v2/model"
11 10
 	"github.com/lejianwen/rustdesk-api/v2/utils"
12 11
 	"golang.org/x/oauth2"
@@ -96,20 +95,16 @@ func (os *OauthService) DeleteOauthCache(key string) {
96 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 99
 	state = utils.RandomString(10) + strconv.FormatInt(time.Now().Unix(), 10)
101 100
 	verifier = ""
102 101
 	nonce = ""
103 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 104
 		//url = "http://localhost:8888/_admin/#/oauth/" + code
110 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 108
 	if err == nil {
114 109
 		extras := make([]oauth2.AuthCodeOption, 0, 3)
115 110
 
@@ -174,18 +169,16 @@ func (os *OauthService) LinuxdoProvider() *oidc.Provider {
174 169
 }
175 170
 
176 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 173
 	//err, oauthInfo, oauthConfig = os.getOauthConfigGeneral(op)
179 174
 	oauthInfo = os.InfoByOp(op)
180 175
 	if oauthInfo.Id == 0 || oauthInfo.ClientId == "" || oauthInfo.ClientSecret == "" {
181 176
 		return errors.New("ConfigNotFound"), nil, nil, nil
182 177
 	}
183
-	redirectUrl := os.buildRedirectURL(c)
184
-	Logger.Debug("Redirect URL: ", redirectUrl)
185 178
 	oauthConfig = &oauth2.Config{
186 179
 		ClientID:     oauthInfo.ClientId,
187 180
 		ClientSecret: oauthInfo.ClientSecret,
188
-		RedirectURL:  redirectUrl,
181
+		RedirectURL:  Config.Rustdesk.ApiServer + "/api/oidc/callback",
189 182
 	}
190 183
 
191 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 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 338
 	// oauthType is already validated in GetOauthConfig
346 339
 	if err != nil {
347 340
 		return err, nil
@@ -527,22 +520,3 @@ func (os *OauthService) getGithubPrimaryEmail(client *http.Client, githubUser *m
527 520
 
528 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
-}