]> git.parisson.com Git - django-social-auth.git/commitdiff
Add support Vkontakte.
authorSlava Bacherikov <slava@bacher09.org>
Fri, 13 Apr 2012 14:46:42 +0000 (17:46 +0300)
committerSlava Bacherikov <slava@bacher09.org>
Fri, 13 Apr 2012 14:46:42 +0000 (17:46 +0300)
example/local_settings.py.template
example/settings.py
social_auth/backends/contrib/vkontakte.py [new file with mode: 0644]

index 7ac26354fe4fa86d8bb950923dc347f3d7f62567..6eaf211b3ce96399986ce8e32b628e04ddb3ee4e 100644 (file)
@@ -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',
index 005ff2ee2a90ea35d3221f60fffae2178b3b93fe..021a3428cedb0ba0184c3aaf424f0294a2aa5984 100644 (file)
@@ -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 (file)
index 0000000..1511bf7
--- /dev/null
@@ -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,
+}