feat: Optimize login workflow (#345)

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

* fix: build RedirectURL by host and scheme, not Origin
This commit is contained in:
Tao Chen
2025-07-31 10:46:11 +08:00
committed by GitHub
parent 862a1d431e
commit b6be4dea21
2 changed files with 24 additions and 5 deletions

View File

@@ -169,6 +169,8 @@ func (ct *Login) LoginOptions(c *gin.Context) {
"ops": ops, "ops": ops,
"register": global.Config.App.Register, "register": global.Config.App.Register,
"need_captcha": needCaptcha, "need_captcha": needCaptcha,
"disable_pwd": global.Config.App.DisablePwdLogin,
"auto_oidc": global.Config.App.DisablePwdLogin && len(ops) == 1,
}) })
} }

View File

@@ -180,14 +180,12 @@ func (os *OauthService) GetOauthConfig(c *gin.Context, op string) (err error, oa
if oauthInfo.Id == 0 || oauthInfo.ClientId == "" || oauthInfo.ClientSecret == "" { if oauthInfo.Id == 0 || oauthInfo.ClientId == "" || oauthInfo.ClientSecret == "" {
return errors.New("ConfigNotFound"), nil, nil, nil return errors.New("ConfigNotFound"), nil, nil, nil
} }
host := c.GetHeader("Origin") redirectUrl := os.buildRedirectURL(c)
if host == "" { Logger.Debug("Redirect URL: ", redirectUrl)
host = Config.Rustdesk.ApiServer
}
oauthConfig = &oauth2.Config{ oauthConfig = &oauth2.Config{
ClientID: oauthInfo.ClientId, ClientID: oauthInfo.ClientId,
ClientSecret: oauthInfo.ClientSecret, ClientSecret: oauthInfo.ClientSecret,
RedirectURL: host + "/api/oidc/callback", RedirectURL: redirectUrl,
} }
// Maybe should validate the oauthConfig here // Maybe should validate the oauthConfig here
@@ -529,3 +527,22 @@ func (os *OauthService) getGithubPrimaryEmail(client *http.Client, githubUser *m
return fmt.Errorf("no primary verified email found") return fmt.Errorf("no primary verified email found")
} }
func (os *OauthService) buildRedirectURL(c *gin.Context) string {
baseUrl := Config.Rustdesk.ApiServer
host := c.Request.Host
if host != "" {
scheme := c.GetHeader("X-Forwarded-Proto")
if scheme == "" {
if c.Request.TLS != nil {
scheme = "https"
} else {
scheme = "http"
}
}
baseUrl = fmt.Sprintf("%s://%s", scheme, host)
}
return fmt.Sprintf("%s/api/oidc/callback", baseUrl)
}