Login

django-tokenapi authentication for django-piston

Author:
yurtaev
Posted:
November 7, 2011
Language:
Python
Version:
1.3
Score:
0 (after 0 ratings)

Support authentication https://github.com/jpulgarin/django-tokenapi for django-piston

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from django.contrib.auth import authenticate
from django.contrib.auth.models import AnonymousUser
from django.http import HttpResponse

class TokenAPIAuthentication(object):
    def __init__(self, auth_func=authenticate, realm='API'):
        self.auth_func = auth_func
        self.realm = realm

    def is_authenticated(self, request):
    	user_pk = request.POST.get("user") or request.GET.get("user")
    	token = request.POST.get("token") or request.GET.get("token")

        if not user_pk or not token:
            return False

        request.user = self.auth_func(pk=user_pk, token=token) or AnonymousUser()

        return not request.user in (False, None, AnonymousUser())

    def challenge(self):
        resp = HttpResponse("Authorization Required")
        resp['WWW-Authenticate'] = 'Basic realm="%s"' % self.realm
        resp.status_code = 401
        return resp

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.