Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: get claims value with path key (#6967)

boojack08a2ba0142a6
diff --git a/backend/plugin/idp/oauth2/oauth2.go b/backend/plugin/idp/oauth2/oauth2.go
index 5dc62c9a34..3d1fbe2f9e 100644
--- a/backend/plugin/idp/oauth2/oauth2.go
+++ b/backend/plugin/idp/oauth2/oauth2.go
@@ -14,6 +14,7 @@ import (
"golang.org/x/oauth2"
"github.com/bytebase/bytebase/backend/common/log"
+ "github.com/bytebase/bytebase/backend/plugin/idp"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -107,7 +108,7 @@ func (p *IdentityProvider) UserInfo(token string) (*storepb.IdentityProviderUser
log.Debug("User info", zap.Any("claims", claims))
userInfo := &storepb.IdentityProviderUserInfo{}
- if v, ok := claims[p.config.FieldMapping.Identifier].(string); ok {
+ if v, ok := idp.GetValueWithKey(claims, p.config.FieldMapping.Identifier).(string); ok {
userInfo.Identifier = v
}
if userInfo.Identifier == "" {
@@ -117,7 +118,7 @@ func (p *IdentityProvider) UserInfo(token string) (*storepb.IdentityProviderUser
// Best effort to map optional fields
if p.config.FieldMapping.DisplayName != "" {
- if v, ok := claims[p.config.FieldMapping.DisplayName].(string); ok {
+ if v, ok := idp.GetValueWithKey(claims, p.config.FieldMapping.DisplayName).(string); ok {
userInfo.DisplayName = v
}
}
@@ -125,7 +126,7 @@ func (p *IdentityProvider) UserInfo(token string) (*storepb.IdentityProviderUser
userInfo.DisplayName = userInfo.Identifier
}
if p.config.FieldMapping.Email != "" {
- if v, ok := claims[p.config.FieldMapping.Email].(string); ok {
+ if v, ok := idp.GetValueWithKey(claims, p.config.FieldMapping.Email).(string); ok {
userInfo.Email = v
}
}
diff --git a/backend/plugin/idp/oidc/oidc.go b/backend/plugin/idp/oidc/oidc.go
index 120466db75..8d5c124cc0 100644
--- a/backend/plugin/idp/oidc/oidc.go
+++ b/backend/plugin/idp/oidc/oidc.go
@@ -12,6 +12,7 @@ import (
"golang.org/x/oauth2"
"github.com/bytebase/bytebase/backend/common/log"
+ "github.com/bytebase/bytebase/backend/plugin/idp"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -129,7 +130,7 @@ func (p *IdentityProvider) UserInfo(ctx context.Context, token *oauth2.Token, no
log.Debug("User info", zap.Any("claims", claims))
userInfo := &storepb.IdentityProviderUserInfo{}
- if v, ok := claims[p.config.FieldMapping.Identifier].(string); ok {
+ if v, ok := idp.GetValueWithKey(claims, p.config.FieldMapping.Identifier).(string); ok {
userInfo.Identifier = v
}
if userInfo.Identifier == "" {
@@ -138,7 +139,7 @@ func (p *IdentityProvider) UserInfo(ctx context.Context, token *oauth2.Token, no
// Best effort to map optional fields
if p.config.FieldMapping.DisplayName != "" {
- if v, ok := claims[p.config.FieldMapping.DisplayName].(string); ok {
+ if v, ok := idp.GetValueWithKey(claims, p.config.FieldMapping.DisplayName).(string); ok {
userInfo.DisplayName = v
}
}
@@ -146,7 +147,7 @@ func (p *IdentityProvider) UserInfo(ctx context.Context, token *oauth2.Token, no
userInfo.DisplayName = userInfo.Identifier
}
if p.config.FieldMapping.Email != "" {
- if v, ok := claims[p.config.FieldMapping.Email].(string); ok {
+ if v, ok := idp.GetValueWithKey(claims, p.config.FieldMapping.Email).(string); ok {
userInfo.Email = v
}
}
diff --git a/backend/plugin/idp/util.go b/backend/plugin/idp/util.go
new file mode 100644
index 0000000000..9cb3f753ed
--- /dev/null
+++ b/backend/plugin/idp/util.go
@@ -0,0 +1,19 @@
+// Package idp is the plugin for Identity Provider.
+package idp
+
+import "strings"
+
+// GetValueWithKey returns the value of the key in the data.
+func GetValueWithKey(data map[string]any, key string) any {
+ keys := strings.Split(key, ".")
+ value := data[keys[0]]
+
+ if len(keys) > 1 {
+ if subData, ok := value.(map[string]any); ok {
+ return GetValueWithKey(subData, strings.Join(keys[1:], "."))
+ }
+ return nil
+ }
+
+ return value
+}
diff --git a/backend/plugin/idp/util_test.go b/backend/plugin/idp/util_test.go
new file mode 100644
index 0000000000..b847f504cc
--- /dev/null
+++ b/backend/plugin/idp/util_test.go
@@ -0,0 +1,69 @@
+package idp
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestGetValueWithKey(t *testing.T) {
+ tests := []struct {
+ data map[string]any
+ key string
+ want any
+ }{
+ {
+ data: map[string]any{
+ "user": "test",
+ },
+ key: "user",
+ want: "test",
+ },
+ {
+ data: map[string]any{
+ "data": map[string]any{
+ "user": "test",
+ },
+ },
+ key: "data.user",
+ want: "test",
+ },
+ }
+
+ for _, test := range tests {
+ got := GetValueWithKey(test.data, test.key)
+ if test.want != got {
+ t.Errorf("GetValueWithKey got %v but want %v", got, test.want)
+ }
+ }
+}
+
+func TestGetValueWithKeyFromJSONString(t *testing.T) {
+ tests := []struct {
+ data string
+ key string
+ want any
+ }{
+ {
+ data: `{"user": "test"}`,
+ key: "user",
+ want: "test",
+ },
+ {
+ data: `{"user":"test","data":{"user":"data-user"}}`,
+ key: "data.user",
+ want: "data-user",
+ },
+ }
+
+ for _, test := range tests {
+ var data map[string]any
+ err := json.Unmarshal([]byte(test.data), &data)
+ require.NoError(t, err)
+ got := GetValueWithKey(data, test.key)
+ if test.want != got {
+ t.Errorf("GetValueWithKey got %v but want %v", got, test.want)
+ }
+ }
+}