Login

django-tokenapi authentication for django-piston

Author:
yurtaev
Posted:
November 7, 2011
Language:
Python
Version:
1.3
Tags:
django piston tokenapi
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. Django authentication for django-piston by chronos 4 years, 5 months ago
  2. Support alternative authentication mechanisms with Piston by erikwright 5 years, 1 month ago
  3. Digest authentication for Piston by erikwright 5 years, 1 month ago
  4. django piston use origin django auth by lettoo 3 years, 1 month ago
  5. Django BasicAuthMixin by icrni 9 months ago

Comments

Please login first before commenting.