当前位置: 首页>>代码示例>>Python>>正文


Python BrowserIDBackend.verify方法代码示例

本文整理汇总了Python中django_browserid.auth.BrowserIDBackend.verify方法的典型用法代码示例。如果您正苦于以下问题:Python BrowserIDBackend.verify方法的具体用法?Python BrowserIDBackend.verify怎么用?Python BrowserIDBackend.verify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django_browserid.auth.BrowserIDBackend的用法示例。


在下文中一共展示了BrowserIDBackend.verify方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: browserid_authenticate

# 需要导入模块: from django_browserid.auth import BrowserIDBackend [as 别名]
# 或者: from django_browserid.auth.BrowserIDBackend import verify [as 别名]
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)
开发者ID:pjajara,项目名称:zamboni,代码行数:37,代码来源:views.py

示例2: browserid_authenticate

# 需要导入模块: from django_browserid.auth import BrowserIDBackend [as 别名]
# 或者: from django_browserid.auth.BrowserIDBackend import verify [as 别名]
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)
开发者ID:caseybecking,项目名称:zamboni,代码行数:32,代码来源:views.py

示例3: browserid_authenticate

# 需要导入模块: from django_browserid.auth import BrowserIDBackend [as 别名]
# 或者: from django_browserid.auth.BrowserIDBackend import verify [as 别名]
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)
开发者ID:dbialer,项目名称:zamboni,代码行数:49,代码来源:views.py

示例4: browserid_verify

# 需要导入模块: from django_browserid.auth import BrowserIDBackend [as 别名]
# 或者: from django_browserid.auth.BrowserIDBackend import verify [as 别名]
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)
开发者ID:imromec,项目名称:kitsune,代码行数:43,代码来源:views.py

示例5: BrowserIDBackendTests

# 需要导入模块: from django_browserid.auth import BrowserIDBackend [as 别名]
# 或者: from django_browserid.auth.BrowserIDBackend import verify [as 别名]
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.
#.........这里部分代码省略.........
开发者ID:Azeez09,项目名称:remo,代码行数:103,代码来源:test_auth.py


注:本文中的django_browserid.auth.BrowserIDBackend.verify方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。