本文整理汇总了Python中rest_framework.authentication.TokenAuthentication方法的典型用法代码示例。如果您正苦于以下问题:Python authentication.TokenAuthentication方法的具体用法?Python authentication.TokenAuthentication怎么用?Python authentication.TokenAuthentication使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.authentication
的用法示例。
在下文中一共展示了authentication.TokenAuthentication方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_authentication_introspectors
# 需要导入模块: from rest_framework import authentication [as 别名]
# 或者: from rest_framework.authentication import TokenAuthentication [as 别名]
def get_authentication_introspectors(view):
"""
Get View Authentication Introspectors
:param view: DjangoRestFramework View
:return: list of authentication introspectors
:rtype: list
"""
from rest_framework import authentication
authenticators_map = {
authentication.BasicAuthentication: BasicAuthenticationIntrospector,
authentication.TokenAuthentication: TokenAuthenticationIntrospector,
}
authenticators = getattr(view, 'authentication_classes', [])
introspectors = []
for authenticator in authenticators:
introspectors.append(
authenticators_map.get(authenticator, BaseAuthenticationIntrospector)(authenticator)
)
return introspectors
示例2: authenticate
# 需要导入模块: from rest_framework import authentication [as 别名]
# 或者: from rest_framework.authentication import TokenAuthentication [as 别名]
def authenticate(self, request):
auth_result = super(TokenAuthenticationWithChangeSet,
self).authenticate(request)
# NOTE(xchu): The `auth_result` can be `None` if uncorrect
# authorization header is privided or nothing privided.
if auth_result is None:
return auth_result
# NOTE(xchu): Update request.changeset.author if the author is None;
# That's because if we use `TokenAuthentication`,
# DRF do the TokenAuthentication after the request passed
# `ChangesetMiddleware` and all other Django middlewares.
# Which means that we will not have the user until `authenticate`
# here.
if hasattr(request, "changeset") and request.changeset.author is None:
# the `auth_result` is `None` or a (user, token) tuple.
request.changeset.author = auth_result[0]
return auth_result
示例3: get_authenticators
# 需要导入模块: from rest_framework import authentication [as 别名]
# 或者: from rest_framework.authentication import TokenAuthentication [as 别名]
def get_authenticators(self):
try:
from knox.auth import TokenAuthentication
except ImportError:
warnings.warn(
'django-rest-knox must be installed for Knox authentication',
ImportWarning,
)
raise
return [TokenAuthentication()]
示例4: authenticate_credentials
# 需要导入模块: from rest_framework import authentication [as 别名]
# 或者: from rest_framework.authentication import TokenAuthentication [as 别名]
def authenticate_credentials(self, key):
""" Mostly duplicated from TokenAuthentication, except that we return
an AnonymousUser """
try:
token = self.model.objects.get(key=key)
except self.model.DoesNotExist:
raise exceptions.AuthenticationFailed(_('Invalid token.'))
return AnonymousUser(), token