當前位置: 首頁>>代碼示例>>Python>>正文


Python MultiAuthentication.is_authenticated方法代碼示例

本文整理匯總了Python中tastypie.authentication.MultiAuthentication.is_authenticated方法的典型用法代碼示例。如果您正苦於以下問題:Python MultiAuthentication.is_authenticated方法的具體用法?Python MultiAuthentication.is_authenticated怎麽用?Python MultiAuthentication.is_authenticated使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tastypie.authentication.MultiAuthentication的用法示例。


在下文中一共展示了MultiAuthentication.is_authenticated方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_apikey_and_basic_auth

# 需要導入模塊: from tastypie.authentication import MultiAuthentication [as 別名]
# 或者: from tastypie.authentication.MultiAuthentication import is_authenticated [as 別名]
    def test_apikey_and_basic_auth(self):
        auth = MultiAuthentication(BasicAuthentication(), ApiKeyAuthentication())
        request = HttpRequest()
        john_doe = User.objects.get(username='johndoe')

        # No API Key or HTTP Basic auth details should fail.
        self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)

        # Basic Auth still returns appropriately.
        self.assertEqual(auth.is_authenticated(request)['WWW-Authenticate'], 'Basic Realm="django-tastypie"')

        # API Key Auth works.
        request = HttpRequest()
        request.GET['username'] = 'johndoe'
        request.GET['api_key'] = john_doe.api_key.key
        self.assertEqual(auth.is_authenticated(request), True)
        self.assertEqual(auth.get_identifier(request), 'johndoe')


        # Basic Auth works.
        request = HttpRequest()
        john_doe = User.objects.get(username='johndoe')
        john_doe.set_password('pass')
        john_doe.save()
        request.META['HTTP_AUTHORIZATION'] = 'Basic %s' % base64.b64encode('johndoe:pass'.encode('utf-8')).decode('utf-8')
        self.assertEqual(auth.is_authenticated(request), True)
開發者ID:mthornhill,項目名稱:django-tastypie,代碼行數:28,代碼來源:authentication.py

示例2: test_apikey_and_authentication_enforce_user

# 需要導入模塊: from tastypie.authentication import MultiAuthentication [as 別名]
# 或者: from tastypie.authentication.MultiAuthentication import is_authenticated [as 別名]
    def test_apikey_and_authentication_enforce_user(self):
        session_auth = SessionAuthentication()
        api_key_auth = ApiKeyAuthentication()
        auth = MultiAuthentication(api_key_auth, session_auth)
        john_doe = User.objects.get(username="johndoe")
        request1 = HttpRequest()
        request2 = HttpRequest()
        request3 = HttpRequest()

        request1.method = "POST"
        request1.META = {"HTTP_X_CSRFTOKEN": "abcdef1234567890abcdef1234567890"}
        request1.COOKIES = {settings.CSRF_COOKIE_NAME: "abcdef1234567890abcdef1234567890"}
        request1.user = john_doe

        request2.POST["username"] = "janedoe"
        request2.POST["api_key"] = "invalid key"

        request3.method = "POST"
        request3.META = {"HTTP_X_CSRFTOKEN": "abcdef1234567890abcdef1234567890"}
        request3.COOKIES = {settings.CSRF_COOKIE_NAME: "abcdef1234567890abcdef1234567890"}
        request3.user = john_doe
        request3.POST["username"] = "janedoe"
        request3.POST["api_key"] = "invalid key"

        # session auth should pass if since john_doe is logged in
        self.assertEqual(session_auth.is_authenticated(request1), True)
        # api key auth should fail because of invalid api key
        self.assertEqual(isinstance(api_key_auth.is_authenticated(request2), HttpUnauthorized), True)

        # multi auth shouldn't change users if api key auth fails
        # multi auth passes since session auth is valid
        self.assertEqual(request3.user.username, "johndoe")
        self.assertEqual(auth.is_authenticated(request3), True)
        self.assertEqual(request3.user.username, "johndoe")
開發者ID:mattbriancon,項目名稱:django-tastypie,代碼行數:36,代碼來源:authentication.py

示例3: test_multiauth_apikey_and_basic_auth__basic_returns_authenticate

# 需要導入模塊: from tastypie.authentication import MultiAuthentication [as 別名]
# 或者: from tastypie.authentication.MultiAuthentication import is_authenticated [as 別名]
    def test_multiauth_apikey_and_basic_auth__basic_returns_authenticate(self):
        auth = MultiAuthentication(BasicAuthentication(), ApiKeyAuthentication())
        request = HttpRequest()

        self.assertEqual(
            auth.is_authenticated(request)['WWW-Authenticate'],
            'Basic Realm="django-tastypie"'
        )
開發者ID:Adusei,項目名稱:django-tastypie,代碼行數:10,代碼來源:authentication.py

示例4: test_multiauth_apikey_and_basic_auth__api_key_works_in_header

