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


Python OAuthAuthentication.is_authenticated方法代码示例

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


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

示例1: test_is_authenticated

# 需要导入模块: from tastypie.authentication import OAuthAuthentication [as 别名]
# 或者: from tastypie.authentication.OAuthAuthentication import is_authenticated [as 别名]
    def test_is_authenticated(self):
        auth = OAuthAuthentication()

        # Invalid request.
        resp = auth.is_authenticated(self.request)
        self.assertEqual(resp.status_code, 401)

        # No username/api_key details should fail.
        self.request.REQUEST = self.request.GET = {
            'oauth_consumer_key': '123',
            'oauth_nonce': 'abc',
            'oauth_signature': '&',
            'oauth_signature_method': 'PLAINTEXT',
            'oauth_timestamp': str(int(time.time())),
            'oauth_token': 'foo',
        }
        self.request.META['Authorization'] = 'OAuth ' + ','.join([key+'='+value for key, value in self.request.REQUEST.items()])
        resp = auth.is_authenticated(self.request)
        self.assertEqual(resp, True)
        self.assertEqual(self.request.user.pk, self.user.pk)
开发者ID:mthornhill,项目名称:django-tastypie,代码行数:22,代码来源:authentication.py

示例2: test_is_authenticated

# 需要导入模块: from tastypie.authentication import OAuthAuthentication [as 别名]
# 或者: from tastypie.authentication.OAuthAuthentication import is_authenticated [as 别名]
    def test_is_authenticated(self):
        from oauth_provider.models import Consumer, Token, Resource
        auth = OAuthAuthentication()
        request = HttpRequest()
        request.META['SERVER_NAME'] = 'testsuite'
        request.META['SERVER_PORT'] = '8080'
        request.REQUEST = request.GET = {}
        request.method = "GET"

        # Invalid request.
        resp = auth.is_authenticated(request)
        self.assertEqual(resp.status_code, 401)

        # No username/api_key details should fail.
        request.REQUEST = request.GET = {
            'oauth_consumer_key': '123',
            'oauth_nonce': 'abc',
            'oauth_signature': '&',
            'oauth_signature_method': 'PLAINTEXT',
            'oauth_timestamp': str(int(time.time())),
            'oauth_token': 'foo',
        }
        user = User.objects.create_user('daniel', '[email protected]', 'password')
        request.META['Authorization'] = 'OAuth ' + ','.join([key+'='+value for key, value in request.REQUEST.items()])
        resource, _ = Resource.objects.get_or_create(url='test', defaults={
            'name': 'Test Resource'
        })
        consumer, _ = Consumer.objects.get_or_create(key='123', defaults={
            'name': 'Test',
            'description': 'Testing...'
        })
        token, _ = Token.objects.get_or_create(key='foo', token_type=Token.ACCESS, defaults={
            'consumer': consumer,
            'resource': resource,
            'secret': '',
            'user': user,
        })
        resp = auth.is_authenticated(request)
        self.assertEqual(resp, True)
        self.assertEqual(request.user.pk, user.pk)
开发者ID:Unholster,项目名称:django-tastypie,代码行数:42,代码来源:authentication.py

示例3: test_is_authenticated

# 需要导入模块: from tastypie.authentication import OAuthAuthentication [as 别名]
# 或者: from tastypie.authentication.OAuthAuthentication import is_authenticated [as 别名]
    def test_is_authenticated(self):
        auth = OAuthAuthentication()

        # Invalid request.
        resp = auth.is_authenticated(self.request)
        self.assertEqual(resp.status_code, 401)

        # No username/api_key details should fail.
        self.request.REQUEST = self.request.GET = {
            "oauth_consumer_key": "123",
            "oauth_nonce": "abc",
            "oauth_signature": "&",
            "oauth_signature_method": "PLAINTEXT",
            "oauth_timestamp": str(int(time.time())),
            "oauth_token": "foo",
        }
        self.request.META["Authorization"] = "OAuth " + ",".join(
            [key + "=" + value for key, value in self.request.REQUEST.items()]
        )
        resp = auth.is_authenticated(self.request)
        self.assertEqual(resp, True)
        self.assertEqual(self.request.user.pk, self.user.pk)
开发者ID:mattbriancon,项目名称:django-tastypie,代码行数:24,代码来源:authentication.py

示例4: test_whitelisting

# 需要导入模块: from tastypie.authentication import OAuthAuthentication [as 别名]
# 或者: from tastypie.authentication.OAuthAuthentication import is_authenticated [as 别名]
    def test_whitelisting(self):
        auth = OAuthAuthentication(whitelisted_methods=['a_method'])

        # Calling with a whitelisted method_name without credentials should work
        self.assertEqual(auth.is_authenticated(self.request, method_name='a_method'), True)

        # Calling any other method should require auth
        resp = auth.is_authenticated(self.request, method_name='another_method')
        self.assertEqual(resp.status_code, 401)

        # No username/api_key details should fail.
        self.request.REQUEST = self.request.GET = {
            'oauth_consumer_key': '123',
            'oauth_nonce': 'abc',
            'oauth_signature': '&',
            'oauth_signature_method': 'PLAINTEXT',
            'oauth_timestamp': str(int(time.time())),
            'oauth_token': 'foo',
        }
        self.request.META['Authorization'] = 'OAuth ' + ','.join([key+'='+value for key, value in self.request.REQUEST.items()])
        self.assertEqual(auth.is_authenticated(self.request, method_name='a_method'), True)
        self.assertEqual(auth.is_authenticated(self.request, method_name='another_method'), True)
