|
6 | 6 | "errors"
|
7 | 7 |
|
8 | 8 | "github.com/coreos/go-oidc/v3/oidc"
|
| 9 | + "github.com/gin-gonic/gin" |
9 | 10 | "github.com/lejianwen/rustdesk-api/v2/model"
|
10 | 11 | "github.com/lejianwen/rustdesk-api/v2/utils"
|
11 | 12 | "golang.org/x/oauth2"
|
@@ -95,16 +96,20 @@ func (os *OauthService) DeleteOauthCache(key string) {
|
95 | 96 | OauthCache.Delete(key)
|
96 | 97 | }
|
97 | 98 |
|
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) { |
99 | 100 | state = utils.RandomString(10) + strconv.FormatInt(time.Now().Unix(), 10)
|
100 | 101 | verifier = ""
|
101 | 102 | nonce = ""
|
102 | 103 | 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 |
104 | 109 | //url = "http://localhost:8888/_admin/#/oauth/" + code
|
105 | 110 | return nil, state, verifier, nonce, url
|
106 | 111 | }
|
107 |
| - err, oauthInfo, oauthConfig, _ := os.GetOauthConfig(op) |
| 112 | + err, oauthInfo, oauthConfig, _ := os.GetOauthConfig(c, op) |
108 | 113 | if err == nil {
|
109 | 114 | extras := make([]oauth2.AuthCodeOption, 0, 3)
|
110 | 115 |
|
@@ -169,16 +174,20 @@ func (os *OauthService) LinuxdoProvider() *oidc.Provider {
|
169 | 174 | }
|
170 | 175 |
|
171 | 176 | // 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) { |
173 | 178 | //err, oauthInfo, oauthConfig = os.getOauthConfigGeneral(op)
|
174 | 179 | oauthInfo = os.InfoByOp(op)
|
175 | 180 | if oauthInfo.Id == 0 || oauthInfo.ClientId == "" || oauthInfo.ClientSecret == "" {
|
176 | 181 | return errors.New("ConfigNotFound"), nil, nil, nil
|
177 | 182 | }
|
| 183 | + host := c.GetHeader("Origin") |
| 184 | + if host == "" { |
| 185 | + host = Config.Rustdesk.ApiServer |
| 186 | + } |
178 | 187 | oauthConfig = &oauth2.Config{
|
179 | 188 | ClientID: oauthInfo.ClientId,
|
180 | 189 | ClientSecret: oauthInfo.ClientSecret,
|
181 |
| - RedirectURL: Config.Rustdesk.ApiServer + "/api/oidc/callback", |
| 190 | + RedirectURL: host + "/api/oidc/callback", |
182 | 191 | }
|
183 | 192 |
|
184 | 193 | // Maybe should validate the oauthConfig here
|
@@ -333,8 +342,8 @@ func (os *OauthService) oidcCallback(oauthConfig *oauth2.Config, provider *oidc.
|
333 | 342 | }
|
334 | 343 |
|
335 | 344 | // 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) |
338 | 347 | // oauthType is already validated in GetOauthConfig
|
339 | 348 | if err != nil {
|
340 | 349 | return err, nil
|
|
0 commit comments