Tao Chen 1 год назад
Родитель
Сommit
7d83226655
1 измененных файлов с 32 добавлено и 31 удалено
  1. 32 31
      service/oauth.go

+ 32 - 31
service/oauth.go

@@ -29,7 +29,6 @@ type OidcEndpoint struct {
29 29
 }
30 30
 
31 31
 type OauthService struct {
32
-	OidcEndpoint *OidcEndpoint
33 32
 }
34 33
 
35 34
 type GithubUserdata struct {
@@ -93,7 +92,6 @@ type OidcUserdata struct {
93 92
 	Email         string `json:"email"`
94 93
 	VerifiedEmail bool   `json:"email_verified"`
95 94
 	Name          string `json:"name"`
96
-	Picture       string `json:"picture"`
97 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 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 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 233
 	// Set scopes
237
-	scopes := g.Scopes
234
+	scopes := strings.TrimSpace(g.Scopes)
238 235
 	if scopes == "" {
239 236
 		scopes = "openid,profile,email"
240 237
 	}
241 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 241
 		return err, nil
246 242
 	}
247
-
248 243
 	return nil, &oauth2.Config{
249 244
 		ClientID:     g.ClientId,
250 245
 		ClientSecret: g.ClientSecret,
251 246
 		RedirectURL:  g.RedirectUrl,
252 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 251
 		Scopes: scopeList,
257 252
 	}
@@ -363,7 +358,6 @@ func (os *OauthService) OidcCallback(code string) (error error, userData *OidcUs
363 358
 	if err != nil {
364 359
 		return err, nil
365 360
 	}
366
-
367 361
 	// 使用代理配置创建 HTTP 客户端
368 362
 	httpClient := getHTTPClientWithProxy()
369 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 372
 	// 使用带有代理的 HTTP 客户端获取用户信息
379 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 382
 	if err != nil {
382 383
 		global.Logger.Warn("failed getting user info: ", err)
383 384
 		error = errors.New("GetOauthUserInfoError")
@@ -413,8 +414,8 @@ func (os *OauthService) BindGoogleUser(email, username string, userId uint) erro
413 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 421
 func (os *OauthService) BindOauthUser(thirdType, openid, username string, userId uint) error {