Skip to content
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

Add email as an option to be asked on sign up #41

Merged
merged 6 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,14 @@ open signup, where all users that do sign up can already log in the system. To d
.. code-block:: python

c.Authenticator.open_signup = True


Ask for extra information on SignUp
-----------------------------------

Native Authenticator is based on username and password only. But if you need extra information about the users, you can add them on the sign up. For now, you can ask for email by adding the following line:


.. code-block:: python

c.Authenticator.ask_email_on_signup = True
13 changes: 10 additions & 3 deletions nativeauthenticator/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ class SignUpHandler(LocalBase):
"""Render the sign in page."""
async def get(self):
self._register_template_path()
html = self.render_template('signup.html')
html = self.render_template(
'signup.html',
ask_email=self.authenticator.ask_email_on_signup,
)
self.finish(html)

def get_result_message(self, user):
Expand All @@ -57,6 +60,7 @@ async def post(self):

html = self.render_template(
'signup.html',
ask_email=self.authenticator.ask_email_on_signup,
result_message=message,
alert=alert
)
Expand All @@ -68,8 +72,11 @@ class AuthorizationHandler(LocalBase):
@admin_only
async def get(self):
self._register_template_path()
html = self.render_template('autorization-area.html',
users=self.db.query(UserInfo).all())
html = self.render_template(
'autorization-area.html',
ask_email=self.authenticator.ask_email_on_signup,
users=self.db.query(UserInfo).all(),
)
self.finish(html)


Expand Down
8 changes: 7 additions & 1 deletion nativeauthenticator/nativeauthenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class NativeAuthenticator(Authenticator):
help=("Allows every user that made sign up to automatically log in "
"the system without needing admin authorization")
)
ask_email_on_signup = Bool(
config=True,
default=False,
help="Asks for email on signup"
)

def __init__(self, add_new_table=True, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -128,7 +133,7 @@ def is_password_strong(self, password):

return all(checks)

def get_or_create_user(self, username, pw):
def get_or_create_user(self, username, pw, **kwargs):
user = UserInfo.find(self.db, username)
if user:
return user
Expand All @@ -138,6 +143,7 @@ def get_or_create_user(self, username, pw):

encoded_pw = bcrypt.hashpw(pw.encode(), bcrypt.gensalt())
infos = {'username': username, 'password': encoded_pw}
infos.update(kwargs)
if username in self.admin_users or self.open_signup:
infos.update({'is_authorized': True})

Expand Down
13 changes: 10 additions & 3 deletions nativeauthenticator/orm.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import bcrypt
import re
from jupyterhub.orm import Base

from sqlalchemy import (
Boolean, Column, Integer, String
)
from sqlalchemy import Boolean, Column, Integer, String
from sqlalchemy.orm import validates


class UserInfo(Base):
Expand All @@ -12,6 +12,7 @@ class UserInfo(Base):
username = Column(String, nullable=False)
password = Column(String, nullable=False)
is_authorized = Column(Boolean, default=False)
email = Column(String)

@classmethod
def find(cls, db, username):
Expand All @@ -31,3 +32,9 @@ def change_authorization(cls, db, username):
user.is_authorized = not user.is_authorized
db.commit()
return user

@validates('email')
def validate_email(self, key, address):
assert re.match(r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$",
address)
return address
9 changes: 9 additions & 0 deletions nativeauthenticator/templates/autorization-area.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ <h1>Authorization Area</h1>
<thead>
<tr>
<th>Username</th>
{% if ask_email %}
<th>Email</th>
{% endif %}
<th>Is Authorized?</th>
</tr>
</thead>
Expand All @@ -18,6 +21,9 @@ <h1>Authorization Area</h1>
{% if user.is_authorized %}
<tr class="success">
<td>{{ user.username }}</td>
{% if ask_email %}
<td>{{ user.email }}</td>
{% endif %}
<td>Yes</td>
<td>
<a class="btn btn-default"
Expand All @@ -27,6 +33,9 @@ <h1>Authorization Area</h1>
{% else %}
<tr>
<td>{{ user.username }}</td>
{% if ask_email %}
<td>{{ user.email }}</td>
{% endif %}
<td>No</td>
<td>
<a class="btn btn-jupyter"
Expand Down
13 changes: 13 additions & 0 deletions nativeauthenticator/templates/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ <h2>
name="username"
id="username_input"
tabindex="2"
required
/>
{% if ask_email %}
<label>Email:</label>
<input
type=""
class="form-control"
name="email"
id="email_input"
tabindex="2"
required
/>
{% endif %}
<label for='password_input'>Password:</label>
<div class="input-group">
<input
Expand All @@ -40,6 +52,7 @@ <h2>
name="password"
id="pwd"
tabindex="2"
required
/>
<span class="input-group-addon">
<button style="border:0;" type="button" id="eye">
Expand Down
28 changes: 28 additions & 0 deletions nativeauthenticator/tests/test_orm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest
from jupyterhub.tests.mocking import MockHub

from ..orm import UserInfo


@pytest.fixture
def tmpcwd(tmpdir):
tmpdir.chdir()


@pytest.fixture
def app():
hub = MockHub()
hub.init_db()
return hub


@pytest.mark.parametrize('email', ['john', 'john@john'])
def test_validate_method_wrong_email(email, tmpdir, app):
with pytest.raises(AssertionError):
UserInfo(username='john', password='pwd', email=email)


def test_validate_method_correct_email(tmpdir, app):
user = UserInfo(username='john', password='pwd', email='john@john.com')
app.db.add(user)
assert UserInfo.find(app.db, 'john')