-
Notifications
You must be signed in to change notification settings - Fork 943
feat: implement api for "forgot password?" flow #14915
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
Changes from 1 commit
b44adff
60fc32c
32a8df4
e486923
da87e9b
39fa0f1
b89dfd7
714e316
c4ed205
fa9d426
883a231
bff384b
26cc758
25581c2
764b0cc
6078409
3a0946c
1a7ad7a
6bbcff2
dfc32b1
9f80082
fe9b3f8
b42b73c
034a7d4
e14d43b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
INSERT INTO notification_templates (id, name, title_template, body_template, "group", actions) | ||
VALUES ('62f86a30-2330-4b61-a26d-311ff3b608cf', 'One Time Passcode', E'Your one time passcode is enclosed.', | ||
E'Hi {{.UserName}},\n\nA request to reset the password for your Coder account has been made. Your one time passcode is:\n\n**{{.Labels.one_time_passcode}}**\n\nIf you did not request to reset your password, you can ignore this message.', | ||
VALUES ('62f86a30-2330-4b61-a26d-311ff3b608cf', 'One-Time Passcode', E'Your one-time passcode is enclosed.', | ||
E'Hi {{.UserName}},\n\nA request to reset the password for your Coder account has been made. Your one-time passcode is:\n\n**{{.Labels.one_time_passcode}}**\n\nIf you did not request to reset your password, you can ignore this message.', | ||
'User Events', '[]'::jsonb); | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Your one time passcode is enclosed. | ||
Your one-time passcode is enclosed. | ||
DanielleMaywood marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -46,6 +46,7 @@ const ( | |||
userAuthLoggerName = "userauth" | ||||
OAuthConvertCookieValue = "coder_oauth_convert_jwt" | ||||
mergeStateStringPrefix = "convert-" | ||||
oneTimePasscodeDuration = 20 * time.Minute | ||||
DanielleMaywood marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
) | ||||
|
||||
type OAuthConvertStateClaims struct { | ||||
|
@@ -202,13 +203,13 @@ func (api *API) postConvertLoginType(rw http.ResponseWriter, r *http.Request) { | |||
}) | ||||
} | ||||
|
||||
// Requests a one-time-passcode for a user. | ||||
// Requests a one-time passcode for a user. | ||||
// | ||||
// @Summary Request one-time-passcode. | ||||
// @Summary Request one-time passcode. | ||||
DanielleMaywood marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
// @ID request-one-time-passcode | ||||
// @Accept json | ||||
// @Tags Authorization | ||||
// @Param request body codersdk.RequestOneTimePasscodeRequest true "Request one time passcode request" | ||||
// @Param request body codersdk.RequestOneTimePasscodeRequest true "Request one-time passcode request" | ||||
// @Success 200 | ||||
// @Router /users/request-one-time-passcode [post] | ||||
func (api *API) postRequestOneTimePasscode(rw http.ResponseWriter, r *http.Request) { | ||||
|
@@ -243,7 +244,7 @@ func (api *API) postRequestOneTimePasscode(rw http.ResponseWriter, r *http.Reque | |||
rw.WriteHeader(http.StatusOK) | ||||
dannykopping marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
}() | ||||
|
||||
//nolint:gocritic // In order to request a one-time-passcode, we need to get the user first! | ||||
//nolint:gocritic // In order to request a one-time passcode, we need to get the user first - and can only do that in the system auth context. | ||||
user, err := api.Database.GetUserByEmailOrUsername(dbauthz.AsSystemRestricted(ctx), database.GetUserByEmailOrUsernameParams{ | ||||
Email: req.Email, | ||||
}) | ||||
|
@@ -254,22 +255,22 @@ func (api *API) postRequestOneTimePasscode(rw http.ResponseWriter, r *http.Reque | |||
aReq.Old = user | ||||
|
||||
passcode := uuid.New() | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side-note: I always feel weirded out using plain UUIDs for auth, but we do it elsewhere and UUIDv4 is supposedly OK. There are varying views on this though. I bring this up because I wonder if we should consider using something with more entropy in the future. 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (According to ChatGPT o1-preview) there are 122 bits of entropy in UUIDv4, isn't that enough for you? 😆 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also worth noting, the UUIDv4 implementation we use does use a cryptographically secure RNG. Line 122 in 764b0cc
https://github.com/google/uuid/blob/0e97ed3b537927cb4afea366bc4cc36f6eb37e75/uuid.go#L45 https://github.com/google/uuid/blob/0e97ed3b537927cb4afea366bc4cc36f6eb37e75/uuid.go#L9 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dannykopping exactly, it's a few bits short. I believe 128 or more bits of entropy is the recommendation (required by RFC6749, for example). 😄 @DanielleMaywood yes, definitely worth noting 👍🏻 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ha! TIL 🥲 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case, should we go for an alternative to using a UUID here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mtojek and I discussed this and have decided that the current solution should be sufficient. The tokens are generated with a cryptographically secure RNG, the rate limiter is pretty strict (60req/s) and tokens do not last very long so the few bits short in entropy should be sufficient. We can always review this in the future if we decide a different approach should be taken. |
||||
passcodeExpiresAt := dbtime.Now().Add(20 * time.Minute) | ||||
passcodeExpiresAt := dbtime.Now().Add(oneTimePasscodeDuration) | ||||
|
||||
hashedPasscode, err := userpassword.Hash(passcode.String()) | ||||
if err != nil { | ||||
logger.Error(ctx, "unable to hash passcode", slog.Error(err)) | ||||
return | ||||
} | ||||
|
||||
//nolint:gocritic // We need to be able to save the one-time-passcode. | ||||
//nolint:gocritic // We need the system auth context to be able to save the one-time passcode. | ||||
err = api.Database.UpdateUserHashedOneTimePasscode(dbauthz.AsSystemRestricted(ctx), database.UpdateUserHashedOneTimePasscodeParams{ | ||||
ID: user.ID, | ||||
HashedOneTimePasscode: []byte(hashedPasscode), | ||||
OneTimePasscodeExpiresAt: sql.NullTime{Time: passcodeExpiresAt, Valid: true}, | ||||
}) | ||||
if err != nil { | ||||
DanielleMaywood marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
logger.Error(ctx, "unable to set user hashed one time passcode", slog.Error(err)) | ||||
logger.Error(ctx, "unable to set user hashed one-time passcode", slog.Error(err)) | ||||
return | ||||
} | ||||
|
||||
|
@@ -278,16 +279,16 @@ func (api *API) postRequestOneTimePasscode(rw http.ResponseWriter, r *http.Reque | |||
newUser.OneTimePasscodeExpiresAt = sql.NullTime{Time: passcodeExpiresAt, Valid: true} | ||||
aReq.New = newUser | ||||
|
||||
// Send the one-time-passcode to the user. | ||||
// Send the one-time passcode to the user. | ||||
err = api.notifyUserRequestedOneTimePasscode(ctx, user, passcode.String()) | ||||
if err != nil { | ||||
logger.Error(ctx, "unable to notify user about one time passcode request", slog.Error(err)) | ||||
logger.Error(ctx, "unable to notify user about one-time passcode request", slog.Error(err)) | ||||
} | ||||
} | ||||
|
||||
func (api *API) notifyUserRequestedOneTimePasscode(ctx context.Context, user database.User, passcode string) error { | ||||
_, err := api.NotificationsEnqueuer.Enqueue( | ||||
//nolint:gocritic // We need to be able to send the user their one time passcode. | ||||
//nolint:gocritic // We need the system auth context to be able to send the user their one-time passcode. | ||||
dbauthz.AsSystemRestricted(ctx), | ||||
user.ID, | ||||
notifications.TemplateUserRequestedOneTimePasscode, | ||||
|
@@ -302,9 +303,9 @@ func (api *API) notifyUserRequestedOneTimePasscode(ctx context.Context, user dat | |||
return nil | ||||
} | ||||
|
||||
// Change a users password with a one-time-passcode. | ||||
// Change a users password with a one-time passcode. | ||||
// | ||||
// @Summary Change password with a one-time-passcode. | ||||
// @Summary Change password with a one-time passcode. | ||||
// @ID change-password-with-a-one-time-passcode | ||||
// @Accept json | ||||
// @Tags Authorization | ||||
|
@@ -338,7 +339,7 @@ func (api *API) postChangePasswordWithOneTimePasscode(rw http.ResponseWriter, r | |||
return | ||||
} | ||||
|
||||
//nolint:gocritic // In order to change a user's password, we need to get the user first! | ||||
//nolint:gocritic // In order to change a user's password, we need to get the user first - and can only do that in the system auth context. | ||||
user, err := api.Database.GetUserByEmailOrUsername(dbauthz.AsSystemRestricted(ctx), database.GetUserByEmailOrUsernameParams{ | ||||
dannykopping marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
Email: req.Email, | ||||
}) | ||||
|
@@ -361,7 +362,7 @@ func (api *API) postChangePasswordWithOneTimePasscode(rw http.ResponseWriter, r | |||
} | ||||
|
||||
if !equal { | ||||
httpapi.Write(ctx, rw, http.StatusUnauthorized, codersdk.Response{ | ||||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||||
Message: "Incorrect email or one-time-passcode.", | ||||
}) | ||||
return | ||||
|
@@ -398,7 +399,7 @@ func (api *API) postChangePasswordWithOneTimePasscode(rw http.ResponseWriter, r | |||
} | ||||
|
||||
err = api.Database.InTx(func(tx database.Store) error { | ||||
//nolint:gocritic // We need to update the user's password. | ||||
//nolint:gocritic // We need the system auth context to be able to update the user's password. | ||||
err = tx.UpdateUserHashedPassword(dbauthz.AsSystemRestricted(ctx), database.UpdateUserHashedPasswordParams{ | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wish we could do this without system context somehow, seems like a potential security risk. |
||||
ID: user.ID, | ||||
HashedPassword: []byte(newHashedPassword), | ||||
|
@@ -407,7 +408,7 @@ func (api *API) postChangePasswordWithOneTimePasscode(rw http.ResponseWriter, r | |||
return xerrors.Errorf("update user hashed password: %w", err) | ||||
} | ||||
|
||||
//nolint:gocritic // We need to delete all API keys for the user. | ||||
//nolint:gocritic // We need the system auth context to be able to delete all API keys for the user. | ||||
err = tx.DeleteAPIKeysByUserID(dbauthz.AsSystemRestricted(ctx), user.ID) | ||||
if err != nil { | ||||
return xerrors.Errorf("delete api keys for user: %w", err) | ||||
|
@@ -430,7 +431,7 @@ func (api *API) postChangePasswordWithOneTimePasscode(rw http.ResponseWriter, r | |||
newUser.HashedOneTimePasscode = nil | ||||
aReq.New = newUser | ||||
|
||||
rw.WriteHeader(http.StatusNoContent) | ||||
rw.WriteHeader(http.StatusOK) | ||||
} | ||||
|
||||
// Authenticates the user with an email and password. | ||||
|
Uh oh!
There was an error while loading. Please reload this page.