django-tokenapi authentication 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. Django authentication for django-piston by chronos 2 years, 8 months ago
  2. Support alternative authentication mechanisms with Piston by erikwright 3 years, 4 months ago
  3. django piston use origin django auth by lettoo 1 year, 3 months ago
  4. Digest authentication for Piston by erikwright 3 years, 4 months ago
  5. Decorator for authenticating token based API calls by jpulgarin 2 years, 4 months ago

Comments

(Forgotten your password?)