Skip to content

Commit d10b30f

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 <losinski.j@gmail.com>
1 parent c505e8b commit d10b30f

File tree

6 files changed

+18
-3
lines changed

6 files changed

+18
-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/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

cli/testdata/coder_server_--help.golden

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ Flags:
112112
OIDC.
113113
Consumes $CODER_OIDC_SCOPES (default
114114
[openid,profile,email])
115+
--oidc-username-field string OIDC claim filed to use as user-name.
116+
Consumes
117+
$CODER_OIDC_USERNAME_FILED (default
118+
"preferred_username")
115119
--postgres-url string URL of a PostgreSQL database. If empty,
116120
PostgreSQL binaries will be downloaded
117121
from Maven

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)