# 需要導入模塊: from tastypie.authentication import MultiAuthentication [as 別名]
# 或者: from tastypie.authentication.MultiAuthentication import is_authenticated [as 別名]
    def test_multiauth_apikey_and_basic_auth__api_key_works_in_header(self):
        auth = MultiAuthentication(BasicAuthentication(), ApiKeyAuthentication())
        request = HttpRequest()
        john_doe = User.objects.get(username='johndoe')

        request.META['HTTP_AUTHORIZATION'] = 'ApiKey %s:%s' % (john_doe.username, john_doe.api_key.key,)

        self.assertEqual(auth.is_authenticated(request), True)
        self.assertEqual(auth.get_identifier(request), john_doe.username)
開發者ID:Adusei,項目名稱:django-tastypie,代碼行數:11,代碼來源:authentication.py

示例5: test_multiauth_apikey_and_basic_auth__api_key_works_in_query

# 需要導入模塊: from tastypie.authentication import MultiAuthentication [as 別名]
# 或者: from tastypie.authentication.MultiAuthentication import is_authenticated [as 別名]
    def test_multiauth_apikey_and_basic_auth__api_key_works_in_query(self):
        auth = MultiAuthentication(BasicAuthentication(), ApiKeyAuthentication())
        request = HttpRequest()
        john_doe = User.objects.get(username='johndoe')

        request.GET['username'] = john_doe.username
        request.GET['api_key'] = john_doe.api_key.key

        self.assertEqual(auth.is_authenticated(request), True)
        self.assertEqual(auth.get_identifier(request), john_doe.username)
開發者ID:Adusei,項目名稱:django-tastypie,代碼行數:12,代碼來源:authentication.py

示例6: test_multiauth_apikey_and_basic_auth__basic_auth_works

# 需要導入模塊: from tastypie.authentication import MultiAuthentication [as 別名]
# 或者: from tastypie.authentication.MultiAuthentication import is_authenticated [as 別名]
    def test_multiauth_apikey_and_basic_auth__basic_auth_works(self):
        auth = MultiAuthentication(BasicAuthentication(), ApiKeyAuthentication())
        request = HttpRequest()
        john_doe = User.objects.get(username='johndoe')
        john_doe.set_password('pass')
        john_doe.save()

        request.META['HTTP_AUTHORIZATION'] = 'Basic %s' % base64.b64encode('johndoe:pass'.encode('utf-8')).decode('utf-8')

        self.assertEqual(auth.is_authenticated(request), True)
        self.assertEqual(auth.get_identifier(request), john_doe.username)
開發者ID:Adusei,項目名稱:django-tastypie,代碼行數:13,代碼來源:authentication.py

示例7: test_multiauth_apikey_and_basic_auth__api_key_works_in_header__space_in_username

# 需要導入模塊: from tastypie.authentication import MultiAuthentication [as 別名]
# 或者: from tastypie.authentication.MultiAuthentication import is_authenticated [as 別名]
    def test_multiauth_apikey_and_basic_auth__api_key_works_in_header__space_in_username(self):
        auth = MultiAuthentication(BasicAuthentication(), ApiKeyAuthentication())
        request = HttpRequest()
        john_doe = User.objects.get(username="johndoe")
        john_doe.username = "john doe"
        john_doe.save()

        request.META["HTTP_AUTHORIZATION"] = "ApiKey %s:%s" % (john_doe.username, john_doe.api_key.key)

        self.assertEqual(auth.is_authenticated(request), True)
        self.assertEqual(auth.get_identifier(request), john_doe.username)
開發者ID:mattbriancon,項目名稱:django-tastypie,代碼行數:13,代碼來源:authentication.py

示例8: test_apikey_and_authentication

# 需要導入模塊: from tastypie.authentication import MultiAuthentication [as 別名]
# 或者: from tastypie.authentication.MultiAuthentication import is_authenticated [as 別名]
    def test_apikey_and_authentication(self):
        auth = MultiAuthentication(ApiKeyAuthentication(), Authentication())
        request = HttpRequest()

        john_doe = User.objects.get(username='johndoe')

        # No username/api_key details should pass.
        self.assertEqual(auth.is_authenticated(request), True)

        # The identifier should be the basic auth stock.
        self.assertEqual(auth.get_identifier(request), 'noaddr_nohost')

        # Wrong username details.
        request = HttpRequest()
        request.GET['username'] = 'foo'

        self.assertEqual(auth.is_authenticated(request), True)
        self.assertEqual(auth.get_identifier(request), 'noaddr_nohost')

        # No api_key.
        request = HttpRequest()
        request.GET['username'] = 'daniel'

        self.assertEqual(auth.is_authenticated(request), True)
        self.assertEqual(auth.get_identifier(request), 'noaddr_nohost')

        # Wrong user/api_key.
        request = HttpRequest()
        request.GET['username'] = 'daniel'
        request.GET['api_key'] = 'foo'

        self.assertEqual(auth.is_authenticated(request), True)
        self.assertEqual(auth.get_identifier(request), 'noaddr_nohost')

        request = HttpRequest()
        request.GET['username'] = 'johndoe'
        request.GET['api_key'] = john_doe.api_key.key

        self.assertEqual(auth.is_authenticated(request), True)
        self.assertEqual(auth.get_identifier(request), john_doe.username)
