Python rest_framework.HTTP_HEADER_ENCODING Examples
The following are 12
code examples of rest_framework.HTTP_HEADER_ENCODING().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
rest_framework
, or try the search function
.
Example #1
Source File: authentication.py From Dailyfresh-B2C with Apache License 2.0 | 6 votes |
def authenticate(self, request): """ Returns a `User` if a correct username and password have been supplied using HTTP Basic authentication. Otherwise returns `None`. """ auth = 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, binascii.Error): msg = _('Invalid basic header. Credentials not correctly base64 encoded.') raise exceptions.AuthenticationFailed(msg) userid, password = auth_parts[0], auth_parts[2] return self.authenticate_credentials(userid, password, request)
Example #2
Source File: authentication.py From desec-stack with MIT License | 5 votes |
def authenticate_credentials(self, basic): invalid_token_message = 'Invalid basic auth token' try: username, key = base64.b64decode(basic).decode(HTTP_HEADER_ENCODING).split(':') user, token = TokenAuthentication().authenticate_credentials(key) domain_names = user.domains.values_list('name', flat=True) if username not in ['', user.email] and not username.lower() in domain_names: raise Exception except Exception: raise exceptions.AuthenticationFailed(invalid_token_message) if not user.is_active: raise exceptions.AuthenticationFailed(invalid_token_message) return user, token
Example #3
Source File: authentication.py From django-rest-framework-simplejwt with MIT License | 5 votes |
def get_header(self, request): """ Extracts the header containing the JSON web token from the given request. """ header = request.META.get('HTTP_AUTHORIZATION') if isinstance(header, str): # Work around django test client oddness header = header.encode(HTTP_HEADER_ENCODING) return header
Example #4
Source File: api.py From appstore with GNU Affero General Public License v3.0 | 5 votes |
def _login(self, user='test', password='test'): credentials = '%s:%s' % (user, password) base64_credentials = base64.b64encode( credentials.encode(HTTP_HEADER_ENCODING) ).decode(HTTP_HEADER_ENCODING) auth = 'Basic %s' % base64_credentials self.api_client.credentials(HTTP_AUTHORIZATION=auth)
Example #5
Source File: headers.py From polyaxon with Apache License 2.0 | 5 votes |
def get_header(request, header_service): """Return request's 'X_POLYAXON_...:' header, as a bytestring. Hide some test client ickyness where the header can be unicode. """ service = request.META.get("HTTP_{}".format(header_service), b"") if isinstance(service, str): # Work around django test client oddness service = service.encode(HTTP_HEADER_ENCODING) return service
Example #6
Source File: utils.py From django-rest-framework-guardian with BSD 3-Clause "New" or "Revised" License | 5 votes |
def basic_auth_header(username, password): credentials = ('%s:%s' % (username, password)) credentials = credentials.encode(HTTP_HEADER_ENCODING) return 'Basic %s' % b64encode(credentials).decode(HTTP_HEADER_ENCODING)
Example #7
Source File: authentication.py From diting with GNU General Public License v2.0 | 5 votes |
def get_request_date_header(request): date = request.META.get('HTTP_DATE', b'') if isinstance(date, text_type): # Work around django test client oddness date = date.encode(HTTP_HEADER_ENCODING) return date
Example #8
Source File: mediatypes.py From Dailyfresh-B2C with Apache License 2.0 | 5 votes |
def __init__(self, media_type_str): self.orig = '' if (media_type_str is None) else media_type_str self.full_type, self.params = parse_header(self.orig.encode(HTTP_HEADER_ENCODING)) self.main_type, sep, self.sub_type = self.full_type.partition('/')
Example #9
Source File: request.py From Dailyfresh-B2C with Apache License 2.0 | 5 votes |
def is_form_media_type(media_type): """ Return True if the media type is a valid form media type. """ base_media_type, params = parse_header(media_type.encode(HTTP_HEADER_ENCODING)) return (base_media_type == 'application/x-www-form-urlencoded' or base_media_type == 'multipart/form-data')
Example #10
Source File: authentication.py From Dailyfresh-B2C with Apache License 2.0 | 5 votes |
def get_authorization_header(request): """ Return request's 'Authorization:' header, as a bytestring. Hide some test client ickyness where the header can be unicode. """ auth = request.META.get('HTTP_AUTHORIZATION', b'') if isinstance(auth, text_type): # Work around django test client oddness auth = auth.encode(HTTP_HEADER_ENCODING) return auth
Example #11
Source File: negotiation.py From Dailyfresh-B2C with Apache License 2.0 | 4 votes |
def select_renderer(self, request, renderers, format_suffix=None): """ Given a request and a list of renderers, return a two-tuple of: (renderer, media type). """ # Allow URL style format override. eg. "?format=json format_query_param = self.settings.URL_FORMAT_OVERRIDE format = format_suffix or request.query_params.get(format_query_param) if format: renderers = self.filter_renderers(renderers, format) accepts = self.get_accept_list(request) # Check the acceptable media types against each renderer, # attempting more specific media types first # NB. The inner loop here isn't as bad as it first looks :) # Worst case is we're looping over len(accept_list) * len(self.renderers) for media_type_set in order_by_precedence(accepts): for renderer in renderers: for media_type in media_type_set: if media_type_matches(renderer.media_type, media_type): # Return the most specific media type as accepted. media_type_wrapper = _MediaType(media_type) if ( _MediaType(renderer.media_type).precedence > media_type_wrapper.precedence ): # Eg client requests '*/*' # Accepted media type is 'application/json' full_media_type = ';'.join( (renderer.media_type,) + tuple('{0}={1}'.format( key, value.decode(HTTP_HEADER_ENCODING)) for key, value in media_type_wrapper.params.items())) return renderer, full_media_type else: # Eg client requests 'application/json; indent=8' # Accepted media type is 'application/json; indent=8' return renderer, media_type raise exceptions.NotAcceptable(available_renderers=renderers)
Example #12
Source File: authentication.py From django-rest-framework-social-oauth2 with MIT License | 4 votes |
def authenticate(self, request): """ Returns two-tuple of (user, token) if authentication succeeds, or None otherwise. """ auth_header = get_authorization_header(request).decode(HTTP_HEADER_ENCODING) auth = auth_header.split() if not auth or auth[0].lower() != 'bearer': return None if len(auth) == 1: msg = 'Invalid token header. No backend provided.' raise exceptions.AuthenticationFailed(msg) elif len(auth) == 2: msg = 'Invalid token header. No credentials provided.' raise exceptions.AuthenticationFailed(msg) elif len(auth) > 3: msg = 'Invalid token header. Token string should not contain spaces.' raise exceptions.AuthenticationFailed(msg) token = auth[2] backend = auth[1] strategy = load_strategy(request=request) try: backend = load_backend(strategy, backend, reverse("%s:%s:complete" % (DRFSO2_URL_NAMESPACE, NAMESPACE), args=(backend,))) except MissingBackend: msg = 'Invalid token header. Invalid backend.' raise exceptions.AuthenticationFailed(msg) try: user = backend.do_auth(access_token=token) except requests.HTTPError as e: msg = e.response.text raise exceptions.AuthenticationFailed(msg) if not user: msg = 'Bad credentials.' raise exceptions.AuthenticationFailed(msg) return user, token