mirror of
https://github.com/casdoor/casdoor.git
synced 2025-09-07 11:00:28 +08:00
feat: support nested fields for configuring user_mapping in the Custom OAuth provider (#4032)
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/casdoor/casdoor/util"
|
"github.com/casdoor/casdoor/util"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
@@ -64,6 +65,25 @@ func (idp *CustomIdProvider) GetToken(code string) (*oauth2.Token, error) {
|
|||||||
return idp.Config.Exchange(ctx, code)
|
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 {
|
type CustomUserInfo struct {
|
||||||
Id string `mapstructure:"id"`
|
Id string `mapstructure:"id"`
|
||||||
Username string `mapstructure:"username"`
|
Username string `mapstructure:"username"`
|
||||||
@@ -108,11 +128,11 @@ func (idp *CustomIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error)
|
|||||||
|
|
||||||
// map user info
|
// map user info
|
||||||
for k, v := range idp.UserMapping {
|
for k, v := range idp.UserMapping {
|
||||||
_, ok := dataMap[v]
|
val, err := getNestedValue(dataMap, v)
|
||||||
if !ok {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot find %s in user from custom provider", v)
|
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
|
// try to parse id to string
|
||||||
|
Reference in New Issue
Block a user