Does Tastypie have a helper function to generate API keys?
Asked Answered
R

5

10

What I'm trying to do is whenever the user requests an API key--regardless of whether the user already generated one or not--the system will generate an entirely new key.

I know that whenever calling ApiKey.objects.create() will generate an API key for the user that doesn't have one generated. However, if a user does have one, then trying to call the .create() method throws an error.

In this case, I figured that it would be best to write my own key generator. However, I am now hoping that maybe someone here might know of a helper function that will allow me to generate a random API key, and then let me save it to the database manually myself.

Would anyone might know of any such a helper function?

Rodenhouse answered 7/6, 2012 at 22:55 Comment(0)
R
6

I figured it out.

First, you make an attempt to get the the user's API key. If it exists, then there will be no error thrown. To regenerate, set the value of the retrieved user's key to None, and then save the key.

If there was an error thrown, then simply create a new key.

try:
    api_key = ApiKey.objects.get(user=someuser)
    api_key.key = None
    api_key.save()

except ApiKey.DoesNotExist:
    api_key = ApiKey.objects.create(user=someuser)
Rodenhouse answered 7/6, 2012 at 23:27 Comment(1)
I'd move the api_key.key = None; api_key.save() out of the try block, possible into an else block, to be as precise as possible about what you're trying.Jacks
B
7

Or, you can just use tastypie's built-in command:

python manage.py backfill_api_keys
Brainchild answered 15/12, 2013 at 21:11 Comment(0)
R
6

I figured it out.

First, you make an attempt to get the the user's API key. If it exists, then there will be no error thrown. To regenerate, set the value of the retrieved user's key to None, and then save the key.

If there was an error thrown, then simply create a new key.

try:
    api_key = ApiKey.objects.get(user=someuser)
    api_key.key = None
    api_key.save()

except ApiKey.DoesNotExist:
    api_key = ApiKey.objects.create(user=someuser)
Rodenhouse answered 7/6, 2012 at 23:27 Comment(1)
I'd move the api_key.key = None; api_key.save() out of the try block, possible into an else block, to be as precise as possible about what you're trying.Jacks
P
5

Yes, the code for generating the key is defined as an instance method ApiKey.generate_key() which you can use directly.

Here's a simpler version that takes out some of the guesswork of whether the user already exists or not and uses ApiKey.generate_key() directly, rather than implicitly through ApiKey.save(), which I believe makes it a bit more clearer of what's trying to be accomplished:

api_key = ApiKey.objects.get_or_create(user=someuser)
api_key.key = api_key.generate_key()
api_key.save()

UPDATE:

Thus, the shortest version is:

return ApiKey.objects.get_or_create(user=someuser)[0].key

This will generate a new key or return an existing one.

Pyroelectric answered 7/6, 2012 at 23:38 Comment(1)
Careful with that. Your code will not work, since calling get_or_create returns a tuple formed by the ApiKey created object and a boolean value, not an ApiKey object. The key is created yet with your first sentence, so you don't need to call again the generate_key() method. You can just see the key generated by the first sentence by typing: api_key[0].key.Mathildamathilde
U
1

Based on Filip Dupanović's answer the working code for me was something like this:

user = get_user_model().objects.get(email="[email protected]")
api_key = ApiKey.objects.get_or_create(user=user)
api_key[0].key = api_key[0].generate_key()
api_key[0].save()
Unhand answered 28/12, 2020 at 7:51 Comment(0)
A
0

Its way too easy to use inbuilt functions, always. To generate Api keys in Tastypie use "create_api_key" of TastypiesApikeyAuthentication`.

you have to just import "create_api_key" from tastypie.models and then call it by django-signal or as per u require.

i.e.

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

Explained in detailed and easier at :

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

Airdrome answered 8/12, 2015 at 7:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.