開發者ID:Adusei,項目名稱:django-tastypie,代碼行數:42,代碼來源:authentication.py

示例9: test_multiauth_apikey_and_basic_auth__basic_auth_works

# 需要導入模塊: from tastypie.authentication import MultiAuthentication [as 別名]
# 或者: from tastypie.authentication.MultiAuthentication import is_authenticated [as 別名]
    def test_multiauth_apikey_and_basic_auth__basic_auth_works(self):
        auth = MultiAuthentication(BasicAuthentication(), ApiKeyAuthentication())
        request = HttpRequest()
        john_doe = User.objects.get(username="johndoe")
        john_doe.set_password("pass")
        john_doe.save()

        request.META["HTTP_AUTHORIZATION"] = "Basic %s" % base64.b64encode("johndoe:pass".encode("utf-8")).decode(
            "utf-8"
        )

        self.assertEqual(auth.is_authenticated(request), True)
        self.assertEqual(auth.get_identifier(request), john_doe.username)
開發者ID:mattbriancon,項目名稱:django-tastypie,代碼行數:15,代碼來源:authentication.py

示例10: test_apikey_and_authentication_enforce_user

# 需要導入模塊: from tastypie.authentication import MultiAuthentication [as 別名]
# 或者: from tastypie.authentication.MultiAuthentication import is_authenticated [as 別名]
    def test_apikey_and_authentication_enforce_user(self):
        session_auth = SessionAuthentication()
        api_key_auth = ApiKeyAuthentication()
        auth = MultiAuthentication(api_key_auth, session_auth)
        john_doe = User.objects.get(username='johndoe')
        request1 = HttpRequest()
        request2 = HttpRequest()
        request3 = HttpRequest()

        request1.method = 'POST'
        request1.META = {
            'HTTP_X_CSRFTOKEN': 'abcdef1234567890abcdef1234567890'
        }
        request1.COOKIES = {
            settings.CSRF_COOKIE_NAME: 'abcdef1234567890abcdef1234567890'
        }
        request1.user = john_doe

        request2.POST['username'] = 'janedoe'
        request2.POST['api_key'] = 'invalid key'

        request3.method = 'POST'
        request3.META = {
            'HTTP_X_CSRFTOKEN': 'abcdef1234567890abcdef1234567890'
        }
        request3.COOKIES = {
            settings.CSRF_COOKIE_NAME: 'abcdef1234567890abcdef1234567890'
        }
        request3.user = john_doe
        request3.POST['username'] = 'janedoe'
        request3.POST['api_key'] = 'invalid key'

        #session auth should pass if since john_doe is logged in
        self.assertTrue(session_auth.is_authenticated(request1))
        #api key auth should fail because of invalid api key
        self.assertEqual(isinstance(api_key_auth.is_authenticated(request2), HttpUnauthorized), True)

        #multi auth shouldn't change users if api key auth fails
        #multi auth passes since session auth is valid
        self.assertEqual(request3.user.username, 'johndoe')
        self.assertTrue(auth.is_authenticated(request3))
        self.assertEqual(request3.user.username, 'johndoe')
開發者ID:mthornhill,項目名稱:django-tastypie,代碼行數:44,代碼來源:authentication.py

示例11: is_authenticated

# 需要導入模塊: from tastypie.authentication import MultiAuthentication [as 別名]
# 或者: from tastypie.authentication.MultiAuthentication import is_authenticated [as 別名]
 def is_authenticated(self, request, **kwargs):
     if request.method == 'GET':
         return True
     multi_auth = MultiAuthentication(SessionAuthentication(),
         BasicAuthentication())
     return multi_auth.is_authenticated(request, **kwargs)
開發者ID:Alexander-Minyushkin,項目名稱:froide,代碼行數:8,代碼來源:api_utils.py

示例12: test_multiauth_apikey_and_basic_auth__no_details_fails

# 需要導入模塊: from tastypie.authentication import MultiAuthentication [as 別名]
# 或者: from tastypie.authentication.MultiAuthentication import is_authenticated [as 別名]
    def test_multiauth_apikey_and_basic_auth__no_details_fails(self):
        auth = MultiAuthentication(BasicAuthentication(), ApiKeyAuthentication())
        request = HttpRequest()

        self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)
開發者ID:Adusei,項目名稱:django-tastypie,代碼行數:7,代碼來源:authentication.py


注:本文中的tastypie.authentication.MultiAuthentication.is_authenticated方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。