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

« back to all changes in this revision

Viewing changes to social_core/backends/nationbuilder.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
"""
 
2
NationBuilder OAuth2 backend, docs at:
 
3
    https://python-social-auth.readthedocs.io/en/latest/backends/nationbuilder.html
 
4
"""
 
5
from .oauth import BaseOAuth2
 
6
 
 
7
 
 
8
class NationBuilderOAuth2(BaseOAuth2):
 
9
    """NationBuilder OAuth2 authentication backend"""
 
10
    name = 'nationbuilder'
 
11
    AUTHORIZATION_URL = 'https://{slug}.nationbuilder.com/oauth/authorize'
 
12
    ACCESS_TOKEN_URL = 'https://{slug}.nationbuilder.com/oauth/token'
 
13
    ACCESS_TOKEN_METHOD = 'POST'
 
14
    REDIRECT_STATE = False
 
15
    SCOPE_SEPARATOR = ','
 
16
    EXTRA_DATA = [
 
17
        ('id', 'id'),
 
18
        ('expires', 'expires')
 
19
    ]
 
20
 
 
21
    def authorization_url(self):
 
22
        return self.AUTHORIZATION_URL.format(slug=self.slug)
 
23
 
 
24
    def access_token_url(self):
 
25
        return self.ACCESS_TOKEN_URL.format(slug=self.slug)
 
26
 
 
27
    @property
 
28
    def slug(self):
 
29
        return self.setting('SLUG')
 
30
 
 
31
    def get_user_details(self, response):
 
32
        """Return user details from Github account"""
 
33
        email = response.get('email') or ''
 
34
        username = email.split('@')[0] if email else ''
 
35
        return {'username': username,
 
36
                'email': email,
 
37
                'fullname': response.get('full_name') or '',
 
38
                'first_name': response.get('first_name') or '',
 
39
                'last_name': response.get('last_name') or ''}
 
40
 
 
41
    def user_data(self, access_token, *args, **kwargs):
 
42
        """Loads user data from service"""
 
43
        url = 'https://{slug}.nationbuilder.com/api/v1/people/me'.format(
 
44
            slug=self.slug
 
45
        )
 
46
        return self.get_json(url, params={
 
47
            'access_token': access_token
 
48
        })['person']