Skip to content
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

feat: set email_confirmed_at to null when autoconfirm is on #1661

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 0 additions & 5 deletions cmd/admin_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ func adminCreateUser(config *conf.GlobalConfiguration, args []string) {
}
}

if config.Mailer.Autoconfirm || autoconfirm {
if terr = user.Confirm(tx); terr != nil {
return terr
}
}
return nil
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/api/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func (a *API) createAccountFromExternalIdentity(tx *storage.Connection, r *http.
return nil, forbiddenError(ErrorCodeUserBanned, "User is banned")
}

if !user.IsConfirmed() {
if !user.IsConfirmed(config.Mailer.Autoconfirm) {
// The user may have other unconfirmed email + password
// combination, phone or oauth identities. These identities
// need to be removed when a new oauth identity is being added
Expand Down
4 changes: 3 additions & 1 deletion internal/api/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type InviteParams struct {
func (a *API) Invite(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
db := a.db.WithContext(ctx)
config := a.config

adminUser := getAdminUser(ctx)
params := &InviteParams{}
if err := retrieveRequestParams(r, params); err != nil {
Expand All @@ -39,7 +41,7 @@ func (a *API) Invite(w http.ResponseWriter, r *http.Request) error {

err = db.Transaction(func(tx *storage.Connection) error {
if user != nil {
if user.IsConfirmed() {
if user.IsConfirmed(config.Mailer.Autoconfirm) {
return unprocessableEntityError(ErrorCodeEmailExists, DuplicateEmailMsg)
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion internal/api/magic_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (a *API) MagicLink(w http.ResponseWriter, r *http.Request) error {
}
}
if user != nil {
isNewUser = !user.IsConfirmed()
isNewUser = !user.IsConfirmed(config.Mailer.Autoconfirm)
}
if isNewUser {
// User either doesn't exist or hasn't completed the signup process.
Expand Down
4 changes: 2 additions & 2 deletions internal/api/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (a *API) adminGenerateLink(w http.ResponseWriter, r *http.Request) error {
}
case mail.InviteVerification:
if user != nil {
if user.IsConfirmed() {
if user.IsConfirmed( /* autoconfirm: */ false) {
return unprocessableEntityError(ErrorCodeEmailExists, DuplicateEmailMsg)
}
} else {
Expand Down Expand Up @@ -187,7 +187,7 @@ func (a *API) adminGenerateLink(w http.ResponseWriter, r *http.Request) error {
}
case mail.SignupVerification:
if user != nil {
if user.IsConfirmed() {
if user.IsConfirmed( /* autoconfirm: */ false) {
return unprocessableEntityError(ErrorCodeEmailExists, DuplicateEmailMsg)
}
if err := user.UpdateUserMetaData(tx, params.Data); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/api/reauthenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (a *API) Reauthenticate(w http.ResponseWriter, r *http.Request) error {
}

if email != "" {
if !user.IsConfirmed() {
if !user.IsConfirmed(config.Mailer.Autoconfirm) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should use false always?

return unprocessableEntityError(ErrorCodeEmailNotConfirmed, "Please verify your email first.")
}
} else if phone != "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/api/resend.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (a *API) Resend(w http.ResponseWriter, r *http.Request) error {

switch params.Type {
case mail.SignupVerification:
if user.IsConfirmed() {
if user.IsConfirmed( /* autoconfirm: */ false) {
// if the user's email is confirmed already, we don't need to send a confirmation email again
return sendJSON(w, http.StatusOK, map[string]string{})
}
Expand Down
11 changes: 5 additions & 6 deletions internal/api/signup.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (a *API) Signup(w http.ResponseWriter, r *http.Request) error {
err = db.Transaction(func(tx *storage.Connection) error {
var terr error
if user != nil {
if (params.Provider == "email" && user.IsConfirmed()) || (params.Provider == "phone" && user.IsPhoneConfirmed()) {
if (params.Provider == "email" && user.IsConfirmed(config.Mailer.Autoconfirm)) || (params.Provider == "phone" && user.IsPhoneConfirmed()) {
return UserExistsError
}
// do not update the user because we can't be sure of their claimed identity
Expand Down Expand Up @@ -221,16 +221,15 @@ func (a *API) Signup(w http.ResponseWriter, r *http.Request) error {
}
user.Identities = []models.Identity{*identity}

if params.Provider == "email" && !user.IsConfirmed() {
if params.Provider == "email" && !user.IsConfirmed(config.Mailer.Autoconfirm) {
if config.Mailer.Autoconfirm {
if terr = models.NewAuditLogEntry(r, tx, user, models.UserSignedUpAction, "", map[string]interface{}{
"provider": params.Provider,
}); terr != nil {
return terr
}
if terr = user.Confirm(tx); terr != nil {
return internalServerError("Database error updating user").WithInternalError(terr)
}

// note the lack of user.Confirm() call here, as "autoconfirm" no longer sets the email_confirmed_at column, but leaves it to be null
} else {
if terr = models.NewAuditLogEntry(r, tx, user, models.UserConfirmationRequestedAction, "", map[string]interface{}{
"provider": params.Provider,
Expand Down Expand Up @@ -313,7 +312,7 @@ func (a *API) Signup(w http.ResponseWriter, r *http.Request) error {
}

// handles case where Mailer.Autoconfirm is true or Phone.Autoconfirm is true
if user.IsConfirmed() || user.IsPhoneConfirmed() {
if user.IsConfirmed(config.Mailer.Autoconfirm) || user.IsPhoneConfirmed() {
var token *AccessTokenResponse
err = db.Transaction(func(tx *storage.Connection) error {
var terr error
Expand Down
2 changes: 1 addition & 1 deletion internal/api/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (a *API) ResourceOwnerPasswordGrant(ctx context.Context, w http.ResponseWri
return oauthError("invalid_grant", InvalidLoginMessage)
}

if params.Email != "" && !user.IsConfirmed() {
if params.Email != "" && !user.IsConfirmed(config.Mailer.Autoconfirm) {
return oauthError("invalid_grant", "Email not confirmed")
} else if params.Phone != "" && !user.IsPhoneConfirmed() {
return oauthError("invalid_grant", "Phone not confirmed")
Expand Down
2 changes: 1 addition & 1 deletion internal/api/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ func (ts *TokenTestSuite) TestMagicLinkPKCESignIn() {

u, err = models.FindUserByEmailAndAudience(ts.API.db, "[email protected]", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
assert.True(ts.T(), u.IsConfirmed())
assert.True(ts.T(), u.IsConfirmed(false))

f, err := url.ParseQuery(rURL.RawQuery)
require.NoError(ts.T(), err)
Expand Down
4 changes: 3 additions & 1 deletion internal/api/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,14 @@ func (a *API) signupVerify(r *http.Request, ctx context.Context, conn *storage.C
}

func (a *API) recoverVerify(r *http.Request, conn *storage.Connection, user *models.User) (*models.User, error) {
config := a.config

err := conn.Transaction(func(tx *storage.Connection) error {
var terr error
if terr = user.Recover(tx); terr != nil {
return terr
}
if !user.IsConfirmed() {
if !user.IsConfirmed(config.Mailer.Autoconfirm) {
if terr = models.NewAuditLogEntry(r, tx, user, models.UserSignedUpAction, "", nil); terr != nil {
return terr
}
Expand Down
18 changes: 9 additions & 9 deletions internal/api/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (ts *VerifyTestSuite) TestVerifyPasswordRecovery() {
require.NoError(ts.T(), err)

assert.WithinDuration(ts.T(), time.Now(), *u.RecoverySentAt, 1*time.Second)
assert.False(ts.T(), u.IsConfirmed())
assert.False(ts.T(), u.IsConfirmed(false))

recoveryToken := u.RecoveryToken

Expand All @@ -119,7 +119,7 @@ func (ts *VerifyTestSuite) TestVerifyPasswordRecovery() {

u, err = models.FindUserByEmailAndAudience(ts.API.db, "[email protected]", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
assert.True(ts.T(), u.IsConfirmed())
assert.True(ts.T(), u.IsConfirmed(false))

if c.isPKCE {
rURL, _ := w.Result().Location()
Expand Down Expand Up @@ -212,7 +212,7 @@ func (ts *VerifyTestSuite) TestVerifySecureEmailChange() {
require.NoError(ts.T(), err)

assert.WithinDuration(ts.T(), time.Now(), *u.EmailChangeSentAt, 1*time.Second)
assert.False(ts.T(), u.IsConfirmed())
assert.False(ts.T(), u.IsConfirmed(false))

// Verify new email
reqURL := fmt.Sprintf("http://localhost/verify?type=%s&token=%s", mail.EmailChangeVerification, newTokenHash)
Expand Down Expand Up @@ -478,7 +478,7 @@ func (ts *VerifyTestSuite) TestVerifyPermitedCustomUri() {
require.NoError(ts.T(), err)

assert.WithinDuration(ts.T(), time.Now(), *u.RecoverySentAt, 1*time.Second)
assert.False(ts.T(), u.IsConfirmed())
assert.False(ts.T(), u.IsConfirmed(false))

redirectURL, _ := url.Parse(ts.Config.URIAllowList[0])

Expand All @@ -493,7 +493,7 @@ func (ts *VerifyTestSuite) TestVerifyPermitedCustomUri() {

u, err = models.FindUserByEmailAndAudience(ts.API.db, "[email protected]", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
assert.True(ts.T(), u.IsConfirmed())
assert.True(ts.T(), u.IsConfirmed(false))
}

func (ts *VerifyTestSuite) TestVerifyNotPermitedCustomUri() {
Expand Down Expand Up @@ -524,7 +524,7 @@ func (ts *VerifyTestSuite) TestVerifyNotPermitedCustomUri() {
require.NoError(ts.T(), err)

assert.WithinDuration(ts.T(), time.Now(), *u.RecoverySentAt, 1*time.Second)
assert.False(ts.T(), u.IsConfirmed())
assert.False(ts.T(), u.IsConfirmed(false))

fakeredirectURL, _ := url.Parse("http://custom-url.com")
siteURL, _ := url.Parse(ts.Config.SiteURL)
Expand All @@ -540,7 +540,7 @@ func (ts *VerifyTestSuite) TestVerifyNotPermitedCustomUri() {

u, err = models.FindUserByEmailAndAudience(ts.API.db, "[email protected]", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
assert.True(ts.T(), u.IsConfirmed())
assert.True(ts.T(), u.IsConfirmed(false))
}

func (ts *VerifyTestSuite) TestVerifySignupWithRedirectURLContainedPath() {
Expand Down Expand Up @@ -671,7 +671,7 @@ func (ts *VerifyTestSuite) TestVerifySignupWithRedirectURLContainedPath() {

u, err = models.FindUserByEmailAndAudience(ts.API.db, "[email protected]", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
assert.True(ts.T(), u.IsConfirmed())
assert.True(ts.T(), u.IsConfirmed(false))
})
}
}
Expand Down Expand Up @@ -734,7 +734,7 @@ func (ts *VerifyTestSuite) TestVerifyPKCEOTP() {

u, err = models.FindUserByEmailAndAudience(ts.API.db, "[email protected]", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
assert.True(ts.T(), u.IsConfirmed())
assert.True(ts.T(), u.IsConfirmed(false))

f, err := url.ParseQuery(rURL.RawQuery)
require.NoError(ts.T(), err)
Expand Down
1 change: 1 addition & 0 deletions internal/conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ func (c *SMTPConfiguration) Validate() error {
}

type MailerConfiguration struct {
// Autoconfirm actually means allow unverified email sign-ups.
Autoconfirm bool `json:"autoconfirm"`
AllowUnverifiedEmailSignIns bool `json:"allow_unverified_email_sign_ins" split_words:"true" default:"false"`

Expand Down
6 changes: 3 additions & 3 deletions internal/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ func (u *User) BeforeSave(tx *pop.Connection) error {

// IsConfirmed checks if a user has already been
// registered and confirmed.
func (u *User) IsConfirmed() bool {
return u.EmailConfirmedAt != nil
func (u *User) IsConfirmed(autoconfirm bool) bool {
return autoconfirm || u.EmailConfirmedAt != nil
}

// HasBeenInvited checks if user has been invited
Expand Down Expand Up @@ -488,7 +488,7 @@ func (u *User) ConfirmEmailChange(tx *storage.Connection, status int) error {
return err
}

if !u.IsConfirmed() {
if !u.IsConfirmed( /* autoconfirm: */ false) {
if err := u.Confirm(tx); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/models/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (ts *UserTestSuite) TestRemoveUnconfirmedIdentities() {
// reload the user
require.NoError(ts.T(), ts.db.Load(user))

require.False(ts.T(), user.IsConfirmed(), "user's email must not be confirmed")
require.False(ts.T(), user.IsConfirmed(false), "user's email must not be confirmed")

require.NoError(ts.T(), user.RemoveUnconfirmedIdentities(ts.db, idTwitter))

Expand Down
Loading