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

View file

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