~ubuntu-branches/debian/sid/social-auth-core/sid

« back to all changes in this revision

Viewing changes to social_core/backends/digitalocean.py

  • Committer: Package Import Robot
  • Author(s): Andre Bianchi
  • Date: 2018-02-22 19:49:12 UTC
  • Revision ID: package-import@ubuntu.com-20180222194912-4lqv8mlhnqc4ncd3
Tags: upstream-1.7.0
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from .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