-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
fix: reimplement password reset #20966
Closed
Closed
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5891461
add actions on user for pw reset
raquelmsmith 08c8550
fix import
raquelmsmith 156e6d6
add throttle
raquelmsmith 145764f
update frontend
raquelmsmith a4eeb54
update admin link generator
raquelmsmith 7799774
update ee test
raquelmsmith f9a8557
test new implementation
raquelmsmith 015eb07
remove old implementation
raquelmsmith dec28c5
fix a bunch of tests
raquelmsmith 94569aa
Update query snapshots
github-actions[bot] 90f4c81
fix mypy
raquelmsmith 8a590b4
Merge branch 'fix/reimplement-pw-reset' of https://github.com/PostHog…
raquelmsmith 046db9f
Merge branch 'master' into fix/reimplement-pw-reset
raquelmsmith cbf75ed
Update UI snapshots for `chromium` (2)
github-actions[bot] b0f1dc4
merge
benjackwhite e484803
fix
benjackwhite fa25949
Added missing test
benjackwhite 63bd492
Update query snapshots
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,7 +178,7 @@ def test_cannot_reset_password_with_enforced_sso(self): | |
EMAIL_HOST="localhost", | ||
SITE_URL="https://my.posthog.net", | ||
): | ||
response = self.client.post("/api/reset/", {"email": "[email protected]"}) | ||
response = self.client.post("/api/users/@me/request_password_reset/", {"email": "[email protected]"}) | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertEqual( | ||
response.json(), | ||
|
Binary file modified
BIN
-2.08 KB
(85%)
frontend/__snapshots__/scenes-other-password-reset-complete--default--dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-2.55 KB
(89%)
frontend/__snapshots__/scenes-other-password-reset-complete--default--light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import datetime | ||
|
||
import structlog | ||
from django.contrib.auth.models import AbstractBaseUser | ||
from django.contrib.auth.tokens import PasswordResetTokenGenerator | ||
from rest_framework import exceptions | ||
from sentry_sdk import capture_exception | ||
|
||
from posthog.models.user import User | ||
from posthog.tasks.email import send_password_reset | ||
|
||
logger = structlog.get_logger(__name__) | ||
|
||
|
||
class PHPasswordResetTokenGenerator(PasswordResetTokenGenerator): | ||
def _make_hash_value(self, user: AbstractBaseUser, timestamp): | ||
# Due to type differences between the user model and the token generator, we need to | ||
# re-fetch the user from the database to get the correct type. | ||
usable_user: User = User.objects.get(pk=user.pk) | ||
logger.info( | ||
f"Password reset token for {usable_user.email} requested at {usable_user.requested_password_reset_at}" | ||
) | ||
return f"{usable_user.pk}{usable_user.email}{usable_user.requested_password_reset_at}{timestamp}" | ||
|
||
|
||
password_reset_token_generator = PHPasswordResetTokenGenerator() | ||
|
||
|
||
class PasswordResetter: | ||
@staticmethod | ||
def create_token_and_send_reset_email(user: User) -> None: | ||
user.requested_password_reset_at = datetime.datetime.now(datetime.timezone.utc) | ||
user.save() | ||
token = password_reset_token_generator.make_token(user) | ||
logger.info(f"Password reset requested for {user.email}") | ||
|
||
try: | ||
send_password_reset(user.pk, token) | ||
except Exception as e: | ||
capture_exception(Exception(f"Verification email failed: {e}")) | ||
raise exceptions.APIException( | ||
detail="Could not send email verification email. Please try again by logging in with your email and password." | ||
) | ||
|
||
@staticmethod | ||
def check_token(user: User, token: str) -> bool: | ||
return password_reset_token_generator.check_token(user, token) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Personally I think this should be in a sub folder like the
services
folder to differentiate it from api routers