【问题标题】:Token Authentication for RESTful API: should the token be periodically changed?RESTful API 的令牌认证:是否应该定期更改令牌?
【发布时间】:2013-01-12 02:51:36
【问题描述】:

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

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

最佳做法是什么?

这里有没有人使用过 Django REST Framework 并可以提出技术解决方案?

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

【问题讨论】:

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


    【解决方案1】:

    让移动客户端定期更新其身份验证令牌是一种很好的做法。这当然是由服务器来强制执行的。

    默认的 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'),
    )
    

    【讨论】:

    • 如果它已过期,您是否不想在ObtainExpiringAuthToken 中创建一个新令牌,而不仅仅是更新旧令牌的时间戳?
    • 创建一个新的令牌是有意义的。您还可以重新生成现有令牌键的值,然后您不必删除旧令牌。
    • 如果我想在到期时清除令牌怎么办?当我再次 get_or_create 时,是否会生成新的令牌或更新时间戳?
    • 另外,您可以通过在 cronjob(Celery Beat 或类似)中定期驱逐旧令牌来使表中的令牌过期,而不是拦截验证
    • @BjornW 我只会驱逐,在我看来,与 API(或您的前端)集成的人有责任提出请求,他们会收到“无效令牌",然后点击刷新/创建新令牌端点
    【解决方案2】:

    如果有人对该解决方案感兴趣,但希望获得一个在特定时间内有效的令牌,则被新令牌替换这是完整的解决方案(Django 1.6):

    你的模块/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()
    

    你的模块/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')),
    

    你的模块/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',
        ),
    }
    

    【讨论】:

    • 当我尝试访问 api 端点时收到错误 'ObtainExpiringAuthToken' object has no attribute 'serializer_class'。不知道我错过了什么。
    • 有趣的解决方案,我稍后会测试;目前,您的帖子帮助我走上了正确的轨道,因为我只是忘记设置 AUTHENTICATION_CLASSES。
    • 聚会迟到了,但我需要做一些细微的改变才能让它发挥作用。 1) utc_now = datetime.datetime.utcnow() 应该是 utc_now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC) 2) 在 ExpiringTokenAuthentication(TokenAuthentication) 类中:你需要模型,self.model = self。 get_model()
    【解决方案3】:

    我想我会使用 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"
    }
    

    【讨论】:

    • 绝对同意你的观点,它有助于更​​轻松地管理身份验证,并且经过实战考验。
    【解决方案4】:

    我试过@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)
    

    【讨论】:

      【解决方案5】:

      作者问

      问题是,应用程序是否应该定期更新/更改令牌?如果是,如何?应该是移动应用程序需要更新令牌还是网络应用程序应该自主执行?

      但是所有的答案都是关于如何自动更改令牌的。

      我认为通过token定期更换token是没有意义的。其余框架创建一个有40个字符的token,如果攻击者每秒测试1000个token,则需要16**40/1000/3600/24/365=4.6*10^7年才能获得token .您不必担心攻击者会一一测试您的令牌。即使你改变了你的token,猜到你token的概率是一样的。

      如果你担心攻击者可能会得到你的token,所以你定期更改它,而不是攻击者得到token后,他也可以更改你的token,而不是真正的用户被踢出去。

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

      顺便说一句,我只是说逐个令牌更改令牌是没有意义的,通过用户名和密码更改令牌有时是有意义的。也许令牌用于某些 http 环境(您应该始终避免这种情况)或某些第三方(在这种情况下,您应该创建不同类型的令牌,使用 oauth2)以及当用户执行一些危险的事情时,例如更改绑定邮箱或删除帐户,您应该确保不再使用原始令牌,因为它可能已被攻击者使用嗅探器或 tcpdump 工具泄露。

      【讨论】:

      • 是的,同意,您应该通过其他方式(而不是旧的访问令牌)获得新的访问令牌。就像使用刷新令牌(或至少使用密码强制新登录的旧方式)。
      【解决方案6】:

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

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

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

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

      【讨论】:

        【解决方案7】:

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

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

        【讨论】:

        • 令牌认证不使用cookies
        【解决方案8】:

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

        身份验证.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
        

        views.py

        import datetime
        from pytz import utc
        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
        from rest_framework.authtoken.serializers import AuthTokenSerializer
        
        
        class ObtainExpiringAuthToken(ObtainAuthToken):
            def post(self, request, **kwargs):
                serializer = AuthTokenSerializer(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().replace(tzinfo=utc)
                        token.save()
        
                    return Response({'token': token.key})
                return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        

        【讨论】:

          【解决方案9】:

          只是为了继续添加@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

          【讨论】:

            【解决方案10】:

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

            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
            

            【讨论】:

              【解决方案11】:

              最好在您的应用上设置过期机制,无论是移动客户端还是网络客户端。有两种常见的解决方案:

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

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

              两种解决方案的共同点:

              settings.py 的变化

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

              创建 token_utils.py

              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)
              

              你观点的变化:

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

              如果使用选项 1,请将这些行添加到 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
              

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

              def handle_token_expired(token):
                  is_expired = is_token_expired(token)
                  if is_expired:
                      token.delete()
                      token = Token.objects.create(user = token.user)
                  return is_expired, token
              
              
              class ExpiringTokenAuthentication(TokenAuthentication):
                  """
                  when token is expired, it will be removed
                  and new one will be created
                  """
                  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 is not active")
              
                      is_expired, token = handle_token_expired(token)
                      if is_expired:
                          raise AuthenticationFailed("The Token is expired")
                  
                      return (token.user, token)
              

              【讨论】:

                猜你喜欢
                • 2017-07-15
                • 2014-07-16
                • 1970-01-01
                • 2014-03-29
                • 1970-01-01
                • 2020-05-12
                • 2013-02-25
                • 1970-01-01
                相关资源
                最近更新 更多