Skip to content

Commit 04f5ff3

Browse files
Add a DigitalOcean backend.
1 parent 4d4c3f8 commit 04f5ff3

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ or current ones extended):
6363
* Clef_ OAuth2
6464
* Coursera_ OAuth2
6565
* Dailymotion_ OAuth2
66+
* DigitalOcean_ OAuth2 https://developers.digitalocean.com/documentation/oauth/
6667
* Disqus_ OAuth2
6768
* Douban_ OAuth1 and OAuth2
6869
* Dropbox_ OAuth1 and OAuth2
@@ -240,6 +241,7 @@ check `django-social-auth LICENSE`_ for details:
240241
.. _Clef: https://getclef.com/
241242
.. _Coursera: https://www.coursera.org/
242243
.. _Dailymotion: https://dailymotion.com
244+
.. _DigitalOcean: https://www.digitalocean.com/
243245
.. _Disqus: https://disqus.com
244246
.. _Douban: http://www.douban.com
245247
.. _Dropbox: https://dropbox.com

docs/backends/digitalocean.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
DigitalOcean
2+
============
3+
4+
DigitalOcean uses OAuth2 for its auth process. See the full `DigitalOcean
5+
developer's documentation`_ for more information.
6+
7+
- Register a new application in the `Apps & API page`_ in the DigitalOcean
8+
control panel, setting the callback URL to ``http://example.com/complete/digitalocean/``
9+
replacing ``example.com`` with your domain.
10+
11+
- Fill the ``Client ID`` and ``Client Secret`` values from GitHub in the settings::
12+
13+
SOCIAL_AUTH_DIGITALOCEAN_KEY = ''
14+
SOCIAL_AUTH_DIGITALOCEAN_SECRET = ''
15+
16+
- By default, only ``read`` permissions are granted. In order to create,
17+
destroy, and take other actions on the user's resources, you must request
18+
``read write`` permissions like so::
19+
20+
SOCIAL_AUTH_DIGITALOCEAN_AUTH_EXTRA_ARGUMENTS = {'scope': 'read write'}
21+
22+
23+
.. _DigitalOcean developer's documentation: https://developers.digitalocean.com/documentation/
24+
.. _Apps & API page: https://cloud.digitalocean.com/settings/applications

docs/backends/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Social backends
6060
coinbase
6161
coursera
6262
dailymotion
63+
digitalocean
6364
disqus
6465
docker
6566
douban

social/backends/digitalocean.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from social.backends.oauth import BaseOAuth2
2+
3+
4+
class DigitalOceanOAuth(BaseOAuth2):
5+
"""
6+
DigitalOcean OAuth authentication backend.
7+
8+
Docs: https://developers.digitalocean.com/documentation/oauth/
9+
"""
10+
name = 'digitalocean'
11+
AUTHORIZATION_URL = 'https://cloud.digitalocean.com/v1/oauth/authorize'
12+
ACCESS_TOKEN_URL = 'https://cloud.digitalocean.com/v1/oauth/token'
13+
ACCESS_TOKEN_METHOD = 'POST'
14+
SCOPE_SEPARATOR = ' '
15+
EXTRA_DATA = [
16+
('expires_in', 'expires_in')
17+
]
18+
19+
def get_user_id(self, details, response):
20+
"""Return user unique id provided by service"""
21+
return response['account'].get('uuid')
22+
23+
def get_user_details(self, response):
24+
"""Return user details from DigitalOcean account"""
25+
fullname, first_name, last_name = self.get_user_names(
26+
response.get('name') or '')
27+
28+
return {'username': response['account'].get('email'),
29+
'email': response['account'].get('email'),
30+
'fullname': fullname,
31+
'first_name': first_name,
32+
'last_name': last_name}
33+
34+
def user_data(self, token, *args, **kwargs):
35+
"""Loads user data from service"""
36+
url = 'https://api.digitalocean.com/v2/account'
37+
auth_header = {"Authorization": "Bearer %s" % token}
38+
try:
39+
return self.get_json(url, headers=auth_header)
40+
except ValueError:
41+
return None
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import json
2+
3+
from social.tests.backends.oauth import OAuth2Test
4+
5+
6+
class DigitalOceanOAuthTest(OAuth2Test):
7+
backend_path = 'social.backends.digitalocean.DigitalOceanOAuth'
8+
user_data_url = 'https://api.digitalocean.com/v2/account'
9+
expected_username = 'sammy@digitalocean.com'
10+
access_token_body = json.dumps({
11+
'access_token': '547cac21118ae7',
12+
'token_type': 'bearer',
13+
'expires_in': 2592000,
14+
'refresh_token': '00a3aae641658d',
15+
'scope': 'read write',
16+
'info': {
17+
'name': 'Sammy Shark',
18+
'email': 'sammy@digitalocean.com'
19+
}
20+
})
21+
user_data_body = json.dumps({
22+
"account": {
23+
'droplet_limit': 25,
24+
'email': 'sammy@digitalocean.com',
25+
'uuid': 'b6fr89dbf6d9156cace5f3c78dc9851d957381ef',
26+
'email_verified': True
27+
}
28+
})
29+
30+
def test_login(self):
31+
self.do_login()
32+
33+
def test_partial_pipeline(self):
34+
self.do_partial_pipeline()

0 commit comments

Comments
 (0)