本文整理汇总了Python中django_browserid.auth.BrowserIDBackend类的典型用法代码示例。如果您正苦于以下问题:Python BrowserIDBackend类的具体用法?Python BrowserIDBackend怎么用?Python BrowserIDBackend使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BrowserIDBackend类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: browserid_authenticate
def browserid_authenticate(request, assertion):
"""
Verify a BrowserID login attempt. If the BrowserID assertion is
good, but no account exists on AMO, create one.
Request is only needed for logging. :(
"""
backend = BrowserIDBackend()
result = backend.verify(assertion, settings.SITE_URL)
if not result:
return (None, "BrowserID authentication failure.")
email = result['email']
users = UserProfile.objects.filter(email=email)
if len(users) == 1:
users[0].user.backend = 'django_browserid.auth.BrowserIDBackend'
return (users[0], None)
username = autocreate_username(email.partition('@')[0])
if (settings.REGISTER_USER_LIMIT and
UserProfile.objects.count() > settings.REGISTER_USER_LIMIT
and not can_override_reg_limit(request)):
_m = ('Sorry, no more registrations are allowed. '
'<a href="https://developer.mozilla.org/en-US/apps">'
'Learn more</a>')
return (None, _m)
profile = UserProfile.objects.create(username=username, email=email)
profile.create_django_user()
profile.user.backend = 'django_browserid.auth.BrowserIDBackend'
if settings.APP_PREVIEW:
profile.notes = '__market__'
profile.user.save()
profile.save()
log_cef('New Account', 5, request, username=username,
signature='AUTHNOTICE',
msg='User created a new account (from BrowserID)')
return (profile, None)
示例2: test_get_user
def test_get_user(self):
"""
Check if user returned by BrowserIDBackend.get_user is correct.
"""
user = new_user('[email protected]')
backend = BrowserIDBackend()
self.assertEqual(backend.get_user(user.pk), user)
示例3: test_get_user
def test_get_user(self):
# If a user is retrieved by the BrowserIDBackend, it should have
# 'django_browserid.auth.BrowserIDBackend' for the backend attribute.
user = new_user('[email protected]')
backend = BrowserIDBackend()
self.assertEqual(backend.get_user(user.id).backend,
'django_browserid.auth.BrowserIDBackend')
示例4: browserid_authenticate
def browserid_authenticate(request, assertion):
"""
Verify a BrowserID login attempt. If the BrowserID assertion is
good, but no account exists on AMO, create one.
Request is only needed for logging. :(
"""
backend = BrowserIDBackend()
result = backend.verify(assertion, settings.SITE_URL)
if not result:
return (None, "BrowserID authentication failure.")
email = result['email']
users = UserProfile.objects.filter(email=email)
if len(users) == 1:
users[0].user.backend = 'django_browserid.auth.BrowserIDBackend'
return (users[0], None)
username = autocreate_username(email.partition('@')[0])
source = (amo.LOGIN_SOURCE_MMO_BROWSERID if settings.MARKETPLACE else
amo.LOGIN_SOURCE_AMO_BROWSERID)
profile = UserProfile.objects.create(username=username, email=email,
source=source, display_name=username)
profile.create_django_user()
profile.user.backend = 'django_browserid.auth.BrowserIDBackend'
profile.user.save()
profile.save()
log_cef('New Account', 5, request, username=username,
signature='AUTHNOTICE',
msg='User created a new account (from BrowserID)')
return (profile, None)
示例5: test_verify_called_with_extra_kwargs
def test_verify_called_with_extra_kwargs(self):
backend = BrowserIDBackend()
verifier = MockVerifier('[email protected]')
verifier.verify = Mock(wraps=verifier.verify)
backend.get_verifier = lambda: verifier
backend.authenticate(assertion='asdf', audience='http://testserver', foo='bar')
verifier.verify.assert_called_with('asdf', 'http://testserver', foo='bar')
示例6: auth
def auth(self, verified_email=None, **kwargs):
"""
Attempt to authenticate a user with BrowserIDBackend.
If verified_email is None, verification will fail, otherwise it will
pass and return the specified email.
"""
with mock_browserid(verified_email):
backend = BrowserIDBackend()
return backend.authenticate(assertion='asdf', audience='asdf', **kwargs)
示例7: test_authenticate_verify_exception
def test_authenticate_verify_exception(self):
"""
If the verifier raises an exception, log it as a warning and
return None.
"""
backend = BrowserIDBackend()
verifier = Mock()
exception = Exception()
backend.get_verifier = lambda: verifier
verifier.verify.side_effect = exception
with patch('django_browserid.auth.logger') as logger:
self.assertEqual(backend.authenticate('asdf', 'asdf'), None)
logger.warn.assert_called_with(exception)
示例8: browserid_authenticate
def browserid_authenticate(request, assertion):
"""
Verify a BrowserID login attempt. If the BrowserID assertion is
good, but no account exists on AMO, create one.
Request is only needed for logging. :(
"""
backend = BrowserIDBackend()
result = backend.verify(assertion, settings.SITE_URL)
if not result:
return (None, "BrowserID authentication failure.")
email = result["email"]
users = UserProfile.objects.filter(email=email)
if len(users) == 1:
users[0].user.backend = "django_browserid.auth.BrowserIDBackend"
return (users[0], None)
username = autocreate_username(email.partition("@")[0])
if (
settings.REGISTER_USER_LIMIT
and UserProfile.objects.count() > settings.REGISTER_USER_LIMIT
and not can_override_reg_limit(request)
):
_m = (
"Sorry, no more registrations are allowed. "
'<a href="https://developer.mozilla.org/en-US/apps">'
"Learn more</a>"
)
return (None, _m)
profile = UserProfile.objects.create(
username=username, email=email, source=amo.LOGIN_SOURCE_BROWSERID, display_name=username
)
profile.create_django_user()
profile.user.backend = "django_browserid.auth.BrowserIDBackend"
if settings.APP_PREVIEW:
profile.notes = "__market__"
profile.user.save()
profile.save()
log_cef(
"New Account",
5,
request,
username=username,
signature="AUTHNOTICE",
msg="User created a new account (from BrowserID)",
)
return (profile, None)
示例9: test_create_user_integrity_error
def test_create_user_integrity_error(self, logger):
# If an IntegrityError is raised during user creation, attempt to re-fetch the user in case
# the user was created since we checked for the existing account.
backend = BrowserIDBackend()
backend.User = Mock()
error = IntegrityError()
backend.User.objects.create_user.side_effect = error
backend.User.objects.get.return_value = 'asdf'
self.assertEqual(backend.create_user('[email protected]'), 'asdf')
# If get raises a DoesNotExist exception, re-raise the original exception.
backend.User.DoesNotExist = Exception
backend.User.objects.get.side_effect = backend.User.DoesNotExist
with self.assertRaises(IntegrityError) as e:
backend.create_user('[email protected]')
self.assertEqual(e.exception, error)
示例10: browserid_verify
def browserid_verify(request):
next = request.REQUEST.get('next')
redirect_to = next or getattr(settings, 'LOGIN_REDIRECT_URL', '/')
redirect_to_failure = getattr(settings, 'LOGIN_REDIRECT_URL_FAILURE', '/')
form = BrowserIDForm(data=request.POST)
if form.is_valid():
verifier = BrowserIDBackend().get_verifier()
result = verifier.verify(form.cleaned_data['assertion'],
get_audience(request))
if result:
if (request.user.is_authenticated()
and request.user.email != result.email):
# User is already signed and wants to change their email.
request.user.email = result.email
request.user.save()
return redirect(reverse('users.edit_profile'))
else:
# Verified so log in
email = result.email
user = User.objects.filter(email=email)
contributor = 'contributor' in request.POST
if len(user) == 0:
# Add the email to the session and redirect to signup
request.session['browserid-email'] = email
signup_url = reverse('users.browserid_signup')
return redirect('%s?%s' % (signup_url,
urlencode({'next': next})))
else:
user = user[0]
user.backend = 'django_browserid.auth.BrowserIDBackend'
if contributor:
add_to_contributors(request, user)
auth.login(request, user)
return redirect(redirect_to)
return redirect(redirect_to_failure)
示例11: _auth
def _auth(self, backend=None, verified_email=None):
if backend is None:
backend = BrowserIDBackend()
with mock_browserid(verified_email):
return backend.authenticate(assertion='asdf', audience='asdf')
示例12: setUp
def setUp(self):
self.backend = BrowserIDBackend()
self.verifier = Mock()
self.backend.get_verifier = lambda: self.verifier
示例13: BrowserIDBackendTests
class BrowserIDBackendTests(TestCase):
def setUp(self):
self.backend = BrowserIDBackend()
self.verifier = Mock()
self.backend.get_verifier = lambda: self.verifier
def test_verify_failure(self):
"""If verification fails, return None."""
self.verifier.verify.return_value = False
self.assertEqual(self.backend.verify('asdf', 'qwer'), None)
self.verifier.verify.assert_called_with('asdf', 'qwer')
def test_verify_success(self):
"""
If verification succeeds, return the email address from the
verification result.
"""
self.verifier.verify.return_value = Mock(email='[email protected]')
self.assertEqual(self.backend.verify('asdf', 'qwer'), '[email protected]')
self.verifier.verify.assert_called_with('asdf', 'qwer')
def test_verify_no_audience_request(self):
"""
If no audience is provided but a request is, retrieve the
audience from the request using get_audience.
"""
request = Mock()
with patch('django_browserid.auth.get_audience') as get_audience:
self.backend.verify('asdf', request=request)
get_audience.assert_called_with(request)
self.verifier.verify.assert_called_with('asdf', get_audience.return_value)
def test_verify_no_audience_no_assertion_no_service(self):
"""
If the assertion isn't provided, or the audience and request
aren't provided, return None.
"""
self.assertEqual(self.backend.verify(audience='asdf'), None)
self.assertEqual(self.backend.verify(assertion='asdf'), None)
with patch('django_browserid.auth.get_audience') as get_audience:
get_audience.return_value = None
self.assertEqual(self.backend.verify('asdf', request=Mock()), None)
def test_verify_kwargs(self):
"""Any extra kwargs should be passed to the verifier."""
self.backend.verify('asdf', 'asdf', request='blah', foo='bar', baz=1)
self.verifier.verify.assert_called_with('asdf', 'asdf', foo='bar', baz=1)
def auth(self, verified_email=None, **kwargs):
"""
Attempt to authenticate a user with BrowserIDBackend.
If verified_email is None, verification will fail, otherwise it will
pass and return the specified email.
"""
self.backend.verify = Mock(return_value=verified_email)
return self.backend.authenticate(assertion='asdf', audience='asdf', **kwargs)
def test_duplicate_emails(self):
"""
If there are two users with the same email address, return None.
"""
new_user('[email protected]', 'test1')
new_user('[email protected]', 'test2')
self.assertTrue(self.auth('[email protected]') is None)
def test_auth_success(self):
"""
If a single user is found with the verified email, return an
instance of their user object.
"""
user = new_user('[email protected]')
self.assertEqual(self.auth('[email protected]'), user)
@patch.object(settings, 'BROWSERID_CREATE_USER', False)
def test_no_create_user(self):
"""
If user creation is disabled and no user is found, return None.
"""
self.assertTrue(self.auth('[email protected]') is None)
@patch.object(settings, 'BROWSERID_CREATE_USER', True)
def test_create_user(self):
"""
If user creation is enabled and no user is found, return a new
User.
"""
user = self.auth('[email protected]')
self.assertTrue(user is not None)
self.assertTrue(isinstance(user, User))
self.assertEqual(user.email, '[email protected]')
@patch.object(settings, 'BROWSERID_CREATE_USER',
'django_browserid.tests.test_auth.new_user')
@patch('django_browserid.tests.test_auth.new_user')
def test_custom_create_user(self, create_user):
"""
If user creation is enabled with a custom create function and no
user is found, return the new user created with the custom
function.
#.........这里部分代码省略.........