From: Matías Aguirre Date: Mon, 7 May 2012 23:42:17 +0000 (-0300) Subject: Move oauth request for consumer-based-oauth to snippet in utils. Refs #338 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=68885fa524b84b29bb8add8bdb0babd3f9490abd;p=django-social-auth.git Move oauth request for consumer-based-oauth to snippet in utils. Refs #338 --- diff --git a/social_auth/backends/__init__.py b/social_auth/backends/__init__.py index 959f398..54a933c 100644 --- a/social_auth/backends/__init__.py +++ b/social_auth/backends/__init__.py @@ -33,6 +33,7 @@ from social_auth.backends.exceptions import StopPipeline, AuthException, \ AuthFailed, AuthCanceled, \ AuthUnknownError, AuthTokenError, \ AuthMissingParameter +from social_auth.backends.utils import build_consumer_oauth_request if setting('SOCIAL_AUTH_USER_MODEL'): @@ -534,12 +535,13 @@ class BaseOAuth(BaseAuth): super(BaseOAuth, self).__init__(request, redirect) self.redirect_uri = self.build_absolute_uri(self.redirect) - def get_key_and_secret(self): + @classmethod + def get_key_and_secret(cls): """Return tuple with Consumer Key and Consumer Secret for current service provider. Must return (key, secret), order *must* be respected. """ - return setting(self.SETTINGS_KEY_NAME), \ - setting(self.SETTINGS_SECRET_NAME) + return setting(cls.SETTINGS_KEY_NAME), \ + setting(cls.SETTINGS_SECRET_NAME) @classmethod def enabled(cls): @@ -615,18 +617,10 @@ class ConsumerBasedOAuth(BaseOAuth): def oauth_request(self, token, url, extra_params=None): """Generate OAuth request, setups callback url""" - params = {'oauth_callback': self.redirect_uri} - if extra_params: - params.update(extra_params) - - if 'oauth_verifier' in self.data: - params['oauth_verifier'] = self.data['oauth_verifier'] - request = OAuthRequest.from_consumer_and_token(self.consumer, - token=token, - http_url=url, - parameters=params) - request.sign_request(SignatureMethod_HMAC_SHA1(), self.consumer, token) - return request + return build_consumer_oauth_request(self, token, url, + self.redirect_uri, + self.data.get('oauth_verifier'), + extra_params) def fetch_response(self, request): """Executes request and fetchs service response""" diff --git a/social_auth/backends/google.py b/social_auth/backends/google.py index b25f9eb..0d517fa 100644 --- a/social_auth/backends/google.py +++ b/social_auth/backends/google.py @@ -150,14 +150,15 @@ class GoogleOAuth(BaseGoogleOAuth): extra_params['xoauth_displayname'] = xoauth_displayname return super(GoogleOAuth, self).oauth_request(token, url, extra_params) - def get_key_and_secret(self): + @classmethod + def get_key_and_secret(cls): """Return Google OAuth Consumer Key and Consumer Secret pair, uses anonymous by default, beware that this marks the application as not registered and a security badge is displayed on authorization page. http://code.google.com/apis/accounts/docs/OAuth_ref.html#SigningOAuth """ try: - return super(GoogleOAuth, self).get_key_and_secret() + return super(GoogleOAuth, cls).get_key_and_secret() except AttributeError: return 'anonymous', 'anonymous' diff --git a/social_auth/backends/utils.py b/social_auth/backends/utils.py new file mode 100644 index 0000000..5607601 --- /dev/null +++ b/social_auth/backends/utils.py @@ -0,0 +1,48 @@ +from urllib2 import urlopen +from oauth2 import Consumer as OAuthConsumer, Token, Request as OAuthRequest, \ + SignatureMethod_HMAC_SHA1 + +from django.utils import simplejson + +from social_auth.models import User + + +def consumer_oauth_url_request(backend, url, user_or_id, json=True): + """Builds and retrieves an OAuth signed response.""" + if isinstance(user_or_id, User): + user = user_or_id + else: + user = User.objects.get(pk=user_or_id) + + oauth_info = user.social_auth.objects.filter( + provider=backend.AUTH_BACKEND.name + ) + if len(oauth_info) > 1: + oauth_info = oauth_info[0] + + token = Token.from_string(oauth_info.tokens['access_token']) + request = build_consumer_oauth_request(backend, token, url) + response = '\n'.join(urlopen(request.to_url()).readlines()) + + if json: + response = simplejson.loads(response) + return response + + +def build_consumer_oauth_request(backend, token, url, redirect_uri='/', + oauth_verifier=None, extra_params=None): + """Builds a Consumer OAuth request.""" + params = {'oauth_callback': redirect_uri} + if extra_params: + params.update(extra_params) + + if oauth_verifier: + params['oauth_verifier'] = oauth_verifier + + consumer = OAuthConsumer(*backend.get_key_and_secret()) + request = OAuthRequest.from_consumer_and_token(consumer, + token=token, + http_url=url, + parameters=params) + request.sign_request(SignatureMethod_HMAC_SHA1(), consumer, token) + return request