[Python-modules-commits] [python-social-auth] 294/322: Add a DigitalOcean backend.

Wolfgang Borgert debacle at moszumanska.debian.org
Sat Dec 24 15:13:21 UTC 2016


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to tag v0.2.10
in repository python-social-auth.

commit 04f5ff30222970e9ccf618ec480eec00448a2fcb
Author: Andrew Starr-Bochicchio <a.starr.b at gmail.com>
Date:   Sat May 16 14:47:23 2015 -0400

    Add a DigitalOcean backend.
---
 README.rst                                 |  2 ++
 docs/backends/digitalocean.rst             | 24 +++++++++++++++++
 docs/backends/index.rst                    |  1 +
 social/backends/digitalocean.py            | 41 ++++++++++++++++++++++++++++++
 social/tests/backends/test_digitalocean.py | 34 +++++++++++++++++++++++++
 5 files changed, 102 insertions(+)

diff --git a/README.rst b/README.rst
index 73f6cf7..ef2e992 100644
--- a/README.rst
+++ b/README.rst
@@ -63,6 +63,7 @@ or current ones extended):
     * Clef_ OAuth2
     * Coursera_ OAuth2
     * Dailymotion_ OAuth2
+    * DigitalOcean_ OAuth2 https://developers.digitalocean.com/documentation/oauth/
     * Disqus_ OAuth2
     * Douban_ OAuth1 and OAuth2
     * Dropbox_ OAuth1 and OAuth2
@@ -240,6 +241,7 @@ check `django-social-auth LICENSE`_ for details:
 .. _Clef: https://getclef.com/
 .. _Coursera: https://www.coursera.org/
 .. _Dailymotion: https://dailymotion.com
+.. _DigitalOcean: https://www.digitalocean.com/
 .. _Disqus: https://disqus.com
 .. _Douban: http://www.douban.com
 .. _Dropbox: https://dropbox.com
diff --git a/docs/backends/digitalocean.rst b/docs/backends/digitalocean.rst
new file mode 100644
index 0000000..f50f413
--- /dev/null
+++ b/docs/backends/digitalocean.rst
@@ -0,0 +1,24 @@
+DigitalOcean
+============
+
+DigitalOcean uses OAuth2 for its auth process. See the full `DigitalOcean
+developer's documentation`_ for more information.
+
+- Register a new application in the `Apps & API page`_ in the DigitalOcean
+  control panel, setting the callback URL to ``http://example.com/complete/digitalocean/``
+  replacing ``example.com`` with your domain.
+
+- Fill the ``Client ID`` and ``Client Secret`` values from GitHub in the settings::
+
+      SOCIAL_AUTH_DIGITALOCEAN_KEY = ''
+      SOCIAL_AUTH_DIGITALOCEAN_SECRET = ''
+
+- By default, only ``read`` permissions are granted. In order to create,
+  destroy, and take other actions on the user's resources, you must request
+  ``read write`` permissions like so::
+
+      SOCIAL_AUTH_DIGITALOCEAN_AUTH_EXTRA_ARGUMENTS = {'scope': 'read write'}
+
+
+.. _DigitalOcean developer's documentation: https://developers.digitalocean.com/documentation/
+.. _Apps & API page: https://cloud.digitalocean.com/settings/applications
\ No newline at end of file
diff --git a/docs/backends/index.rst b/docs/backends/index.rst
index d42e414..0de8564 100644
--- a/docs/backends/index.rst
+++ b/docs/backends/index.rst
@@ -60,6 +60,7 @@ Social backends
    coinbase
    coursera
    dailymotion
+   digitalocean
    disqus
    docker
    douban
diff --git a/social/backends/digitalocean.py b/social/backends/digitalocean.py
new file mode 100644
index 0000000..780e4be
--- /dev/null
+++ b/social/backends/digitalocean.py
@@ -0,0 +1,41 @@
+from social.backends.oauth import BaseOAuth2
+
+
+class DigitalOceanOAuth(BaseOAuth2):
+    """
+    DigitalOcean OAuth authentication backend.
+
+    Docs: https://developers.digitalocean.com/documentation/oauth/
+    """
+    name = 'digitalocean'
+    AUTHORIZATION_URL = 'https://cloud.digitalocean.com/v1/oauth/authorize'
+    ACCESS_TOKEN_URL = 'https://cloud.digitalocean.com/v1/oauth/token'
+    ACCESS_TOKEN_METHOD = 'POST'
+    SCOPE_SEPARATOR = ' '
+    EXTRA_DATA = [
+        ('expires_in', 'expires_in')
+    ]
+
+    def get_user_id(self, details, response):
+        """Return user unique id provided by service"""
+        return response['account'].get('uuid')
+
+    def get_user_details(self, response):
+        """Return user details from DigitalOcean account"""
+        fullname, first_name, last_name = self.get_user_names(
+            response.get('name') or '')
+
+        return {'username': response['account'].get('email'),
+                'email': response['account'].get('email'),
+                'fullname': fullname,
+                'first_name': first_name,
+                'last_name': last_name}
+
+    def user_data(self, token, *args, **kwargs):
+        """Loads user data from service"""
+        url = 'https://api.digitalocean.com/v2/account'
+        auth_header = {"Authorization": "Bearer %s" % token}
+        try:
+            return self.get_json(url, headers=auth_header)
+        except ValueError:
+            return None
diff --git a/social/tests/backends/test_digitalocean.py b/social/tests/backends/test_digitalocean.py
new file mode 100644
index 0000000..5eeedb3
--- /dev/null
+++ b/social/tests/backends/test_digitalocean.py
@@ -0,0 +1,34 @@
+import json
+
+from social.tests.backends.oauth import OAuth2Test
+
+
+class DigitalOceanOAuthTest(OAuth2Test):
+    backend_path = 'social.backends.digitalocean.DigitalOceanOAuth'
+    user_data_url = 'https://api.digitalocean.com/v2/account'
+    expected_username = 'sammy at digitalocean.com'
+    access_token_body = json.dumps({
+        'access_token': '547cac21118ae7',
+        'token_type': 'bearer',
+        'expires_in': 2592000,
+        'refresh_token': '00a3aae641658d',
+        'scope': 'read write',
+        'info': {
+            'name': 'Sammy Shark',
+            'email': 'sammy at digitalocean.com'
+        }
+    })
+    user_data_body = json.dumps({
+        "account": {
+            'droplet_limit': 25,
+            'email': 'sammy at digitalocean.com',
+            'uuid': 'b6fr89dbf6d9156cace5f3c78dc9851d957381ef',
+            'email_verified': True
+        }
+    })
+
+    def test_login(self):
+        self.do_login()
+
+    def test_partial_pipeline(self):
+        self.do_partial_pipeline()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-social-auth.git



More information about the Python-modules-commits mailing list