From 52f2b885960a5c2143cc0d6b9e03b5f909ede3a1 Mon Sep 17 00:00:00 2001 From: Slava Bacherikov Date: Fri, 13 Apr 2012 17:46:42 +0300 Subject: [PATCH] Add support Vkontakte. --- example/local_settings.py.template | 2 + example/settings.py | 1 + social_auth/backends/contrib/vkontakte.py | 74 +++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 social_auth/backends/contrib/vkontakte.py diff --git a/example/local_settings.py.template b/example/local_settings.py.template index 7ac2635..6eaf211 100644 --- a/example/local_settings.py.template +++ b/example/local_settings.py.template @@ -19,6 +19,8 @@ GITHUB_APP_ID = '' GITHUB_API_SECRET = '' FOURSQUARE_CONSUMER_KEY = '' FOURSQUARE_CONSUMER_SECRET = '' +VK_APP_ID = '' +VK_API_SECRET = '' SOCIAL_AUTH_PIPELINE = ( 'social_auth.backends.pipeline.social.social_auth_user', diff --git a/example/settings.py b/example/settings.py index 005ff2e..021a342 100644 --- a/example/settings.py +++ b/example/settings.py @@ -77,6 +77,7 @@ AUTHENTICATION_BACKENDS = ( 'social_auth.backends.contrib.linkedin.LinkedinBackend', 'social_auth.backends.contrib.flickr.FlickrBackend', 'social_auth.backends.contrib.instagram.InstagramBackend', + 'social_auth.backends.contrib.vkontakte.VkontakteBackend', 'social_auth.backends.OpenIDBackend', 'social_auth.backends.contrib.livejournal.LiveJournalBackend', 'social_auth.backends.browserid.BrowserIDBackend', diff --git a/social_auth/backends/contrib/vkontakte.py b/social_auth/backends/contrib/vkontakte.py new file mode 100644 index 0000000..1511bf7 --- /dev/null +++ b/social_auth/backends/contrib/vkontakte.py @@ -0,0 +1,74 @@ +""" +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 +VK_AUTHORIZATION_URL = 'http://oauth.vk.com/authorize' +VK_ACCESS_TOKEN_URL = 'https://oauth.vk.com/access_token' +VK_USER_DATA_URL = 'https://api.vk.com/method/users.get' +VK_SERVER = 'vk.com' + + +class VkontakteBackend(OAuthBackend): + """Vkontakte OAuth authentication backend""" + name = 'vkontakte' + EXTRA_DATA = [ + ('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'] + + def get_user_details(self, response): + """Return user details from Vkontakte account""" + print response + return {USERNAME: response.get('nickname') or response.get('screen_name') , + 'email': '', + 'first_name': response.get('first_name'), + 'last_name': response.get('last_name')} + + +class VkontakteAuth(BaseOAuth2): + """Vkontakte OAuth mechanism""" + AUTHORIZATION_URL = VK_AUTHORIZATION_URL + ACCESS_TOKEN_URL = VK_ACCESS_TOKEN_URL + SERVER_URL = VK_SERVER + AUTH_BACKEND = VkontakteBackend + SETTINGS_KEY_NAME = 'VK_APP_ID' + SETTINGS_SECRET_NAME = 'VK_API_SECRET' + + def user_data(self, access_token, response = None): + """Loads user data from service""" + params = {'access_token': access_token, + 'fields': 'first_name,last_name,screen_name,nickname', + 'uids': response.get('user_id') + } + url = VK_USER_DATA_URL + '?' + urlencode(params) + try: + return simplejson.load(urlopen(url)).get('response')[0] + except (ValueError, IndexError): + return None + + def get_scope(self): + """Return list with needed access scope""" + # Look at http://vk.com/developers.php?oid=-1&p=%D0%9F%D1%80%D0%B0%D0%B2%D0%B0_%D0%B4%D0%BE%D1%81%D1%82%D1%83%D0%BF%D0%B0_%D0%BF%D1%80%D0%B8%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D0%B9 + return setting('VK_EXTRA_SCOPE', []) + + + +# Backend definition +BACKENDS = { + 'vkontakte': VkontakteAuth, +} -- 2.39.5