feat: support nested fields for configuring user_mapping in the Custom OAuth provider (#4032)

This commit is contained in:
Seele.Clover
2025-08-03 00:33:52 +08:00
committed by GitHub
parent d537377b31
commit 75c1ae4366

View File

@@ -20,6 +20,7 @@ import (
"fmt"
"io"
"net/http"
"strings"
"github.com/casdoor/casdoor/util"
"github.com/mitchellh/mapstructure"
@@ -64,6 +65,25 @@ func (idp *CustomIdProvider) GetToken(code string) (*oauth2.Token, error) {
return idp.Config.Exchange(ctx, code)
}
func getNestedValue(data map[string]interface{}, path string) (interface{}, error) {
keys := strings.Split(path, ".")
var val interface{} = data
for _, key := range keys {
m, ok := val.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("path '%s' is not valid: %s is not a map", path, key)
}
val, ok = m[key]
if !ok {
return nil, fmt.Errorf("key '%s' not found in path '%s'", key, path)
}
}
return val, nil
}
type CustomUserInfo struct {
Id string `mapstructure:"id"`
Username string `mapstructure:"username"`
@@ -108,11 +128,11 @@ func (idp *CustomIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error)
// map user info
for k, v := range idp.UserMapping {
_, ok := dataMap[v]
if !ok {
return nil, fmt.Errorf("cannot find %s in user from custom provider", v)
val, err := getNestedValue(dataMap, v)
if err != nil {
return nil, fmt.Errorf("cannot find %s in user from custom provider: %v", v, err)
}
dataMap[k] = dataMap[v]
dataMap[k] = val
}
// try to parse id to string