本文整理汇总了Python中rest_framework.authentication.get_authorization_header函数的典型用法代码示例。如果您正苦于以下问题:Python get_authorization_header函数的具体用法?Python get_authorization_header怎么用?Python get_authorization_header使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_authorization_header函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_view
def process_view(self, request, callback, callback_args, callback_kwargs):
view_class = getattr(request.resolver_match.func, 'view_class', None)
if hasattr(view_class, 'conditional_forward'):
app = get_current_app(request)
request.matched_rule, request.matched_params = find_rule(
request, app)
if (request.matched_rule and request.matched_rule.is_forward
and request.method not in ('GET', 'HEAD', 'OPTIONS', 'TRACE')):
# We are forwarding the request so the CSRF is delegated
# to the application handling the forwarded request.
#pylint:disable=protected-access
request._dont_enforce_csrf_checks = True
LOGGER.debug("dont enforce csrf checks on %s %s",
request.method, request.path)
auth = get_authorization_header(request).split()
if auth and auth[0].lower() in [b'basic', b'bearer']:
# We need to support API calls from the command line.
#pylint:disable=protected-access
request._dont_enforce_csrf_checks = True
LOGGER.debug("dont enforce csrf checks on %s %s because"\
" we have an authorization header",
request.method, request.path)
return super(RulesMiddleware, self).process_view(
request, callback, callback_args, callback_kwargs)
示例2: authenticate
def authenticate(self, request):
auth = get_authorization_header(request).split()
if not auth or auth[0].lower() != b'jwt':
return None
if len(auth) == 1:
msg = 'Invalid token header. No credentials provided.'
log.warn('Invalid token header: "%s" No credentials provided.', auth)
raise AuthenticationFailed(msg)
elif len(auth) > 2:
msg = 'Invalid token header. Token string should not contain spaces.'
log.warn('Invalid token header: "%s" Token string should not contain spaces.', auth)
raise AuthenticationFailed(msg)
token = auth[1]
try:
data = jwt.decode(token, settings.FACES_SECRET)
except jwt.InvalidTokenError as e:
raise AuthenticationFailed(e)
user_id = data['user_id']
try:
user = User.objects.get(id=user_id)
except User.DoesNotExist:
return AnonymousUser(), token
return user, data
示例3: authenticate
def authenticate(self, request):
"""
Raises an exception for an expired token, or returns two-tuple of
(user, project) if authentication succeeds, or None otherwise.
"""
request.oauth2_error = getattr(request, "oauth2_error", {})
access_token = None
try:
auth = get_authorization_header(request).split()
token = auth[1].decode()
access_token = AccessToken.objects.get(token=token)
except Exception:
pass
if access_token and access_token.is_expired():
raise exceptions.AuthenticationFailed("Expired token.")
auth = super(CustomOAuth2Authentication, self).authenticate(request)
if auth:
project = OAuth2DataRequestProject.objects.get(
application=auth[1].application
)
return (auth[0], project)
return auth
示例4: authenticate
def authenticate(self, request):
auth = get_authorization_header(request).split()
internal_service = get_internal_header(request)
try:
internal_service = internal_service.decode()
except UnicodeError:
msg = ('Invalid internal_service header. '
'internal_service string should not contain invalid characters.')
raise exceptions.AuthenticationFailed(msg)
if internal_service not in settings.INTERNAL_SERVICES:
return None
if not auth or auth[0].lower() != self.keyword.lower().encode():
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)
示例5: authenticate
def authenticate(self, request):
auth = authentication.get_authorization_header(request).split()
if not auth or auth[0].lower() != self.keyword.lower().encode():
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)
try:
connection = models.Connection.objects.get(token=token)
except:
raise exceptions.AuthenticationFailed(_("Invalid token."))
return (AnonymousUser(), connection)
示例6: authenticate
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):
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)
示例7: authenticate
def authenticate(self,request):
auth=get_authorization_header(request).split()
if not auth:
msg=_('No token header.')
raise exceptions.AuthenticationFailed(msg)
# return None
if auth[0].lower()!= b'token':
msg=_('Invalid token header. Add keyword "Token" before token string.')
raise exceptions.AuthenticationFailed(msg)
# 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)
示例8: authenticate
def authenticate(self, request):
"""
Returns two-tuple of (user, token) if authentication succeeds,
or None otherwise.
"""
auth = get_authorization_header(request).split()
if len(auth) == 1:
msg = 'Invalid bearer header. No credentials provided.'
raise exceptions.AuthenticationFailed(msg)
elif len(auth) > 2:
msg = 'Invalid bearer header. Token string should not contain spaces.'
raise exceptions.AuthenticationFailed(msg)
if auth and auth[0].lower() == b'bearer':
access_token = auth[1]
elif 'access_token' in request.POST:
access_token = request.POST['access_token']
elif 'access_token' in request.GET and self.allow_query_params_token:
access_token = request.GET['access_token']
else:
return None
return self.authenticate_credentials(request, access_token)
示例9: authenticate
def authenticate(self, request):
"""
Returns a two-tuple of `User` and token if a valid signature has been
supplied using JWT-based authentication. Otherwise returns `None`.
"""
auth = get_authorization_header(request).split()
if not auth or auth[0].lower() != b'jwt':
return None
if len(auth) == 1:
msg = 'Invalid JWT header. No credentials provided.'
raise exceptions.AuthenticationFailed(msg)
elif len(auth) > 2:
msg = ('Invalid JWT header. Credentials string '
'should not contain spaces.')
raise exceptions.AuthenticationFailed(msg)
try:
payload = jwt_decode_handler(auth[1])
except jwt.ExpiredSignature:
msg = 'Signature has expired.'
raise exceptions.AuthenticationFailed(msg)
except jwt.DecodeError:
msg = 'Error decoding signature.'
raise exceptions.AuthenticationFailed(msg)
user = self.authenticate_credentials(payload)
return (user, auth[1])
示例10: authenticate
def authenticate( self, request ):
auth = get_authorization_header( request ).split()
if not auth:
return None
token_type = auth[0].decode().lower()
if token_type == 'test_token':
token_type = 'test'
elif token_type == 'token':
token_type = ''
else:
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, token_type )
示例11: register_by_access_token
def register_by_access_token(request, backend):
uri=''
strategy = load_strategy(request)
backend = load_backend(strategy, backend, uri)
# Split by spaces and get the array
auth = get_authorization_header(request).split()
if not auth:
msg= 'No auth provided'
return msg
if not auth or auth[0].lower() != b'bearer':
msg = 'No token header provided.'
return msg
if len(auth) == 1:
msg = 'Invalid token header. No credentials provided.'
return msg
access_token = auth[1].decode(encoding='UTF-8')
user = backend.do_auth(access_token)
return user
示例12: authenticate
def authenticate(self, request):
auth = get_authorization_header(request).split()
authenticate_header = self.authenticate_header(request=request)
if not auth or smart_text(auth[0].lower()) != authenticate_header.lower():
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)
try:
payload = decode_jwt_token(token=token)
except jwt.exceptions.ExpiredSignature:
msg = _('Signature has expired.')
raise exceptions.AuthenticationFailed(msg)
except jwt.exceptions.DecodeError:
msg = _('Error decoding signature.')
raise exceptions.AuthenticationFailed(msg)
except jwt.exceptions.InvalidKeyError:
msg = _('Unauthorized token signing key.')
raise exceptions.AuthenticationFailed(msg)
except jwt.exceptions.InvalidTokenError:
raise exceptions.AuthenticationFailed()
return self.authenticate_credentials(payload=payload)
示例13: authenticate
def authenticate(self, request):
auth = authentication.get_authorization_header(request).split()
if not auth or auth[0].lower() != b'apikey':
# check for URL parameter authentication
username = request.GET.get('username')
key = request.GET.get('api_key')
if username and key:
return self.authenticate_credentials(username, key)
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:
username, key = auth[1].decode('utf-8').split(':')
except AttributeError:
username, key = auth[1].split(':')
except ValueError:
raise exceptions.AuthenticationFailed('Invalid username token pair')
return self.authenticate_credentials(username, key)
示例14: authenticate
def authenticate(self, request):
auth = get_authorization_header(request).split()
if not auth:
return None
if len(auth) == 1:
msg = "Invalid swr header." \
"No credentials provided."
raise AuthenticationFailed(msg)
elif len(auth) > 2:
msg = "Invalid swr header." \
"Id string should not contain spaces."
raise AuthenticationFailed(msg)
try:
element_pk = int(auth[1].decode())
if auth[0].lower() == b'channel':
return self.authenticate_channel(element_pk)
elif auth[0].lower() == b'mount':
return self.authenticate_mount(element_pk)
elif auth[0].lower() == b'recording':
return self.authenticate_recording(element_pk)
else:
return None
except UnicodeError:
msg = "Invalid swr header." \
"Id string should not contain invalid characters."
raise AuthenticationFailed(msg)
except ValueError:
msg = "Invalid swr header." \
"Id is not a valid number."
raise AuthenticationFailed(msg)
示例15: authenticate
def authenticate(self, request):
auth = authentication.get_authorization_header(request).split()
if not auth or auth[0].lower() != self.keyword.lower().encode():
return None
if len(auth) == 1:
msg = _('Invalid signature header. No credentials provided.')
raise exceptions.AuthenticationFailed(msg)
elif len(auth) > 2:
msg = _('Invalid signature header. Signature '
'string should not contain spaces.')
raise exceptions.AuthenticationFailed(msg)
try:
sign = auth[1].decode().split(':')
if len(sign) != 2:
msg = _('Invalid signature header. '
'Format like AccessKeyId:Signature')
raise exceptions.AuthenticationFailed(msg)
except UnicodeError:
msg = _('Invalid signature header. '
'Signature string should not contain invalid characters.')
raise exceptions.AuthenticationFailed(msg)
access_key_id = sign[0]
try:
uuid.UUID(access_key_id)
except ValueError:
raise exceptions.AuthenticationFailed('Access key id invalid')
request_signature = sign[1]
return self.authenticate_credentials(
request, access_key_id, request_signature
)