From: Daniel G. Taylor Date: Thu, 6 Oct 2011 14:07:17 +0000 (-0400) Subject: Add support for Dropbox OAuth so that users can associate their account with Dropbox... X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=d6dca85feb255213ea4bc9a07fbb98859332d3f0;p=django-social-auth.git Add support for Dropbox OAuth so that users can associate their account with Dropbox and allow getting / putting files easily --- diff --git a/README.rst b/README.rst index a202ebd..6fe9191 100644 --- a/README.rst +++ b/README.rst @@ -42,6 +42,7 @@ credentials, some features are: * `Linkedin OAuth`_ * `Foursquare OAuth2`_ * `GitHub OAuth`_ + * `Drobpox OAuth`_ - Basic user data population and signaling, to allows custom fields values from providers response @@ -114,6 +115,7 @@ Configuration 'social_auth.backends.contrib.orkut.OrkutBackend', 'social_auth.backends.contrib.foursquare.FoursquareBackend', 'social_auth.backends.contrib.github.GithubBackend', + 'social_auth.backends.contrib.dropbox.DropboxBackend', 'social_auth.backends.OpenIDBackend', 'django.contrib.auth.backends.ModelBackend', ) @@ -154,6 +156,8 @@ Configuration FOURSQUARE_CONSUMER_SECRET = '' GITHUB_APP_ID = '' GITHUB_API_SECRET = '' + DROPBOX_APP_ID = '' + DROPBOX_API_SECRET = '' - Setup login URLs:: @@ -561,6 +565,18 @@ GitHub works similar to Facebook (OAuth). GITHUB_EXTENDED_PERMISSIONS = [...] +------- +Dropbox +------- +Dropbox uses OAuth v1.0 for authentication. + +- Register a new application at `Dropbox Developers`_, and + +- fill ``App Key`` and ``App Secret`` values in the settings:: + + DROPBOX_APP_ID = '' + DROPBOX_API_SECRET = '' + ------- Testing ------- @@ -749,3 +765,5 @@ Base work is copyrighted by: .. _GitHub OAuth: http://developer.github.com/v3/oauth/ .. _GitHub Developers: https://github.com/account/applications/new .. _djangopackages.com: http://djangopackages.com/grids/g/social-auth-backends/ +.. _Dropbox OAuth: https://www.dropbox.com/developers_beta/reference/api +.. _Dropbox Developers: https://www.dropbox.com/developers/apps diff --git a/social_auth/backends/contrib/dropbox.py b/social_auth/backends/contrib/dropbox.py new file mode 100644 index 0000000..ee58ca8 --- /dev/null +++ b/social_auth/backends/contrib/dropbox.py @@ -0,0 +1,73 @@ +""" +Dropbox OAuth support. + +This contribution adds support for Dropbox OAuth service. The settings +DROPBOX_APP_ID and DROPBOX_API_SECRET must be defined with the values +given by Dropbox application registration process. + +By default account id and token expiration time are stored in extra_data +field, check OAuthBackend class for details on how to extend it. +""" +from django.conf import settings +from django.utils import simplejson + +from social_auth.backends import ConsumerBasedOAuth, OAuthBackend, USERNAME + +# Dropbox configuration +DROPBOX_SERVER = 'dropbox.com' +DROPBOX_API = 'api.%s' % DROPBOX_SERVER +DROPBOX_REQUEST_TOKEN_URL = 'https://%s/1/oauth/request_token' % DROPBOX_API +DROPBOX_AUTHORIZATION_URL = 'https://www.%s/1/oauth/authorize' % DROPBOX_SERVER +DROPBOX_ACCESS_TOKEN_URL = 'https://%s/1/oauth/access_token' % DROPBOX_API +EXPIRES_NAME = getattr(settings, 'SOCIAL_AUTH_EXPIRATION', 'expires') + + +class DropboxBackend(OAuthBackend): + """Dropbox OAuth authentication backend""" + name = 'dropbox' + # Default extra data to store + EXTRA_DATA = [('id', 'id'), ('expires', EXPIRES_NAME)] + + def get_user_details(self, response): + """Return user details from Dropbox account""" + print response + return {USERNAME: response.get('uid'), + 'email': response.get('email'), + 'first_name': response.get('display_name')} + + def get_user_id(self, details, response): + """OAuth providers return an unique user id in response""" + # Dropbox uses a uid parameter instead of id like most others... + return response['uid'] + +class DropboxAuth(ConsumerBasedOAuth): + """Dropbox OAuth authentication mechanism""" + AUTHORIZATION_URL = DROPBOX_AUTHORIZATION_URL + REQUEST_TOKEN_URL = DROPBOX_REQUEST_TOKEN_URL + ACCESS_TOKEN_URL = DROPBOX_ACCESS_TOKEN_URL + SERVER_URL = DROPBOX_API + AUTH_BACKEND = DropboxBackend + SETTINGS_KEY_NAME = 'DROPBOX_APP_ID' + SETTINGS_SECRET_NAME = 'DROPBOX_API_SECRET' + + def user_data(self, access_token): + """Loads user data from service""" + url = 'https://' + DROPBOX_API + '/1/account/info' + request = self.oauth_request(access_token, url) + response = self.fetch_response(request) + try: + return simplejson.loads(response) + except ValueError: + return None + + @classmethod + def enabled(cls): + """Return backend enabled status by checking basic settings""" + return all(hasattr(settings, name) for name in + ('DROPBOX_APP_ID', + 'DROPBOX_API_SECRET')) + +# Backend definition +BACKENDS = { + 'dropbox': DropboxAuth, +}