本文整理汇总了Python中rest_framework.HTTP_HEADER_ENCODING属性的典型用法代码示例。如果您正苦于以下问题:Python rest_framework.HTTP_HEADER_ENCODING属性的具体用法?Python rest_framework.HTTP_HEADER_ENCODING怎么用?Python rest_framework.HTTP_HEADER_ENCODING使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类rest_framework
的用法示例。
在下文中一共展示了rest_framework.HTTP_HEADER_ENCODING属性的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: authenticate
# 需要导入模块: import rest_framework [as 别名]
# 或者: from rest_framework import HTTP_HEADER_ENCODING [as 别名]
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)
示例2: authenticate_credentials
# 需要导入模块: import rest_framework [as 别名]
# 或者: from rest_framework import HTTP_HEADER_ENCODING [as 别名]
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
示例3: get_header
# 需要导入模块: import rest_framework [as 别名]
# 或者: from rest_framework import HTTP_HEADER_ENCODING [as 别名]
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
示例4: _login
# 需要导入模块: import rest_framework [as 别名]
# 或者: from rest_framework import HTTP_HEADER_ENCODING [as 别名]
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)
示例5: get_header
# 需要导入模块: import rest_framework [as 别名]
# 或者: from rest_framework import HTTP_HEADER_ENCODING [as 别名]
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
示例6: basic_auth_header
# 需要导入模块: import rest_framework [as 别名]
# 或者: from rest_framework import HTTP_HEADER_ENCODING [as 别名]
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)
示例7: get_request_date_header
# 需要导入模块: import rest_framework [as 别名]
# 或者: from rest_framework import HTTP_HEADER_ENCODING [as 别名]
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
示例8: __init__
# 需要导入模块: import rest_framework [as 别名]
# 或者: from rest_framework import HTTP_HEADER_ENCODING [as 别名]
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('/')
示例9: is_form_media_type
# 需要导入模块: import rest_framework [as 别名]
# 或者: from rest_framework import HTTP_HEADER_ENCODING [as 别名]
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')
示例10: get_authorization_header
# 需要导入模块: import rest_framework [as 别名]
# 或者: from rest_framework import HTTP_HEADER_ENCODING [as 别名]
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
示例11: select_renderer
# 需要导入模块: import rest_framework [as 别名]
# 或者: from rest_framework import HTTP_HEADER_ENCODING [as 别名]
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)
示例12: authenticate
# 需要导入模块: import rest_framework [as 别名]
# 或者: from rest_framework import HTTP_HEADER_ENCODING [as 别名]
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