本文整理汇总了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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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'