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

Feature/email change improvements #78

Closed
Closed
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
1 change: 1 addition & 0 deletions api/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
gotrueIssuer = "gotrue"
ValidateEvent = "validate"
SignupEvent = "signup"
EmailChangeEvent = "email_change"
LoginEvent = "login"
)

Expand Down
21 changes: 5 additions & 16 deletions api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import (

// UserUpdateParams parameters for updating a user
type UserUpdateParams struct {
Email string `json:"email"`
Password string `json:"password"`
EmailChangeToken string `json:"email_change_token"`
Data map[string]interface{} `json:"data"`
AppData map[string]interface{} `json:"app_metadata,omitempty"`
Email string `json:"email"`
Password string `json:"password"`
Data map[string]interface{} `json:"data"`
AppData map[string]interface{} `json:"app_metadata,omitempty"`
}

// UserGet returns a user
Expand Down Expand Up @@ -106,17 +105,7 @@ func (a *API) UserUpdate(w http.ResponseWriter, r *http.Request) error {
}
}

if params.EmailChangeToken != "" {
log.Debugf("Got change token %v", params.EmailChangeToken)

if params.EmailChangeToken != user.EmailChangeToken {
return unauthorizedError("Email Change Token didn't match token on file")
}

if terr = user.ConfirmEmailChange(tx); terr != nil {
return internalServerError("Error updating user").WithInternalError(terr)
}
} else if params.Email != "" && params.Email != user.Email {
if params.Email != "" && params.Email != user.Email {
if terr = a.validateEmail(ctx, params.Email); terr != nil {
return terr
}
Expand Down
55 changes: 51 additions & 4 deletions api/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ var (
)

const (
signupVerification = "signup"
recoveryVerification = "recovery"
inviteVerification = "invite"
magicLinkVerification = "magiclink"
signupVerification = "signup"
recoveryVerification = "recovery"
inviteVerification = "invite"
magicLinkVerification = "magiclink"
emailChangeVerification = "email_change"
)

// VerifyParams are the parameters the Verify endpoint accepts
Expand Down Expand Up @@ -77,6 +78,8 @@ func (a *API) Verify(w http.ResponseWriter, r *http.Request) error {
user, terr = a.signupVerify(ctx, tx, params)
case recoveryVerification, magicLinkVerification:
user, terr = a.recoverVerify(ctx, tx, params)
case emailChangeVerification:
user, terr = a.emailChangeVerify(ctx, tx, params)
default:
return unprocessableEntityError("Verify requires a verification type")
}
Expand Down Expand Up @@ -242,3 +245,47 @@ func (a *API) prepErrorRedirectURL(err *HTTPError, r *http.Request) string {
q.Set("error_description", err.Message)
return rurl + "#" + q.Encode()
}

func (a *API) emailChangeVerify(ctx context.Context, conn *storage.Connection, params *VerifyParams) (*models.User, error) {
instanceID := getInstanceID(ctx)
config := a.getConfig(ctx)
user, err := models.FindUserByEmailChangeToken(conn, params.Token)
if err != nil {
if models.IsNotFoundError(err) {
return nil, notFoundError(err.Error()).WithInternalError(redirectWithQueryError)
}
return nil, internalServerError("Database error finding user").WithInternalError(err)
}

nextDay := user.EmailChangeSentAt.Add(24 * time.Hour)
if user.EmailChangeSentAt != nil && time.Now().After(nextDay) {
return nil, expiredTokenError("Recovery token expired").WithInternalError(redirectWithQueryError)
}

err = a.db.Transaction(func(tx *storage.Connection) error {
var terr error

if params.Token != user.EmailChangeToken {
return unauthorizedError("Email Change Token didn't match token on file")
}
Comment on lines +268 to +270
Copy link
Member

Choose a reason for hiding this comment

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

We don't have to do this check here because we already do a models.FindUserByEmailChangeToken in line 252. If the token is invalid (doesn't match the email change token), then line 252 would return a models.IsNotFoundError


if terr = models.NewAuditLogEntry(tx, instanceID, user, models.UserModifiedAction, nil); terr != nil {
return terr
}

if terr = triggerEventHooks(ctx, tx, EmailChangeEvent, user, instanceID, config); terr != nil {
return terr
}

if terr = user.ConfirmEmailChange(tx); terr != nil {
return internalServerError("Error confirm email").WithInternalError(terr)
}

return nil
})
if err != nil {
return nil, err
}

return user, nil
}
2 changes: 1 addition & 1 deletion mailer/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (m *TemplateMailer) EmailChangeMail(user *models.User, referrerURL string)
redirectParam = "&redirect_to=" + referrerURL
}

url, err := getSiteURL(referrerURL, m.Config.SiteURL, m.Config.Mailer.URLPaths.EmailChange, "email_change_token="+user.EmailChangeToken+"&type=email_change"+redirectParam)
url, err := getSiteURL(referrerURL, m.Config.SiteURL, m.Config.Mailer.URLPaths.EmailChange, "token="+user.EmailChangeToken+"&type=email_change"+redirectParam)
if err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ func FindUserByRecoveryToken(tx *storage.Connection, token string) (*User, error
return findUser(tx, "recovery_token = ?", token)
}

// FindUserByRecoveryToken finds a user with the matching recovery token.
func FindUserByEmailChangeToken(tx *storage.Connection, token string) (*User, error) {
return findUser(tx, "email_change_token = ?", token)
}

// FindUserWithRefreshToken finds a user from the provided refresh token.
func FindUserWithRefreshToken(tx *storage.Connection, token string) (*User, *RefreshToken, error) {
refreshToken := &RefreshToken{}
Expand Down