Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a DigitalOcean backend. #619

Merged
merged 1 commit into from
May 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions docs/backends/digitalocean.rst
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions docs/backends/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Social backends
coinbase
coursera
dailymotion
digitalocean
disqus
docker
douban
Expand Down
41 changes: 41 additions & 0 deletions social/backends/digitalocean.py
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions social/tests/backends/test_digitalocean.py
Original file line number Diff line number Diff line change
@@ -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@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@digitalocean.com'
}
})
user_data_body = json.dumps({
"account": {
'droplet_limit': 25,
'email': 'sammy@digitalocean.com',
'uuid': 'b6fr89dbf6d9156cace5f3c78dc9851d957381ef',
'email_verified': True
}
})

def test_login(self):
self.do_login()

def test_partial_pipeline(self):
self.do_partial_pipeline()