]> git.parisson.com Git - django-social-auth.git/commitdiff
Add support for Dropbox OAuth so that users can associate their account with Dropbox...
authorDaniel G. Taylor <dan@programmer-art.org>
Thu, 6 Oct 2011 14:07:17 +0000 (10:07 -0400)
committerDaniel G. Taylor <dan@programmer-art.org>
Thu, 6 Oct 2011 14:07:17 +0000 (10:07 -0400)
README.rst
social_auth/backends/contrib/dropbox.py [new file with mode: 0644]

index a202ebd92a4ea068fd3bad0f31aaf6551734a4f5..6fe9191dd111a18a89f0d892e50edd4da7b65549 100644 (file)
@@ -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 (file)
index 0000000..ee58ca8
--- /dev/null
@@ -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,
+}