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

feat: Optimize login workflow (#345)

* add "disable_pwd" and "auto_oidc" at /admin/login-options

* fix: build RedirectURL by host and scheme, not Origin
Tao Chen месяцев назад: 6
Родитель
Сommit
b6be4dea21
2 измененных файлов с 24 добавлено и 5 удалено
  1. 2 0
      http/controller/admin/login.go
  2. 22 5
      service/oauth.go

+ 2 - 0
http/controller/admin/login.go

@@ -169,6 +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
 	})
174
 	})
173
 }
175
 }
174
 
176
 

+ 22 - 5
service/oauth.go

@@ -180,14 +180,12 @@ func (os *OauthService) GetOauthConfig(c *gin.Context, op string) (err error, oa
180
 	if oauthInfo.Id == 0 || oauthInfo.ClientId == "" || oauthInfo.ClientSecret == "" {
180
 	if oauthInfo.Id == 0 || oauthInfo.ClientId == "" || oauthInfo.ClientSecret == "" {
181
 		return errors.New("ConfigNotFound"), nil, nil, nil
181
 		return errors.New("ConfigNotFound"), nil, nil, nil
182
 	}
182
 	}
183
-	host := c.GetHeader("Origin")
184
-	if host == "" {
185
-		host = Config.Rustdesk.ApiServer
186
-	}
183
+	redirectUrl := os.buildRedirectURL(c)
184
+	Logger.Debug("Redirect URL: ", redirectUrl)
187
 	oauthConfig = &oauth2.Config{
185
 	oauthConfig = &oauth2.Config{
188
 		ClientID:     oauthInfo.ClientId,
186
 		ClientID:     oauthInfo.ClientId,
189
 		ClientSecret: oauthInfo.ClientSecret,
187
 		ClientSecret: oauthInfo.ClientSecret,
190
-		RedirectURL:  host + "/api/oidc/callback",
188
+		RedirectURL:  redirectUrl,
191
 	}
189
 	}
192
 
190
 
193
 	// Maybe should validate the oauthConfig here
191
 	// Maybe should validate the oauthConfig here
@@ -529,3 +527,22 @@ func (os *OauthService) getGithubPrimaryEmail(client *http.Client, githubUser *m
529
 
527
 
530
 	return fmt.Errorf("no primary verified email found")
528
 	return fmt.Errorf("no primary verified email found")
531
 }
529
 }
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
+}