【问题标题】:How the client gets api-key when using tastypie ApiKeyAuthentication?使用tastepie ApiKeyAuthentication时客户端如何获取api-key?
【发布时间】:2015-08-19 04:31:57
【问题描述】:

我使用 django-tastypie 进行 REST API 和 ApiKeyAuthentication 进行身份验证。我在api.py 中有一个CreateUserResource 用于创建新用户。现在在所有教程中都指定,无论何时完成任何 GET 或 POST 请求,都必须指定 apikey。但是客户端是如何收到这个apikey的呢?

是不是在登录时,客户端要进行身份验证,并给出一个apikey,当客户端注销时,该apikey会被撤销?

【问题讨论】:

    标签: django tastypie django-authentication api-key


    【解决方案1】:

    用于在后端使用 Tastypie 在单页应用中实现基本身份验证:

    1. 让用户输入他的用户名和密码,然后将其发布到仅用于处理用户身份验证的资源上。
    2. 让此资源查找用户并对其进行身份验证,然后发送“api 密钥”作为响应。
    3. 在前端捕获此“api 密钥”并将其存储在本地存储中。
    4. 现在使用本地存储中的“api 密钥”并将其作为标头发送到您的所有请求中。在美味派的情况下,将是“授权:ApiKey user_name:generated_api_token”
    5. 现在,如果您的资源正在使用 ApiKeyAuthentication,如果令牌无效或不存在,则会出错。
    6. 用户注销后,从本地存储中删除密钥。

    此类身份验证资源可能如下所示:

    from django.contrib.auth.models import User
    from django.contrib.auth import authenticate, login
    from tastypie.http import HttpUnauthorized, HttpForbidden, HttpNotFound
    from tastypie.authentication import ApiKeyAuthentication
    from django.conf.urls import url
    from tastypie.resources import ModelResource
    from tastypie.utils import trailing_slash
    
    class AuthenticationResource(ModelResource):
    
        def __get_api_key_for_user(self, user):
            return '%s' % (user.api_key.key)
    
        class Meta:
            resource_name = 'authentication'
    
        def prepend_urls(self):
            return [
                url(r"^(?P<resource_name>%s)/login%s$" %
                    (self._meta.resource_name, trailing_slash()),
                    self.wrap_view('login'), name="api_login"),
            ]
    
    
        def login(self, request, **kwargs):
            self.method_check(request, allowed=['post'])
            data = self.deserialize(request, request.body, format=request.META.get('CONTENT_TYPE', 'application/json'))
            username = data.get('username', '')
            password = data.get('password', '')
            user = authenticate(username=username, password=password)
            if user:
                if user.is_active:
                    last_login = user.last_login
                    login(request, user) // updates the last login
                    return self.create_response(request, {
                        'api_key': self.__get_api_key_for_user(user),
                        'last_login': last_login,
                        'username': username
                        })
                else:
                    return self.create_response(request, {
                        'success': False,
                        'reason': 'disabled',
                        }, HttpForbidden )
            else:
                return self.create_response(request, {
                    'success': False,
                    'reason': 'Incorrect user name or password',
                    }, HttpUnauthorized )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2014-08-18
      • 2013-07-05
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多