forked from uselotus/lotus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing Reset Password flow (uselotus#221)
Co-authored-by: Diego Escobedo <[email protected]>
- Loading branch information
1 parent
bc09c76
commit 276cfe0
Showing
14 changed files
with
439 additions
and
50 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -78,7 +78,7 @@ | |
posthog.disabled = True | ||
POSTHOG_PERSON = hash(SECRET_KEY) if SELF_HOSTED else None | ||
|
||
if DEBUG: | ||
if DEBUG or SELF_HOSTED: | ||
ALLOWED_HOSTS = ["*"] | ||
else: | ||
ALLOWED_HOSTS = [ | ||
|
@@ -105,7 +105,34 @@ | |
"drf_spectacular", | ||
"simple_history", | ||
"knox", | ||
"anymail", | ||
] | ||
|
||
ANYMAIL = { | ||
"MAILGUN_API_KEY": os.environ.get("MAILGUN_API_KEY"), | ||
"MAILGUN_DOMAIN": os.environ.get("MAILGUN_DOMAIN"), | ||
"MAILGUN_PUBLIC_KEY": os.environ.get("MAILGUN_PUBLIC_KEY"), | ||
"MAILGUN_SMTP_LOGIN": os.environ.get("MAILGUN_SMTP_LOGIN"), | ||
"MAILGUN_SMTP_PASSWORD": os.environ.get("MAILGUN_SMTP_PASSWORD"), | ||
"MAILGUN_SMTP_PORT": os.environ.get("MAILGUN_SMTP_PORT"), | ||
"MAILGUN_SMTP_SERVER": os.environ.get("MAILGUN_SMTP_SERVER"), | ||
} | ||
|
||
if DEBUG: | ||
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" | ||
APP_URL = "http://localhost:8000" | ||
elif SELF_HOSTED: | ||
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" | ||
APP_URL = "http://localhost" | ||
else: | ||
EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend" | ||
APP_URL = "https://app.uselotus.io" | ||
|
||
|
||
DEFAULT_FROM_EMAIL = "[email protected]" | ||
SECURITY_FROM_EMAIL = "[email protected]" | ||
SERVER_EMAIL = "[email protected]" # ditto (default from-email for Django errors) | ||
|
||
if PROFILER_ENABLED: | ||
INSTALLED_APPS.append("silk") | ||
|
||
|
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,90 @@ | ||
from django.conf import settings | ||
from django.contrib.auth import get_user_model | ||
from django.contrib.auth.password_validation import validate_password | ||
from django.contrib.auth.tokens import default_token_generator | ||
from django.core import exceptions as django_exceptions | ||
from django.core.mail import BadHeaderError, EmailMultiAlternatives | ||
from rest_framework.authtoken.models import Token | ||
|
||
|
||
class UserAlreadyExists(Exception): | ||
pass | ||
|
||
|
||
class UserService(object): | ||
def __init__(self, User): | ||
self.User = User | ||
|
||
def get(self, user_id=None, email=None): | ||
try: | ||
if user_id: | ||
return self.User.objects.get(id=user_id) | ||
if email: | ||
return self.User.objects.get(email=email.lower()) | ||
except User.DoesNotExist: | ||
return None | ||
|
||
def get_or_create_token(self, user): | ||
token, _ = Token.objects.get_or_create(user=user) | ||
return token | ||
|
||
def send_reset_password_email(self, reset_url, to): | ||
subject = "Reset Your Password" | ||
body = f"Use this link to reset your password: {reset_url}" | ||
from_email = f"Lotus <{settings.SECURITY_FROM_EMAIL}>" | ||
html = """ | ||
<p>Please <a href={url}>reset your password</a></p>""".format( | ||
url=reset_url | ||
) | ||
msg = EmailMultiAlternatives(subject, body, from_email, [to]) | ||
msg.attach_alternative(html, "text/html") | ||
msg.tags = ["reset_password"] | ||
msg.track_clicks = True | ||
try: | ||
msg.send() | ||
except BadHeaderError: | ||
print("Invalid header found.") | ||
return False | ||
|
||
return True | ||
|
||
def init_reset_password(self, email): | ||
"""Given an email, creates a token and emails the user a reset link.""" | ||
|
||
# For security reasons, we don't error if the user doesn't exist | ||
# since bad-actors cannot deduce which users exist | ||
user = None | ||
user = self.get(email=email) | ||
if not user: | ||
return False | ||
|
||
token = default_token_generator.make_token(user) | ||
path = "set-new-password?token=%s&userId=%s" % (token, user.id) | ||
password_reset_url = "%s/%s" % (settings.APP_URL, path) | ||
|
||
self.send_reset_password_email(reset_url=password_reset_url, to=email) | ||
|
||
return True | ||
|
||
def reset_password(self, user_id, raw_password, token): | ||
"""Given a valid token, update user's password.""" | ||
user = self.get(user_id=user_id) | ||
if not user: | ||
return False | ||
|
||
if not default_token_generator.check_token(user, token): | ||
print( | ||
{ | ||
"message": "User submitted invalid reset password token", | ||
"userId": user.id, | ||
} | ||
) | ||
return False | ||
|
||
user.set_password(raw_password) | ||
user.save() | ||
return user | ||
|
||
|
||
User = get_user_model() | ||
user_service = UserService(User=User) |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.