RESTful API的令牌认证:令牌是否应定期更改?

时间:2013-01-28 17:21:29

标签: django rest restful-authentication django-rest-framework

我正在使用Django和django-rest-framework构建RESTful API。

作为身份验证机制,我们选择了“令牌身份验证”,并且我已经按照Django-REST-Framework的文档实现了它,问题是,如果应用程序定期更新/更改令牌,如果是,如何?它应该是需要续订令牌的移动应用程序,还是Web应用程序应该自动执行此操作?

最佳做法是什么?

这里的任何人都有使用Django REST Framework的经验,可以提出技术解决方案吗?

(最后一个问题的优先级较低)

11 个答案:

答案 0 :(得分:85)

优良作法是让移动客户端定期更新其身份验证令牌。这当然取决于服务器的执行情况。

默认的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

还需要覆盖默认的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()

不要忘记修改网址:

urlpatterns += patterns(
    '',
    url(r'^users/login/?$', '<path_to_file>.obtain_expiring_auth_token'),
)

答案 1 :(得分:20)

如果有人对该解决方案感兴趣但希望拥有一段有效的令牌,那么将替换为新令牌这里是完整的解决方案(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()

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')
)

你的项目urls.py(在urlpatterns数组中):

url(r'^', include('yourmodule.urls')),

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)

在REST_FRAMEWORK设置中,将ExpiringTokenAuthentication添加为Authentification类而不是TokenAuthentication:

REST_FRAMEWORK = {

    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        #'rest_framework.authentication.TokenAuthentication',
        'yourmodule.authentication.ExpiringTokenAuthentication',
    ),
}

答案 2 :(得分:5)

我已经尝试了@odedfos的答案,但I had misleading error。这是相同的答案,修复和适当的进口。

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)

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)

答案 3 :(得分:3)

您可以利用http://getblimp.github.io/django-rest-framework-jwt

此库能够生成具有到期日期的令牌

要了解DRF默认令牌与DRF提供的令牌之间的区别,请查看:

How to make Django REST JWT Authentication scale with mulitple webservers?

答案 4 :(得分:2)

我以为我使用DRY给出了Django 2.0的答案。有人已经为我们建立了这个,谷歌Django OAuth ToolKit。可用pip pip install django-oauth-toolkit。有关使用路由器添加令牌ViewSet的说明: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"
}

答案 5 :(得分:1)

如果您注意到令牌就像会话cookie那么您可以坚持Django中会话cookie的默认生命周期:https://docs.djangoproject.com/en/1.4/ref/settings/#session-cookie-age

我不知道Django Rest Framework是否自动处理,但你总是可以编写一个简短的脚本来过滤掉过时的脚本并将它们标记为过期。

答案 6 :(得分:1)

作者问

  

问题是,应用程序应该定期更新/更改令牌吗?是需要续订令牌的移动应用程序,还是应该由网络应用程序自主执行?

但是所有答案都写着如何自动更改令牌。

我认为定期逐个令牌更改令牌是没有意义的。其余框架会创建一个包含40个字符的令牌,如果攻击者每秒测试1000个令牌,则需要16**40/1000/3600/24/365=4.6*10^7年才能获得令牌。您不必担心攻击者会一一测试您的令牌。即使您更改了令牌,猜测您令牌的可能性也相同。

如果您担心攻击者可以获取您的令牌,那么您可以定期更改它,而不是在攻击者获取令牌之后,他也可以更改您的令牌,而不是将实际用户踢出。

您真正应该做的是使用https 来防止攻击者获取用户的令牌。

顺便说一句,我只是说逐个令牌更改令牌是没有意义的,有时由用户名和密码更改令牌是有意义的。令牌可能在某些http环境(您应始终避免这种情况)或某些第三方(在这种情况下,您应创建其他类型的令牌,请使用oauth2)中使用,并且当用户执行某些危险的操作(如更改)时绑定邮箱或删除帐户后,应确保不再使用原始令牌,因为攻击者可能已使用嗅探器或tcpdump工具将其泄露。

答案 7 :(得分:0)

只是以为我会加我的,因为这对我有帮助。我通常使用JWT方法,但有时这样会更好。我用正确的导入方式更新了django 2.1的可接受答案。

authentication.py

import React from 'react'; 
import ReactDOM from 'react-dom';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import CssBaseline from '@material-ui/core/CssBaseline';
import App from './App';

