Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 415439b

Browse files
committed
feat: allow to set the username claim field in OIDC
Gitlab does not set the preferred_username field. Therefore, coder generates something from the users email address, which is not very helpful. This allows the administrator to change the field used for the username (e.g. to "nickname") Signed-off-by: Jan Losinski <[email protected]>
1 parent c505e8b commit 415439b

File tree

6 files changed

+15
-3
lines changed

6 files changed

+15
-3
lines changed

cli/deployment/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ func newConfig() *codersdk.DeploymentConfig {
248248
Flag: "oidc-ignore-email-verified",
249249
Default: false,
250250
},
251+
UsernameField: &codersdk.DeploymentConfigField[string]{
252+
Name: "OIDC Username field",
253+
Usage: "OIDC claim filed to use as user-name.",
254+
Flag: "oidc-username-field",
255+
Default: "preferred_username",
256+
},
251257
},
252258

253259
Telemetry: &codersdk.TelemetryConfig{

cli/root_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ ExtractCommandPathsLoop:
124124
require.NoError(t, err, "read golden file, run \"make update-golden-files\" and commit the changes")
125125
// Remove CRLF newlines (Windows).
126126
want = bytes.ReplaceAll(want, []byte{'\r', '\n'}, []byte{'\n'})
127+
fmt.Printf(string(got))
127128
require.Equal(t, string(want), string(got), "golden file mismatch: %s, run \"make update-golden-files\", verify and commit the changes", gf)
128129
})
129130
}

cli/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,9 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
526526
Verifier: oidcProvider.Verifier(&oidc.Config{
527527
ClientID: cfg.OIDC.ClientID.Value,
528528
}),
529-
EmailDomain: cfg.OIDC.EmailDomain.Value,
530-
AllowSignups: cfg.OIDC.AllowSignups.Value,
529+
EmailDomain: cfg.OIDC.EmailDomain.Value,
530+
AllowSignups: cfg.OIDC.AllowSignups.Value,
531+
UsernameField: cfg.OIDC.UsernameField.Value,
531532
}
532533
}
533534

coderd/coderdtest/coderdtest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ func (o *OIDCConfig) OIDCConfig() *coderd.OIDCConfig {
880880
}, &oidc.Config{
881881
SkipClientIDCheck: true,
882882
}),
883+
UsernameField: "preferred_username",
883884
}
884885
}
885886

coderd/userauth.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ type OIDCConfig struct {
198198
// IgnoreEmailVerified allows ignoring the email_verified claim
199199
// from an upstream OIDC provider. See #5065 for context.
200200
IgnoreEmailVerified bool
201+
// UsernameField selects the claim field to be used as username
202+
UsernameField string
201203
}
202204

203205
func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
@@ -236,7 +238,7 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
236238
})
237239
return
238240
}
239-
usernameRaw, ok := claims["preferred_username"]
241+
usernameRaw, ok := claims[api.OIDCConfig.UsernameField]
240242
var username string
241243
if ok {
242244
username, _ = usernameRaw.(string)

codersdk/deploymentconfig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ type OIDCConfig struct {
9999
IssuerURL *DeploymentConfigField[string] `json:"issuer_url" typescript:",notnull"`
100100
Scopes *DeploymentConfigField[[]string] `json:"scopes" typescript:",notnull"`
101101
IgnoreEmailVerified *DeploymentConfigField[bool] `json:"ignore_email_verified" typescript:",notnull"`
102+
UsernameField *DeploymentConfigField[string] `json:"username_filed" typescript:",notnull"`
102103
}
103104

104105
type TelemetryConfig struct {

0 commit comments

Comments
 (0)