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::
- revolunet_ (Julien Bouquillon)
- - Github support
+ - GitHub support
----------
Copyrights
.. _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
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.
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'
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"""
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,
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"""