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

You are doing good for your kids that each of them have unique username and password to watch videos which they watch their favorite section. But if you are not able to change their mind then you can get writing helpers to manage her issues. I recommended you to join for the best quality work. Thank you!

Great  post, you have pointed out some  fantastic  points , I  likewise  think  this s a very  wonderful website

Parc Life

I respect what you have done here. I like the part where you say you are doing this to give back however I would expect by every one of the remarks this is working for you also. Nairabargain

You are doing helpful for your youngsters that each of them have exceptional username and watchword to watch accounts which they watch their most cherished portion. In any case, Best Essay Service in case you are not prepared to adjust their sentiment then you can propel associate to manage her issues. I recommended you to join for the best quality work.

I'm only also satisfied to enjoy my next Starbucks peppermint mocha gratis if Microsoft wants to approach folks to utilize its Bing search engine as well as Edge browser. That would certainly leave you sufficient points to buy you $10 of complimentary Amazon.com credit rating with some points left over.

Debemos discutir el sentido comun, y lo que dice sobre la pornografia, y la sexualidad moderna en general. Debido a que el sentido comun, y esos sentimientos intuitivos de rectitud y equivocacion son la línea divisoria entre las personas que estan cada vez mas preocupadas por los peligros crecientes de la pornografia en Internet y los que sostienen que los efectos de la pornografia son minimos o incluso positivos. chicas xxx

trex game
I want you to thank for your time of this wonderful read!!! I definately enjoy every little bit of it and I have you bookmarked to check out new stuff of your blog a must read blog!

Mowgli (Neel Sethi) who was raised since his birth in the middle of the wolves was made to leave the jungle due to a fearsome tiger Shere Khan, who consistently remained after him to attack. showbox app

Nowadays, it is all possible with the application of Showbox, as it proves an unlimited collection of movies and TV shows, from which the user can select his favorite choice of media. Showbox

It does not matter which iPhone/iPad you have and the version of iOS you are running right now. Any of these methods will help you install MovieBox and enjoy the unlimited movie streaming and downloads. Movie Box

This was nice and impressive post i read it and it was incredible thank you so much for sharing this kind of info with us you are so good and amazing with your work. I appreciate that thank you. Judi Togel

The announcement follows the recent deal between Disney and Hotstar that announced last month. The latter gives Hotstar access to films made under four different studios - Marvel, Pixar, Lucasfilm, and Disney itself - plus television content thanks to Disney's ownership of ABC Studios. hotstar movies

Very informative and understandable. I think this is an informative post and it is very useful and knowledgeable. I hope you will continue posting new article! website

A good presentation of information, I think it will be clear to both beginners and more advanced people. Thank you for such uniqueness of the articles 192.168.0.1

As a regular reader of your blog, i would like to say you, i feel so bless to come here often your study material so good and beneficial for me and other member of your blog. I appreciate it. Keep writing!

Ripped T-Rex/Dino game of Chromium. ... to jump your Dino and start the game. Sorry, this game only runs on the Google Chrome! You can download it free here.
trex game

It is superb blog and I really appreciate your blog. It is because I always like the informative blogs. You did a great job and thanks for sharing. webdesign

Is it accurate to say that you are attempting to get an android application which streams you most recent and old movie (TV appears) for nothing? Playbox

Did you know that 84% of people decide to order something or not after reading reviews online according to the survey made by BrightLocal. It means they look for common complaints connected with the brand or product they are concerned about. Such a situation is seen in the essay writing service niche and any other area as well. It is logical, but the problem is a fake feedback. If you do not want to be fooled, read the ultius review.

Slack has this element Enchantment interface which enables the client to login into the slack application without a watchword. This Passwordless login framework is exceptionally helpful for end clients on the grounds that the client doesn't need to recollect and sort the watchword.
write my essay for me uk

Add new comment