ljw 1 год назад
Родитель
Сommit
0ed40318cb

+ 97 - 0
docs/admin/admin_docs.go

@@ -1453,6 +1453,38 @@ const docTemplateadmin = `{
1453 1453
                 }
1454 1454
             }
1455 1455
         },
1456
+        "/admin/login-options": {
1457
+            "post": {
1458
+                "description": "登录选项",
1459
+                "consumes": [
1460
+                    "application/json"
1461
+                ],
1462
+                "produces": [
1463
+                    "application/json"
1464
+                ],
1465
+                "tags": [
1466
+                    "登录"
1467
+                ],
1468
+                "summary": "登录选项",
1469
+                "responses": {
1470
+                    "200": {
1471
+                        "description": "OK",
1472
+                        "schema": {
1473
+                            "type": "array",
1474
+                            "items": {
1475
+                                "type": "string"
1476
+                            }
1477
+                        }
1478
+                    },
1479
+                    "500": {
1480
+                        "description": "Internal Server Error",
1481
+                        "schema": {
1482
+                            "$ref": "#/definitions/response.ErrorResponse"
1483
+                        }
1484
+                    }
1485
+                }
1486
+            }
1487
+        },
1456 1488
         "/admin/loginLog/delete": {
1457 1489
             "post": {
1458 1490
                 "security": [
@@ -1922,6 +1954,63 @@ const docTemplateadmin = `{
1922 1954
                 }
1923 1955
             }
1924 1956
         },
