Tao Chen 1 год назад
Родитель
Сommit
3acfb36c5d
3 измененных файлов с 59 добавлено и 55 удалено
  1. 0 17
      http/request/admin/oauth.go
  2. 47 11
      model/oauth.go
  3. 12 27
      service/oauth.go

+ 0 - 17
http/request/admin/oauth.go

@@ -2,7 +2,6 @@ package admin
2 2
 
3 3
 import (
4 4
 	"Gwen/model"
5
-	"strings"
6 5
 )
7 6
 
8 7
 type BindOauthForm struct {
@@ -28,22 +27,6 @@ type OauthForm struct {
28 27
 }
29 28
 
30 29
 func (of *OauthForm) ToOauth() *model.Oauth {
31
-	op := strings.ToLower(of.Op)
32
-	op = strings.TrimSpace(op)
33
-	if op == "" {
34
-		switch of.OauthType {
35
-		case model.OauthTypeGithub:
36
-			of.Op = model.OauthNameGithub
37
-		case model.OauthTypeGoogle:
38
-			of.Op = model.OauthNameGoogle
39
-		case model.OauthTypeOidc:
40
-			of.Op = model.OauthNameOidc
41
-		case model.OauthTypeWebauth:
42
-			of.Op = model.OauthNameWebauth
43
-		default:
44
-			of.Op = of.OauthType
45
-		}
46
-	}
47 30
 	oa := &model.Oauth{
48 31
 		Op:           of.Op,
49 32
 		OauthType:	  of.OauthType,

+ 47 - 11
model/oauth.go

@@ -3,17 +3,29 @@ package model
3 3
 import (
4 4
 	"strconv"
5 5
 	"strings"
6
+	"errors"
6 7
 )
7 8
 
8 9
 const OIDC_DEFAULT_SCOPES = "openid,profile,email"
9 10
 
10 11
 const (
12
+	// make sure the value shouldbe lowercase
11 13
 	OauthTypeGithub  string = "github"
12 14
 	OauthTypeGoogle  string = "google"
13 15
 	OauthTypeOidc    string = "oidc"
14 16
 	OauthTypeWebauth string = "webauth"
15 17
 )
16 18
 
19
+// Validate the oauth type
20
+func ValidateOauthType(oauthType string) error {
21
+	switch oauthType {
22
+	case OauthTypeGithub, OauthTypeGoogle, OauthTypeOidc, OauthTypeWebauth:
23
+		return nil
24
+	default:
25
+		return errors.New("invalid Oauth type")
26
+	}
27
+}
28
+
17 29
 const (
18 30
 	OauthNameGithub  string = "GitHub"
19 31
 	OauthNameGoogle  string = "Google"
@@ -23,8 +35,7 @@ const (
23 35
 
24 36
 const (
25 37
 	UserEndpointGithub  string = "https://api.github.com/user"
26
-	UserEndpointGoogle  string = "https://www.googleapis.com/oauth2/v3/userinfo"
27
-	UserEndpointOidc    string = ""
38
+	IssuerGoogle 		string = "https://accounts.google.com"
28 39
 )
29 40
 
30 41
 type Oauth struct {
@@ -40,6 +51,40 @@ type Oauth struct {
40 51
 	TimeModel
41 52
 }
42 53
 
54
+
55
+
56
+// Helper function to format oauth info, it's used in the update and create method
57
+func (oa *Oauth) FormatOauthInfo() error {
58
+	oauthType := strings.TrimSpace(oa.OauthType)
59
+	err := ValidateOauthType(oa.OauthType)
60
+	if err != nil {
61
+		return err
62
+	}
63
+	// check if the op is empty, set the default value
64
+	op := strings.TrimSpace(oa.Op)
65
+	if op == "" {
66
+		switch oauthType {
67
+		case OauthTypeGithub:
68
+			oa.Op = OauthNameGithub
69
+		case OauthTypeGoogle:
70
+			oa.Op = OauthNameGoogle
71
+		case OauthTypeOidc:
72
+			oa.Op = OauthNameOidc
73
+		case OauthTypeWebauth:
74
+			oa.Op = OauthNameWebauth
75
+		default:
76
+			oa.Op = oauthType
77
+		}
78
+	}
79
+	// check the issuer, if the oauth type is google and the issuer is empty, set the issuer to the default value
80
+	issuer := strings.TrimSpace(oa.Issuer)
81
+	// If the oauth type is google and the issuer is empty, set the issuer to the default value 
82
+	if oauthType == OauthTypeGoogle && issuer == "" {
83
+		oa.Issuer = IssuerGoogle
84
+	}
85
+	return nil
86
+}
87
+
43 88
 type OauthUser struct {
44 89
 	OpenId 			string 	`json:"open_id" gorm:"not null;index"`
45 90
 	Name   			string 	`json:"name"`
@@ -90,15 +135,6 @@ func (ou *OidcUser) ToOauthUser() *OauthUser {
90 135
 	}
91 136
 }
92 137
 
93
-type GoogleUser struct {
94
-	OidcUser
95
-}
96
-
97
-// GoogleUser 使用特定的 Username 规则来调用 ToOauthUser
98
-func (gu *GoogleUser) ToOauthUser() *OauthUser {
99
-	return gu.OidcUser.ToOauthUser()
100
-}
101
-
102 138
 
103 139
 type GithubUser struct {
104 140
 	OauthUserBase

+ 12 - 27
service/oauth.go

@@ -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