[MM-41343] Password reset must be 24hour (#19484)

Этот коммит содержится в:
Julien Tant
2022-02-05 13:52:34 -07:00
коммит произвёл GitHub
родитель 5fa8499602
Коммит d2cabaebab
2 изменённых файлов: 50 добавлений и 27 удалений

Просмотреть файл

@@ -35,7 +35,7 @@ const (
TokenTypeTeamInvitation = "team_invitation" TokenTypeTeamInvitation = "team_invitation"
TokenTypeGuestInvitation = "guest_invitation" TokenTypeGuestInvitation = "guest_invitation"
TokenTypeCWSAccess = "cws_access_token" TokenTypeCWSAccess = "cws_access_token"
PasswordRecoverExpiryTime = 1000 * 60 * 60 // 1 hour PasswordRecoverExpiryTime = 1000 * 60 * 60 * 24 // 24 hours
InvitationExpiryTime = 1000 * 60 * 60 * 48 // 48 hours InvitationExpiryTime = 1000 * 60 * 60 * 48 // 48 hours
ImageProfilePixelDimension = 128 ImageProfilePixelDimension = 128
) )
@@ -1266,11 +1266,15 @@ func (a *App) UpdateHashedPassword(user *model.User, newHashedPassword string) *
} }
func (a *App) ResetPasswordFromToken(userSuppliedTokenString, newPassword string) *model.AppError { func (a *App) ResetPasswordFromToken(userSuppliedTokenString, newPassword string) *model.AppError {
return a.resetPasswordFromToken(userSuppliedTokenString, newPassword, model.GetMillis())
}
func (a *App) resetPasswordFromToken(userSuppliedTokenString, newPassword string, nowMilli int64) *model.AppError {
token, err := a.GetPasswordRecoveryToken(userSuppliedTokenString) token, err := a.GetPasswordRecoveryToken(userSuppliedTokenString)
if err != nil { if err != nil {
return err return err
} }
if model.GetMillis()-token.CreateAt >= PasswordRecoverExpiryTime { if nowMilli-token.CreateAt >= PasswordRecoverExpiryTime {
return model.NewAppError("resetPassword", "api.user.reset_password.link_expired.app_error", nil, "", http.StatusBadRequest) return model.NewAppError("resetPassword", "api.user.reset_password.link_expired.app_error", nil, "", http.StatusBadRequest)
} }

Просмотреть файл

@@ -966,6 +966,7 @@ func TestPasswordRecovery(t *testing.T) {
th := Setup(t).InitBasic() th := Setup(t).InitBasic()
defer th.TearDown() defer th.TearDown()
t.Run("password token with same email as during creation", func(t *testing.T) {
token, err := th.App.CreatePasswordRecoveryToken(th.BasicUser.Id, th.BasicUser.Email) token, err := th.App.CreatePasswordRecoveryToken(th.BasicUser.Id, th.BasicUser.Email)
assert.Nil(t, err) assert.Nil(t, err)
@@ -979,12 +980,12 @@ func TestPasswordRecovery(t *testing.T) {
assert.Equal(t, th.BasicUser.Id, tokenData.UserId) assert.Equal(t, th.BasicUser.Id, tokenData.UserId)
assert.Equal(t, th.BasicUser.Email, tokenData.Email) assert.Equal(t, th.BasicUser.Email, tokenData.Email)
// Password token with same eMail as during creation
err = th.App.ResetPasswordFromToken(token.Token, "abcdefgh") err = th.App.ResetPasswordFromToken(token.Token, "abcdefgh")
assert.Nil(t, err) assert.Nil(t, err)
})
// Password token with modified eMail after creation t.Run("password token with modified email as during creation", func(t *testing.T) {
token, err = th.App.CreatePasswordRecoveryToken(th.BasicUser.Id, th.BasicUser.Email) token, err := th.App.CreatePasswordRecoveryToken(th.BasicUser.Id, th.BasicUser.Email)
assert.Nil(t, err) assert.Nil(t, err)
th.App.UpdateConfig(func(c *model.Config) { th.App.UpdateConfig(func(c *model.Config) {
@@ -997,6 +998,24 @@ func TestPasswordRecovery(t *testing.T) {
err = th.App.ResetPasswordFromToken(token.Token, "abcdefgh") err = th.App.ResetPasswordFromToken(token.Token, "abcdefgh")
assert.NotNil(t, err) assert.NotNil(t, err)
})
t.Run("non-expired token", func(t *testing.T) {
token, err := th.App.CreatePasswordRecoveryToken(th.BasicUser.Id, th.BasicUser.Email)
assert.Nil(t, err)
err = th.App.resetPasswordFromToken(token.Token, "abcdefgh", model.GetMillis())
assert.Nil(t, err)
})
t.Run("expired token", func(t *testing.T) {
token, err := th.App.CreatePasswordRecoveryToken(th.BasicUser.Id, th.BasicUser.Email)
assert.Nil(t, err)
err = th.App.resetPasswordFromToken(token.Token, "abcdefgh", model.GetMillisForTime(time.Now().Add(25*time.Hour)))
assert.NotNil(t, err)
})
} }
func TestGetViewUsersRestrictions(t *testing.T) { func TestGetViewUsersRestrictions(t *testing.T) {