2

我正在开发一个第三方有兴趣与我们集成的应用程序。我想为每个用户创建一个令牌,并且想知道 django 中最有效和最常用的方法是什么?我试图找到一个,发现这个看起来很有希望:

https://github.com/jpulgarin/django-tokenapi

显然开箱即用它不会工作,但我可以类似地实现它。上述项目基本使用了sha散列算法。将这个或 MD5 用于 api 令牌是否安全?任何帮助表示赞赏!

4

2 回答 2

2

当您选择 API 密钥时,您要寻找的两个因素是

  1. 猜不透。这意味着您的密钥必须随机出现,并且太长而无法暴力破解

  2. 是独特的。意味着没有两个用户可以意外获得相同的 API 密钥。您可以使用数据库唯一性约束来强制执行此操作。

请记住,如果您只使用 1 个密钥,则必须在所有 api 端点上启用 SSL。在运输途中钥匙被偷是不好的,M'kay?

现在,为了实现,如果您使用自己的自定义视图来提供 API,那么https://github.com/jpulgarin/django-tokenapi应该适合您。

但是,如果您使用的是 TasyPie 或类似产品,您可能希望使用它们的内置授权代码,包括 api-key 管理。

http://django-tastypie.readthedocs.org/en/latest/authentication_authorization.html#apikeyauthentication

http://django-rest-framework.org/api-guide/authentication.html#tokenauthentication

于 2013-07-25T06:30:10.197 回答
1

我一直使用 TastyPie http://django-tastypie.readthedocs.org/en/latest/authentication.html#apikeyauthentication提供的 API 密钥。

模型.py

    def generate_key(self):
        # Get a random UUID.
        new_uuid = uuid.uuid4()
        # Hmac that beast.
        return hmac.new(str(new_uuid), digestmod=sha1).hexdigest() 
于 2013-07-24T03:16:15.377 回答