Logging in to a Django site with a magic token

I have a simple video website for my kids and each kid has a separate login. This is so they can each have their own videos, but also so that some videos can be private (ie. hidden from the outside world, or other logged in users). Typing in a username and password is impossible for my kids to do, as they are almost 5 and 2 years old, and they use this website on Google TV. So, with a magic token-style login, all they need to do is navigate to their bookmark on the Google TV homepage and press OK on the remote control.

(I don't need crazy security--it wouldn't be the end of the world if somehow someone guessed the magic token and saw some private videos, which are basically just home videos uploaded to Youtube. Videos that I really wouldn't want the public to see don't get uploaded to Youtube in the first place.)

I couldn't find how to do this easily, although one person on stackoverflow suggested "logging in the user in the view by calling 'login'". The tricky part was figuring out that I had to set the User object's backend to 'django.contrib.auth.backends.ModelBackend'. It's a bit of a hack, but it works, and it's simple.

models.py:

class MagicToken(models.Model):
    user = models.OneToOneField(User)
    magictoken = models.CharField(max_length=128, unique=True)
 
    def __unicode__(self):
        return unicode(self.user)

views.py:

from django.http import HttpResponse, HttpResponseRedirect, Http404
import django.contrib.auth.login
 
class MagicTokenLogin(View):
    def get(self, request, token):
        try:
            magic_token_obj = MagicToken.objects.get(magictoken=token)
        except MagicToken.DoesNotExist:
            raise Http404
 
        user = magic_token_obj.user
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        django.contrib.auth.login(request, user)
        if request.user.is_authenticated():
            # login successful
            return HttpResponseRedirect(reverse('some-view-for-logged-in-users'))
        else:
            # login failed
            return HttpResponseRedirect(reverse('some-view'))

Comments

Controversies involving campaign finance and disclosure reports can get a little tricky sometimes, but this one is actually pretty straightforward: Land has given her Senate campaign nearly $3 million of her own money. What wrong with that? In theory, nothing there are no legal limits on how much candidates can spend on their own behalf.

Teachers should really help each other to justify their experience for getting new ideas.

Thanks for your post. I’ve been thinking about writing a very comparable post over the last couple of weeks, I’ll probably keep it short and sweet and link to this instead if thats cool. Thanks.
Movers in calgary

useful information on topics that plenty are interested on for this wonderful post.Admiring the time and effort you put into your b!..

This is a good post. This post gives truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. Thank you so much. Keep up the good works.
click here

I really impressed after read this because of some quality work and informative thoughts . I just wanna say thanks for the writer and wish you all the best for coming!.

click here

Keep up the good work , I read few posts on this web site and I conceive that your blog is very interesting and has sets of fantastic information.
Sarm

I have been following your blog for some time now and have found it quite informative and also interesting and you have very nice way of expressing the article.It's hard to discover fantastic composition like yours nowadays.Looking forward to another article.

I enjoyed over read your blog post. Your blog have nice information, I got good ideas from this amazing blog. I am always searching like this type blog post. I hope I will see again.

I think your suggestion would be helpful for me. I will let you know if this works for me Thanks and keep posting such a informative blog. 350-029 vce

Nice to see your new and magic token post. Its really very interesting post. I always like your hard working because your post gives us great ideas. Please keep posting. Heavy duty new playground tiles available in black colors

The post is written in a very good manner and it entails much useful information for me. I am happy to find your distinguished chaussures nike running france http://www.chaurunning.com/nike-c-1.html way of writing the post. I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day!

An interesting dialogue is price comment. I feel that it is best to write more on this matter, it may not be a taboo topic however usually individuals are not enough to talk on such topics. To the next. Cheers.
oddsportal

Our website is No. 1 in Computer Network Assignment Help and Computer Networking Project Help. Feel free to hire us for Computer Network Assignment Help - Computer Networking Project Help - Computer Network Homework Help

This article gives the light in which we can observe the reality. This is very nice one and gives in depth information. Thanks for this nice article.
Zentec

Excellent .. Amazing .. I’ll bookmark your blog and take the feeds also…I’m happy to find so many useful info here in the post, we need work out more techniques in this regard, thanks for sharing.
local plumber

It is mainly the blending of cultures, in the imaginative works of daddy time 350 million years back and also the produced rock formations colour striking red to the first human animals, amazing cave homes, modest homesteads and also imaginative homages like the Tlaquepaque shopping mall that make Sedona the primary one-of-a-kind area it's called. A trip to Sedona is most definitely an experience that will certainly not quickly be forgotten.

As a beginner it is a perfect platform your blogs really helps to understand me in a much easier way i regular participate on your website.
Read More

Pages

Add new comment