Skip to content

Commit db03f59

Browse files
committed
feat: align oauth endpoint with the hostname in requests
1 parent 98a310a commit db03f59

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

http/controller/admin/login.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package admin
22

33
import (
44
"fmt"
5+
56
"github.com/gin-gonic/gin"
67
"github.com/lejianwen/rustdesk-api/v2/global"
78
"github.com/lejianwen/rustdesk-api/v2/http/controller/api"
@@ -188,7 +189,7 @@ func (ct *Login) OidcAuth(c *gin.Context) {
188189
return
189190
}
190191

191-
err, state, verifier, nonce, url := service.AllService.OauthService.BeginAuth(f.Op)
192+
err, state, verifier, nonce, url := service.AllService.OauthService.BeginAuth(c, f.Op)
192193
if err != nil {
193194
response.Error(c, response.TranslateMsg(c, err.Error()))
194195
return

http/controller/admin/oauth.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package admin
22

33
import (
4+
"strconv"
5+
46
"github.com/gin-gonic/gin"
57
"github.com/lejianwen/rustdesk-api/v2/global"
68
"github.com/lejianwen/rustdesk-api/v2/http/request/admin"
79
adminReq "github.com/lejianwen/rustdesk-api/v2/http/request/admin"
810
"github.com/lejianwen/rustdesk-api/v2/http/response"
911
"github.com/lejianwen/rustdesk-api/v2/service"
10-
"strconv"
1112
)
1213

1314
type Oauth struct {
@@ -43,7 +44,7 @@ func (o *Oauth) ToBind(c *gin.Context) {
4344
return
4445
}
4546

46-
err, state, verifier, nonce, url := service.AllService.OauthService.BeginAuth(f.Op)
47+
err, state, verifier, nonce, url := service.AllService.OauthService.BeginAuth(c, f.Op)
4748
if err != nil {
4849
response.Error(c, response.TranslateMsg(c, err.Error()))
4950
return

http/controller/api/ouath.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package api
22

33
import (
4+
"net/http"
5+
46
"github.com/gin-gonic/gin"
57
"github.com/lejianwen/rustdesk-api/v2/global"
68
"github.com/lejianwen/rustdesk-api/v2/http/request/api"
@@ -10,7 +12,6 @@ import (
1012
"github.com/lejianwen/rustdesk-api/v2/service"
1113
"github.com/lejianwen/rustdesk-api/v2/utils"
1214
"github.com/nicksnyder/go-i18n/v2/i18n"
13-
"net/http"
1415
)
1516

1617
type Oauth struct {
@@ -35,7 +36,7 @@ func (o *Oauth) OidcAuth(c *gin.Context) {
3536

3637
oauthService := service.AllService.OauthService
3738

38-
err, state, verifier, nonce, url := oauthService.BeginAuth(f.Op)
39+
err, state, verifier, nonce, url := oauthService.BeginAuth(c, f.Op)
3940
if err != nil {
4041
response.Error(c, response.TranslateMsg(c, err.Error()))
4142
return
@@ -169,7 +170,7 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
169170
var user *model.User
170171
// 获取用户信息
171172
code := c.Query("code")
172-
err, oauthUser := oauthService.Callback(code, verifier, op, nonce)
173+
err, oauthUser := oauthService.Callback(c, code, verifier, op, nonce)
173174
if err != nil {
174175
c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
175176
"message": "OauthFailed",
@@ -225,7 +226,7 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
225226
if !*oauthConfig.AutoRegister {
226227
//c.String(http.StatusInternalServerError, "还未绑定用户,请先绑定")
227228
oauthCache.UpdateFromOauthUser(oauthUser)
228-
c.Redirect(http.StatusFound, "/_admin/#/oauth/bind/" + cacheKey)
229+
c.Redirect(http.StatusFound, "/_admin/#/oauth/bind/"+cacheKey)
229230
return
230231
}
231232

service/oauth.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77

88
"github.com/coreos/go-oidc/v3/oidc"
9+
"github.com/gin-gonic/gin"
910
"github.com/lejianwen/rustdesk-api/v2/model"
1011
"github.com/lejianwen/rustdesk-api/v2/utils"
1112
"golang.org/x/oauth2"
@@ -95,16 +96,20 @@ func (os *OauthService) DeleteOauthCache(key string) {
9596
OauthCache.Delete(key)
9697
}
9798

98-
func (os *OauthService) BeginAuth(op string) (error error, state, verifier, nonce, url string) {
99+
func (os *OauthService) BeginAuth(c *gin.Context, op string) (error error, state, verifier, nonce, url string) {
99100
state = utils.RandomString(10) + strconv.FormatInt(time.Now().Unix(), 10)
100101
verifier = ""
101102
nonce = ""
102103
if op == model.OauthTypeWebauth {
103-
url = Config.Rustdesk.ApiServer + "/_admin/#/oauth/" + state
104+
host := c.GetHeader("Origin")
105+
if host == "" {
106+
host = Config.Rustdesk.ApiServer
107+
}
108+
url = host + "/_admin/#/oauth/" + state
104109
//url = "http://localhost:8888/_admin/#/oauth/" + code
105110
return nil, state, verifier, nonce, url
106111
}
107-
err, oauthInfo, oauthConfig, _ := os.GetOauthConfig(op)
112+
err, oauthInfo, oauthConfig, _ := os.GetOauthConfig(c, op)
108113
if err == nil {
109114
extras := make([]oauth2.AuthCodeOption, 0, 3)
110115

@@ -169,16 +174,20 @@ func (os *OauthService) LinuxdoProvider() *oidc.Provider {
169174
}
170175

171176
// GetOauthConfig retrieves the OAuth2 configuration based on the provider name
172-
func (os *OauthService) GetOauthConfig(op string) (err error, oauthInfo *model.Oauth, oauthConfig *oauth2.Config, provider *oidc.Provider) {
177+
func (os *OauthService) GetOauthConfig(c *gin.Context, op string) (err error, oauthInfo *model.Oauth, oauthConfig *oauth2.Config, provider *oidc.Provider) {
173178
//err, oauthInfo, oauthConfig = os.getOauthConfigGeneral(op)
174179
oauthInfo = os.InfoByOp(op)
175180
if oauthInfo.Id == 0 || oauthInfo.ClientId == "" || oauthInfo.ClientSecret == "" {
176181
return errors.New("ConfigNotFound"), nil, nil, nil
177182
}
183+
host := c.GetHeader("Origin")
184+
if host == "" {
185+
host = Config.Rustdesk.ApiServer
186+
}
178187
oauthConfig = &oauth2.Config{
179188
ClientID: oauthInfo.ClientId,
180189
ClientSecret: oauthInfo.ClientSecret,
181-
RedirectURL: Config.Rustdesk.ApiServer + "/api/oidc/callback",
190+
RedirectURL: host + "/api/oidc/callback",
182191
}
183192

184193
// Maybe should validate the oauthConfig here
@@ -333,8 +342,8 @@ func (os *OauthService) oidcCallback(oauthConfig *oauth2.Config, provider *oidc.
333342
}
334343

335344
// Callback: Get user information by code and op(Oauth provider)
336-
func (os *OauthService) Callback(code, verifier, op, nonce string) (err error, oauthUser *model.OauthUser) {
337-
err, oauthInfo, oauthConfig, provider := os.GetOauthConfig(op)
345+
func (os *OauthService) Callback(c *gin.Context, code, verifier, op, nonce string) (err error, oauthUser *model.OauthUser) {
346+
err, oauthInfo, oauthConfig, provider := os.GetOauthConfig(c, op)
338347
// oauthType is already validated in GetOauthConfig
339348
if err != nil {
340349
return err, nil

0 commit comments

Comments
 (0)