const theme = createMuiTheme({
  palette: {
    type: 'dark',
  },
});

ReactDOM.render(

<MuiThemeProvider theme={theme}>
    <React.Fragment>
        <CssBaseline />
            <App/>
    </React.Fragment>
</MuiThemeProvider>,
     document.getElementById('app'));

views.py

from datetime import timedelta
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
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.get_model().objects.get(key=key)
        except ObjectDoesNotExist:
            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

答案 8 :(得分:0)

为了继续添加到@odedfos答案,我认为语法已经进行了一些更改,因此ExpiringTokenAuthentication的代码需要进行一些调整:

from rest_framework.authentication import TokenAuthentication
from datetime import timedelta
from datetime import datetime
import datetime as dtime
import pytz

class ExpiringTokenAuthentication(TokenAuthentication):

    def authenticate_credentials(self, key):
        model = self.get_model()
        try:
            token = model.objects.get(key=key)
        except 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.now(dtime.timezone.utc)
        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

此外,不要忘记将其添加到DEFAULT_AUTHENTICATION_CLASSES而不是rest_framework.authentication.TokenAuthentication

答案 9 :(得分:0)

如果有人希望在不活动一段时间后使令牌过期,则下面的答案将有所帮助。我正在调整此处给出的答案之一。我已经在添加的代码中添加了注释

from rest_framework.authentication import TokenAuthentication
from datetime import timedelta
from datetime import datetime
import datetime as dtime
import pytz

class ExpiringTokenAuthentication(TokenAuthentication):

    def authenticate_credentials(self, key):
        model = self.get_model()
        try:
            token = model.objects.get(key=key)
        except 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.now(dtime.timezone.utc)
        utc_now = utc_now.replace(tzinfo=pytz.utc)

        if token.created < utc_now - timedelta(minutes=15):  # TOKEN WILL EXPIRE AFTER 15 MINUTES OF INACTIVITY
            token.delete() # ADDED THIS LINE SO THAT EXPIRED TOKEN IS DELETED
            raise exceptions.AuthenticationFailed('Token has expired')
        else: 
            token.created = utc_now #THIS WILL SET THE token.created TO CURRENT TIME WITH EVERY REQUEST
            token.save() #SAVE THE TOKEN

        return token.user, token

答案 10 :(得分:0)

无论是针对移动客户端还是 Web 客户端,在您的应用上设置过期机制都是一种很好的做法。有两种常见的解决方案:

  1. 系统使令牌过期(在特定时间之后),用户必须重新登录才能获得新的有效令牌。

  2. 系统自动使旧令牌过期(在特定时间后)并用新令牌替换它(更改令牌)。

两种解决方案的共同点:

settings.py 中的更改

127.0.0.1

创建 token_utils.py

DEFAULT_AUTHENTICATION_CLASSES = [
# you replace right path of 'ExpiringTokenAuthentication' class
'accounts.token_utils.ExpiringTokenAuthentication'
]

TOKEN_EXPIRED_AFTER_MINUTES = 300

您的观点的变化:

from django.conf import settings
from datetime import timedelta

from django.conf import settings
from django.utils import timezone
from rest_framework.authentication import TokenAuthentication
from rest_framework.authtoken.models import Token
from rest_framework.exceptions import AuthenticationFailed


def expires_in(token: Token):
elapsed_time = timezone.now() - token.created
return timedelta(minutes=settings.TOKEN_EXPIRED_AFTER_MINUTES) - elapsed_time

def is_token_expired(token):
return expires_in(token) < timedelta(seconds=0)

如果使用选项 1,请将这些行添加到 token_utils.py

@api_view(['GET'])
@authentication_classes([ExpiringTokenAuthentication])
@permission_classes([IsAuthenticated])
def test(request):
    ...
return Response(response, stat_code)

如果使用选项 2,请将这些行添加到 token_utils.py

def handle_token_expired(token):
Token.objects.filter(key=token).delete()


class ExpiringTokenAuthentication(TokenAuthentication):

    def authenticate_credentials(self, key):
        try:
            token = Token.objects.get(key=key)
        except Token.DoesNotExist:
            raise AuthenticationFailed("Invalid Token!")

        if not token.user.is_active:
            raise AuthenticationFailed("User inactive or deleted")

        if is_token_expired(token):
            handle_token_expired(token)
            msg = "The token is expired!, user have to login again." 
            response = {"msg": msg}
            raise AuthenticationFailed(response)

    return token.user, token