Skip to content

Commit c8d3ead

Browse files
author
Rui Yang
committed
use PreferredUsername
Signed-off-by: Rui Yang <[email protected]>
1 parent 31f3fce commit c8d3ead

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

connector/oauth/oauth.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import (
1414
"strings"
1515
"time"
1616

17+
"golang.org/x/oauth2"
18+
1719
"github.com/dexidp/dex/connector"
1820
"github.com/dexidp/dex/pkg/log"
19-
"golang.org/x/oauth2"
2021
)
2122

2223
type oauthConnector struct {
@@ -113,7 +114,6 @@ func newHTTPClient(rootCAs []string, insecureSkipVerify bool) (*http.Client, err
113114
}
114115

115116
func (c *oauthConnector) LoginURL(scopes connector.Scopes, callbackURL, state string) (string, error) {
116-
117117
if c.redirectURI != callbackURL {
118118
return "", fmt.Errorf("expected callback URL %q did not match the URL in the config %q", callbackURL, c.redirectURI)
119119
}
@@ -130,7 +130,6 @@ func (c *oauthConnector) LoginURL(scopes connector.Scopes, callbackURL, state st
130130
}
131131

132132
func (c *oauthConnector) HandleCallback(s connector.Scopes, r *http.Request) (identity connector.Identity, err error) {
133-
134133
q := r.URL.Query()
135134
if errType := q.Get("error"); errType != "" {
136135
return identity, errors.New(q.Get("error_description"))
@@ -185,7 +184,7 @@ func (c *oauthConnector) HandleCallback(s connector.Scopes, r *http.Request) (id
185184

186185
identity.UserID, _ = userInfoResult[c.userIDKey].(string)
187186
identity.Username, _ = userInfoResult[c.userNameKey].(string)
188-
identity.Name, _ = userInfoResult["name"].(string)
187+
identity.PreferredUsername, _ = userInfoResult["name"].(string)
189188
identity.Email, _ = userInfoResult["email"].(string)
190189
identity.EmailVerified, _ = userInfoResult["email_verified"].(bool)
191190

@@ -195,7 +194,7 @@ func (c *oauthConnector) HandleCallback(s connector.Scopes, r *http.Request) (id
195194
c.addGroupsFromMap(groups, userInfoResult)
196195
c.addGroupsFromToken(groups, token.AccessToken)
197196

198-
for groupName, _ := range groups {
197+
for groupName := range groups {
199198
identity.Groups = append(identity.Groups, groupName)
200199
}
201200
}
@@ -215,7 +214,7 @@ func (c *oauthConnector) HandleCallback(s connector.Scopes, r *http.Request) (id
215214
func (c *oauthConnector) addGroupsFromMap(groups map[string]bool, result map[string]interface{}) error {
216215
groupsClaim, ok := result[c.groupsKey].([]interface{})
217216
if !ok {
218-
return errors.New("Cant convert to array")
217+
return errors.New("cant convert to array")
219218
}
220219

221220
for _, group := range groupsClaim {
@@ -230,7 +229,7 @@ func (c *oauthConnector) addGroupsFromMap(groups map[string]bool, result map[str
230229
func (c *oauthConnector) addGroupsFromToken(groups map[string]bool, token string) error {
231230
parts := strings.Split(token, ".")
232231
if len(parts) < 2 {
233-
return errors.New("Invalid token")
232+
return errors.New("invalid token")
234233
}
235234

236235
decoded, err := decode(parts[1])

connector/oauth/oauth_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import (
1313
"sort"
1414
"testing"
1515

16-
"github.com/dexidp/dex/connector"
1716
"github.com/sirupsen/logrus"
1817
jose "gopkg.in/square/go-jose.v2"
18+
19+
"github.com/dexidp/dex/connector"
1920
)
2021

2122
func TestOpen(t *testing.T) {
@@ -67,7 +68,6 @@ func TestLoginURL(t *testing.T) {
6768
}
6869

6970
func TestHandleCallBackForGroupsInUserInfo(t *testing.T) {
70-
7171
tokenClaims := map[string]interface{}{}
7272

7373
userInfoClaims := map[string]interface{}{
@@ -92,15 +92,14 @@ func TestHandleCallBackForGroupsInUserInfo(t *testing.T) {
9292
expectEqual(t, len(identity.Groups), 2)
9393
expectEqual(t, identity.Groups[0], "admin-group")
9494
expectEqual(t, identity.Groups[1], "user-group")
95-
expectEqual(t, identity.Name, "test-name")
95+
expectEqual(t, identity.PreferredUsername, "test-name")
9696
expectEqual(t, identity.UserID, "test-user-id")
9797
expectEqual(t, identity.Username, "test-username")
9898
expectEqual(t, identity.Email, "test-email")
9999
expectEqual(t, identity.EmailVerified, true)
100100
}
101101

102102
func TestHandleCallBackForGroupsInToken(t *testing.T) {
103-
104103
tokenClaims := map[string]interface{}{
105104
"groups_key": []string{"test-group"},
106105
}
@@ -124,15 +123,14 @@ func TestHandleCallBackForGroupsInToken(t *testing.T) {
124123

125124
expectEqual(t, len(identity.Groups), 1)
126125
expectEqual(t, identity.Groups[0], "test-group")
127-
expectEqual(t, identity.Name, "test-name")
126+
expectEqual(t, identity.PreferredUsername, "test-name")
128127
expectEqual(t, identity.UserID, "test-user-id")
129128
expectEqual(t, identity.Username, "test-username")
130129
expectEqual(t, identity.Email, "test-email")
131130
expectEqual(t, identity.EmailVerified, true)
132131
}
133132

134133
func testSetup(t *testing.T, tokenClaims map[string]interface{}, userInfoClaims map[string]interface{}) *httptest.Server {
135-
136134
key, err := rsa.GenerateKey(rand.Reader, 1024)
137135
if err != nil {
138136
t.Fatal("Failed to generate rsa key", err)

0 commit comments

Comments
 (0)