nem*_*ign 103 django rest restful-authentication django-rest-framework
我正在使用Django和django-rest-framework构建RESTful API .
作为认证机制,我们选择了"令牌认证",并且我已经在Django-REST-Framework的文档之后实现了它,问题是,应用程序是否应该定期更新/更改令牌,如果是,如何?应该是需要续订令牌的移动应用程序还是Web应用程序应该自动执行此操作?
什么是最佳做法?
这里的任何人都有使用Django REST Framework的经验,可以提出技术解决方案吗?
(最后一个问题的优先级较低)
ode*_*fos 93
移动客户端定期更新其身份验证令牌是一种很好的做法.这当然取决于服务器的执行情况.
默认的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
Run Code Online (Sandbox Code Playgroud)
还需要覆盖默认的rest框架登录视图,以便在完成登录时刷新令牌:
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()
Run Code Online (Sandbox Code Playgroud)
并且不要忘记修改网址:
urlpatterns += patterns(
'',
url(r'^users/login/?$', '<path_to_file>.obtain_expiring_auth_token'),
)
Run Code Online (Sandbox Code Playgroud)
gal*_*lex 22
如果某人对该解决方案感兴趣但希望拥有一段有效的令牌,那么就会被新令牌取代,这就是完整的解决方案(Django 1.6):
yourmodule/views.py:
import datetime
from django.utils.timezone import utc
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token
from django.http import HttpResponse
import json
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.object['user'])
utc_now = datetime.datetime.utcnow()
if not created and token.created < utc_now - datetime.timedelta(hours=24):
token.delete()
token = Token.objects.create(user=serializer.object['user'])
token.created = datetime.datetime.utcnow()
token.save()
#return Response({'token': token.key})
response_data = {'token': token.key}
return HttpResponse(json.dumps(response_data), content_type="application/json")
return HttpResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
obtain_expiring_auth_token = ObtainExpiringAuthToken.as_view()
Run Code Online (Sandbox Code Playgroud)
yourmodule/urls.py:
from django.conf.urls import patterns, include, url
from weights import views
urlpatterns = patterns('',
url(r'^token/', 'yourmodule.views.obtain_expiring_auth_token')
)
Run Code Online (Sandbox Code Playgroud)
你的项目urls.py(在urlpatterns数组中):
url(r'^', include('yourmodule.urls')),
Run Code Online (Sandbox Code Playgroud)
yourmodule/authentication.py:
import datetime
from django.utils.timezone import utc
from rest_framework.authentication import TokenAuthentication
from rest_framework import exceptions
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')
utc_now = datetime.datetime.utcnow()
if token.created < utc_now - datetime.timedelta(hours=24):
raise exceptions.AuthenticationFailed('Token has expired')
return (token.user, token)
Run Code Online (Sandbox Code Playgroud)
在REST_FRAMEWORK设置中,将ExpiringTokenAuthentication添加为Authentification类而不是TokenAuthentication:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
#'rest_framework.authentication.TokenAuthentication',
'yourmodule.authentication.ExpiringTokenAuthentication',
),
}
Run Code Online (Sandbox Code Playgroud)
以为我会使用 DRY 给出 Django 2.0 的答案。有人已经为我们构建了这个,谷歌 Django OAuth ToolKit。可用于 pip, pip install django-oauth-toolkit
. 使用路由器添加令牌视图集的说明:https ://django-oauth-toolkit.readthedocs.io/en/latest/rest-framework/getting_started.html 。它类似于官方教程。
所以基本上 OAuth1.0 更像是昨天的安全性,这就是 TokenAuthentication。为了获得花哨的过期令牌,OAuth2.0 现在风靡一时。您将获得一个 AccessToken、RefreshToken 和范围变量来微调权限。你最终会得到这样的信用:
{
"access_token": "<your_access_token>",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "<your_refresh_token>",
"scope": "read"
}
Run Code Online (Sandbox Code Playgroud)
我试过@odedfos的答案,但我有误导性的错误.这是相同的答案,修复和适当的进口.
views.py
from django.utils import timezone
from rest_framework import status
from rest_framework.response import Response
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.views import ObtainAuthToken
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.object['user'])
if not created:
# update the created time of the token to keep it valid
token.created = datetime.datetime.utcnow().replace(tzinfo=utc)
token.save()
return Response({'token': token.key})
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Run Code Online (Sandbox Code Playgroud)
authentication.py
from datetime import timedelta
from django.conf import settings
from django.utils import timezone
from rest_framework.authentication import TokenAuthentication
from rest_framework import exceptions
EXPIRE_HOURS = getattr(settings, 'REST_FRAMEWORK_TOKEN_EXPIRE_HOURS', 24)
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')
if token.created < timezone.now() - timedelta(hours=EXPIRE_HOURS):
raise exceptions.AuthenticationFailed('Token has expired')
return (token.user, token)
Run Code Online (Sandbox Code Playgroud)
作者问
问题是,应用程序是否应该定期更新/更改令牌,如果是,如何更新?应该是移动应用程序需要更新令牌,还是网络应用程序应该自动完成?
但是所有的答案都是关于如何自动更改令牌的。
我认为通过令牌定期更改令牌是没有意义的。 其余框架创建一个 40 个字符的令牌,如果攻击者每秒测试 1000 个令牌,则需要数16**40/1000/3600/24/365=4.6*10^7
年才能获得令牌。您不必担心攻击者会一一测试您的令牌。即使你改变了你的代币,猜测你代币的概率是一样的。
如果你担心攻击者可能会得到你的令牌,那么你定期更改它,而不是在攻击者获得令牌后,他还可以更改你的令牌,而不是真正的用户被踢出去。
您真正应该做的是防止攻击者获取您用户的令牌,请使用 https。
顺便说一句,我只是说逐个令牌更改令牌没有意义,而按用户名和密码更改令牌有时是有意义的。也许令牌用于某些http环境(您应该始终避免这种情况)或某些第三方(在这种情况下,您应该创建不同类型的令牌,使用oauth2)以及当用户正在做一些危险的事情时,例如更改绑定邮箱或删除帐户,您应该确保不再使用原始令牌,因为它可能已被攻击者使用嗅探器或 tcpdump 工具泄露。
归档时间: |
|
查看次数: |
44196 次 |
最近记录: |