开发者ID:YACFirm,项目名称:django-tastypie,代码行数:24,代码来源:authentication.py

示例5: test_is_authenticated

# 需要导入模块: from tastypie.authentication import OAuthAuthentication [as 别名]
# 或者: from tastypie.authentication.OAuthAuthentication import is_authenticated [as 别名]
    def test_is_authenticated(self):
        from oauth_provider.models import Consumer, Token, Resource

        auth = OAuthAuthentication()
        request = HttpRequest()
        request.META["SERVER_NAME"] = "testsuite"
        request.META["SERVER_PORT"] = "8080"
        request.REQUEST = request.GET = {}
        request.method = "GET"

        # Invalid request.
        resp = auth.is_authenticated(request)
        self.assertEqual(resp.status_code, 401)

        # No username/api_key details should fail.
        request.REQUEST = request.GET = {
            "oauth_consumer_key": "123",
            "oauth_nonce": "abc",
            "oauth_signature": "&",
            "oauth_signature_method": "PLAINTEXT",
            "oauth_timestamp": str(int(time.time())),
            "oauth_token": "foo",
        }
        user = User.objects.create_user("daniel", "[email protected]", "password")
        request.META["Authorization"] = "OAuth " + ",".join(
            [key + "=" + value for key, value in request.REQUEST.items()]
        )
        resource, _ = Resource.objects.get_or_create(url="test", defaults={"name": "Test Resource"})
        consumer, _ = Consumer.objects.get_or_create(key="123", defaults={"name": "Test", "description": "Testing..."})
        token, _ = Token.objects.get_or_create(
            key="foo",
            token_type=Token.ACCESS,
            defaults={"consumer": consumer, "resource": resource, "secret": "", "user": user},
        )
        resp = auth.is_authenticated(request)
        self.assertEqual(resp, True)
        self.assertEqual(request.user.pk, user.pk)
开发者ID:coffindragger,项目名称:django-tastypie,代码行数:39,代码来源:authentication.py

示例6: test_check_active_true

# 需要导入模块: from tastypie.authentication import OAuthAuthentication [as 别名]
# 或者: from tastypie.authentication.OAuthAuthentication import is_authenticated [as 别名]
    def test_check_active_true(self):
        auth = OAuthAuthentication()

        # No username/api_key details should fail.
        self.request.REQUEST = self.request.GET = {
            'oauth_consumer_key': '123',
            'oauth_nonce': 'abc',
            'oauth_signature': '&',
            'oauth_signature_method': 'PLAINTEXT',
            'oauth_timestamp': str(int(time.time())),
            'oauth_token': 'bar',
        }
        self.request.META['Authorization'] = 'OAuth ' + ','.join([key+'='+value for key, value in self.request.REQUEST.items()])
        resp = auth.is_authenticated(self.request)
        self.assertFalse(resp)
开发者ID:mthornhill,项目名称:django-tastypie,代码行数:17,代码来源:authentication.py

示例7: test_check_active_true

# 需要导入模块: from tastypie.authentication import OAuthAuthentication [as 别名]
# 或者: from tastypie.authentication.OAuthAuthentication import is_authenticated [as 别名]
    def test_check_active_true(self):
        auth = OAuthAuthentication()

        # No username/api_key details should fail.
        self.request.REQUEST = self.request.GET = {
            "oauth_consumer_key": "123",
            "oauth_nonce": "abc",
            "oauth_signature": "&",
            "oauth_signature_method": "PLAINTEXT",
            "oauth_timestamp": str(int(time.time())),
            "oauth_token": "bar",
        }
        self.request.META["Authorization"] = "OAuth " + ",".join(
            [key + "=" + value for key, value in self.request.REQUEST.items()]
        )
        resp = auth.is_authenticated(self.request)
        self.assertFalse(resp)
开发者ID:mattbriancon,项目名称:django-tastypie,代码行数:19,代码来源:authentication.py

示例8: MultiAuthentication

# 需要导入模块: from tastypie.authentication import OAuthAuthentication [as 别名]
# 或者: from tastypie.authentication.OAuthAuthentication import is_authenticated [as 别名]
class MultiAuthentication(object):
    """
    A custom authentication backend that supports anonymous access, OAuth authentication, and API key authentication.
    """
    def __init__(self, **kwargs):
        super(MultiAuthentication, self).__init__(**kwargs)
        self.ApiKeyBackend = ApiKeyAuthentication()
        self.OAuthBackend = OAuthAuthentication()

    def is_authenticated(self, request, **kwargs):
        """
        Identifies if the user is authenticated to continue or not.

        Should return either ``True`` if allowed, ``False`` if not or an
        ``HttpResponse`` if you need something custom.
        """
        
        if self.ApiKeyBackend.is_valid_request(request):
            check = self.ApiKeyBackend.is_authenticated(request, **kwargs)
            if check is True:
                request._authentication_backend = self.ApiKeyBackend
            return check
        if self.OAuthBackend.is_valid_request(request):
            check = self.OAuthBackend.is_authenticated(request, **kwargs)
            if check is True:
                request._authentication_backend = self.OAuthBackend 
            return check
        else:
            # Authenticate the request as the anonymous user
            request.user = User.objects.get(id=ANONYMOUS_USER_ID)
            return True
        

    def get_identifier(self, request):
        """
        Provides a unique string identifier for the requestor.

        This implementation returns a combination of IP address and hostname.
        """
        try:
            return request._authentication_backend.get_identifier(request)
        except AttributeError:
            return 'nouser'
开发者ID:readingr,项目名称:provenance,代码行数:45,代码来源:auth.py


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