1957
+        "/admin/oidc/auth": {
1958
+            "post": {
1959
+                "description": "OidcAuth",
1960
+                "consumes": [
1961
+                    "application/json"
1962
+                ],
1963
+                "produces": [
1964
+                    "application/json"
1965
+                ],
1966
+                "tags": [
1967
+                    "Oauth"
1968
+                ],
1969
+                "summary": "OidcAuth",
1970
+                "responses": {}
1971
+            }
1972
+        },
1973
+        "/admin/oidc/auth-query": {
1974
+            "get": {
1975
+                "description": "OidcAuthQuery",
1976
+                "consumes": [
1977
+                    "application/json"
1978
+                ],
1979
+                "produces": [
1980
+                    "application/json"
1981
+                ],
1982
+                "tags": [
1983
+                    "Oauth"
1984
+                ],
1985
+                "summary": "OidcAuthQuery",
1986
+                "responses": {
1987
+                    "200": {
1988
+                        "description": "OK",
1989
+                        "schema": {
1990
+                            "allOf": [
1991
+                                {
1992
+                                    "$ref": "#/definitions/response.Response"
1993
+                                },
1994
+                                {
1995
+                                    "type": "object",
1996
+                                    "properties": {
1997
+                                        "data": {
1998
+                                            "$ref": "#/definitions/admin.LoginPayload"
1999
+                                        }
2000
+                                    }
2001
+                                }
2002
+                            ]
2003
+                        }
2004
+                    },
2005
+                    "500": {
2006
+                        "description": "Internal Server Error",
2007
+                        "schema": {
2008
+                            "$ref": "#/definitions/response.Response"
2009
+                        }
2010
+                    }
2011
+                }
2012
+            }
2013
+        },
1925 2014
         "/admin/peer/create": {
1926 2015
             "post": {
1927 2016
                 "security": [
@@ -3979,6 +4068,14 @@ const docTemplateadmin = `{
3979 4068
                 }
3980 4069
             }
3981 4070
         },
4071
+        "response.ErrorResponse": {
4072
+            "type": "object",
4073
+            "properties": {
4074
+                "error": {
4075
+                    "type": "string"
4076
+                }
4077
+            }
4078
+        },
3982 4079
         "response.Response": {
3983 4080
             "type": "object",
3984 4081
             "properties": {

+ 97 - 0
docs/admin/admin_swagger.json

@@ -1446,6 +1446,38 @@
1446 1446
                 }
1447 1447
             }
1448 1448
         },
1449
+        "/admin/login-options": {
1450
+            "post": {
1451
+                "description": "登录选项",
1452
+                "consumes": [
1453
+                    "application/json"
1454
+                ],
1455
+                "produces": [
1456
+                    "application/json"
1457
+                ],
1458
+                "tags": [
1459
+                    "登录"
1460
+                ],
1461
+                "summary": "登录选项",
1462
+                "responses": {
1463
+                    "200": {
1464
+                        "description": "OK",
1465
+                        "schema": {
1466
+                            "type": "array",
1467
+                            "items": {
1468
+                                "type": "string"
1469
+                            }
1470
+                        }
1471
+                    },
1472
+                    "500": {
1473
+                        "description": "Internal Server Error",
1474
+                        "schema": {
1475
+                            "$ref": "#/definitions/response.ErrorResponse"
1476
+                        }
1477
+                    }
1478
+                }
1479
+            }
1480
+        },
1449 1481
         "/admin/loginLog/delete": {
1450 1482
             "post": {
1451 1483
                 "security": [
@@ -1915,6 +1947,63 @@
1915 1947
                 }
1916 1948
             }
1917 1949
         },
1950
+        "/admin/oidc/auth": {
1951
+            "post": {
1952
+                "description": "OidcAuth",
1953
+                "consumes": [
1954
+                    "application/json"
1955
+                ],
1956
+                "produces": [
1957
+                    "application/json"
1958
+                ],
1959
+                "tags": [
1960
+                    "Oauth"
1961
+                ],
1962
+                "summary": "OidcAuth",
1963
+                "responses": {}
1964
+            }
1965
+        },
1966
+        "/admin/oidc/auth-query": {
1967
+            "get": {
1968
+                "description": "OidcAuthQuery",
1969
+                "consumes": [
1970
+                    "application/json"
1971
+                ],
1972
+                "produces": [
1973
+                    "application/json"
1974
+                ],
1975
+                "tags": [
1976
+                    "Oauth"
1977
+                ],
1978
+                "summary": "OidcAuthQuery",
1979
+                "responses": {
1980
+                    "200": {
1981
+                        "description": "OK",
1982
+                        "schema": {
1983
+                            "allOf": [
1984
+                                {
1985
+                                    "$ref": "#/definitions/response.Response"
1986
+                                },
1987
+                                {
1988
+                                    "type": "object",
1989
+                                    "properties": {
1990
+                                        "data": {
1991
+                                            "$ref": "#/definitions/admin.LoginPayload"
1992
+                                        }
1993
+                                    }
1994
+                                }
1995
+                            ]
1996
+                        }
1997
+                    },
1998
+                    "500": {
1999
+                        "description": "Internal Server Error",
2000
+                        "schema": {
2001
+                            "$ref": "#/definitions/response.Response"
2002
+                        }
2003
+                    }
2004
+                }
2005
+            }
2006
+        },
1918 2007
         "/admin/peer/create": {
1919 2008
             "post": {
1920 2009
                 "security": [
@@ -3972,6 +4061,14 @@
3972 4061
                 }
3973 4062
             }
3974 4063
         },
4064
+        "response.ErrorResponse": {
4065
+            "type": "object",
4066
+            "properties": {
4067
+                "error": {
4068
+                    "type": "string"
4069
+                }
4070
+            }
4071
+        },
3975 4072
         "response.Response": {
3976 4073
             "type": "object",
3977 4074
             "properties": {

+ 61 - 0
docs/admin/admin_swagger.yaml

@@ -653,6 +653,11 @@ definitions:
653 653
       total:
654 654
         type: integer
655 655
     type: object
656
+  response.ErrorResponse:
657
+    properties:
658
+      error:
659
+        type: string
660
+    type: object
656 661
   response.Response:
657 662
     properties:
658 663
       code:
@@ -1520,6 +1525,27 @@ paths:
1520 1525
       summary: 登录
1521 1526
       tags:
1522 1527
       - 登录
1528
+  /admin/login-options:
1529
+    post:
1530
+      consumes:
1531
+      - application/json
1532
+      description: 登录选项
1533
+      produces:
1534
+      - application/json
1535
+      responses:
1536
+        "200":
1537
+          description: OK
1538
+          schema:
1539
+            items:
1540
+              type: string
1541
+            type: array
1542
+        "500":
1543
+          description: Internal Server Error
1544
+          schema:
1545
+            $ref: '#/definitions/response.ErrorResponse'
1546
+      summary: 登录选项
1547
+      tags:
1548
+      - 登录
1523 1549
   /admin/loginLog/delete:
1524 1550
     post:
1525 1551
       consumes:
@@ -1799,6 +1825,41 @@ paths:
1799 1825
       summary: Oauth编辑
1800 1826
       tags:
1801 1827
       - Oauth
1828
+  /admin/oidc/auth:
1829
+    post:
1830
+      consumes:
1831
+      - application/json
1832
+      description: OidcAuth
1833
+      produces:
1834
+      - application/json
1835
+      responses: {}
1836
+      summary: OidcAuth
1837
+      tags:
1838
+      - Oauth
1839
+  /admin/oidc/auth-query:
1840
+    get:
1841
+      consumes:
1842
+      - application/json
1843
+      description: OidcAuthQuery
1844
+      produces:
1845
+      - application/json
1846
+      responses:
1847
+        "200":
1848
+          description: OK
1849
+          schema:
1850
+            allOf:
1851
+            - $ref: '#/definitions/response.Response'
1852
+            - properties:
1853
+                data:
1854
+                  $ref: '#/definitions/admin.LoginPayload'
1855
+              type: object
1856
+        "500":
1857
+          description: Internal Server Error
1858
+          schema:
1859
+            $ref: '#/definitions/response.Response'
1860
+      summary: OidcAuthQuery
1861
+      tags:
1862
+      - Oauth
1802 1863
   /admin/peer/create:
1803 1864
     post:
1804 1865
       consumes:

+ 19 - 17
http/controller/admin/login.go

@@ -2,15 +2,16 @@ package admin
2 2
 
3 3
 import (
4 4
 	"Gwen/global"
5
+	"Gwen/http/controller/api"
5 6
 	"Gwen/http/request/admin"
7
+	apiReq "Gwen/http/request/api"
6 8
 	"Gwen/http/response"
7 9
 	adResp "Gwen/http/response/admin"
8
-	apiReq "Gwen/http/request/api"
9
-	"Gwen/http/controller/api"
10 10
 	"Gwen/model"
11 11
 	"Gwen/service"
12 12
 	"fmt"
13 13
 	"github.com/gin-gonic/gin"
14
+	"gorm.io/gorm"
14 15
 )
15 16
 
16 17
 type Login struct {
@@ -85,7 +86,6 @@ func (ct *Login) Logout(c *gin.Context) {
85 86
 	response.Success(c, nil)
86 87
 }
87 88
 
88
-
89 89
 // LoginOptions
90 90
 // @Tags 登录
91 91
 // @Summary 登录选项
@@ -95,13 +95,17 @@ func (ct *Login) Logout(c *gin.Context) {
95 95
 // @Success 200 {object} []string
96 96
 // @Failure 500 {object} response.ErrorResponse
97 97
 // @Router /admin/login-options [post]
98
-// 直接调用/api/login的LoginOptions方法
99 98
 func (ct *Login) LoginOptions(c *gin.Context) {
100
-	l := &api.Login{}
101
-    l.LoginOptions(c)
99
+	res := service.AllService.OauthService.List(1, 100, func(tx *gorm.DB) {
100
+		tx.Select("op").Order("id")
101
+	})
102
+	var ops []string
103
+	for _, v := range res.Oauths {
104
+		ops = append(ops, v.Op)
105
+	}
106
+	response.Success(c, ops)
102 107
 }
103 108
 
104
-
105 109
 // OidcAuth
106 110
 // @Tags Oauth
107 111
 // @Summary OidcAuth
@@ -126,13 +130,13 @@ func (ct *Login) OidcAuth(c *gin.Context) {
126 130
 	}
127 131
 
128 132
 	service.AllService.OauthService.SetOauthCache(code, &service.OauthCacheItem{
129
-		Action: service.OauthActionTypeLogin,
130
-		Op:     	f.Op,
131
-		Id: 		f.Id,
133
+		Action:     service.OauthActionTypeLogin,
134
+		Op:         f.Op,
135
+		Id:         f.Id,
132 136
 		DeviceType: "webadmin",
133 137
 		// DeviceOs: ct.Platform(c),
134
-		DeviceOs: 	f.DeviceInfo.Os,
135
-		Uuid: 		f.Uuid,
138
+		DeviceOs: f.DeviceInfo.Os,
139
+		Uuid:     f.Uuid,
136 140
 	}, 5*60)
137 141
 
138 142
 	response.Success(c, gin.H{
@@ -141,8 +145,6 @@ func (ct *Login) OidcAuth(c *gin.Context) {
141 145
 	})
142 146
 }
143 147
 
144
-
145
-
146 148
 // OidcAuthQuery
147 149
 // @Tags Oauth
148 150
 // @Summary OidcAuthQuery
@@ -158,12 +160,12 @@ func (ct *Login) OidcAuthQuery(c *gin.Context) {
158 160
 	if ut == nil {
159 161
 		return
160 162
 	}
161
-	fmt.Println("u:", u)
162
-	fmt.Println("ut:", ut)
163
+	//fmt.Println("u:", u)
164
+	//fmt.Println("ut:", ut)
163 165
 	response.Success(c, &adResp.LoginPayload{
164 166
 		Token:      ut.Token,
165 167
 		Username:   u.Username,
166 168
 		RouteNames: service.AllService.UserService.RouteNames(u),
167 169
 		Nickname:   u.Nickname,
168 170
 	})
169
-}
171
+}

+ 79 - 178
http/controller/api/ouath.go

@@ -32,6 +32,7 @@ func (o *Oauth) OidcAuth(c *gin.Context) {
32 32
 		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
33 33
 		return
34 34
 	}
35
+	//fmt.Println(f)
35 36
 	if f.Op != model.OauthTypeWebauth && f.Op != model.OauthTypeGoogle && f.Op != model.OauthTypeGithub && f.Op != model.OauthTypeOidc {
36 37
 		response.Error(c, response.TranslateMsg(c, "ParamsError"))
37 38
 		return
@@ -79,7 +80,7 @@ func (o *Oauth) OidcAuthQueryPre(c *gin.Context) (*model.User, *model.UserToken)
79 80
 
80 81
 	// 如果 UserId 为 0,说明还在授权中
81 82
 	if v.UserId == 0 {
82
-		c.JSON(http.StatusOK, gin.H{"message": "Authorization in progress"})
83
+		c.JSON(http.StatusOK, gin.H{"message": "Authorization in progress, please login and bind"})
83 84
 		return nil, nil
84 85
 	}
85 86
 
@@ -123,6 +124,9 @@ func (o *Oauth) OidcAuthQueryPre(c *gin.Context) (*model.User, *model.UserToken)
123 124
 // @Router /oidc/auth-query [get]
124 125
 func (o *Oauth) OidcAuthQuery(c *gin.Context) {
125 126
 	u, ut := o.OidcAuthQueryPre(c)
127
+	if u == nil || ut == nil {
128
+		return
129
+	}
126 130
 	c.JSON(http.StatusOK, apiResp.LoginRes{
127 131
 		AccessToken: ut.Token,
128 132
 		Type:        "access_token",
@@ -157,7 +161,10 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
157 161
 	ty := v.Op
158 162
 	ac := v.Action
159 163
 	var u *model.User
164
+	openid := ""
165
+	thirdName := ""
160 166
 	//fmt.Println("ty ac ", ty, ac)
167
+
161 168
 	if ty == model.OauthTypeGithub {
162 169
 		code := c.Query("code")
163 170
 		err, userData := service.AllService.OauthService.GithubCallback(code)
@@ -165,59 +172,8 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
165 172
 			c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthFailed")+response.TranslateMsg(c, err.Error()))
166 173
 			return
167 174
 		}
168
-		if ac == service.OauthActionTypeBind {
169
-			//fmt.Println("bind", ty, userData)
170
-			utr := service.AllService.OauthService.UserThirdInfo(ty, strconv.Itoa(userData.Id))
171
-			if utr.UserId > 0 {
172
-				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBindOtherUser"))
173
-				return
174
-			}
175
-			//绑定
176
-			u = service.AllService.UserService.InfoById(v.UserId)
177
-			if u == nil {
178
-				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "ItemNotFound"))
179
-				return
180
-			}
181
-			//绑定github
182
-			err = service.AllService.OauthService.BindGithubUser(strconv.Itoa(userData.Id), userData.Login, v.UserId)
183
-			if err != nil {
184
-				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "BindFail"))
185
-				return
186
-			}
187
-			c.String(http.StatusOK, response.TranslateMsg(c, "BindSuccess"))
188
-			return
189
-		} else if ac == service.OauthActionTypeLogin {
190
-			//登录
191
-			if v.UserId != 0 {
192
-				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBeenSuccess"))
193
-				return
194
-			}
195
-			u = service.AllService.UserService.InfoByGithubId(strconv.Itoa(userData.Id))
196
-			if u == nil {
197
-				oa := service.AllService.OauthService.InfoByOp(ty)
198
-				if !*oa.AutoRegister {
199
-					//c.String(http.StatusInternalServerError, "还未绑定用户,请先绑定")
200
-					v.ThirdName = userData.Login
201
-					v.ThirdOpenId = strconv.Itoa(userData.Id)
202
-					url := global.Config.Rustdesk.ApiServer + "/_admin/#/oauth/bind/" + cacheKey
203
-					c.Redirect(http.StatusFound, url)
204
-					return
205
-				}
206
-
207
-				//自动注册
208
-				u = service.AllService.UserService.RegisterByGithub(userData.Login, strconv.Itoa(userData.Id))
209
-				if u.Id == 0 {
210
-					c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthRegisterFailed"))
211
-					return
212
-				}
213
-			}
214
-
215
-			// v.UserId = u.Id
216
-			// service.AllService.OauthService.SetOauthCache(cacheKey, v, 0)
217
-			// c.String(http.StatusOK, response.TranslateMsg(c, "OauthSuccess"))
218
-			// return
219
-		}
220
-
175
+		openid = strconv.Itoa(userData.Id)
176
+		thirdName = userData.Login
221 177
 	} else if ty == model.OauthTypeGoogle {
222 178
 		code := c.Query("code")
223 179
 		err, userData := service.AllService.OauthService.GoogleCallback(code)
@@ -225,60 +181,9 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
225 181
 			c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthFailed")+response.TranslateMsg(c, err.Error()))
226 182
 			return
227 183
 		}
184
+		openid = userData.Email
228 185
 		//将空格替换成_
229
-		googleName := strings.Replace(userData.Name, " ", "_", -1)
230
-		if ac == service.OauthActionTypeBind {
231
-			//fmt.Println("bind", ty, userData)
232
-			utr := service.AllService.OauthService.UserThirdInfo(ty, userData.Email)
233
-			if utr.UserId > 0 {
234
-				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBindOtherUser"))
235
-				return
236
-			}
237
-			//绑定
238
-			u = service.AllService.UserService.InfoById(v.UserId)
239
-			if u == nil {
240
-				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "ItemNotFound"))
241
-				return
242
-			}
243
-			//绑定
244
-			err = service.AllService.OauthService.BindGoogleUser(userData.Email, googleName, v.UserId)
245
-			if err != nil {
246
-				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "BindFail"))
247
-				return
248
-			}
249
-			c.String(http.StatusOK, response.TranslateMsg(c, "BindSuccess"))
250
-			return
251
-		} else if ac == service.OauthActionTypeLogin {
252
-			if v.UserId != 0 {
253
-				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBeenSuccess"))
254
-				return
255
-			}
256
-			u = service.AllService.UserService.InfoByGoogleEmail(userData.Email)
257
-			if u == nil {
258
-				oa := service.AllService.OauthService.InfoByOp(ty)
259
-				if !*oa.AutoRegister {
260
-					//c.String(http.StatusInternalServerError, "还未绑定用户,请先绑定")
261
-
262
-					v.ThirdName = googleName
263
-					v.ThirdOpenId = userData.Email
264
-					url := global.Config.Rustdesk.ApiServer + "/_admin/#/oauth/bind/" + cacheKey
265
-					c.Redirect(http.StatusFound, url)
266
-					return
267
-				}
268
-
269
-				//自动注册
270
-				u = service.AllService.UserService.RegisterByGoogle(googleName, userData.Email)
271
-				if u.Id == 0 {
272
-					c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthRegisterFailed"))
273
-					return
274
-				}
275
-			}
276
-
277
-			// v.UserId = u.Id
278
-			// service.AllService.OauthService.SetOauthCache(cacheKey, v, 0)
279
-			// c.String(http.StatusOK, response.TranslateMsg(c, "OauthSuccess"))
280
-			// return
281
-		}
186
+		thirdName = strings.Replace(userData.Name, " ", "_", -1)
282 187
 	} else if ty == model.OauthTypeOidc {
283 188
 		code := c.Query("code")
284 189
 		err, userData := service.AllService.OauthService.OidcCallback(code)
@@ -286,85 +191,81 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
286 191
 			c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthFailed")+response.TranslateMsg(c, err.Error()))
287 192
 			return
288 193
 		}
289
-		//将空格替换成_
290
-		// OidcName := strings.Replace(userData.Name, " ", "_", -1)
291
-		if ac == service.OauthActionTypeBind {
292
-			//fmt.Println("bind", ty, userData)
293
-			utr := service.AllService.OauthService.UserThirdInfo(ty, userData.Sub)
294
-			if utr.UserId > 0 {
295
-				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBindOtherUser"))
296
-				return
297
-			}
298
-			//绑定
299
-			u = service.AllService.UserService.InfoById(v.UserId)
300
-			if u == nil {
301
-				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "ItemNotFound"))
302
-				return
303
-			}
304
-			//绑定, user preffered_username as username
305
-			err = service.AllService.OauthService.BindOidcUser(userData.Sub, userData.PreferredUsername, v.UserId)
306
-			if err != nil {
307
-				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "BindFail"))
308
-				return
309
-			}
310
-			c.String(http.StatusOK, response.TranslateMsg(c, "BindSuccess"))
194
+		openid = userData.Sub
195
+		thirdName = userData.PreferredUsername
196
+	} else {
197
+		c.String(http.StatusInternalServerError, response.TranslateMsg(c, "ParamsError"))
198
+		return
199
+	}
200
+	if ac == service.OauthActionTypeBind {
201
+
202
+		//fmt.Println("bind", ty, userData)
203
+		utr := service.AllService.OauthService.UserThirdInfo(ty, openid)
204
+		if utr.UserId > 0 {
205
+			c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBindOtherUser"))
311 206
 			return
312
-		} else if ac == service.OauthActionTypeLogin {
313
-			if v.UserId != 0 {
314
-				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBeenSuccess"))
207
+		}
208
+		//绑定
209
+		u = service.AllService.UserService.InfoById(v.UserId)
210
+		if u == nil {
211
+			c.String(http.StatusInternalServerError, response.TranslateMsg(c, "ItemNotFound"))
212
+			return
213
+		}
214
+		//绑定
215
+		err := service.AllService.OauthService.BindOauthUser(ty, openid, thirdName, v.UserId)
216
+		if err != nil {
217
+			c.String(http.StatusInternalServerError, response.TranslateMsg(c, "BindFail"))
218
+			return
219
+		}
220
+		c.String(http.StatusOK, response.TranslateMsg(c, "BindSuccess"))
221
+		return
222
+
223
+	} else if ac == service.OauthActionTypeLogin {
224
+		//登录
225
+		if v.UserId != 0 {
226
+			c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBeenSuccess"))
227
+			return
228
+		}
229
+		u = service.AllService.UserService.InfoByGithubId(openid)
230
+		if u == nil {
231
+			oa := service.AllService.OauthService.InfoByOp(ty)
232
+			if !*oa.AutoRegister {
233
+				//c.String(http.StatusInternalServerError, "还未绑定用户,请先绑定")
234
+				v.ThirdName = thirdName
235
+				v.ThirdOpenId = openid
236
+				url := global.Config.Rustdesk.ApiServer + "/_admin/#/oauth/bind/" + cacheKey
237
+				c.Redirect(http.StatusFound, url)
315 238
 				return
316 239
 			}
317
-			u = service.AllService.UserService.InfoByOidcSub(userData.Sub)
318
-			if u == nil {
319
-				oa := service.AllService.OauthService.InfoByOp(ty)
320
-				if !*oa.AutoRegister {
321
-					//c.String(http.StatusInternalServerError, "还未绑定用户,请先绑定")
322
-
323
-					v.ThirdName = userData.PreferredUsername
324
-					v.ThirdOpenId = userData.Sub
325
-					v.ThirdEmail = userData.Email
326
-					url := global.Config.Rustdesk.ApiServer + "/_admin/#/oauth/bind/" + cacheKey
327
-					c.Redirect(http.StatusFound, url)
328
-					return
329
-				}
330 240
 
331
-				//自动注册
332
-				u = service.AllService.UserService.RegisterByOidc(userData.PreferredUsername, userData.Sub)
333
-				if u.Id == 0 {
334
-					c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthRegisterFailed"))
335
-					return
336
-				}
241
+			//自动注册
242
+			u = service.AllService.UserService.RegisterByOauth(ty, thirdName, openid)
243
+			if u.Id == 0 {
244
+				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthRegisterFailed"))
245
+				return
337 246
 			}
338
-
339
-			// v.UserId = u.Id
340
-			// service.AllService.OauthService.SetOauthCache(cacheKey, v, 0)
341
-			// c.String(http.StatusOK, response.TranslateMsg(c, "OauthSuccess"))
342
-			// return
343 247
 		}
344
-	}
345
-	// 如果u为空,说明没有绑定用户
346
-	if u == nil {
347
-		c.String(http.StatusInternalServerError, response.TranslateMsg(c, "SystemError"))
248
+		v.UserId = u.Id
249
+		service.AllService.OauthService.SetOauthCache(cacheKey, v, 0)
250
+		// 如果是webadmin,登录成功后跳转到webadmin
251
+		if v.DeviceType == "webadmin" {
252
+			/*service.AllService.UserService.Login(u, &model.LoginLog{
253
+				UserId:   u.Id,
254
+				Client:   "webadmin",
255
+				Uuid:     "", //must be empty
256
+				Ip:       c.ClientIP(),
257
+				Type:     model.LoginLogTypeOauth,
258
+				Platform: v.DeviceOs,
259
+			})*/
260
+			url := global.Config.Rustdesk.ApiServer + "/_admin/#/"
261
+			c.Redirect(http.StatusFound, url)
262
+			return
263
+		}
264
+		c.String(http.StatusOK, response.TranslateMsg(c, "OauthSuccess"))
348 265
 		return
349
-	}
350
-	// 认证成功,设置缓存
351
-	v.UserId = u.Id
352
-	service.AllService.OauthService.SetOauthCache(cacheKey, v, 0)
353
-	// 如果是webadmin,登录成功后跳转到webadmin
354
-	if v.DeviceType == "webadmin" {
355
-		service.AllService.UserService.Login(u, &model.LoginLog{
356
-			UserId:   u.Id,
357
-			Client:   "webadmin",
358
-			Uuid:     "",//must be empty
359
-			Ip:       c.ClientIP(),
360
-			Type:     "account",
361
-			Platform: v.DeviceOs,
362
-		})
363
-		url := global.Config.Rustdesk.ApiServer + "/_admin/#/"
364
-		c.Redirect(http.StatusFound, url)
266
+	} else {
267
+		c.String(http.StatusInternalServerError, response.TranslateMsg(c, "ParamsError"))
365 268
 		return
366 269
 	}
367
-	c.String(http.StatusOK, response.TranslateMsg(c, "OauthSuccess"))
368
-	return
369 270
 
370 271
 }

+ 1 - 1
service/oauth.go

@@ -253,6 +253,7 @@ func (os *OauthService) getOidcConfig() (error, *oauth2.Config) {
253 253
 }
254 254
 
255 255
 func getHTTPClientWithProxy() *http.Client {
256
+	//todo add timeout
256 257
 	if global.Config.Proxy.Enable {
257 258
 		if global.Config.Proxy.Host == "" {
258 259
 			global.Logger.Warn("Proxy is enabled but proxy host is empty.")
@@ -446,7 +447,6 @@ func (os *OauthService) DeleteUserByUserId(userid uint) error {
446 447
 	return global.DB.Where("user_id = ?", userid).Delete(&model.UserThird{}).Error
447 448
 }
448 449
 
449
-
450 450
 // InfoById 根据id取用户信息
451 451
 func (os *OauthService) InfoById(id uint) *model.Oauth {
452 452
 	u := &model.Oauth{}