|
|
@@ -9,7 +9,7 @@ import (
|
|
9
|
9
|
"errors"
|
|
10
|
10
|
"golang.org/x/oauth2"
|
|
11
|
11
|
"golang.org/x/oauth2/github"
|
|
12
|
|
- "golang.org/x/oauth2/google"
|
|
|
12
|
+ // "golang.org/x/oauth2/google"
|
|
13
|
13
|
"gorm.io/gorm"
|
|
14
|
14
|
// "io"
|
|
15
|
15
|
"net/http"
|
|
|
@@ -71,16 +71,6 @@ func (oa *OauthCacheItem) UpdateFromOauthUser(oauthUser *model.OauthUser) {
|
|
71
|
71
|
oa.Email = oauthUser.Email
|
|
72
|
72
|
}
|
|
73
|
73
|
|
|
74
|
|
-// Validate the oauth type
|
|
75
|
|
-func (os *OauthService) ValidateOauthType(oauthType string) error {
|
|
76
|
|
- switch oauthType {
|
|
77
|
|
- case model.OauthTypeGithub, model.OauthTypeGoogle, model.OauthTypeOidc, model.OauthTypeWebauth:
|
|
78
|
|
- return nil
|
|
79
|
|
- default:
|
|
80
|
|
- return errors.New("invalid Oauth type")
|
|
81
|
|
- }
|
|
82
|
|
-}
|
|
83
|
|
-
|
|
84
|
74
|
|
|
85
|
75
|
func (os *OauthService) GetOauthCache(key string) *OauthCacheItem {
|
|
86
|
76
|
v, ok := OauthCache.Load(key)
|
|
|
@@ -160,7 +150,7 @@ func (os *OauthService) GetOauthConfig(op string) (err error, oauthInfo *model.O
|
|
160
|
150
|
}
|
|
161
|
151
|
// Maybe should validate the oauthConfig here
|
|
162
|
152
|
oauthType := oauthInfo.OauthType
|
|
163
|
|
- err = os.ValidateOauthType(oauthType)
|
|
|
153
|
+ err = model.ValidateOauthType(oauthType)
|
|
164
|
154
|
if err != nil {
|
|
165
|
155
|
return err, nil, nil
|
|
166
|
156
|
}
|
|
|
@@ -168,10 +158,7 @@ func (os *OauthService) GetOauthConfig(op string) (err error, oauthInfo *model.O
|
|
168
|
158
|
case model.OauthTypeGithub:
|
|
169
|
159
|
oauthConfig.Endpoint = github.Endpoint
|
|
170
|
160
|
oauthConfig.Scopes = []string{"read:user", "user:email"}
|
|
171
|
|
- case model.OauthTypeGoogle:
|
|
172
|
|
- oauthConfig.Endpoint = google.Endpoint
|
|
173
|
|
- oauthConfig.Scopes = os.constructScopes(model.OIDC_DEFAULT_SCOPES)
|
|
174
|
|
- case model.OauthTypeOidc:
|
|
|
161
|
+ case model.OauthTypeOidc, model.OauthTypeGoogle:
|
|
175
|
162
|
var endpoint OidcEndpoint
|
|
176
|
163
|
err, endpoint = os.FetchOidcEndpoint(oauthInfo.Issuer)
|
|
177
|
164
|
if err != nil {
|
|
|
@@ -272,14 +259,6 @@ func (os *OauthService) githubCallback(oauthConfig *oauth2.Config, code string)
|
|
272
|
259
|
return nil, user.ToOauthUser()
|
|
273
|
260
|
}
|
|
274
|
261
|
|
|
275
|
|
-// googleCallback google回调
|
|
276
|
|
-func (os *OauthService) googleCallback(oauthConfig *oauth2.Config, code string) (error, *model.OauthUser) {
|
|
277
|
|
- var user = &model.GoogleUser{}
|
|
278
|
|
- if err, _ := os.callbackBase(oauthConfig, code, model.UserEndpointGoogle, user); err != nil {
|
|
279
|
|
- return err, nil
|
|
280
|
|
- }
|
|
281
|
|
- return nil, user.ToOauthUser()
|
|
282
|
|
-}
|
|
283
|
262
|
|
|
284
|
263
|
// oidcCallback oidc回调, 通过code获取用户信息
|
|
285
|
264
|
func (os *OauthService) oidcCallback(oauthConfig *oauth2.Config, code string, userInfoEndpoint string) (error, *model.OauthUser,) {
|
|
|
@@ -303,9 +282,7 @@ func (os *OauthService) Callback(code string, op string) (err error, oauthUser *
|
|
303
|
282
|
switch oauthType {
|
|
304
|
283
|
case model.OauthTypeGithub:
|
|
305
|
284
|
err, oauthUser = os.githubCallback(oauthConfig, code)
|
|
306
|
|
- case model.OauthTypeGoogle:
|
|
307
|
|
- err, oauthUser = os.googleCallback(oauthConfig, code)
|
|
308
|
|
- case model.OauthTypeOidc:
|
|
|
285
|
+ case model.OauthTypeOidc, model.OauthTypeGoogle:
|
|
309
|
286
|
err, endpoint := os.FetchOidcEndpoint(oauthInfo.Issuer)
|
|
310
|
287
|
if err != nil {
|
|
311
|
288
|
return err, nil
|
|
|
@@ -422,6 +399,10 @@ func (os *OauthService) IsOauthProviderExist(op string) bool {
|
|
422
|
399
|
|
|
423
|
400
|
// Create 创建
|
|
424
|
401
|
func (os *OauthService) Create(oauthInfo *model.Oauth) error {
|
|
|
402
|
+ err := oauthInfo.FormatOauthInfo()
|
|
|
403
|
+ if err != nil {
|
|
|
404
|
+ return err
|
|
|
405
|
+ }
|
|
425
|
406
|
res := global.DB.Create(oauthInfo).Error
|
|
426
|
407
|
return res
|
|
427
|
408
|
}
|
|
|
@@ -431,6 +412,10 @@ func (os *OauthService) Delete(oauthInfo *model.Oauth) error {
|
|
431
|
412
|
|
|
432
|
413
|
// Update 更新
|
|
433
|
414
|
func (os *OauthService) Update(oauthInfo *model.Oauth) error {
|
|
|
415
|
+ err := oauthInfo.FormatOauthInfo()
|
|
|
416
|
+ if err != nil {
|
|
|
417
|
+ return err
|
|
|
418
|
+ }
|
|
434
|
419
|
return global.DB.Model(oauthInfo).Updates(oauthInfo).Error
|
|
435
|
420
|
}
|
|
436
|
421
|
|