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

« back to all changes in this revision

Viewing changes to social_core/backends/justgiving.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 requests.auth import HTTPBasicAuth
 
2
from ..utils import handle_http_errors
 
3
from .oauth import BaseOAuth2
 
4
 
 
5
 
 
6
class JustGivingOAuth2(BaseOAuth2):
 
7
    """Just Giving OAuth authentication backend"""
 
8
    name = 'justgiving'
 
9
    ID_KEY = 'userId'
 
10
    AUTHORIZATION_URL = 'https://identity.justgiving.com/connect/authorize'
 
11
    ACCESS_TOKEN_URL = 'https://identity.justgiving.com/connect/token'
 
12
    ACCESS_TOKEN_METHOD = 'POST'
 
13
    USER_DATA_URL = 'https://api.justgiving.com/v1/account'
 
14
    DEFAULT_SCOPE = ['openid', 'account', 'profile', 'email', 'fundraise']
 
15
 
 
16
    def get_user_details(self, response):
 
17
        """Return user details from Just Giving account"""
 
18
        fullname, first_name, last_name = self.get_user_names(
 
19
            '',
 
20
            response.get('firstName'),
 
21
            response.get('lastName'))
 
22
        return {
 
23
            'username': response.get('email'),
 
24
            'email': response.get('email'),
 
25
            'fullname': fullname,
 
26
            'first_name': first_name,
 
27
            'last_name': last_name
 
28
        }
 
29
 
 
30
    def user_data(self, access_token, *args, **kwargs):
 
31
        """Loads user data from service"""
 
32
        key, secret = self.get_key_and_secret()
 
33
        return self.get_json(self.USER_DATA_URL, headers={
 
34
            'Authorization': 'Bearer {0}'.format(access_token),
 
35
            'Content-Type': 'application/json',
 
36
            'x-application-key': secret,
 
37
            'x-api-key': key
 
38
        })
 
39
 
 
40
    @handle_http_errors
 
41
    def auth_complete(self, *args, **kwargs):
 
42
        """Completes loging process, must return user instance"""
 
43
        state = self.validate_state()
 
44
        self.process_error(self.data)
 
45
 
 
46
        key, secret = self.get_key_and_secret()
 
47
        response = self.request_access_token(
 
48
            self.access_token_url(),
 
49
            data=self.auth_complete_params(state),
 
50
            headers=self.auth_headers(),
 
51
            auth=HTTPBasicAuth(key, secret),
 
52
            method=self.ACCESS_TOKEN_METHOD
 
53
        )
 
54
        self.process_error(response)
 
55
        return self.do_auth(response['access_token'], response=response,
 
56
                            *args, **kwargs)