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


Python authentication.BasicAuthentication類代碼示例

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


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

示例1: test_check_active_false

    def test_check_active_false(self):
        auth = BasicAuthentication(require_active=False)
        request = HttpRequest()

        bob_doe = User.objects.get(username='bobdoe')
        create_api_key(User, instance=bob_doe, created=True)
        request.META['HTTP_AUTHORIZATION'] = 'ApiKey bobdoe:%s' % bob_doe.api_key.key
        self.assertTrue(auth.is_authenticated(request))
開發者ID:mthornhill,項目名稱:django-tastypie,代碼行數:8,代碼來源:authentication.py

示例2: test_check_active_false

    def test_check_active_false(self):
        user_class = get_user_model()
        auth = BasicAuthentication(require_active=False)
        request = HttpRequest()

        bob_doe = user_class.objects.get(**{user_class.USERNAME_FIELD: 'bobdoe'})
        create_api_key(User, instance=bob_doe, created=True)
        request.META['HTTP_AUTHORIZATION'] = 'ApiKey bobdoe:%s' % bob_doe.api_key.key
        self.assertTrue(auth.is_authenticated(request))
開發者ID:amitu,項目名稱:django-tastypie,代碼行數:9,代碼來源:authentication.py

示例3: test_check_active_true

    def test_check_active_true(self):
        auth = BasicAuthentication()
        request = HttpRequest()

        bob_doe = User.objects.get(username='bobdoe')
        bob_doe.set_password('pass')
        bob_doe.save()
        request.META['HTTP_AUTHORIZATION'] = 'Basic %s' % base64.b64encode('bobdoe:pass'.encode('utf-8')).decode('utf-8')
        auth_res = auth.is_authenticated(request)
        # is_authenticated() returns HttpUnauthorized for inactive users in Django >= 1.10, False for < 1.10
        self.assertTrue(auth_res is False or isinstance(auth_res, HttpUnauthorized))
開發者ID:7Geese,項目名稱:django-tastypie,代碼行數:11,代碼來源:authentication.py

示例4: test_is_authenticated

    def test_is_authenticated(self):
        auth = BasicAuthentication()
        request = HttpRequest()

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

        # HttpUnauthorized with auth type and realm
        self.assertEqual(auth.is_authenticated(request)['WWW-Authenticate'], 'Basic Realm="django-tastypie"')

        # Wrong basic auth details.
        request.META['HTTP_AUTHORIZATION'] = 'abcdefg'
        self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)

        # No password.
        request.META['HTTP_AUTHORIZATION'] = base64.b64encode('daniel')
        self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)

        # Wrong user/password.
        request.META['HTTP_AUTHORIZATION'] = base64.b64encode('daniel:pass')
        self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)

        # Correct user/password.
        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')
        self.assertEqual(auth.is_authenticated(request), True)

        # Regression: Password with colon.
        john_doe = User.objects.get(username='johndoe')
        john_doe.set_password('pass:word')
        john_doe.save()
        request.META['HTTP_AUTHORIZATION'] = 'Basic %s' % base64.b64encode('johndoe:pass:word')
        self.assertEqual(auth.is_authenticated(request), True)
開發者ID:Unholster,項目名稱:django-tastypie,代碼行數:35,代碼來源:authentication.py

示例5: api_user_follow

def api_user_follow(request):
    if request.method == 'POST':
        basic_auth = BasicAuthentication()
        if basic_auth.is_authenticated(request):
            to_user_id = int(request.POST.get("to_user", 0))
            try:
                to_user = User.objects.get(id=to_user_id)
            except User.DoesNotExist:
                raise Http404
            from_user = request.user
            from_user.relationships.add(to_user)

            response_data={'status':'success'}
            return HttpResponse(simplejson.dumps(response_data), mimetype='application/json')
    raise Http404
開發者ID:zhiwehu,項目名稱:iphonebackend,代碼行數:15,代碼來源:views.py

示例6: test_whitelisting

    def test_whitelisting(self):
        auth = BasicAuthentication(whitelisted_methods=['a_method'])
        request = HttpRequest()

        # Calling with a whitelisted method_name without credentials should work
        self.assertEqual(auth.is_authenticated(request, method_name='a_method'), True)
        
        # Calling any other method should require auth
        self.assertEqual(isinstance(auth.is_authenticated(request, method_name='another_method'), HttpUnauthorized), True)

        # Correct user/password.
        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')
        self.assertEqual(auth.is_authenticated(request, method_name="another_method"), True)
        self.assertEqual(auth.is_authenticated(request, method_name="a_method"), True)
開發者ID:YACFirm,項目名稱:django-tastypie,代碼行數:17,代碼來源:authentication.py

示例7: test_is_authenticated

    def test_is_authenticated(self):
        auth = BasicAuthentication()
        request = HttpRequest()

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

        # HttpUnauthorized with auth type and realm
        self.assertEqual(auth.is_authenticated(request)["WWW-Authenticate"], 'Basic Realm="django-tastypie"')

        # Wrong basic auth details.
        request.META["HTTP_AUTHORIZATION"] = "abcdefg"
        self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)

        # No password.
        request.META["HTTP_AUTHORIZATION"] = base64.b64encode("daniel")
        self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)

        # Wrong user/password.
        request.META["HTTP_AUTHORIZATION"] = base64.b64encode("daniel:pass")
        self.assertEqual(isinstance(auth.is_authenticated(request), HttpUnauthorized), True)

        # Correct user/password.
        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")
        self.assertEqual(auth.is_authenticated(request), True)
