Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: oidc auth endpoint (#14867)
boojackcd6dd19f6324
diff --git a/backend/api/v1/idp_service.go b/backend/api/v1/idp_service.goindex 20e6d233d8..c2b1fc5ce2 100644--- a/backend/api/v1/idp_service.go+++ b/backend/api/v1/idp_service.go@@ -3,6 +3,7 @@ package v1import ("context""fmt"+ "log/slog""slices""strings"@@ -12,6 +13,7 @@ import ("google.golang.org/protobuf/types/known/emptypb""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/log"enterprise "github.com/bytebase/bytebase/backend/enterprise/api"api "github.com/bytebase/bytebase/backend/legacyapi""github.com/bytebase/bytebase/backend/plugin/idp/ldap"@@ -428,35 +430,43 @@ func convertIdentityProviderConfigFromStore(identityProviderConfig *storepb.Iden},}, nil} else if v := identityProviderConfig.GetOidcConfig(); v != nil {- // Fetch openid configuration to get the auth endpoint and supported scopes.- openidConfiguration, err := oidc.GetOpenIDConfiguration(v.Issuer)- if err != nil {- return nil, errors.Wrap(err, "failed to fetch openid configuration")- }- scopes := oidc.DefaultScopes- // Some IdPs like authning.cn doesn't expose "username" as part of standard claims.- // We need to check if it's supported by the IdP and add it to the scopes.- if slices.Contains(openidConfiguration.ScopesSupported, "username") {- scopes = append(scopes, "username")- }fieldMapping := v1pb.FieldMapping{Identifier: v.FieldMapping.Identifier,DisplayName: v.FieldMapping.DisplayName,Email: v.FieldMapping.Email,Phone: v.FieldMapping.Phone,}+ oidcConfig := &v1pb.OIDCIdentityProviderConfig{+ Issuer: v.Issuer,+ ClientId: v.ClientId,+ ClientSecret: "", // SECURITY: We do not expose the client secret+ FieldMapping: &fieldMapping,+ SkipTlsVerify: v.SkipTlsVerify,+ AuthStyle: v1pb.OAuth2AuthStyle(v.AuthStyle),+ AuthEndpoint: "", // Leave it empty as it's not stored in the database.+ }++ // Fetch openid configuration to get the auth endpoint and supported scopes.+ openidConfiguration, err := oidc.GetOpenIDConfiguration(v.Issuer)+ if err != nil {+ // Log the error but continue as it's not critical.+ slog.Warn("failed to fetch openid configuration", slog.String("issuer", v.Issuer), log.BBError(err))+ }+ scopes := oidc.DefaultScopes+ // If the openid configuration is fetched successfully, we can use the scopes supported by the IdP.+ if openidConfiguration != nil {+ // Some IdPs like authning.cn doesn't expose "username" as part of standard claims.+ // We need to check if it's supported by the IdP and add it to the scopes.+ if slices.Contains(openidConfiguration.ScopesSupported, "username") {+ scopes = append(scopes, "username")+ }+ // Update the auth endpoint if it's available.+ oidcConfig.AuthEndpoint = openidConfiguration.AuthorizationEndpoint+ }+ oidcConfig.Scopes = scopesreturn &v1pb.IdentityProviderConfig{Config: &v1pb.IdentityProviderConfig_OidcConfig{- OidcConfig: &v1pb.OIDCIdentityProviderConfig{- Issuer: v.Issuer,- ClientId: v.ClientId,- ClientSecret: "", // SECURITY: We do not expose the client secret- FieldMapping: &fieldMapping,- SkipTlsVerify: v.SkipTlsVerify,- AuthStyle: v1pb.OAuth2AuthStyle(v.AuthStyle),- Scopes: scopes,- AuthEndpoint: openidConfiguration.AuthorizationEndpoint,- },+ OidcConfig: oidcConfig,},}, nil} else if v := identityProviderConfig.GetLdapConfig(); v != nil {diff --git a/backend/plugin/idp/oidc/oidc.go b/backend/plugin/idp/oidc/oidc.goindex 269eb405e5..adb9a1b484 100644--- a/backend/plugin/idp/oidc/oidc.go+++ b/backend/plugin/idp/oidc/oidc.go@@ -184,10 +184,16 @@ var openidConfigResponseCache = make(map[string]*OpenIDConfigurationResponse)// GetOpenIDConfiguration fetches the OpenID Configuration from the given issuer.func GetOpenIDConfiguration(issuer string) (*OpenIDConfigurationResponse, error) {+ // Return from cache if available.if config, found := openidConfigResponseCache[issuer]; found {return config, nil}+ req, err := http.NewRequest(http.MethodGet, strings.TrimSuffix(issuer, "/")+"/.well-known/openid-configuration", nil)+ if err != nil {+ return nil, errors.Wrap(err, "construct GET request")+ }+ req.Header.Set("Content-Type", "application/json")client := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{@@ -195,16 +201,16 @@ func GetOpenIDConfiguration(issuer string) (*OpenIDConfigurationResponse, error)},},}- resp, err := client.Get(strings.TrimSuffix(issuer, "/") + "/.well-known/openid-configuration")+ resp, err := client.Do(req)if err != nil {return nil, errors.Wrap(err, "fetch openid configuration")}- defer resp.Body.Close()b, err := io.ReadAll(resp.Body)if err != nil {return nil, errors.Wrapf(err, "failed to read body")}+ defer resp.Body.Close()if resp.StatusCode != http.StatusOK {return nil, errors.Errorf("received non-200 response code, code: %d, body: %s", resp.StatusCode, string(b))diff --git a/frontend/src/utils/sso.ts b/frontend/src/utils/sso.tsindex 1787365ad3..b024a9803a 100644--- a/frontend/src/utils/sso.ts+++ b/frontend/src/utils/sso.ts@@ -41,6 +41,11 @@ export async function openWindowForSSO(if (!oidcConfig) {return null;}+ if (oidcConfig.authEndpoint === "") {+ throw new Error(+ `Invalid authentication URL from issuer ${oidcConfig.issuer}, please check your configuration`+ );+ }uri.basePath = oidcConfig.authEndpoint;Object.assign(uri.query, {client_id: oidcConfig.clientId,