Created
May 7, 2012 05:26
-
-
Save samuraisam/2626088 to your computer and use it in GitHub Desktop.
An OAuth store for Piston that will cache your consumers and tokens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from hashlib import md5 | |
from django.db.models import Q | |
from django.core.cache import cache | |
from piston.models import Consumer, Token | |
from piston.store import DataStore as PistonDataStore | |
from model_cache import ModelCache | |
consumer_cache = ModelCache(Consumer) | |
token_cache = ModelCache(Token) | |
class CachedOAuthDataStore(PistonDataStore): | |
def lookup_consumer(self, key): | |
self.consumer = consumer_cache(Q(key=key)) | |
self.consumer.key = self.consumer.key.encode('ascii') | |
self.consumer.secret = self.consumer.secret.encode('ascii') | |
return self.consumer | |
def lookup_token(self, token_type, token): | |
if token_type == 'request': | |
token_type = Token.REQUEST | |
elif token_type == 'access': | |
token_type = Token.ACCESS | |
try: | |
self.request_token = token_cache(Q(token_type=token_type, | |
key=token)) | |
return self.request_token | |
except Token.DoesNotExist: | |
return None | |
def lookup_nonce(self, oauth_consumer, oauth_token, nonce): | |
bits = (oauth_consumer.key + oauth_consumer.secret | |
+ getattr(oauth_token, 'key', '') | |
+ getattr(oauth_token, 'secret', '') | |
+ nonce) | |
key = 'nonce' + md5(bits).hexdigest() | |
r = cache.get(key) | |
if not r: | |
cache.set(key, 'v', 60*60*24) | |
return None | |
return r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment