From: ryr Date: Thu, 18 Aug 2011 11:23:32 +0000 (+0700) Subject: Merge remote-tracking branch 'upstream/master' X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=69f1ccbe88605d1c8296cb0201596a76bb86d62f;p=django-social-auth.git Merge remote-tracking branch 'upstream/master' Conflicts: social_auth/__init__.py social_auth/backends/contrib/vkontakte.py social_auth/views.py --- 69f1ccbe88605d1c8296cb0201596a76bb86d62f diff --cc social_auth/backends/contrib/vkontakte.py index edfe22b,7c17a24..9b865c7 --- a/social_auth/backends/contrib/vkontakte.py +++ b/social_auth/backends/contrib/vkontakte.py @@@ -16,7 -16,12 +16,10 @@@ from time import tim from social_auth.backends import SocialAuthBackend, OAuthBackend, BaseAuth, BaseOAuth2, USERNAME - VKONTAKTE_API_URL = 'https://api.vkontakte.ru/method/' -VKONTAKTE_LOCAL_HTML = 'vkontakte.html' - + VKONTAKTE_API_URL = 'https://api.vkontakte.ru/method/' + VKONTAKTE_SERVER_API_URL = 'http://api.vkontakte.ru/api.php' + VKONTAKTE_API_VERSION = '3.0' + VKONTAKTE_OAUTH2_SCOPE = [''] # Enough for authentication EXPIRES_NAME = getattr(settings, 'SOCIAL_AUTH_EXPIRATION', 'expires') @@@ -30,7 -34,7 +33,7 @@@ class VKontakteBackend(SocialAuthBacken def get_user_id(self, details, response): """Return user unique id provided by VKontakte""" return int(response.GET['id']) -- ++ def get_user_details(self, response): """Return user details from VKontakte request""" nickname = unquote(response.GET['nickname']) @@@ -47,17 -51,28 +50,28 @@@ class VKontakteOAuth2Backend(OAuthBacke def get_user_id(self, details, response): """Return user unique id provided by VKontakte""" return int(response['user_id']) -- ++ def get_user_details(self, response): """Return user details from VKontakte request""" - values = { USERNAME: str(response['user_id']), 'email': '', 'fullname': unquote(response['response']['user_name']), - 'first_name': '', 'last_name': ''} - - if ' ' in values['fullname']: - values['first_name'], values['last_name'] = values['fullname'].split() - else: - values['first_name'] = values['fullname'] - + values = { USERNAME: str(response['user_id']), 'email': ''} + + details = response['response'] + user_name = details.get('user_name') + + if user_name: + values['fullname'] = unquote(user_name) + + if ' ' in values['fullname']: + values['first_name'], values['last_name'] = values['fullname'].split() + else: + values['first_name'] = values['fullname'] + + if 'last_name' in details: + values['last_name'] = unquote(details['last_name']) + + if 'first_name' in details: + values['first_name'] = unquote(details['first_name']) + return values @@@ -65,34 -80,34 +79,34 @@@ class VKontakteAuth(BaseAuth) """VKontakte OpenAPI authorization mechanism""" AUTH_BACKEND = VKontakteBackend APP_ID = settings.VKONTAKTE_APP_ID -- ++ def auth_html(self): """Returns local VK authentication page, not necessary for VK to authenticate """ from django.core.urlresolvers import reverse from django.template import RequestContext, loader -- ++ dict = { 'VK_APP_ID' : self.APP_ID, 'VK_COMPLETE_URL': reverse(settings.SOCIAL_AUTH_COMPLETE_URL_NAME, args=[VKontakteBackend.name]) } -- - vk_template = loader.get_template(VKONTAKTE_LOCAL_HTML) ++ + vk_template = loader.get_template(LOCAL_HTML) context = RequestContext(self.request, dict) -- ++ return vk_template.render(context) -- ++ def auth_complete(self, *args, **kwargs): """Performs check of authentication in VKontakte, returns User if succeeded""" app_cookie = 'vk_app_' + self.APP_ID -- ++ if not 'id' in self.request.GET or not app_cookie in self.request.COOKIES: raise ValueError('VKontakte authentication is not completed') -- ++ cookie_dict = dict(item.split('=') for item in self.request.COOKIES[app_cookie].split('&')) check_str = ''.join([item + '=' + cookie_dict[item] for item in ['expire', 'mid', 'secret', 'sid']]) -- ++ hash = md5(check_str + settings.VKONTAKTE_APP_SECRET).hexdigest() -- ++ if hash != cookie_dict['sig'] or int(cookie_dict['expire']) < time() : -- raise ValueError('VKontakte authentication failed: invalid hash') ++ raise ValueError('VKontakte authentication failed: invalid hash') else: kwargs.update({'response': self.request, self.AUTH_BACKEND.name: True}) return authenticate(*args, **kwargs) @@@ -104,7 -119,7 +118,7 @@@ Their current implementation is just an example""" return False -- ++ class VKontakteOAuth2(BaseOAuth2): """VKontakte OAuth2 support""" AUTH_BACKEND = VKontakteOAuth2Backend @@@ -136,14 -151,28 +150,28 @@@ def user_data(self, access_token): """Return user data from VKontakte API""" data = {'access_token': access_token } -- ++ return vkontakte_api('getUserInfoEx', data) - def is_app_user(self, access_token): - """Returs app usage flag from VKontakte API""" - data = {'access_token': access_token } + def user_profile(self, user_id, access_token = None): + data = {'uids': user_id, 'fields': 'photo'} + + if access_token: + data['access_token'] = access_token + + profiles = vkontakte_api('getProfiles', data).get('response', None) + + return profiles[0] if profiles else None + + def is_app_user(self, user_id, access_token = None): + """Returns app usage flag from VKontakte API""" + + data = {'uid': user_id} - return vkontakte_api('isAppUser', data)['response'] + if access_token: + data['access_token'] = access_token + + return vkontakte_api('isAppUser', data).get('response', 0) def application_auth(self): required_params = ('is_app_user', 'viewer_id', 'access_token', 'api_id', ) @@@ -181,7 -206,7 +205,7 @@@ def vkontakte_api(method, data): """ Calls VKontakte OpenAPI method -- http://vkontakte.ru/apiclub, ++ http://vkontakte.ru/apiclub, http://vkontakte.ru/pages.php?o=-1&p=%C2%FB%EF%EE%EB%ED%E5%ED%E8%E5%20%E7%E0%EF%F0%EE%F1%EE%E2%20%EA%20API """ @@@ -197,4 -242,4 +241,3 @@@ BACKENDS = 'vkontakte': VKontakteAuth, 'vkontakte-oauth2': VKontakteOAuth2 } -- diff --cc social_auth/views.py index 8f88d9d,2ab18f4..7947b58 --- a/social_auth/views.py +++ b/social_auth/views.py @@@ -143,4 -167,4 +167,4 @@@ def auth_complete(request, backend, use error_key = getattr(settings, 'SOCIAL_AUTH_ERROR_KEY', None) if error_key: # store error in session request.session[error_key] = str(e) -- return user ++ return user