Browse Source

fix bug - oidc scopes

Tao Chen 1 year ago
parent
commit
ffa47177aa
1 changed files with 32 additions and 31 deletions
  1. 32 31
      service/oauth.go

+ 32 - 31
service/oauth.go

@@ -29,7 +29,6 @@ type OidcEndpoint struct {
29
 }
29
 }
30
 
30
 
31
 type OauthService struct {
31
 type OauthService struct {
32
-	OidcEndpoint *OidcEndpoint
33
 }
32
 }
34
 
33
 
35
 type GithubUserdata struct {
34
 type GithubUserdata struct {
@@ -93,7 +92,6 @@ type OidcUserdata struct {
93
 	Email         string `json:"email"`
92
 	Email         string `json:"email"`
94
 	VerifiedEmail bool   `json:"email_verified"`
93
 	VerifiedEmail bool   `json:"email_verified"`
95
 	Name          string `json:"name"`
94
 	Name          string `json:"name"`
96
-	Picture       string `json:"picture"`
97
 	PrefferedUsername string `json:"preffered_username"`
95
 	PrefferedUsername string `json:"preffered_username"`
98
 }
96
 }
99
 
97
 
@@ -157,29 +155,28 @@ func (os *OauthService) BeginAuth(op string) (error error, code, url string) {
157
 }
155
 }
158
 
156
 
159
 // Method to fetch OIDC configuration dynamically
157
 // Method to fetch OIDC configuration dynamically
160
-func (os *OauthService) FetchOIDCConfig(issuer string) error {
161
-	configURL := strings.TrimSuffix(issuer, "/") + "/.well-known/openid-configuration"
158
+func FetchOidcConfig(issuer string) (error, OidcEndpoint) {
159
+    configURL := strings.TrimSuffix(issuer, "/") + "/.well-known/openid-configuration"
162
 
160
 
163
-	// Get the HTTP client (with or without proxy based on configuration)
164
-	client := getHTTPClientWithProxy()
161
+    // Get the HTTP client (with or without proxy based on configuration)
162
+    client := getHTTPClientWithProxy()
165
 
163
 
166
-	resp, err := client.Get(configURL)
167
-	if err != nil {
168
-		return errors.New("failed to fetch OIDC configuration")
169
-	}
170
-	defer resp.Body.Close()
164
+    resp, err := client.Get(configURL)
165
+    if err != nil {
166
+        return errors.New("failed to fetch OIDC configuration"), OidcEndpoint{}
167
+    }
168
+    defer resp.Body.Close()
171
 
169
 
172
-	if resp.StatusCode != http.StatusOK {
173
-		return errors.New("OIDC configuration not found")
174
-	}
170
+    if resp.StatusCode != http.StatusOK {
171
+        return errors.New("OIDC configuration not found, status code: %d"), OidcEndpoint{}
172
+    }
175
 
173
 
176
-	var endpoint OidcEndpoint
177
-	if err := json.NewDecoder(resp.Body).Decode(&endpoint); err != nil {
178
-		return errors.New("failed to parse OIDC configuration")
179
-	}
174
+    var endpoint OidcEndpoint
175
+    if err := json.NewDecoder(resp.Body).Decode(&endpoint); err != nil {
176
+        return errors.New("failed to parse OIDC configuration"), OidcEndpoint{}
177
+    }
180
 
178
 
181
-	os.OidcEndpoint = &endpoint
182
-	return nil
179
+    return nil, endpoint
183
 }
180
 }
184
 
181
 
185
 // GetOauthConfig retrieves the OAuth2 configuration based on the provider type
182
 // GetOauthConfig retrieves the OAuth2 configuration based on the provider type
@@ -234,24 +231,22 @@ func (os *OauthService) getOidcConfig() (error, *oauth2.Config) {
234
 	}
231
 	}
235
 
232
 
236
 	// Set scopes
233
 	// Set scopes
237
-	scopes := g.Scopes
234
+	scopes := strings.TrimSpace(g.Scopes)
238
 	if scopes == "" {
235
 	if scopes == "" {
239
 		scopes = "openid,profile,email"
236
 		scopes = "openid,profile,email"
240
 	}
237
 	}
