示例#1
0
    def authenticate(self, assertion=None, audience=None, browserid_extra=None, **kw):
        """``django.contrib.auth`` compatible authentication method.

        Given a BrowserID assertion and an audience, it attempts to
        verify them and then extract the email address for the authenticated
        user.

        An audience should be in the form ``https://example.com`` or
        ``http://localhost:8001``.

        See django_browserid.base.get_audience()
        """
        result = verify(assertion, audience, extra_params=browserid_extra)
        if not result:
            return None

        email = result['email']
        if not self.is_valid_email(email):
            return None

        # In the rare case that two user accounts have the same email address,
        # log and bail. Randomly selecting one seems really wrong.
        users = self.filter_users_by_email(email=email)
        if len(users) > 1:
            logger.warn('%s users with email address %s.', len(users), email)
            return None
        if len(users) == 1:
            return users[0]

        create_user = getattr(settings, 'BROWSERID_CREATE_USER', True)
        if not create_user:
            logger.debug('Login failed: No user with email %s found, and '
                         'BROWSERID_CREATE_USER is False', email)
            return None
        else:
            if create_user is True:
                create_function = self.create_user
            else:
                # Find the function to call.
                create_function = import_function_from_setting('BROWSERID_CREATE_USER')

            user = create_function(email)
            user_created.send(create_function, user=user)
            return user
示例#2
0
 def test_missing_attribute(self, import_module):
     """If the module is imported, but the function isn't found, raise ImproperlyConfigured."""
     import_module.return_value = Mock(spec=[])
     with self.assertRaises(ImproperlyConfigured):
         import_function_from_setting('TEST_SETTING')
示例#3
0
 def test_existing_attribute(self, import_module):
     """If the module is imported and has the requested function, return it."""
     module = Mock(spec=['baz'])
     import_module.return_value = module
     self.assertEqual(import_function_from_setting('TEST_SETTING'),
                      module.baz)
示例#4
0
 def test_invalid_import(self):
     """If the setting isn't a proper string, raise ImproperlyConfigured."""
     with self.assertRaises(ImproperlyConfigured):
         import_function_from_setting('TEST_SETTING')
示例#5
0
 def test_error_importing(self, import_module):
     """If there is an error importing the module, raise ImproperlyConfigured."""
     import_module.side_effect = ImportError
     with self.assertRaises(ImproperlyConfigured):
         import_function_from_setting('TEST_SETTING')
     import_module.assert_called_with('foo.bar')
示例#6
0
 def test_existing_attribute(self, import_module):
     """If the module is imported and has the requested function, return it."""
     module = Mock(spec=['baz'])
     import_module.return_value = module
     self.assertEqual(import_function_from_setting('TEST_SETTING'), module.baz)
示例#7
0
 def test_no_setting(self):
     """If the setting doesn't exist, raise ImproperlyConfigured."""
     with self.assertRaises(ImproperlyConfigured):
         import_function_from_setting('DOES_NOT_EXIST')
示例#8
0
 def test_missing_attribute(self, import_module):
     """If the module is imported, but the function isn't found, raise ImproperlyConfigured."""
     import_module.return_value = Mock(spec=[])
     with self.assertRaises(ImproperlyConfigured):
         import_function_from_setting('TEST_SETTING')
示例#9
0
 def test_error_importing(self, import_module):
     """If there is an error importing the module, raise ImproperlyConfigured."""
     import_module.side_effect = ImportError
     with self.assertRaises(ImproperlyConfigured):
         import_function_from_setting('TEST_SETTING')
     import_module.assert_called_with('foo.bar')
示例#10
0
 def test_invalid_import(self):
     """If the setting isn't a proper string, raise ImproperlyConfigured."""
     with self.assertRaises(ImproperlyConfigured):
         import_function_from_setting('TEST_SETTING')
示例#11
0
 def test_no_setting(self):
     """If the setting doesn't exist, raise ImproperlyConfigured."""
     with self.assertRaises(ImproperlyConfigured):
         import_function_from_setting('DOES_NOT_EXIST')
示例#12
0
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import logging

from django.conf import settings
try:
    from django.conf.urls import patterns, url
except ImportError:
    from django.conf.urls.defaults import patterns, url
from django.contrib.auth.views import logout
from django.core.exceptions import ImproperlyConfigured

from django_browserid.util import import_function_from_setting

logger = logging.getLogger(__name__)


try:
    Verify = import_function_from_setting('BROWSERID_VERIFY_CLASS')
except ImproperlyConfigured as e:
    logger.info('Loading BROWSERID_VERIFY_CLASS failed: {0}.\nFalling back to '
                'default.'.format(e))
    from django_browserid.views import Verify


urlpatterns = patterns('',
    url(r'^login/', Verify.as_view(), name='browserid_login'),
    url(r'^logout/', logout,
        {'next_page': getattr(settings, 'LOGOUT_REDIRECT_URL', '/')},
        name='browserid_logout')
)