-
Notifications
You must be signed in to change notification settings - Fork 936
feat: implement sign up with GitHub for the first user #16629
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import ( | |
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/atomic" | ||
"golang.org/x/oauth2" | ||
"golang.org/x/xerrors" | ||
|
||
|
@@ -254,37 +255,64 @@ func TestUserOAuth2Github(t *testing.T) { | |
}) | ||
t.Run("BlockSignups", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
db, ps := dbtestutil.NewDB(t) | ||
|
||
id := atomic.NewInt64(100) | ||
login := atomic.NewString("testuser") | ||
email := atomic.NewString("[email protected]") | ||
|
||
client := coderdtest.New(t, &coderdtest.Options{ | ||
Database: db, | ||
Pubsub: ps, | ||
GithubOAuth2Config: &coderd.GithubOAuth2Config{ | ||
OAuth2Config: &testutil.OAuth2Config{}, | ||
AllowOrganizations: []string{"coder"}, | ||
ListOrganizationMemberships: func(ctx context.Context, client *http.Client) ([]*github.Membership, error) { | ||
ListOrganizationMemberships: func(_ context.Context, _ *http.Client) ([]*github.Membership, error) { | ||
return []*github.Membership{{ | ||
State: &stateActive, | ||
Organization: &github.Organization{ | ||
Login: github.String("coder"), | ||
}, | ||
}}, nil | ||
}, | ||
AuthenticatedUser: func(ctx context.Context, client *http.Client) (*github.User, error) { | ||
AuthenticatedUser: func(_ context.Context, _ *http.Client) (*github.User, error) { | ||
id := id.Load() | ||
login := login.Load() | ||
return &github.User{ | ||
ID: github.Int64(100), | ||
Login: github.String("testuser"), | ||
ID: &id, | ||
Login: &login, | ||
Name: github.String("The Right Honorable Sir Test McUser"), | ||
}, nil | ||
}, | ||
ListEmails: func(ctx context.Context, client *http.Client) ([]*github.UserEmail, error) { | ||
ListEmails: func(_ context.Context, _ *http.Client) ([]*github.UserEmail, error) { | ||
email := email.Load() | ||
return []*github.UserEmail{{ | ||
Email: github.String("[email protected]"), | ||
Email: &email, | ||
Verified: github.Bool(true), | ||
Primary: github.Bool(true), | ||
}}, nil | ||
}, | ||
}, | ||
}) | ||
|
||
// The first user in a deployment with signups disabled will be allowed to sign up, | ||
// but all the other users will not. | ||
resp := oauth2Callback(t, client) | ||
require.Equal(t, http.StatusTemporaryRedirect, resp.StatusCode) | ||
|
||
ctx := testutil.Context(t, testutil.WaitLong) | ||
|
||
// nolint:gocritic // Unit test | ||
count, err := db.GetUserCount(dbauthz.AsSystemRestricted(ctx)) | ||
require.NoError(t, err) | ||
require.Equal(t, int64(1), count) | ||
|
||
id.Store(101) | ||
email.Store("[email protected]") | ||
login.Store("someotheruser") | ||
|
||
resp = oauth2Callback(t, client) | ||
require.Equal(t, http.StatusForbidden, resp.StatusCode) | ||
}) | ||
t.Run("MultiLoginNotAllowed", func(t *testing.T) { | ||
|
@@ -988,6 +1016,7 @@ func TestUserOIDC(t *testing.T) { | |
IgnoreEmailVerified bool | ||
IgnoreUserInfo bool | ||
UseAccessToken bool | ||
PrecreateFirstUser bool | ||
}{ | ||
{ | ||
Name: "NoSub", | ||
|
@@ -1150,7 +1179,17 @@ func TestUserOIDC(t *testing.T) { | |
"email_verified": true, | ||
"sub": uuid.NewString(), | ||
}, | ||
StatusCode: http.StatusForbidden, | ||
StatusCode: http.StatusForbidden, | ||
PrecreateFirstUser: true, | ||
}, | ||
{ | ||
Name: "FirstSignup", | ||
IDTokenClaims: jwt.MapClaims{ | ||
"email": "[email protected]", | ||
"email_verified": true, | ||
"sub": uuid.NewString(), | ||
}, | ||
StatusCode: http.StatusOK, | ||
}, | ||
{ | ||
Name: "UsernameFromEmail", | ||
|
@@ -1443,15 +1482,22 @@ func TestUserOIDC(t *testing.T) { | |
}) | ||
numLogs := len(auditor.AuditLogs()) | ||
|
||
ctx := testutil.Context(t, testutil.WaitShort) | ||
if tc.PrecreateFirstUser { | ||
owner.CreateFirstUser(ctx, codersdk.CreateFirstUserRequest{ | ||
Email: "[email protected]", | ||
Username: "precreated", | ||
Password: "SomeSecurePassword!", | ||
}) | ||
} | ||
|
||
client, resp := fake.AttemptLogin(t, owner, tc.IDTokenClaims) | ||
numLogs++ // add an audit log for login | ||
require.Equal(t, tc.StatusCode, resp.StatusCode) | ||
if tc.AssertResponse != nil { | ||
tc.AssertResponse(t, resp) | ||
} | ||
|
||
ctx := testutil.Context(t, testutil.WaitShort) | ||
|
||
if tc.AssertUser != nil { | ||
user, err := client.User(ctx, "me") | ||
require.NoError(t, err) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,18 +13,15 @@ import { SetupPage } from "./SetupPage"; | |
import { Language as PageViewLanguage } from "./SetupPageView"; | ||
|
||
const fillForm = async ({ | ||
username = "someuser", | ||
email = "[email protected]", | ||
password = "password", | ||
}: { | ||
username?: string; | ||
email?: string; | ||
password?: string; | ||
} = {}) => { | ||
const usernameField = screen.getByLabelText(PageViewLanguage.usernameLabel); | ||
const emailField = screen.getByLabelText(PageViewLanguage.emailLabel); | ||
const passwordField = screen.getByLabelText(PageViewLanguage.passwordLabel); | ||
await userEvent.type(usernameField, username); | ||
await userEvent.type(emailField, email); | ||
await userEvent.type(passwordField, password); | ||
const submitButton = screen.getByRole("button", { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️