Skip to content

Commit 8c1716d

Browse files
committed
make userName configurable
1 parent dfb2dfd commit 8c1716d

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

Documentation/connectors/oidc.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ connectors:
7171
# Default: sub
7272
# Claims list at https://openid.net/specs/openid-connect-core-1_0.html#Claims
7373
#
74-
# userIdKey: nickname
74+
# userIDKey: nickname
75+
76+
# The set claim is used as user name.
77+
# Default: name
78+
# userNameKey: nickname
7579
```
7680

7781
[oidc-doc]: openid-connect.md

connector/oidc/oidc.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ type Config struct {
4747

4848
// Configurable key which contains the user id claim
4949
UserIDKey string `json:"userIDKey"`
50+
51+
// Configurable key which contains the user name claim
52+
UserNameKey string `json:"userNameKey"`
5053
}
5154

5255
// Domains that don't support basic auth. golang.org/x/oauth2 has an internal
@@ -131,6 +134,7 @@ func (c *Config) Open(id string, logger log.Logger) (conn connector.Connector, e
131134
insecureSkipEmailVerified: c.InsecureSkipEmailVerified,
132135
getUserInfo: c.GetUserInfo,
133136
userIDKey: c.UserIDKey,
137+
userNameKey: c.UserNameKey,
134138
}, nil
135139
}
136140

@@ -151,6 +155,7 @@ type oidcConnector struct {
151155
insecureSkipEmailVerified bool
152156
getUserInfo bool
153157
userIDKey string
158+
userNameKey string
154159
}
155160

156161
func (c *oidcConnector) Close() error {
@@ -209,9 +214,13 @@ func (c *oidcConnector) HandleCallback(s connector.Scopes, r *http.Request) (ide
209214
return identity, fmt.Errorf("oidc: failed to decode claims: %v", err)
210215
}
211216

212-
name, found := claims["name"].(string)
217+
userNameKey := "name"
218+
if c.userNameKey != "" {
219+
userNameKey = c.userNameKey
220+
}
221+
name, found := claims[userNameKey].(string)
213222
if !found {
214-
return identity, errors.New("missing \"name\" claim")
223+
return identity, fmt.Errorf("missing \"%s\" claim", userNameKey)
215224
}
216225
email, found := claims["email"].(string)
217226
if !found {

connector/oidc/oidc_test.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ func TestHandleCallback(t *testing.T) {
4747
tests := []struct {
4848
name string
4949
userIDKey string
50+
userNameKey string
5051
insecureSkipEmailVerified bool
5152
expectUserID string
53+
expectUserName string
5254
token map[string]interface{}
5355
}{
5456
{
55-
name: "simpleCase",
56-
userIDKey: "", // not configured
57-
expectUserID: "subvalue",
57+
name: "simpleCase",
58+
userIDKey: "", // not configured
59+
userNameKey: "", // not configured
60+
expectUserID: "subvalue",
61+
expectUserName: "namevalue",
5862
token: map[string]interface{}{
5963
"sub": "subvalue",
6064
"name": "namevalue",
@@ -66,23 +70,37 @@ func TestHandleCallback(t *testing.T) {
6670
name: "email_verified not in claims, configured to be skipped",
6771
insecureSkipEmailVerified: true,
6872
expectUserID: "subvalue",
73+
expectUserName: "namevalue",
6974
token: map[string]interface{}{
7075
"sub": "subvalue",
7176
"name": "namevalue",
7277
"email": "emailvalue",
7378
},
7479
},
7580
{
76-
name: "withUserIDKey",
77-
userIDKey: "name",
78-
expectUserID: "namevalue",
81+
name: "withUserIDKey",
82+
userIDKey: "name",
83+
expectUserID: "namevalue",
84+
expectUserName: "namevalue",
7985
token: map[string]interface{}{
8086
"sub": "subvalue",
8187
"name": "namevalue",
8288
"email": "emailvalue",
8389
"email_verified": true,
8490
},
8591
},
92+
{
93+
name: "withUserNameKey",
94+
userNameKey: "user_name",
95+
expectUserID: "subvalue",
96+
expectUserName: "username",
97+
token: map[string]interface{}{
98+
"sub": "subvalue",
99+
"user_name": "username",
100+
"email": "emailvalue",
101+
"email_verified": true,
102+
},
103+
},
86104
}
87105

88106
for _, tc := range tests {
@@ -100,6 +118,7 @@ func TestHandleCallback(t *testing.T) {
100118
Scopes: []string{"groups"},
101119
RedirectURI: fmt.Sprintf("%s/callback", serverURL),
102120
UserIDKey: tc.userIDKey,
121+
UserNameKey: tc.userNameKey,
103122
InsecureSkipEmailVerified: tc.insecureSkipEmailVerified,
104123
}
105124

@@ -119,7 +138,7 @@ func TestHandleCallback(t *testing.T) {
119138
}
120139

121140
expectEquals(t, identity.UserID, tc.expectUserID)
122-
expectEquals(t, identity.Username, "namevalue")
141+
expectEquals(t, identity.Username, tc.expectUserName)
123142
expectEquals(t, identity.Email, "emailvalue")
124143
expectEquals(t, identity.EmailVerified, true)
125144
})

0 commit comments

Comments
 (0)