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

Skip to content

feat: allow configurable username claim field in OIDC #5507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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]>
  • Loading branch information
janLo committed Dec 23, 2022
commit d10b30fc67af8697f1f39da1276b11ad4db50958
6 changes: 6 additions & 0 deletions cli/deployment/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ func newConfig() *codersdk.DeploymentConfig {
Flag: "oidc-ignore-email-verified",
Default: false,
},
UsernameField: &codersdk.DeploymentConfigField[string]{
Name: "OIDC Username field",
Usage: "OIDC claim filed to use as user-name.",
Flag: "oidc-username-field",
Default: "preferred_username",
},
},

Telemetry: &codersdk.TelemetryConfig{
Expand Down
5 changes: 3 additions & 2 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,9 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
Verifier: oidcProvider.Verifier(&oidc.Config{
ClientID: cfg.OIDC.ClientID.Value,
}),
EmailDomain: cfg.OIDC.EmailDomain.Value,
AllowSignups: cfg.OIDC.AllowSignups.Value,
EmailDomain: cfg.OIDC.EmailDomain.Value,
AllowSignups: cfg.OIDC.AllowSignups.Value,
UsernameField: cfg.OIDC.UsernameField.Value,
}
}

Expand Down
4 changes: 4 additions & 0 deletions cli/testdata/coder_server_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ Flags:
OIDC.
Consumes $CODER_OIDC_SCOPES (default
[openid,profile,email])
--oidc-username-field string OIDC claim filed to use as user-name.
Consumes
$CODER_OIDC_USERNAME_FILED (default
"preferred_username")
--postgres-url string URL of a PostgreSQL database. If empty,
PostgreSQL binaries will be downloaded
from Maven
Expand Down
1 change: 1 addition & 0 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,7 @@ func (o *OIDCConfig) OIDCConfig() *coderd.OIDCConfig {
}, &oidc.Config{
SkipClientIDCheck: true,
}),
UsernameField: "preferred_username",
}
}

Expand Down
4 changes: 3 additions & 1 deletion coderd/userauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ type OIDCConfig struct {
// IgnoreEmailVerified allows ignoring the email_verified claim
// from an upstream OIDC provider. See #5065 for context.
IgnoreEmailVerified bool
// UsernameField selects the claim field to be used as username
UsernameField string
}

func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -236,7 +238,7 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
})
return
}
usernameRaw, ok := claims["preferred_username"]
usernameRaw, ok := claims[api.OIDCConfig.UsernameField]
var username string
if ok {
username, _ = usernameRaw.(string)
Expand Down
1 change: 1 addition & 0 deletions codersdk/deploymentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type OIDCConfig struct {
IssuerURL *DeploymentConfigField[string] `json:"issuer_url" typescript:",notnull"`
Scopes *DeploymentConfigField[[]string] `json:"scopes" typescript:",notnull"`
IgnoreEmailVerified *DeploymentConfigField[bool] `json:"ignore_email_verified" typescript:",notnull"`
UsernameField *DeploymentConfigField[string] `json:"username_filed" typescript:",notnull"`
}

type TelemetryConfig struct {
Expand Down