241
 	scopeList := strings.Split(scopes, ",")
238
 	scopeList := strings.Split(scopes, ",")
242
-
243
-	// Fetch OIDC configuration
244
-	if err := os.FetchOIDCConfig(g.Issuer); err != nil {
239
+	err, endpoint := FetchOidcConfig(g.Issuer)
240
+	if err != nil {
245
 		return err, nil
241
 		return err, nil
246
 	}
242
 	}
247
-
248
 	return nil, &oauth2.Config{
243
 	return nil, &oauth2.Config{
249
 		ClientID:     g.ClientId,
244
 		ClientID:     g.ClientId,
250
 		ClientSecret: g.ClientSecret,
245
 		ClientSecret: g.ClientSecret,
251
 		RedirectURL:  g.RedirectUrl,
246
 		RedirectURL:  g.RedirectUrl,
252
 		Endpoint: oauth2.Endpoint{
247
 		Endpoint: oauth2.Endpoint{
253
-			AuthURL:  os.OidcEndpoint.AuthURL,
254
-			TokenURL: os.OidcEndpoint.TokenURL,
248
+			AuthURL:  endpoint.AuthURL,
249
+			TokenURL: endpoint.TokenURL,
255
 		},
250
 		},
256
 		Scopes: scopeList,
251
 		Scopes: scopeList,
257
 	}
252
 	}
@@ -363,7 +358,6 @@ func (os *OauthService) OidcCallback(code string) (error error, userData *OidcUs
363
 	if err != nil {
358
 	if err != nil {
364
 		return err, nil
359
 		return err, nil
365
 	}
360
 	}
366
-
367
 	// 使用代理配置创建 HTTP 客户端
361
 	// 使用代理配置创建 HTTP 客户端
368
 	httpClient := getHTTPClientWithProxy()
362
 	httpClient := getHTTPClientWithProxy()
369
 	ctx := context.WithValue(context.Background(), oauth2.HTTPClient, httpClient)
363
 	ctx := context.WithValue(context.Background(), oauth2.HTTPClient, httpClient)
@@ -377,7 +371,14 @@ func (os *OauthService) OidcCallback(code string) (error error, userData *OidcUs
377
 
371
 
378
 	// 使用带有代理的 HTTP 客户端获取用户信息
372
 	// 使用带有代理的 HTTP 客户端获取用户信息
379
 	client := oauthConfig.Client(ctx, token)
373
 	client := oauthConfig.Client(ctx, token)
380
-	resp, err := client.Get(os.OidcEndpoint.UserInfo)
374
+	g := os.InfoByOp(model.OauthTypeOidc)
375
+	err, endpoint := FetchOidcConfig(g.Issuer)
376
+	if err != nil {
377
+		global.Logger.Warn("failed fetching OIDC configuration: ", err)
378
+		error = errors.New("FetchOidcConfigError")
379
+		return
380
+	}
381
+	resp, err := client.Get(endpoint.UserInfo)
381
 	if err != nil {
382
 	if err != nil {
382
 		global.Logger.Warn("failed getting user info: ", err)
383
 		global.Logger.Warn("failed getting user info: ", err)
383
 		error = errors.New("GetOauthUserInfoError")
384
 		error = errors.New("GetOauthUserInfoError")
@@ -413,8 +414,8 @@ func (os *OauthService) BindGoogleUser(email, username string, userId uint) erro
413
 	return os.BindOauthUser(model.OauthTypeGoogle, email, username, userId)
414
 	return os.BindOauthUser(model.OauthTypeGoogle, email, username, userId)
414
 }
415
 }
415
 
416
 
416
-func (os *OauthService) BindOidcUser(openid, username string, userId uint) error {
417
-	return os.BindOauthUser(model.OauthTypeOidc, openid, username, userId)
417
+func (os *OauthService) BindOidcUser(sub, username string, userId uint) error {
418
+	return os.BindOauthUser(model.OauthTypeOidc, sub, username, userId)
418
 }
419
 }
419
 
420
 
420
 func (os *OauthService) BindOauthUser(thirdType, openid, username string, userId uint) error {
421
 func (os *OauthService) BindOauthUser(thirdType, openid, username string, userId uint) error {