Skip to content

Instantly share code, notes, and snippets.

@vmihailenco
Forked from zyegfryed/gist:814432
Created June 15, 2011 17:49
Show Gist options
  • Save vmihailenco/1027648 to your computer and use it in GitHub Desktop.
Save vmihailenco/1027648 to your computer and use it in GitHub Desktop.
Tastypie OAuth
import logging
from tastypie.authentication import Authentication
import oauth2
from oauth_provider.consts import OAUTH_PARAMETERS_NAMES
from oauth_provider.store import store
from oauth_provider.store import Error as OAuthError
from oauth_provider.utils import get_oauth_request
from oauth_provider.utils import get_oauth_server
logger = logging.getLogger('tastypie.oauth')
class OAuthException(OAuthError):
pass
class OAuthChecker(object):
def __init__(self):
self.oauth_server = get_oauth_server()
def check(self, oauth_request):
for p in OAUTH_PARAMETERS_NAMES:
if not p in oauth_request:
raise OAuthException('Missing request parameter: %s' % p)
consumer = store.get_consumer(None, oauth_request,
oauth_request.get_parameter('oauth_consumer_key'))
token = store.get_access_token(None, oauth_request,
consumer, oauth_request.get_parameter('oauth_token'))
try:
self.oauth_server.verify_request(oauth_request, consumer, token)
except oauth2.Error, exc:
raise OAuthException(exc)
return token.user
oauth_checker = OAuthChecker()
class OAuthAuthentication(Authentication):
def is_authenticated(self, request, **kwargs):
oauth_request = get_oauth_request(request)
if not oauth_request:
logger.debug('Can not create oAuth request: not enough arguments')
return False
try:
oauth_checker.check(oauth_request)
except Exception, exc:
logger.debug('oAuth exception: %s' % exc, exc_info=True)
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment