This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Allows the user to login only via email without reseting the password #426
Open
ghost
wants to merge
4
commits into
liqd:develop
Choose a base branch
from
unknown repository
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,97 @@ | ||
""" Logs in user via email """ | ||
|
||
import re | ||
import adhocracy.model as model | ||
from adhocracy.lib.auth.authorization import has | ||
from paste.deploy.converters import asbool | ||
import pylons | ||
from repoze.who.interfaces import IAuthenticator, IIdentifier | ||
from webob.exc import HTTPFound | ||
from zope.interface import implements | ||
import time | ||
import hashlib | ||
import base64 | ||
from adhocracy.lib.session.session import get_secret | ||
|
||
|
||
def email_url(user, code): | ||
from adhocracy.lib.helpers import base_url | ||
return base_url("/emaillogin/%s/%s" % (user.user_name, code), | ||
absolute=True) | ||
|
||
|
||
def create_token(email, config): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should definitely use the new crypto functions. |
||
secret = get_secret(config) | ||
timestamp = str(int(time.time())) | ||
message = timestamp + "_" + create_hash(email, timestamp, config) | ||
return base64.urlsafe_b64encode(message.encode("utf-8")) | ||
|
||
|
||
def create_hash(email, time, config): | ||
secret = get_secret(config) | ||
value = secret + email + time | ||
return hashlib.sha256(value).hexdigest() | ||
|
||
|
||
def validate_token(user_token, email, config): | ||
try: | ||
decoded = base64.urlsafe_b64decode(user_token) | ||
user_time = decoded.split('_')[0] | ||
user_hash = decoded.split('_')[1] | ||
time_dif = int(time.time()) - int(user_time) | ||
except (TypeError, ValueError): | ||
return False | ||
correct_value = create_hash(email, user_time, config) | ||
if (user_hash == correct_value) and (time_dif < 3600): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of 3600, we should use a configuration option here, and default it to 3600. |
||
return True | ||
return False | ||
|
||
|
||
class EmailLoginRepozeWho(object): | ||
implements(IAuthenticator, IIdentifier) | ||
|
||
def __init__(self, config, rememberer_name, prefix='/emaillogin/'): | ||
self.config = config | ||
self.rememberer_name = rememberer_name | ||
self.url_rex = re.compile(r'^' + re.escape(prefix) + | ||
r'(?P<id>[^/]+)/(?P<code>[^/]+)$') | ||
|
||
def identify(self, environ): | ||
path_info = environ['PATH_INFO'] | ||
m = self.url_rex.match(path_info) | ||
if not m: | ||
return None | ||
u = model.User.find(m.group('id')) | ||
if not u: | ||
return None | ||
|
||
if not validate_token(m.group('code'), u.email, self.config): | ||
return None | ||
|
||
from adhocracy.lib.helpers import base_url | ||
root_url = base_url('/', instance=None, config=self.config) | ||
environ['repoze.who.application'] = HTTPFound(location=root_url) | ||
return { | ||
'repoze.who.plugins.emaillogin.userid': u.user_name, | ||
} | ||
|
||
def forget(self, environ, identity): | ||
rememberer = environ['repoze.who.plugins'][self.rememberer_name] | ||
return rememberer.forget(environ, identity) | ||
|
||
def remember(self, environ, identity): | ||
rememberer = environ['repoze.who.plugins'][self.rememberer_name] | ||
return rememberer.remember(environ, identity) | ||
|
||
def authenticate(self, environ, identity): | ||
userid = identity.get('repoze.who.plugins.emaillogin.userid') | ||
if userid is None: | ||
return None | ||
identity['repoze.who.userid'] = userid | ||
return userid | ||
|
||
|
||
def setup_auth(config, idenitifiers, authenticators): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should name the second variable |
||
email_rwho = EmailLoginRepozeWho(config, 'auth_tkt') | ||
idenitifiers.append(('emaillogin', email_rwho)) | ||
authenticators.append(('emaillogin', email_rwho)) |
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,17 @@ | ||
<%inherit file="/template.html" /> | ||
<%def name="title()">${_("Email login request sent")}</%def> | ||
|
||
<%block name="headline"> | ||
<h1 class="page_title">${_("Email login request sent")}</h1> | ||
</%block> | ||
|
||
<%block name="main_content"> | ||
<div class="sidebar"> | ||
| ||
</div> | ||
<div class="mainbar"> | ||
<div class="infobox"> | ||
${_("You will be sent an link to your email address. that, when opened will authenticate you automatically.")} | ||
</div> | ||
</div> | ||
</%block> |
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.
Instead of abandoning the
nopassword
target, it should simply call here if required.