我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用rest_framework.authentication.get_authorization_header()。
def _token_from_request_headers(self, request): auth = authentication.get_authorization_header(request).split() if not auth or auth[0].lower() != b'basic': return None if len(auth) == 1: 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: auth_parts = base64.b64decode(auth[1]).decode(HTTP_HEADER_ENCODING).partition(':') except (TypeError, UnicodeDecodeError): msg = _('Invalid basic header. Credentials not correctly base64 encoded.') raise exceptions.AuthenticationFailed(msg) return auth_parts[0]
def get_jwt_value(request): auth = get_authorization_header(request).split() auth_header_prefix = auth0_api_settings.JWT_AUTH_HEADER_PREFIX.lower() if not auth or smart_text(auth[0].lower()) != auth_header_prefix: return None if len(auth) == 1: msg = _('Invalid Authorization header. No credentials provided.') raise exceptions.AuthenticationFailed(msg) elif len(auth) > 2: msg = _('Invalid Authorization header. Credentials string ' 'should not contain spaces.') raise exceptions.AuthenticationFailed(msg) return auth[1] # Auth0 Metadata --------------------------------------------------------------
def authenticate(self, request): auth = get_authorization_header(request).split() if not auth or auth[0].lower() != b'token': return None if len(auth) == 1: 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() except UnicodeError: msg = 'Invalid token header. Token string should not contain invalid characters.' raise exceptions.AuthenticationFailed(msg) return self.authenticate_credentials(token)
def authenticate(self, request): auth = get_authorization_header(request).split() if not auth or auth[0].lower() != b'tokenservice': return None if len(auth) == 1: 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_key = auth[1].decode() except UnicodeError: msg = 'Invalid token header. Token string should not contain invalid characters.' raise exceptions.AuthenticationFailed(msg) return self._check_token(token_key)
def authenticate(self, request): """ Returns a `Person` if a correct access token has been supplied. Otherwise returns `None`. """ auth = get_authorization_header(request).split() if not auth or auth[0].lower() != b'bearer': return None if len(auth) == 1: 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: token = AccessToken.get_token(auth[1].decode()) except (InvalidTokenException, UnicodeDecodeError): msg = _('Token invalide.') raise exceptions.AuthenticationFailed(msg) token.person.role.token = token return token.person.role, token
def authenticate(self, request): if not self.get_user_info_url(): logger.warning('The setting OAUTH2_USER_INFO_URL is invalid!') return None auth = get_authorization_header(request).split() if not auth or auth[0].lower() != b'bearer': return None if len(auth) == 1: raise exceptions.AuthenticationFailed('Invalid token header. No credentials provided.') elif len(auth) > 2: raise exceptions.AuthenticationFailed('Invalid token header. Token string should not contain spaces.') return self.authenticate_credentials(auth[1].decode('utf8'))
def authenticate(self, request): auth = authentication.get_authorization_header(request).split() if not auth or auth[0].lower() != b'token': return None if len(auth) == 1: 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() except UnicodeError: msg = _('Invalid token header. Token string should not contain invalid characters.') raise exceptions.AuthenticationFailed(msg) return self.authenticate_credentials(token)
def post(self, request, *args, **kwargs): # ToDo: Remove Support For Django 1.8 and 1.9 and use request.user.is_authenticated if user_is_authenticated_helper(request.user): # delete this users auth token auth_header = get_authorization_header(request) token = auth_header.split()[1].decode() tokens = MultiToken.objects.filter(key=token, user=request.user) if len(tokens) == 1: tokens.delete() return Response({'status': 'logged out'}) else: return Response({'error': 'invalid token'}, status=status.HTTP_400_BAD_REQUEST) return Response({'error': 'not logged in'}, status=status.HTTP_401_UNAUTHORIZED)
def authenticate(self, request): auth = get_authorization_header(request).split() if not auth or auth[0].lower() != b'basic': return None if len(auth) == 1: msg = 'Invalid basic auth token header. No credentials provided.' raise exceptions.AuthenticationFailed(msg) elif len(auth) > 2: msg = 'Invalid basic auth token header. Basic authentication string should not contain spaces.' raise exceptions.AuthenticationFailed(msg) return self.authenticate_credentials(auth[1])
def authenticate(self, request): auth = get_authorization_header(request).split() if not auth or auth[0].lower() != b'digest': return None if self.authenticator.authenticate(request): return request.user, None else: raise AuthenticationFailed( _(u"Invalid username/password"))
def authenticate(self, request): auth = get_authorization_header(request).split() if not auth or auth[0].lower() != b'temptoken': return None if len(auth) == 1: m = 'Invalid token header. No credentials provided.' raise exceptions.AuthenticationFailed(m) elif len(auth) > 2: m = 'Invalid token header. Token string should not contain spaces.' raise exceptions.AuthenticationFailed(m) return self.authenticate_credentials(auth[1])
def authenticate(self, request): """ The `authenticate` method is called on every request, regardless of whether the endpoint requires authentication. `authenticate` has two possible return values: 1) `None` - We return `None` if we do not wish to authenticate. Usually this means we know authentication will fail. An example of this is when the request does not include a token in the headers. 2) `(user, token)` - We return a user/token combination when authentication was successful. If neither of these two cases were met, that means there was an error. In the event of an error, we do not return anything. We simple raise the `AuthenticationFailed` exception and let Django REST Framework handle the rest. """ request.user = None # `auth_header` should be an array with two elements: 1) the name of # the authentication header (in this case, "Token") and 2) the JWT # that we should authenticate against. auth_header = authentication.get_authorization_header(request).split() auth_header_prefix = self.authentication_header_prefix.lower() if not auth_header: return None if len(auth_header) == 1: # Invalid token header. No credentials provided. Do not attempt to # authenticate. return None elif len(auth_header) > 2: # Invalid token header. Token string should not contain spaces. Do # not attempt to authenticate. return None # The JWT library we're using can't handle the `byte` type, which is # commonly used by standard libraries in Python 3. To get around this, # we simply have to decode `prefix` and `token`. This does not make for # clean code, but it is a good decision because we would get an error # if we didn't decode these values. prefix = auth_header[0].decode('utf-8') token = auth_header[1].decode('utf-8') if prefix.lower() != auth_header_prefix: # The auth header prefix is not what we expected. Do not attempt to # authenticate. return None # By now, we are sure there is a *chance* that authentication will # succeed. We delegate the actual credentials authentication to the # method below. return self._authenticate_credentials(request, token)