Skip to content

Commit

Permalink
Accept an alternate username generation algo in settings for user cre…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
lmorchard committed Oct 10, 2011
1 parent 3405b0c commit 0fa2d89
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
18 changes: 12 additions & 6 deletions django_browserid/auth.py
Expand Up @@ -21,6 +21,15 @@
OKAY_RESPONSE = 'okay'


def default_username_algo(email):
# store the username as a base64 encoded sha1 of the email address
# this protects against data leakage because usernames are often
# treated as public identifiers (so we can't use the email address).
username = base64.urlsafe_b64encode(
hashlib.sha1(email).digest()).rstrip('=')
return username


class BrowserIDBackend(object):
supports_anonymous_user = False
supports_object_permissions = False
Expand Down Expand Up @@ -66,12 +75,9 @@ def authenticate(self, assertion=None, host=None, port=None):
create_user = getattr(settings, 'BROWSERID_CREATE_USER', False)
if not create_user:
return None
# store the username as a base64 encoded sha1 of the email address
# this protects against data leakage because usernames are often
# treated as public identifiers (so we can't use the email address).
username = base64.urlsafe_b64encode(
hashlib.sha1(email).digest()).rstrip('=')
user = User.objects.create_user(username, email)
username_algo = getattr(settings, 'BROWSERID_USERNAME_ALGO',
default_username_algo)
user = User.objects.create_user(username_algo(email), email)
user.is_active = True
user.save()
return user
Expand Down
18 changes: 18 additions & 0 deletions django_browserid/tests/test_verification.py
Expand Up @@ -85,6 +85,7 @@ def test_authenticate_create_user(fake):
"""Test that automatic user creation works when enabled."""
with positive_assertion(fake):
setattr(settings, 'BROWSERID_CREATE_USER', True)
delattr(settings, 'BROWSERID_USERNAME_ALGO')
user = auth.authenticate(**authenticate_kwargs)
# user should have been created
assert user
Expand All @@ -93,6 +94,23 @@ def test_authenticate_create_user(fake):
hashlib.sha1(user.email).digest()).rstrip('=')


@fudge.patch('django_browserid.auth.BrowserIDBackend._verify_http_request')
def test_authenticate_create_user_with_alternate_username_algo(fake):
"""Test that automatic user creation with an alternate username algo
works."""

def username_algo(email):
return email.split('@')[0]

with positive_assertion(fake, email=u'myemail@example.org'):
setattr(settings, 'BROWSERID_CREATE_USER', True)
setattr(settings, 'BROWSERID_USERNAME_ALGO', username_algo)
user = auth.authenticate(**authenticate_kwargs)
assert user
assert user.email == 'myemail@example.org'
assert user.username == 'myemail'


@fudge.patch('django_browserid.auth.BrowserIDBackend._verify_http_request')
def test_authenticate_missing_user(fake):
"""Test that authenticate() returns None when user creation disabled."""
Expand Down

0 comments on commit 0fa2d89

Please sign in to comment.