本文介绍了Tastypie APIKey认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

Tastypie APIKey身份验证如何工作?我知道文档中提到了一个信号:

  from django.contrib.auth.models import User 
import django.db import model
from tastypie.models import create_api_key

models.signals.post_save.connect(create_api_key,sender = User)
/ pre>

但是,这个叫什么时候?如果我想给一个用户他们的APIkey,我知道我可以在APIKey数据库中找到这个create_api_key函数将键加入,但是在哪里和什么时候调用这个models.signals.post_save函数?



这只是另一个django模型吗?我认为是吗?



每次保存用户帐户时都会调用这个?

解决方案

您可以将其放在相关的 models.py 应用程式(例如 main / )。 post_save.connect(create_api_key,sender = User)的确是每次 User 实例被保存时, create_api_key()将被调用。



现在,我们来看看 create_api_key()是通过潜入某一个:

  class ApiKey(models.Model):
user = models.OneToOneField(User,related_name ='api_key')
key = models .CharField(max_length = 256,blank = True,default ='')
created = models.DateTimeField(default = datetime.datetime.now)

def __unicode __(self):
return u%s for%s%(self.key,self.user)

def save(self,* args,** kwargs):
if not self。 key:
self.key = self.generate_key()

返回超级(ApiKey,self).save(* args,** kwargs)

def generate_key (self):
#获取随机的UUID。
new_uuid = uuid.uuid4()
#Hmac那个野兽。
return hmac.new(str(new_uuid),digestmod = sha1).hexdigest()


def create_api_key(sender,** kwargs):

一个用于挂起自动ApiKey创建的信号

如果kwargs.get('created')为True:
ApiKey.objects.create (user = kwargs.get('instance'))

如您所见, create_api_key()将创建一个新的 ApiKey 记录,这将与调用用户。当这个记录保存到 HMAC 密钥> ApiKey 表。密钥由 generate_key()函数生成。


How does the Tastypie APIKey authentication work? I know there is a signal as mentioned in the documentation:

from django.contrib.auth.models import User
from django.db import models
from tastypie.models import create_api_key

models.signals.post_save.connect(create_api_key, sender=User)

However, when is this called? If I want to give a user their APIkey I know I can find it in the APIKey db that this create_api_key function adds the key into, but where and when do I call this models.signals.post_save function?

Is this just another django model? I think it is?

Is this called everytime a user account is saved?

解决方案

You can put this in models.py file of the relevant app (such as main/). What post_save.connect(create_api_key, sender=User) does is that everytime an User instance is saved, create_api_key() will be called.

Now let's look into what create_api_key() does by diving a bit into the source of tastypie:

class ApiKey(models.Model):
    user = models.OneToOneField(User, related_name='api_key')
    key = models.CharField(max_length=256, blank=True, default='')
    created = models.DateTimeField(default=datetime.datetime.now)

    def __unicode__(self):
        return u"%s for %s" % (self.key, self.user)

    def save(self, *args, **kwargs):
        if not self.key:
            self.key = self.generate_key()

        return super(ApiKey, self).save(*args, **kwargs)

    def generate_key(self):
        # Get a random UUID.
        new_uuid = uuid.uuid4()
        # Hmac that beast.
        return hmac.new(str(new_uuid), digestmod=sha1).hexdigest()


def create_api_key(sender, **kwargs):
    """
    A signal for hooking up automatic ``ApiKey`` creation.
    """
    if kwargs.get('created') is True:
        ApiKey.objects.create(user=kwargs.get('instance'))

As you can see, create_api_key() will create a new ApiKey record, which will be related to the calling User. This record will also have a HMAC key when it was saved to the ApiKey table. The key is generated by generate_key() function.

这篇关于Tastypie APIKey认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:10