我正在使用Django和django-rest-framework构建一个REST风格的API.
作为认证机制,我们 Select 了"令牌认证",我已经按照django-睡觉框架的文档实现了它,问题是,应用程序是否应该定期更新/更改令牌,如果是,该如何更新/更改令牌?应该是移动应用程序需要续签令牌,还是web应用程序应该自主更新令牌?
最佳实践是什么?
这里有谁有过使用Django 睡觉框架的经验,能给出一个技术解决方案吗?
(最后一个问题的优先级较低)
我正在使用Django和django-rest-framework构建一个REST风格的API.
作为认证机制,我们 Select 了"令牌认证",我已经按照django-睡觉框架的文档实现了它,问题是,应用程序是否应该定期更新/更改令牌,如果是,该如何更新/更改令牌?应该是移动应用程序需要续签令牌,还是web应用程序应该自主更新令牌?
最佳实践是什么?
这里有谁有过使用Django 睡觉框架的经验,能给出一个技术解决方案吗?
(最后一个问题的优先级较低)
让移动客户端定期更新其身份验证令牌是一种很好的做法.这当然取决于服务器来执行.
默认的TokenAuthentication类不支持此功能,但是您可以对其进行扩展以实现此功能.
例如:
from rest_framework.authentication import TokenAuthentication, get_authorization_header
from rest_framework.exceptions import AuthenticationFailed
class ExpiringTokenAuthentication(TokenAuthentication):
def authenticate_credentials(self, key):
try:
token = self.model.objects.get(key=key)
except self.model.DoesNotExist:
raise exceptions.AuthenticationFailed('Invalid token')
if not token.user.is_active:
raise exceptions.AuthenticationFailed('User inactive or deleted')
# This is required for the time comparison
utc_now = datetime.utcnow()
utc_now = utc_now.replace(tzinfo=pytz.utc)
if token.created < utc_now - timedelta(hours=24):
raise exceptions.AuthenticationFailed('Token has expired')
return token.user, token
还需要覆盖默认的睡觉框架登录视图,这样每次登录都会刷新令牌:
class ObtainExpiringAuthToken(ObtainAuthToken):
def post(self, request):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
token, created = Token.objects.get_or_create(user=serializer.validated_data['user'])
if not created:
# update the created time of the token to keep it valid
token.created = datetime.datetime.utcnow()
token.save()
return Response({'token': token.key})
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
obtain_expiring_auth_token = ObtainExpiringAuthToken.as_view()
别忘了修改URL:
urlpatterns += patterns(
'',
url(r'^users/login/?$', '<path_to_file>.obtain_expiring_auth_token'),
)