開發者ID:thepeopleseason,項目名稱:django-tastypie,代碼行數:28,代碼來源:authentication.py

示例8: is_authenticated

    def is_authenticated(self, request, **kwargs):  # noqa # too complex
        '''
        handles backends explicitly so that it can return False when
        credentials are given but wrong and return Anonymous User when
        credentials are not given or the session has expired (web use).
        '''
        auth_info = request.META.get('HTTP_AUTHORIZATION')

        if 'HTTP_AUTHORIZATION' not in request.META:
            if hasattr(request.user, 'allowed_tokens'):
                tokens = request.user.allowed_tokens
            session_auth = SessionAuthentication()
            check = session_auth.is_authenticated(request, **kwargs)
            if check:
                if isinstance(check, HttpUnauthorized):
                    session_auth_result = False
                else:
                    request._authentication_backend = session_auth
                    session_auth_result = check
            else:
                request.user = AnonymousUser()
                session_auth_result = True
            request.user.allowed_tokens = tokens
            return session_auth_result
        else:
            if auth_info.startswith('Basic'):
                basic_auth = BasicAuthentication()
                check = basic_auth.is_authenticated(request, **kwargs)
                if check:
                    if isinstance(check, HttpUnauthorized):
                        return False
                    else:
                        request._authentication_backend = basic_auth
                        return check
            if auth_info.startswith('ApiKey'):
                apikey_auth = ApiKeyAuthentication()
                check = apikey_auth.is_authenticated(request, **kwargs)
                if check:
                    if isinstance(check, HttpUnauthorized):
                        return False
                    else:
                        request._authentication_backend = apikey_auth
                        return check
開發者ID:jasonrig,項目名稱:mytardis,代碼行數:43,代碼來源:api.py

示例9: api_upload_photo

def api_upload_photo(request):
    if request.method == 'POST':
        basic_auth = BasicAuthentication()
        if basic_auth.is_authenticated(request):
            photo = Photo(user=request.user, title=request.POST.get('title', None), file=request.FILES['file'])
            photo.save()

            at_users = request.POST.get('atusers', '').split(',')
            for user_id in at_users:
                try:
                    to_user = User.objects.get(id=int(user_id))
                    message = Message(from_user=request.user, to_user=to_user, description=get_photo_info(photo)).save()
                except Exception:
                    pass

            # TODO the api resource_uri need to dynamic
            response_data={"resource_uri": "/api/v1/photo/%d/" % (photo.id) , "file_url": photo.file.url}
            return HttpResponse(simplejson.dumps(response_data), mimetype='application/json')
    raise Http404
開發者ID:zhiwehu,項目名稱:iphonebackend,代碼行數:19,代碼來源:views.py

示例10: is_authenticated

    def is_authenticated(self, request, **kwargs):
        '''
        handles backends explicitly so that it can return False when
        credentials are given but wrong and return Anonymous User when
        credentials are not given or the session has expired (web use).
        '''
        auth_info = request.META.get('HTTP_AUTHORIZATION')

        if 'HTTP_AUTHORIZATION' not in request.META:
            session_auth = SessionAuthentication()
            check = session_auth.is_authenticated(request, **kwargs)
            if check:
                if isinstance(check, HttpUnauthorized):
                    return(False)
                else:
                    request._authentication_backend = session_auth
                    return(check)
            else:
                request.user = AnonymousUser()
                return(True)
        else:
            if auth_info.startswith('Basic'):
                basic_auth = BasicAuthentication()
                check = basic_auth.is_authenticated(request, **kwargs)
                if check:
                    if isinstance(check, HttpUnauthorized):
                        return(False)
                    else:
                        request._authentication_backend = basic_auth
                        return(check)
            if auth_info.startswith('ApiKey'):
                apikey_auth = ApiKeyAuthentication()
                check = apikey_auth.is_authenticated(request, **kwargs)
                if check:
                    if isinstance(check, HttpUnauthorized):
                        return(False)
                    else:
                        request._authentication_backend = apikey_auth
                        return(check)
開發者ID:TheGoodRob,項目名稱:mytardis,代碼行數:39,代碼來源:api.py

示例11: Authentication

class Authentication(ApiKeyAuthentication):
    def __init__(self):
        self.api_key_auth = ApiKeyAuthentication()
        self.basic_auth = BasicAuthentication(backend=ApiKeyBackend())

    def is_authenticated(self, request, **kwargs):
        if request.user.is_authenticated():
            return True
        ret = self.basic_auth.is_authenticated(request, **kwargs)
        if isinstance(ret, HttpUnauthorized):
            ret2 = self.api_key_auth.is_authenticated(request, **kwargs)
            if not isinstance(ret2, HttpUnauthorized):
                return ret2
        return ret

    def get_identifier(self, request):
        if request.user.is_authenticated():
            return request.user.username
        ret = self.basic_auth.get_identifier(request)
        if isinstance(ret, HttpUnauthorized):
            ret2 = self.api_key_auth.get_identifier(request)
            if not isinstance(ret2, HttpUnauthorized):
                return ret2
        return ret
開發者ID:cuker,項目名稱:django-eggproxy,代碼行數:24,代碼來源:authorization.py

示例12: __init__

 def __init__(self):
     self.api_key_auth = ApiKeyAuthentication()
     self.basic_auth = BasicAuthentication(backend=ApiKeyBackend())
開發者ID:cuker,項目名稱:django-eggproxy,代碼行數:3,代碼來源:authorization.py


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