-
Notifications
You must be signed in to change notification settings - Fork 875
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.', | ||
VALUES ('62f86a30-2330-4b61-a26d-311ff3b608cf', 'One-Time Passcode', E'Your One-Time Passcode for Coder.', | ||
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 for Coder. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,10 +43,10 @@ import ( | |
) | ||
|
||
const ( | ||
userAuthLoggerName = "userauth" | ||
OAuthConvertCookieValue = "coder_oauth_convert_jwt" | ||
mergeStateStringPrefix = "convert-" | ||
oneTimePasscodeDuration = 20 * time.Minute | ||
userAuthLoggerName = "userauth" | ||
OAuthConvertCookieValue = "coder_oauth_convert_jwt" | ||
mergeStateStringPrefix = "convert-" | ||
oneTimePasscodeValidityPeriod = 20 * time.Minute | ||
) | ||
|
||
type OAuthConvertStateClaims struct { | ||
|
@@ -210,7 +210,7 @@ func (api *API) postConvertLoginType(rw http.ResponseWriter, r *http.Request) { | |
// @Accept json | ||
// @Tags Authorization | ||
// @Param request body codersdk.RequestOneTimePasscodeRequest true "Request one-time passcode request" | ||
// @Success 200 | ||
// @Success 204 | ||
// @Router /users/request-one-time-passcode [post] | ||
func (api *API) postRequestOneTimePasscode(rw http.ResponseWriter, r *http.Request) { | ||
var ( | ||
|
@@ -241,7 +241,7 @@ func (api *API) postRequestOneTimePasscode(rw http.ResponseWriter, r *http.Reque | |
defer func() { | ||
// We always send the same response. If we give a more detailed response | ||
// it would open us up to an enumeration attack. | ||
rw.WriteHeader(http.StatusOK) | ||
rw.WriteHeader(http.StatusNoContent) | ||
}() | ||
|
||
//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. | ||
|
@@ -255,7 +255,7 @@ func (api *API) postRequestOneTimePasscode(rw http.ResponseWriter, r *http.Reque | |
aReq.Old = user | ||
|
||
passcode := uuid.New() | ||
passcodeExpiresAt := dbtime.Now().Add(oneTimePasscodeDuration) | ||
passcodeExpiresAt := dbtime.Now().Add(oneTimePasscodeValidityPeriod) | ||
|
||
hashedPasscode, err := userpassword.Hash(passcode.String()) | ||
if err != nil { | ||
|
@@ -279,13 +279,15 @@ func (api *API) postRequestOneTimePasscode(rw http.ResponseWriter, r *http.Reque | |
auditUser.OneTimePasscodeExpiresAt = sql.NullTime{Time: passcodeExpiresAt, Valid: true} | ||
aReq.New = auditUser | ||
|
||
if user.ID != uuid.Nil { | ||
// 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)) | ||
go func() { | ||
if user.ID != uuid.Nil { | ||
// Send the one-time passcode to the user. | ||
err = api.notifyUserRequestedOneTimePasscode(context.Background(), user, passcode.String()) | ||
if err != nil { | ||
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 { | ||
|
@@ -312,7 +314,7 @@ func (api *API) notifyUserRequestedOneTimePasscode(ctx context.Context, user dat | |
// @Accept json | ||
// @Tags Authorization | ||
// @Param request body codersdk.ChangePasswordWithOneTimePasscodeRequest true "Change password request" | ||
// @Success 200 | ||
// @Success 204 | ||
// @Router /users/change-password-with-one-time-passcode [post] | ||
func (api *API) postChangePasswordWithOneTimePasscode(rw http.ResponseWriter, r *http.Request) { | ||
var ( | ||
|
@@ -354,14 +356,14 @@ func (api *API) postChangePasswordWithOneTimePasscode(rw http.ResponseWriter, r | |
|
||
equal, err := userpassword.Compare(string(user.HashedOneTimePasscode), req.OneTimePasscode) | ||
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. What happens here when you give a valid user email, but the token is null? And where do we verify the expiry of the token? 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. Oh I've completely forgotten to check the expiry 🤦♀️ Thank you for spotting that. Will definitely get that added. 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've now added a test to verify that tokens do expire. |
||
if err != nil { | ||
logger.Error(ctx, "unable to compare one time passcode", slog.Error(err)) | ||
return xerrors.Errorf("compare one time passcode: %w", err) | ||
logger.Error(ctx, "unable to compare one-time passcode", slog.Error(err)) | ||
return xerrors.Errorf("compare one-time passcode: %w", err) | ||
} | ||
|
||
now := dbtime.Now() | ||
if !equal || now.After(user.OneTimePasscodeExpiresAt.Time) { | ||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: "Incorrect email or one-time-passcode.", | ||
Message: "Incorrect email or one-time passcode.", | ||
}) | ||
return nil | ||
} | ||
|
@@ -415,7 +417,7 @@ func (api *API) postChangePasswordWithOneTimePasscode(rw http.ResponseWriter, r | |
auditUser.HashedOneTimePasscode = nil | ||
aReq.New = auditUser | ||
|
||
rw.WriteHeader(http.StatusOK) | ||
rw.WriteHeader(http.StatusNoContent) | ||
|
||
return nil | ||
}, nil) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
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 comment
The 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 comment
The 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.
coder/go.mod
Line 122 in 764b0cc
https://github.com/google/uuid/blob/0e97ed3b537927cb4afea366bc4cc36f6eb37e75/uuid.go#L45
https://github.com/google/uuid/blob/0e97ed3b537927cb4afea366bc4cc36f6eb37e75/uuid.go#L9
https://pkg.go.dev/crypto/rand#pkg-variables
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.
@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 comment
The 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 comment
The 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 comment
The 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.