-
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.
Merge pull request #79 from wri/odp-159
[ODP-159] Authentication
- Loading branch information
Showing
26 changed files
with
1,842 additions
and
1,160 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
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
31 changes: 31 additions & 0 deletions
31
ckan-backend-dev/src/ckanext-wri/ckanext/wri/lib/mailer.py
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,31 @@ | ||
from ckan.lib.mailer import create_reset_key, mail_user, MailerException | ||
from ckan.lib.base import render | ||
from ckan.common import config | ||
import ckan.model as model | ||
|
||
def get_reset_link(user: model.User) -> str: | ||
odp_url = config.get('ckanext.wri.odp_url') | ||
return "{}/auth/password-reset?token={}&user_id={}".format(odp_url, user.reset_key, user.id) | ||
|
||
def get_reset_link_body(user: model.User) -> str: | ||
extra_vars = { | ||
'reset_link': get_reset_link(user), | ||
'site_title': "WRI Open Data Portal", | ||
'site_url': config.get('ckanext.wri.odp_url'), | ||
'user_name': user.name, | ||
} | ||
# NOTE: This template is translated | ||
return render('emails/reset_password.txt', extra_vars) | ||
|
||
def send_reset_link(user: model.User) -> None: | ||
create_reset_key(user) | ||
body = get_reset_link_body(user) | ||
extra_vars = { | ||
'site_title': config.get('ckan.site_title') | ||
} | ||
subject = render('emails/reset_password_subject.txt', extra_vars) | ||
|
||
# Make sure we only use the first line | ||
subject = subject.split('\n')[0] | ||
|
||
mail_user(user, subject, body, body) |
35 changes: 35 additions & 0 deletions
35
ckan-backend-dev/src/ckanext-wri/ckanext/wri/logic/action.py
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,35 @@ | ||
import ckan.plugins.toolkit as tk | ||
import ckan.logic as logic | ||
import ckanext.wri.lib.mailer as mailer | ||
from ckan.types import Context | ||
from ckan.common import _ | ||
|
||
|
||
ValidationError = logic.ValidationError | ||
|
||
import logging | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
def password_reset(context: Context, data_dict: [str, any]): | ||
email = data_dict.get("email", False) | ||
|
||
if not email: | ||
raise ValidationError({"email": [_("Please provide an email address")]}) | ||
model = context['model'] | ||
session = context['session'] | ||
|
||
user = session.query(model.User).filter_by(email=email).all() | ||
|
||
if not user: | ||
# Do not leak whether the email is registered or not | ||
return "Password reset link sent to email address" | ||
|
||
try: | ||
mailer.send_reset_link(user[0]) | ||
return "Password reset link sent to email address" | ||
except mailer.MailerException as e: | ||
log.exception(e) | ||
return "Password reset link sent to email address" | ||
|
||
|
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
23 changes: 23 additions & 0 deletions
23
ckan-backend-dev/src/ckanext-wri/ckanext/wri/templates/emails/reset_password.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{% trans %} | ||
<p> | ||
Dear {{ user_name }}, | ||
<br /> | ||
<br /> | ||
You have requested your password on {{ site_title }} to be reset. | ||
<br /> | ||
<br /> | ||
Please click the following link to confirm this request: | ||
<br /> | ||
<br /> | ||
<a target="_blank" href="{{ reset_link }}">Reset password</a> | ||
<br /> | ||
<br /> | ||
Have a nice day. | ||
<br /> | ||
<br /> | ||
-- | ||
Message sent by {{ site_title }} ({{ site_url }}) | ||
</p> | ||
{% endtrans %} | ||
|
||
|
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.