]> git.parisson.com Git - django-social-auth.git/commitdiff
Rewrited Github auth backend to new api.
authorSlava Bacherikov <slava@bacher09.org>
Sun, 15 Apr 2012 12:57:17 +0000 (15:57 +0300)
committerSlava Bacherikov <slava@bacher09.org>
Sun, 15 Apr 2012 12:57:17 +0000 (15:57 +0300)
example/settings.py
social_auth/backends/__init__.py
social_auth/backends/contrib/github.py

index 021a3428cedb0ba0184c3aaf424f0294a2aa5984..877ed9aadbf6a2324896199eea3427b5fc089233 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.github.GithubBackend',
     'social_auth.backends.contrib.vkontakte.VkontakteBackend',
     'social_auth.backends.OpenIDBackend',
     'social_auth.backends.contrib.livejournal.LiveJournalBackend',
index 48f6320dac6daafd44dd6f9f489736151da99b71..45a679414948e492a23241509fce3b823f4a4dc7 100644 (file)
@@ -680,10 +680,11 @@ class BaseOAuth2(BaseOAuth):
                   'client_id': client_id,
                   'client_secret': client_secret,
                   'redirect_uri': self.redirect_uri}
-        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
+        headers = {'Content-Type': 'application/x-www-form-urlencoded',
+                    'Accept': 'application/json'}
         request = Request(self.ACCESS_TOKEN_URL, data=urlencode(params),
                           headers=headers)
-
+        
         try:
             response = simplejson.loads(urlopen(request).read())
         except HTTPError, e:
index 84d65861622d6c759571ffb6a9b51d23be2d8973..75d5c9d8a67b3b7bddf9f2d8c1eb2a3304e77b13 100644 (file)
@@ -18,14 +18,14 @@ from django.utils import simplejson
 from django.contrib.auth import authenticate
 
 from social_auth.utils import setting
-from social_auth.backends import BaseOAuth, OAuthBackend, USERNAME
-from social_auth.backends.exceptions import AuthFailed
+from social_auth.backends import BaseOAuth2, OAuthBackend, USERNAME
 
 
 # GitHub configuration
-GITHUB_AUTHORIZATION_URL = 'https://github.com/login/oauth/authorize?'
-GITHUB_ACCESS_TOKEN_URL = 'https://github.com/login/oauth/access_token?'
-GITHUB_USER_DATA_URL = 'https://api.github.com/user?'
+GITHUB_AUTHORIZATION_URL = 'https://github.com/login/oauth/authorize'
+GITHUB_ACCESS_TOKEN_URL = 'https://github.com/login/oauth/access_token'
+GITHUB_USER_DATA_URL = 'https://api.github.com/user'
+GITHUB_SERVER = 'github.com'
 
 
 class GithubBackend(OAuthBackend):
@@ -44,56 +44,24 @@ class GithubBackend(OAuthBackend):
                 'first_name': response.get('name')}
 
 
-class GithubAuth(BaseOAuth):
-    """Github OAuth mechanism"""
+class GithubAuth(BaseOAuth2):
+    """Github OAuth2 mechanism"""
+    AUTHORIZATION_URL = GITHUB_AUTHORIZATION_URL
+    ACCESS_TOKEN_URL = GITHUB_ACCESS_TOKEN_URL
+    SERVER_URL = GITHUB_SERVER
     AUTH_BACKEND = GithubBackend
-
-    def auth_url(self):
-        """Returns redirect url"""
-        args = {
-            'client_id': setting('GITHUB_APP_ID'),
-            'redirect_uri': self.redirect_uri
-        }
-        if setting('GITHUB_EXTENDED_PERMISSIONS'):
-            args['scope'] = ','.join(setting('GITHUB_EXTENDED_PERMISSIONS'))
-        args.update(self.auth_extra_arguments())
-        return GITHUB_AUTHORIZATION_URL + urlencode(args)
-
-    def auth_complete(self, *args, **kwargs):
-        """Returns user, might be logged in"""
-        if 'code' not in self.data:
-            error = self.data.get('error') or 'unknown error'
-            raise AuthFailed(self, error)
-
-        url = GITHUB_ACCESS_TOKEN_URL + urlencode({
-              'client_id': setting('GITHUB_APP_ID'),
-              'redirect_uri': self.redirect_uri,
-              'client_secret': setting('GITHUB_API_SECRET'),
-              'code': self.data['code']
-        })
-        response = cgi.parse_qs(urlopen(url).read())
-        if response.get('error'):
-            error = self.data.get('error') or 'unknown error'
-            raise AuthFailed(self, error)
-
-        access_token = response['access_token'][0]
-        data = self.user_data(access_token)
-        if data is not None:
-            if 'error' in data:
-                error = self.data.get('error') or 'unknown error'
-                raise AuthFailed(self, error)
-            data['access_token'] = access_token
-
-        kwargs.update({
-            'auth': self,
-            'response': data,
-            self.AUTH_BACKEND.name: True
-        })
-        return authenticate(*args, **kwargs)
-
+    SETTINGS_KEY_NAME = 'GITHUB_APP_ID'
+    SETTINGS_SECRET_NAME = 'GITHUB_API_SECRET'
+    SCOPE_SEPARATOR = ','
+
+    def get_scope(self):
+        """Return list with needed access scope"""
+        # Look at http://developer.github.com/v3/oauth/
+        return setting('GITHUB_EXTENDED_PERMISSIONS', [])
+    
     def user_data(self, access_token, *args, **kwargs):
         """Loads user data from service"""
-        url = GITHUB_USER_DATA_URL + urlencode({
+        url = GITHUB_USER_DATA_URL + '?' + urlencode({
             'access_token': access_token
         })
         try:
@@ -101,11 +69,6 @@ class GithubAuth(BaseOAuth):
         except ValueError:
             return None
 
-    @classmethod
-    def enabled(cls):
-        """Return backend enabled status by checking basic settings"""
-        return setting('GITHUB_APP_ID') and setting('GITHUB_API_SECRET')
-
 
 # Backend definition
 BACKENDS = {