From 3a373f2af33cd416159dbc5fd6e82007a9d33bf7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mat=C3=ADas=20Aguirre?= Date: Mon, 22 Aug 2011 16:24:31 -0300 Subject: [PATCH] Small changes to github backend doc and code. Refs #122 --- README.rst | 18 +++++++++--------- doc/backends/github.rst | 17 +++++++++++++++++ doc/backends/index.rst | 1 + social_auth/backends/contrib/github.py | 23 ++++++++++------------- 4 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 doc/backends/github.rst diff --git a/README.rst b/README.rst index f9f4ca0..b18cfa8 100644 --- a/README.rst +++ b/README.rst @@ -526,17 +526,17 @@ way the values will be stored in ``UserSocialAuth.extra_data`` field. By default ``id``, ``first-name`` and ``last-name`` are requested and stored. --------- -Github --------- -Github works similar to Facebook (OAuth). +------ +GitHub +------ +GitHub works similar to Facebook (OAuth). -- Register a new application at `Github Developers`_, and +- Register a new application at `GitHub Developers`_, and - fill ``App Id`` and ``App Secret`` values in the settings:: - GITHUB_APP_ID - GITHUB_API_SECRET + GITHUB_APP_ID = '' + GITHUB_API_SECRET = '' - also it's possible to define extra permissions with:: @@ -649,7 +649,7 @@ Attributions to whom deserves: - revolunet_ (Julien Bouquillon) - - Github support + - GitHub support ---------- Copyrights @@ -722,4 +722,4 @@ Base work is copyrighted by: .. _Read the Docs: http://django-social-auth.readthedocs.org/ .. _revolunet: https://github.com/revolunet .. _GitHub OAuth: http://developer.github.com/v3/oauth/ -.. _github developers: https://github.com/account/applications/new +.. _GitHub Developers: https://github.com/account/applications/new diff --git a/doc/backends/github.rst b/doc/backends/github.rst new file mode 100644 index 0000000..ea46e46 --- /dev/null +++ b/doc/backends/github.rst @@ -0,0 +1,17 @@ +GitHub +====== +Github works similar to Facebook (OAuth). + +- Register a new application at `GitHub Developers`_, and + +- fill ``App Id`` and ``App Secret`` values in the settings:: + + GITHUB_APP_ID = '' + GITHUB_API_SECRET = '' + +- also it's possible to define extra permissions with:: + + GITHUB_EXTENDED_PERMISSIONS = [...] + + +.. _GitHub Developers: https://github.com/account/applications/new diff --git a/doc/backends/index.rst b/doc/backends/index.rst index d3fb25a..4cb8465 100644 --- a/doc/backends/index.rst +++ b/doc/backends/index.rst @@ -12,3 +12,4 @@ Contents: twitter facebook linkedin + github diff --git a/social_auth/backends/contrib/github.py b/social_auth/backends/contrib/github.py index d53801e..7c2e8b8 100644 --- a/social_auth/backends/contrib/github.py +++ b/social_auth/backends/contrib/github.py @@ -3,7 +3,7 @@ GitHub OAuth support. This contribution adds support for GitHub OAuth service. The settings GITHUB_APP_ID and GITHUB_API_SECRET must be defined with the values -given by Facebook application registration process. +given by GitHub application registration process. Extended permissions are supported by defining GITHUB_EXTENDED_PERMISSIONS setting, it must be a list of values to request. @@ -20,13 +20,15 @@ from django.contrib.auth import authenticate from social_auth.backends import BaseOAuth, OAuthBackend, USERNAME -# Facebook configuration + +# GitHub configuration GITHUB_SERVER = 'github.com' GITHUB_AUTHORIZATION_URL = 'https://%s/login/oauth/authorize' % GITHUB_SERVER GITHUB_ACCESS_TOKEN_URL = 'https://%s/login/oauth/access_token' % GITHUB_SERVER GITHUB_API_URL = 'https://api.%s' % GITHUB_SERVER EXPIRES_NAME = getattr(settings, 'SOCIAL_AUTH_EXPIRATION', 'expires') + class GithubBackend(OAuthBackend): """Github OAuth authentication backend""" name = 'github' @@ -35,11 +37,9 @@ class GithubBackend(OAuthBackend): def get_user_details(self, response): """Return user details from Github account""" - return { - 'username':response.get('login'), - 'email':response.get('email'), - 'firstname':response.get('name') - } + return {USERNAME: response.get('login'), + 'email': response.get('email'), + 'firstname': response.get('name')} class GithubAuth(BaseOAuth): """Github OAuth mechanism""" @@ -56,7 +56,6 @@ class GithubAuth(BaseOAuth): def auth_complete(self, *args, **kwargs): """Returns user, might be logged in""" if 'code' in self.data: - url = GITHUB_ACCESS_TOKEN_URL + '?' + \ urllib.urlencode({'client_id': settings.GITHUB_APP_ID, 'redirect_uri': self.redirect_uri, @@ -81,15 +80,13 @@ class GithubAuth(BaseOAuth): def user_data(self, access_token): """Loads user data from service""" - params = {'access_token': access_token,} - url = '%s/user' % GITHUB_API_URL + '?' + urllib.urlencode(params) + params = {'access_token': access_token} + url = GITHUB_API_URL + '/user?' + urllib.urlencode(params) try: - data = simplejson.load(urllib.urlopen(url)) - return data + return simplejson.load(urllib.urlopen(url)) except ValueError: return None - @classmethod def enabled(cls): """Return backend enabled status by checking basic settings""" -- 2.39.5