default to preferred_username claim

Signed-off-by: Rui Yang <ruiya@vmware.com>
This commit is contained in:
Rui Yang 2020-01-22 00:12:35 +08:00 committed by Rui Yang
parent 9a4e0fcd00
commit d9afb7e59c
2 changed files with 23 additions and 21 deletions

View File

@ -55,8 +55,8 @@ type Config struct {
// Configurable key which contains the user name claim
UserNameKey string `json:"userNameKey"`
// Configurable key which contains the username claims
PreferredUsernameKey string `json:"preferredUsernameKey"` // defaults to "username"
// Configurable key which contains the preferred username claims
PreferredUsernameKey string `json:"preferredUsernameKey"`
// PromptType will be used fot the prompt parameter (when offline_access, by default prompt=consent)
PromptType string `json:"promptType"`
@ -302,9 +302,9 @@ func (c *oidcConnector) createIdentity(ctx context.Context, identity connector.I
hostedDomain, _ := claims["hd"].(string)
if c.preferredUsernameKey == "" {
c.preferredUsernameKey = "username"
c.preferredUsernameKey = "preferred_username"
}
username, _ := claims[c.preferredUsernameKey].(string)
preferredUsername, _ := claims[c.preferredUsernameKey].(string)
if len(c.hostedDomains) > 0 {
found := false
@ -332,7 +332,7 @@ func (c *oidcConnector) createIdentity(ctx context.Context, identity connector.I
identity = connector.Identity{
UserID: idToken.Subject,
Username: name,
PreferredUsername: username,
PreferredUsername: preferredUsername,
Email: email,
EmailVerified: emailVerified,
ConnectorData: connData,

View File

@ -85,16 +85,18 @@ func TestHandleCallback(t *testing.T) {
},
},
{
name: "withUserIDKey",
userIDKey: "name",
expectUserID: "namevalue",
expectUserName: "namevalue",
expectedEmailField: "emailvalue",
name: "withUserIDKey",
userIDKey: "name",
expectUserID: "namevalue",
expectUserName: "namevalue",
expectPreferredUsername: "usernamevalue",
expectedEmailField: "emailvalue",
token: map[string]interface{}{
"sub": "subvalue",
"name": "namevalue",
"email": "emailvalue",
"email_verified": true,
"sub": "subvalue",
"name": "namevalue",
"preferred_username": "usernamevalue",
"email": "emailvalue",
"email_verified": true,
},
},
{
@ -112,17 +114,17 @@ func TestHandleCallback(t *testing.T) {
},
{
name: "withPreferredUsernameKey",
preferredUsernameKey: "preferred_username",
preferredUsernameKey: "username_key",
expectUserID: "subvalue",
expectUserName: "namevalue",
expectPreferredUsername: "usernamevalue",
expectPreferredUsername: "username_value",
expectedEmailField: "emailvalue",
token: map[string]interface{}{
"sub": "subvalue",
"name": "namevalue",
"preferred_username": "usernamevalue",
"email": "emailvalue",
"email_verified": true,
"sub": "subvalue",
"name": "namevalue",
"username_key": "username_value",
"email": "emailvalue",
"email_verified": true,
},
},
{