This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Send password reset from HS: Sending the email #5345
Merged
anoadragon453
merged 33 commits into
anoa/feature_hs_password_resets
from
anoa/hs_password_reset_sending_email
Jun 6, 2019
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
dbdebc2
Ability to send password reset emails
anoadragon453 9567c60
Merge branch 'develop' into anoa/hs_password_reset_sending_email
anoadragon453 ed35302
Fix validation token lifetime email_ prefix
anoadragon453 094c351
Add changelog
anoadragon453 899219c
Update manifest to include txt/html template files
anoadragon453 309943f
Update db
anoadragon453 354d749
mark jinja2 and bleach as required dependencies
anoadragon453 62e1ec0
Add email settings to default unit test config
anoadragon453 a0e2a10
Update unit test template dir
anoadragon453 a862f2a
gen sample config
anoadragon453 752dbee
Merge branch 'anoa/feature_hs_password_resets' into anoa/hs_password_…
anoadragon453 177f024
Add html5lib as a required dep
anoadragon453 6d2d3c9
Modify check for smtp settings to be kinder to CI
anoadragon453 6394715
silly linting rules
anoadragon453 fe0af29
Correct html5lib dep version number
anoadragon453 91eac88
one more time
anoadragon453 c9573ca
Change template_dir to originate from synapse root dir
anoadragon453 4c406f5
Revert "Modify check for smtp settings to be kinder to CI"
anoadragon453 70b161d
Move templates. New option to disable password resets
anoadragon453 79bc668
Update templates and make password reset option work
anoadragon453 f522cde
Change jinja2 and bleach back to opt deps
anoadragon453 a4c0907
Update email condition requirement
anoadragon453 efa1a56
Only import jinja2/bleach if we need it
anoadragon453 6a9588c
Update sample config
anoadragon453 78ca92a
Revert manifest changes for new res directory
anoadragon453 12ed769
Remove public_baseurl from unittest config
anoadragon453 6efb301
infer ability to reset password from email config
anoadragon453 3478213
Address review comments
anoadragon453 a37a2f1
regen sample config
anoadragon453 cd4f4a2
test for ci
anoadragon453 92090d3
Remove CI test
anoadragon453 7168dee
fix bug?
anoadragon453 828cdbb
Run bg update on the master process
anoadragon453 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Add ability to perform password reset via email without trusting the identity server. |
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 |
---|---|---|
|
@@ -1018,33 +1018,67 @@ password_config: | |
|
||
|
||
|
||
# Enable sending emails for notification events or expiry notices | ||
# Defining a custom URL for Riot is only needed if email notifications | ||
# should contain links to a self-hosted installation of Riot; when set | ||
# the "app_name" setting is ignored. | ||
# Enable sending emails for password resets, notification events or | ||
# account expiry notices. | ||
# | ||
# If your SMTP server requires authentication, the optional smtp_user & | ||
# smtp_pass variables should be used | ||
# | ||
#email: | ||
# enable_notifs: false | ||
# enable_notifs: False | ||
# smtp_host: "localhost" | ||
# smtp_port: 25 | ||
# smtp_port: 25 # SSL: 465, STARTTLS: 587 | ||
# smtp_user: "exampleusername" | ||
# smtp_pass: "examplepassword" | ||
# require_transport_security: False | ||
# notif_from: "Your Friendly %(app)s Home Server <[email protected]>" | ||
# app_name: Matrix | ||
# # if template_dir is unset, uses the example templates that are part of | ||
# # the Synapse distribution. | ||
# | ||
# # Enable email notifications by default | ||
# notif_for_new_users: True | ||
# | ||
# # Defining a custom URL for Riot is only needed if email notifications | ||
# # should contain links to a self-hosted installation of Riot; when set | ||
# # the "app_name" setting is ignored | ||
# riot_base_url: "http://localhost/riot" | ||
# | ||
# # Enable sending password reset emails via the configured, trusted | ||
# # identity servers | ||
# # | ||
# # IMPORTANT! This will give a malicious or overtaken identity server | ||
# # the ability to reset passwords for your users! Make absolutely sure | ||
# # that you want to do this! It is strongly recommended that password | ||
# # reset emails be sent by the homeserver instead | ||
# # | ||
# # If this option is set to false and SMTP options have not been | ||
# # configured, resetting user passwords via email will be disabled | ||
# #trust_identity_server_for_password_resets: false | ||
# | ||
# # Configure the time that a validation email or text message code | ||
# # will expire after sending | ||
# # | ||
# # This is currently used for password resets | ||
# #validation_token_lifetime: 1h | ||
# | ||
# # Template directory. All template files should be stored within this | ||
# # directory | ||
# # | ||
# #template_dir: res/templates | ||
# | ||
# # Templates for email notifications | ||
# # | ||
# notif_template_html: notif_mail.html | ||
# notif_template_text: notif_mail.txt | ||
# # Templates for account expiry notices. | ||
# | ||
# # Templates for account expiry notices | ||
# # | ||
# expiry_template_html: notice_expiry.html | ||
# expiry_template_text: notice_expiry.txt | ||
# notif_for_new_users: True | ||
# riot_base_url: "http://localhost/riot" | ||
# | ||
# # Templates for password reset emails sent by the homeserver | ||
# # | ||
# #password_reset_template_html: password_reset.html | ||
# #password_reset_template_text: password_reset.txt | ||
|
||
|
||
#password_providers: | ||
|
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 |
---|---|---|
|
@@ -50,6 +50,11 @@ def read_config(self, config): | |
else: | ||
self.email_app_name = "Matrix" | ||
|
||
# TODO: Rename notif_from to something more generic, or have a separate | ||
# from for password resets, message notifications, etc? | ||
# Currently the email section is a bit bogged down with settings for | ||
# multiple functions. Would be good to split it out into separate | ||
# sections and only put the common ones under email: | ||
self.email_notif_from = email_config.get("notif_from", None) | ||
if self.email_notif_from is not None: | ||
# make sure it's valid | ||
|
@@ -74,14 +79,76 @@ def read_config(self, config): | |
"account_validity", {}, | ||
).get("renew_at") | ||
|
||
if self.email_enable_notifs or account_validity_renewal_enabled: | ||
email_trust_identity_server_for_password_resets = email_config.get( | ||
"trust_identity_server_for_password_resets", False, | ||
) | ||
self.email_password_reset_behaviour = ( | ||
"remote" if email_trust_identity_server_for_password_resets else "local" | ||
) | ||
if self.email_password_reset_behaviour == "local" and email_config == {}: | ||
logger.warn( | ||
"User password resets have been disabled due to lack of email config" | ||
) | ||
self.email_password_reset_behaviour = "off" | ||
|
||
# Get lifetime of a validation token in milliseconds | ||
self.email_validation_token_lifetime = self.parse_duration( | ||
email_config.get("validation_token_lifetime", "1h") | ||
) | ||
|
||
if ( | ||
self.email_enable_notifs | ||
or account_validity_renewal_enabled | ||
or self.email_password_reset_behaviour == "local" | ||
): | ||
# make sure we can import the required deps | ||
import jinja2 | ||
import bleach | ||
# prevent unused warnings | ||
jinja2 | ||
bleach | ||
|
||
if self.email_password_reset_behaviour == "local": | ||
required = [ | ||
"smtp_host", | ||
"smtp_port", | ||
"notif_from", | ||
] | ||
|
||
missing = [] | ||
for k in required: | ||
if k not in email_config: | ||
missing.append(k) | ||
|
||
if (len(missing) > 0): | ||
raise RuntimeError( | ||
"email.password_reset_behaviour is set to 'local' " | ||
"but required keys are missing: %s" % | ||
(", ".join(["email." + k for k in missing]),) | ||
) | ||
|
||
# Templates for password reset emails | ||
self.email_password_reset_template_html = email_config.get( | ||
"password_reset_template_html", "password_reset.html", | ||
) | ||
self.email_password_reset_template_text = email_config.get( | ||
"password_reset_template_text", "password_reset.txt", | ||
) | ||
|
||
# Check templates exist | ||
for f in [self.email_password_reset_template_html, | ||
self.email_password_reset_template_text]: | ||
p = os.path.join(self.email_template_dir, f) | ||
if not os.path.isfile(p): | ||
raise ConfigError("Unable to find template file %s" % (p, )) | ||
|
||
if config.get("public_baseurl") is None: | ||
raise RuntimeError( | ||
"email.password_reset_behaviour is set to 'local' but no " | ||
"public_baseurl is set. This is necessary to generate password " | ||
"reset links" | ||
) | ||
|
||
if self.email_enable_notifs: | ||
required = [ | ||
"smtp_host", | ||
|
@@ -141,31 +208,65 @@ def read_config(self, config): | |
|
||
def default_config(self, config_dir_path, server_name, **kwargs): | ||
return """ | ||
# Enable sending emails for notification events or expiry notices | ||
# Defining a custom URL for Riot is only needed if email notifications | ||
# should contain links to a self-hosted installation of Riot; when set | ||
# the "app_name" setting is ignored. | ||
# Enable sending emails for password resets, notification events or | ||
# account expiry notices. | ||
# | ||
# If your SMTP server requires authentication, the optional smtp_user & | ||
# smtp_pass variables should be used | ||
# | ||
#email: | ||
# enable_notifs: false | ||
# enable_notifs: False | ||
# smtp_host: "localhost" | ||
# smtp_port: 25 | ||
# smtp_port: 25 # SSL: 465, STARTTLS: 587 | ||
# smtp_user: "exampleusername" | ||
# smtp_pass: "examplepassword" | ||
# require_transport_security: False | ||
# notif_from: "Your Friendly %(app)s Home Server <[email protected]>" | ||
# app_name: Matrix | ||
# # if template_dir is unset, uses the example templates that are part of | ||
# # the Synapse distribution. | ||
# | ||
# # Enable email notifications by default | ||
# notif_for_new_users: True | ||
# | ||
# # Defining a custom URL for Riot is only needed if email notifications | ||
# # should contain links to a self-hosted installation of Riot; when set | ||
# # the "app_name" setting is ignored | ||
# riot_base_url: "http://localhost/riot" | ||
# | ||
# # Enable sending password reset emails via the configured, trusted | ||
# # identity servers | ||
# # | ||
# # IMPORTANT! This will give a malicious or overtaken identity server | ||
# # the ability to reset passwords for your users! Make absolutely sure | ||
# # that you want to do this! It is strongly recommended that password | ||
# # reset emails be sent by the homeserver instead | ||
# # | ||
# # If this option is set to false and SMTP options have not been | ||
# # configured, resetting user passwords via email will be disabled | ||
# #trust_identity_server_for_password_resets: false | ||
# | ||
# # Configure the time that a validation email or text message code | ||
# # will expire after sending | ||
# # | ||
# # This is currently used for password resets | ||
# #validation_token_lifetime: 1h | ||
# | ||
# # Template directory. All template files should be stored within this | ||
# # directory | ||
# # | ||
# #template_dir: res/templates | ||
# | ||
# # Templates for email notifications | ||
# # | ||
# notif_template_html: notif_mail.html | ||
# notif_template_text: notif_mail.txt | ||
# # Templates for account expiry notices. | ||
# | ||
# # Templates for account expiry notices | ||
# # | ||
# expiry_template_html: notice_expiry.html | ||
# expiry_template_text: notice_expiry.txt | ||
# notif_for_new_users: True | ||
# riot_base_url: "http://localhost/riot" | ||
# | ||
# # Templates for password reset emails sent by the homeserver | ||
# # | ||
# #password_reset_template_html: password_reset.html | ||
# #password_reset_template_text: password_reset.txt | ||
""" |
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
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.
We should probably call this something else....
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.
Yes, but, backwards compatibility.