Skip to content

fix: allow for alternate usernames on conflict #4614

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 7 commits into from
Oct 18, 2022
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
Prev Previous commit
Next Next commit
forgot to save
  • Loading branch information
sreya committed Oct 18, 2022
commit c5975859693d923fdb4eec88b458372f5e26aee2
16 changes: 8 additions & 8 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,12 +727,12 @@ func NewAWSInstanceIdentity(t *testing.T, instanceID string) (awsidentity.Certif
}
}

type FakeOIDCConfig struct {
type OIDCConfig struct {
key *rsa.PrivateKey
issuer string
}

func NewFakeOIDCConfig(t *testing.T, issuer string) *FakeOIDCConfig {
func NewOIDCConfig(t *testing.T, issuer string) *OIDCConfig {
t.Helper()

pkey, err := rsa.GenerateKey(rand.Reader, 2048)
Expand All @@ -742,21 +742,21 @@ func NewFakeOIDCConfig(t *testing.T, issuer string) *FakeOIDCConfig {
issuer = "https://coder.com"
}

return &FakeOIDCConfig{
return &OIDCConfig{
key: pkey,
issuer: issuer,
}
}

func (*FakeOIDCConfig) AuthCodeURL(state string, _ ...oauth2.AuthCodeOption) string {
func (*OIDCConfig) AuthCodeURL(state string, _ ...oauth2.AuthCodeOption) string {
return "/?state=" + url.QueryEscape(state)
}

func (*FakeOIDCConfig) TokenSource(context.Context, *oauth2.Token) oauth2.TokenSource {
func (*OIDCConfig) TokenSource(context.Context, *oauth2.Token) oauth2.TokenSource {
return nil
}

func (*FakeOIDCConfig) Exchange(_ context.Context, code string, _ ...oauth2.AuthCodeOption) (*oauth2.Token, error) {
func (*OIDCConfig) Exchange(_ context.Context, code string, _ ...oauth2.AuthCodeOption) (*oauth2.Token, error) {
token, err := base64.StdEncoding.DecodeString(code)
if err != nil {
return nil, xerrors.Errorf("decode code: %w", err)
Expand All @@ -768,7 +768,7 @@ func (*FakeOIDCConfig) Exchange(_ context.Context, code string, _ ...oauth2.Auth
}), nil
}

func (o *FakeOIDCConfig) EncodeClaims(t *testing.T, claims jwt.MapClaims) string {
func (o *OIDCConfig) EncodeClaims(t *testing.T, claims jwt.MapClaims) string {
t.Helper()

if _, ok := claims["exp"]; !ok {
Expand All @@ -789,7 +789,7 @@ func (o *FakeOIDCConfig) EncodeClaims(t *testing.T, claims jwt.MapClaims) string
return base64.StdEncoding.EncodeToString([]byte(signed))
}

func (o *FakeOIDCConfig) OIDCConfig() *coderd.OIDCConfig {
func (o *OIDCConfig) OIDCConfig() *coderd.OIDCConfig {
return &coderd.OIDCConfig{
OAuth2Config: o,
Verifier: oidc.NewVerifier(o.issuer, &oidc.StaticKeySet{
Expand Down
2 changes: 1 addition & 1 deletion coderd/userauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cook
validUsername bool
)
for i := 0; i < 10; i++ {
alternate := fmt.Sprintf("%s_%s", original, namesgenerator.GetRandomName(1))
alternate := fmt.Sprintf("%s-%s", original, namesgenerator.GetRandomName(1))

params.Username = httpapi.UsernameFrom(alternate)

Expand Down
2 changes: 1 addition & 1 deletion coderd/userauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func TestUserOIDC(t *testing.T) {
client.SessionToken = authCookieValue(resp.Cookies())
user, err = client.User(ctx, "me")
require.NoError(t, err)
require.True(t, strings.HasPrefix(user.Username, "jon_"), "username %q should have prefix %q", user.Username, "jon")
require.True(t, strings.HasPrefix(user.Username, "jon-"), "username %q should have prefix %q", user.Username, "jon-")
})

t.Run("Disabled", func(t *testing.T) {
Expand Down