From d9afb7e59ce5efb62b7f537c30ce8236b5382edc Mon Sep 17 00:00:00 2001 From: Rui Yang Date: Wed, 22 Jan 2020 00:12:35 +0800 Subject: [PATCH] default to preferred_username claim Signed-off-by: Rui Yang --- connector/oidc/oidc.go | 10 +++++----- connector/oidc/oidc_test.go | 34 ++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/connector/oidc/oidc.go b/connector/oidc/oidc.go index 4c3dc6ce..4ec86a05 100644 --- a/connector/oidc/oidc.go +++ b/connector/oidc/oidc.go @@ -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, diff --git a/connector/oidc/oidc_test.go b/connector/oidc/oidc_test.go index b4d939e1..1515e0ad 100644 --- a/connector/oidc/oidc_test.go +++ b/connector/oidc/oidc_test.go @@ -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, }, }, {