Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.db.models import Q
- from django.contrib.auth.models import User
- from django.contrib.auth.backends import ModelBackend
- from piston.authentication import (OAuthAuthentication as
- PistonOAuthAuthentication, send_oauth_error)
- from piston import oauth
- from model_cache import ModelCache
- user_cache = ModelCache(User)
- class OAuthAuthentication(PistonOAuthAuthentication):
- """
- Replacement for piston.authentication.OAuthAuthentication that utilizes
- the cache for it's lookups
- """
- def is_authenticated(self, request):
- """
- piston.authentication.OAuthAuthentication sets request.user,
- request.consumer and request.throttle_extra from token.consumer.id.
- This little hack alleviates having to fetch `token.consumer` and
- `token.user` if the request does not need it.
- """
- if self.is_valid_request(request):
- try:
- consumer, token, params = self.validate_token(request)
- except oauth.OAuthError, err:
- print send_oauth_error(err)
- return False
- if consumer and token:
- #import pdb; pdb.set_trace()
- request.user = user_cache(Q(pk=token.user_id))
- request.consumer = consumer
- request.token = token
- request.throttle_extra = consumer.id
- return True
- return False
- class CachedUserBackend(ModelBackend):
- """
- A django.contrib.auth.backends.ModelBackend that gets it's user from the
- cache if possible
- """
- def authenticate(self, username=None, password=None):
- try:
- user = user_cache(Q(username=username))
- if user.check_password(password):
- return user
- except User.DoesNotExist:
- return None
Add Comment
Please, Sign In to add comment