From d83a7942162602b5a386bc64fa36aae582bfb2b1 Mon Sep 17 00:00:00 2001 From: Farid Darabi Date: Wed, 24 Nov 2021 14:15:01 +0330 Subject: [PATCH] Refa(authentication): improve code quality --- rest_framework/authentication.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/rest_framework/authentication.py b/rest_framework/authentication.py index 382abf158..4a4d0f8aa 100644 --- a/rest_framework/authentication.py +++ b/rest_framework/authentication.py @@ -65,13 +65,10 @@ class BasicAuthentication(BaseAuthentication): if not auth or auth[0].lower() != b'basic': return None - - if len(auth) == 1: + + if len(auth) != 2: msg = _('Invalid basic header. No credentials provided.') raise exceptions.AuthenticationFailed(msg) - elif len(auth) > 2: - msg = _('Invalid basic header. Credentials string should not contain spaces.') - raise exceptions.AuthenticationFailed(msg) try: try: @@ -180,12 +177,9 @@ class TokenAuthentication(BaseAuthentication): if not auth or auth[0].lower() != self.keyword.lower().encode(): return None - if len(auth) == 1: + if len(auth) != 2: msg = _('Invalid token header. No credentials provided.') raise exceptions.AuthenticationFailed(msg) - elif len(auth) > 2: - msg = _('Invalid token header. Token string should not contain spaces.') - raise exceptions.AuthenticationFailed(msg) try: token = auth[1].decode()