From: Matías Aguirre Date: Fri, 13 Apr 2012 23:35:16 +0000 (-0300) Subject: Flexible arguments on user_data() method. Refs #313. X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=496e6099880d6f6837acf5ee784c0d0349fd94d9;p=django-social-auth.git Flexible arguments on user_data() method. Refs #313. --- diff --git a/social_auth/backends/__init__.py b/social_auth/backends/__init__.py index bf7767d..48f6320 100644 --- a/social_auth/backends/__init__.py +++ b/social_auth/backends/__init__.py @@ -616,7 +616,7 @@ class ConsumerBasedOAuth(BaseOAuth): request = self.oauth_request(token, self.ACCESS_TOKEN_URL) return Token.from_string(self.fetch_response(request)) - def user_data(self, access_token): + def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" raise NotImplementedError('Implement in subclass') @@ -698,16 +698,8 @@ class BaseOAuth2(BaseOAuth): error = response.get('error_description') or response.get('error') raise AuthFailed(self, error) else: - try: - argnum = self.user_data.im_func.func_code.co_argcount - except AttributeError: - argnum = 2 - finally: - user_data_args = [response['access_token']] - if argnum == 3: - user_data_args.append(response) - - response.update(self.user_data(*user_data_args) or {}) + data = self.user_data(response['access_token'], response) + response.update(data or {}) kwargs.update({ 'auth': self, 'response': response, diff --git a/social_auth/backends/contrib/dropbox.py b/social_auth/backends/contrib/dropbox.py index 2e4a9cd..abdf6ce 100644 --- a/social_auth/backends/contrib/dropbox.py +++ b/social_auth/backends/contrib/dropbox.py @@ -53,7 +53,7 @@ class DropboxAuth(ConsumerBasedOAuth): SETTINGS_KEY_NAME = 'DROPBOX_APP_ID' SETTINGS_SECRET_NAME = 'DROPBOX_API_SECRET' - def user_data(self, access_token): + def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" url = 'https://' + DROPBOX_API + '/1/account/info' request = self.oauth_request(access_token, url) diff --git a/social_auth/backends/contrib/fitbit.py b/social_auth/backends/contrib/fitbit.py index 4c7539c..b60d6b3 100644 --- a/social_auth/backends/contrib/fitbit.py +++ b/social_auth/backends/contrib/fitbit.py @@ -72,7 +72,7 @@ class FitbitAuth(ConsumerBasedOAuth): else None return token - def user_data(self, access_token): + def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" return { 'id': access_token.user_nsid, diff --git a/social_auth/backends/contrib/flickr.py b/social_auth/backends/contrib/flickr.py index 9d4e467..4487d58 100644 --- a/social_auth/backends/contrib/flickr.py +++ b/social_auth/backends/contrib/flickr.py @@ -73,7 +73,7 @@ class FlickrAuth(ConsumerBasedOAuth): else None return token - def user_data(self, access_token): + def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" return { 'id': access_token.user_nsid, diff --git a/social_auth/backends/contrib/foursquare.py b/social_auth/backends/contrib/foursquare.py index 369bd6b..23409cb 100644 --- a/social_auth/backends/contrib/foursquare.py +++ b/social_auth/backends/contrib/foursquare.py @@ -38,7 +38,7 @@ class FoursquareAuth(BaseOAuth2): SETTINGS_KEY_NAME = 'FOURSQUARE_CONSUMER_KEY' SETTINGS_SECRET_NAME = 'FOURSQUARE_CONSUMER_SECRET' - def user_data(self, access_token): + def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" params = {'oauth_token': access_token} url = FOURSQUARE_CHECK_AUTH + '?' + urllib.urlencode(params) diff --git a/social_auth/backends/contrib/github.py b/social_auth/backends/contrib/github.py index bd0b960..84d6586 100644 --- a/social_auth/backends/contrib/github.py +++ b/social_auth/backends/contrib/github.py @@ -91,7 +91,7 @@ class GithubAuth(BaseOAuth): }) return authenticate(*args, **kwargs) - def user_data(self, access_token): + def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" url = GITHUB_USER_DATA_URL + urlencode({ 'access_token': access_token diff --git a/social_auth/backends/contrib/instagram.py b/social_auth/backends/contrib/instagram.py index a40c0ee..faa2e82 100644 --- a/social_auth/backends/contrib/instagram.py +++ b/social_auth/backends/contrib/instagram.py @@ -38,7 +38,7 @@ class InstagramAuth(BaseOAuth2): SETTINGS_KEY_NAME = 'INSTAGRAM_CLIENT_ID' SETTINGS_SECRET_NAME = 'INSTAGRAM_CLIENT_SECRET' - def user_data(self, access_token): + def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" params = {'access_token': access_token} url = INSTAGRAM_CHECK_AUTH + '?' + urllib.urlencode(params) diff --git a/social_auth/backends/contrib/linkedin.py b/social_auth/backends/contrib/linkedin.py index 910a0a2..1e2a7f8 100644 --- a/social_auth/backends/contrib/linkedin.py +++ b/social_auth/backends/contrib/linkedin.py @@ -50,7 +50,7 @@ class LinkedinAuth(ConsumerBasedOAuth): SETTINGS_KEY_NAME = 'LINKEDIN_CONSUMER_KEY' SETTINGS_SECRET_NAME = 'LINKEDIN_CONSUMER_SECRET' - def user_data(self, access_token): + def user_data(self, access_token, *args, **kwargs): """Return user data provided""" fields_selectors = LINKEDIN_FIELD_SELECTORS + \ setting('LINKEDIN_EXTRA_FIELD_SELECTORS', []) diff --git a/social_auth/backends/contrib/orkut.py b/social_auth/backends/contrib/orkut.py index c7ed4d2..c2397b2 100644 --- a/social_auth/backends/contrib/orkut.py +++ b/social_auth/backends/contrib/orkut.py @@ -51,7 +51,7 @@ class OrkutAuth(BaseGoogleOAuth): SETTINGS_KEY_NAME = 'ORKUT_CONSUMER_KEY' SETTINGS_SECRET_NAME = 'ORKUT_CONSUMER_SECRET' - def user_data(self, access_token): + def user_data(self, access_token, *args, **kwargs): """Loads user data from Orkut service""" fields = ORKUT_DEFAULT_DATA if setting('ORKUT_EXTRA_DATA'): diff --git a/social_auth/backends/contrib/vkontakte.py b/social_auth/backends/contrib/vkontakte.py index baa8643..7026579 100644 --- a/social_auth/backends/contrib/vkontakte.py +++ b/social_auth/backends/contrib/vkontakte.py @@ -5,11 +5,9 @@ Vkontakte OAuth support. from urllib import urlencode, urlopen from django.utils import simplejson -from django.contrib.auth import authenticate from social_auth.utils import setting from social_auth.backends import BaseOAuth2, OAuthBackend, USERNAME -from social_auth.backends.exceptions import AuthFailed # Vkontakte configuration @@ -26,7 +24,7 @@ class VkontakteBackend(OAuthBackend): ('id', 'id'), ('expires', setting('SOCIAL_AUTH_EXPIRATION', 'expires')) ] - + def get_user_id(self, details, response): "OAuth providers return an unique user id in response""" return response['user_id'] @@ -34,7 +32,8 @@ class VkontakteBackend(OAuthBackend): def get_user_details(self, response): """Return user details from Vkontakte account""" print response - return {USERNAME: response.get('nickname') or response.get('screen_name') , + return {USERNAME: response.get('nickname') or \ + response.get('screen_name'), 'email': '', 'first_name': response.get('first_name'), 'last_name': response.get('last_name')} @@ -48,8 +47,8 @@ class VkontakteAuth(BaseOAuth2): AUTH_BACKEND = VkontakteBackend SETTINGS_KEY_NAME = 'VK_APP_ID' SETTINGS_SECRET_NAME = 'VK_API_SECRET' - - def user_data(self, access_token, response): + + def user_data(self, access_token, response, *args, **kwargs): """Loads user data from service""" params = {'access_token': access_token, 'fields': 'first_name,last_name,screen_name,nickname', @@ -67,7 +66,6 @@ class VkontakteAuth(BaseOAuth2): return setting('VK_EXTRA_SCOPE', []) - # Backend definition BACKENDS = { 'vkontakte': VkontakteAuth, diff --git a/social_auth/backends/facebook.py b/social_auth/backends/facebook.py index 0669340..ac7e56f 100644 --- a/social_auth/backends/facebook.py +++ b/social_auth/backends/facebook.py @@ -59,7 +59,7 @@ class FacebookAuth(BaseOAuth2): def get_scope(self): return setting('FACEBOOK_EXTENDED_PERMISSIONS', []) - def user_data(self, access_token): + def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" data = None params = setting('FACEBOOK_PROFILE_EXTRA_PARAMS', {}) diff --git a/social_auth/backends/google.py b/social_auth/backends/google.py index 2e0ccb6..e7d76ab 100644 --- a/social_auth/backends/google.py +++ b/social_auth/backends/google.py @@ -115,7 +115,7 @@ class BaseGoogleOAuth(ConsumerBasedOAuth): ACCESS_TOKEN_URL = ACCESS_TOKEN_URL SERVER_URL = GOOGLE_OAUTH_SERVER - def user_data(self, access_token): + def user_data(self, access_token, *args, **kwargs): """Loads user data from G service""" raise NotImplementedError('Implement in subclass') @@ -126,7 +126,7 @@ class GoogleOAuth(BaseGoogleOAuth): SETTINGS_KEY_NAME = 'GOOGLE_CONSUMER_KEY' SETTINGS_SECRET_NAME = 'GOOGLE_CONSUMER_SECRET' - def user_data(self, access_token): + def user_data(self, access_token, *args, **kwargs): """Return user data from Google API""" request = self.oauth_request(access_token, GOOGLEAPIS_EMAIL, {'alt': 'json'}) @@ -188,7 +188,7 @@ class GoogleOAuth2(BaseOAuth2): def get_scope(self): return GOOGLE_OAUTH2_SCOPE + setting('GOOGLE_OAUTH_EXTRA_SCOPE', []) - def user_data(self, access_token): + def user_data(self, access_token, *args, **kwargs): """Return user data from Google API""" return googleapis_profile(GOOGLEAPIS_PROFILE, access_token) diff --git a/social_auth/backends/twitter.py b/social_auth/backends/twitter.py index 9df1d17..d685017 100644 --- a/social_auth/backends/twitter.py +++ b/social_auth/backends/twitter.py @@ -70,7 +70,7 @@ class TwitterAuth(ConsumerBasedOAuth): SETTINGS_KEY_NAME = 'TWITTER_CONSUMER_KEY' SETTINGS_SECRET_NAME = 'TWITTER_CONSUMER_SECRET' - def user_data(self, access_token): + def user_data(self, access_token, *args, **kwargs): """Return user data provided""" request = self.oauth_request(access_token, TWITTER_CHECK_AUTH) json = self.fetch_response(request)