问题描述
Tastypie APIKey身份验证如何工作?我知道文档中提到了一个信号:
from django.contrib.auth.models import User
/ pre>
import django.db import model
from tastypie.models import create_api_key
models.signals.post_save.connect(create_api_key,sender = User)
但是,这个叫什么时候?如果我想给一个用户他们的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 asmain/
). Whatpost_save.connect(create_api_key, sender=User)
does is that everytime anUser
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 newApiKey
record, which will be related to the callingUser
. This record will also have a HMAC key when it was saved to theApiKey
table. The key is generated bygenerate_key()
function.这篇关于Tastypie APIKey认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!