-
Notifications
You must be signed in to change notification settings - Fork 1
/
DjangoCookieAuth.py
21 lines (19 loc) · 1005 Bytes
/
DjangoCookieAuth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from django.contrib.auth.models import User
from tastypie.authentication import BasicAuthentication
class DjangoCookieBasicAuthentication(BasicAuthentication):
'''
If the user is already authenticated by a django session it will
allow the request (useful for ajax calls) . If it is not, defaults
to basic authentication, which other clients could use.
'''
def __init__(self, *args, **kwargs):
super(DjangoCookieBasicAuthentication, self).__init__(*args, **kwargs)
def is_authenticated(self, request, **kwargs):
from django.contrib.sessions.models import Session
if 'sessionid' in request.COOKIES:
s = Session.objects.get(pk=request.COOKIES['sessionid'])
if '_auth_user_id' in s.get_decoded():
u = User.objects.get(id=s.get_decoded()['_auth_user_id'])
request.user = u
return True
return super(DjangoCookieBasicAuthentication, self).is_authenticated(request, **kwargs)